Free cookie consent management tool by TermsFeed Policy Generator

source: branches/UserManagement/HeuristicLab.Services.Authentication/AuthenticationService.cs @ 6868

Last change on this file since 6868 was 5350, checked in by jwolfing, 14 years ago

#1196 added setpassword to the model

File size: 11.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.ServiceModel;
26using HeuristicLab.Services.Authentication.DataTransfer;
27using HeuristicLab.Services.Authentication.DataAccess;
28using System.Data.Linq;
29using System.Diagnostics;
30
31namespace HeuristicLab.Services.Authentication {
32  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
33  public class AuthenticationService : IAuthenticationService {
34
35    #region User
36
37    public DataTransfer.User GetUser(Guid id) {
38      using (UserManagementDataContext db = new UserManagementDataContext()) {
39        var user = db.aspnet_Users.FirstOrDefault(x => x.UserId == id);
40        var membership = db.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault();
41        return Convert.ToDto(user, membership);
42      }
43    }
44
45    public IEnumerable<User> GetUsers() {
46      using (UserManagementDataContext db = new UserManagementDataContext()) {
47        var users = db.aspnet_Users.OrderBy(x => x.UserId).ToList().Zip(db.aspnet_Memberships.OrderBy(x => x.UserId), (x, y) => Convert.ToDto(x, y));
48        return users;
49      }
50    }
51
52
53    public IEnumerable<User> GetUsersForApplication(Guid applicationId) {
54      using (UserManagementDataContext db = new UserManagementDataContext()) {
55        var users = db.aspnet_Users.Where(x => x.ApplicationId == applicationId).OrderBy(x => x.UserId).ToList().Zip(db.aspnet_Memberships.Where(x => x.ApplicationId == applicationId).OrderBy(x => x.UserId), (x, y) => Convert.ToDto(x, y)).ToArray();
56        return users;
57      }
58    }
59
60
61    public Guid AddUser(User user) {
62      if (user != null) {
63        using (UserManagementDataContext db = new UserManagementDataContext()) {
64          aspnet_User eUser;
65          aspnet_Membership eMembership;
66          user.Id = Guid.NewGuid();
67          Convert.ToEntity(user, out eUser, out  eMembership);
68          db.aspnet_Users.InsertOnSubmit(eUser);
69          db.aspnet_Memberships.InsertOnSubmit(eMembership);
70          db.SubmitChanges();
71          return user.Id;
72        }
73      }
74      return Guid.Empty;
75
76    }
77
78    public void DeleteUser(Guid id) {
79      using (UserManagementDataContext db = new UserManagementDataContext()) {
80        var user = db.aspnet_Users.Where(x => x.UserId == id).FirstOrDefault();
81        var membership = db.aspnet_Memberships.Where(x => x.UserId == id).FirstOrDefault();
82        var userinroles = db.aspnet_UsersInRoles.Where(x => x.UserId == id).ToList<aspnet_UsersInRole>();
83        db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles);
84        db.aspnet_Memberships.DeleteOnSubmit(membership);
85        db.aspnet_Users.DeleteOnSubmit(user);
86        db.SubmitChanges();
87      }
88
89    }
90
91
92    public void UpdateUser(User user) {
93      using (UserManagementDataContext db = new UserManagementDataContext()) {
94        var eUser = db.aspnet_Users.Where(x => x.UserId == user.Id).FirstOrDefault();
95        var eMembership = db.aspnet_Memberships.Where(x => x.UserId == user.Id).FirstOrDefault();
96        Convert.ToEntity(user, eUser, eMembership);
97        db.SubmitChanges();
98      }
99    }
100
101
102    public void AddUserToRole(Guid roleId, Guid userId) {
103      using (UserManagementDataContext db = new UserManagementDataContext()) {
104        aspnet_UsersInRole r = new aspnet_UsersInRole();
105        r.RoleId = roleId;
106        r.UserId = userId;
107        db.aspnet_UsersInRoles.InsertOnSubmit(r);
108        db.SubmitChanges();
109      }
110    }
111
112
113
114    public IEnumerable<Guid> GetUsersInRole(Guid roleId) {
115      using (UserManagementDataContext db = new UserManagementDataContext()) {
116        List<Guid> userList = new List<Guid>();
117        var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId).ToList<aspnet_UsersInRole>();
118        foreach (aspnet_UsersInRole u in users) {
119          userList.Add(GetUser(u.UserId).Id);
120        }
121        return userList;
122      }
123    }
124
125
126    public bool IsUserInRole(Guid userId, Guid roleId) {
127      using (UserManagementDataContext db = new UserManagementDataContext()) {
128        bool isInRole = false;
129        var users = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId && x.UserId == userId).ToList<aspnet_UsersInRole>();
130        foreach (aspnet_UsersInRole u in users) {
131          isInRole = true;
132        }
133        return isInRole;
134      }
135    }
136
137
138    public void RemoveUserFromRole(Guid roleId, Guid userId) {
139      using (UserManagementDataContext db = new UserManagementDataContext()) {
140        var role = db.aspnet_UsersInRoles.Where(x => x.RoleId == roleId && x.UserId == userId).FirstOrDefault();
141        db.aspnet_UsersInRoles.DeleteOnSubmit(role);
142        db.SubmitChanges();
143      }
144    }
145
146
147
148    public User ResetPassword(string applicationName, string userName, string password) {
149
150      string salt = "";
151      int format = 1; //Password format (0=Plaintext, 1=Hashed, 2=Encrypted)
152
153      using (UserManagementDataContext db = new UserManagementDataContext()) {
154        db.aspnet_Membership_SetPassword(applicationName, userName, password, salt, DateTime.UtcNow, format);
155        return null;
156      }
157    }
158
159    #endregion
160
161    #region Role
162
163    public Role GetRole(Guid id) {
164      using (UserManagementDataContext db = new UserManagementDataContext()) {
165        var role = db.aspnet_Roles.Where(x => x.RoleId == id).FirstOrDefault();
166        return Convert.ToDto(role);
167      }
168    }
169
170    public IEnumerable<Role> GetRoles() {
171
172      using (UserManagementDataContext db = new UserManagementDataContext()) {
173        List<DataTransfer.Role> roleList = new List<DataTransfer.Role>();
174        var roles = db.aspnet_Roles.ToList<aspnet_Role>();
175        foreach (aspnet_Role role in roles) {
176          roleList.Add(Convert.ToDto(role));
177        }
178        return roleList;
179      }
180
181    }
182
183    public IEnumerable<Role> GetRolesForApplication(Guid applicationId) {
184      using (UserManagementDataContext db = new UserManagementDataContext()) {
185        List<Role> roleList = new List<Role>();
186        var roles = db.aspnet_Roles.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Role>();
187        foreach (aspnet_Role role in roles) {
188          roleList.Add(Convert.ToDto(role));
189        }
190        return roleList;
191      }
192    }
193
194    public Guid AddRole(Role role) {
195      if (role != null) {
196        using (UserManagementDataContext db = new UserManagementDataContext()) {
197          aspnet_Role eRole = new aspnet_Role();
198          role.Id = Guid.NewGuid();
199          Convert.ToEntity(role, eRole);
200          db.aspnet_Roles.InsertOnSubmit(eRole);
201          db.SubmitChanges();
202          return role.Id;
203        }
204      }
205      return Guid.Empty;
206    }
207
208    public void DeleteRole(Guid id) {
209      using (UserManagementDataContext db = new UserManagementDataContext()) {
210        var role = db.aspnet_Roles.Where(x => x.RoleId == id).FirstOrDefault();
211        var userinroles = db.aspnet_UsersInRoles.Where(x => x.RoleId == id).ToList<aspnet_UsersInRole>();
212        db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles);
213        db.aspnet_Roles.DeleteOnSubmit(role);
214        db.SubmitChanges();
215      }
216    }
217
218    public IEnumerable<Guid> GetRolesForUser(Guid userId) {
219      using (UserManagementDataContext db = new UserManagementDataContext()) {
220        List<Guid> roleList = new List<Guid>();
221        var roles = db.aspnet_UsersInRoles.Where(x => x.UserId == userId).ToList<aspnet_UsersInRole>();
222        foreach (aspnet_UsersInRole r in roles) {
223          roleList.Add(GetRole(r.RoleId).Id);
224        }
225        return roleList;
226      }
227    }
228
229    public void UpdateRole(Role role) {
230      using (UserManagementDataContext db = new UserManagementDataContext()) {
231        var eRole = db.aspnet_Roles.Where(x => x.RoleId == role.Id).FirstOrDefault();
232        Convert.ToEntity(role, eRole);
233        db.SubmitChanges();
234      }
235    }
236
237    #endregion
238
239    #region Application
240
241    public Guid AddApplication(Application application) {
242      if (application != null) {
243        using (UserManagementDataContext db = new UserManagementDataContext()) {
244          aspnet_Application eApplication = new aspnet_Application();
245          application.Id = Guid.NewGuid();
246          Convert.ToEntity(application, eApplication);
247          db.aspnet_Applications.InsertOnSubmit(eApplication);
248          db.SubmitChanges();
249          return application.Id;
250        }
251      }
252      return Guid.Empty;
253    }
254
255    public void UpdateApplication(Application application) {
256      using (UserManagementDataContext db = new UserManagementDataContext()) {
257        var eApplication = db.aspnet_Applications.Where(x => x.ApplicationId == application.Id).FirstOrDefault();
258        Convert.ToEntity(application, eApplication);
259        db.SubmitChanges();
260      }
261    }
262
263
264    public void DeleteApplication(Guid applicationId) {
265      using (UserManagementDataContext db = new UserManagementDataContext()) {
266        var users = db.aspnet_Users.Where(x => x.ApplicationId == applicationId).ToList<aspnet_User>();
267        var memberships = db.aspnet_Memberships.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Membership>();
268        var roles = db.aspnet_Roles.Where(x => x.ApplicationId == applicationId).ToList<aspnet_Role>();
269        var application = db.aspnet_Applications.Where(x => x.ApplicationId == applicationId).FirstOrDefault();
270        foreach (aspnet_User u in users) {
271          var userinroles = db.aspnet_UsersInRoles.Where(x => x.UserId == u.UserId).ToList<aspnet_UsersInRole>();
272          db.aspnet_UsersInRoles.DeleteAllOnSubmit(userinroles);
273        }
274        db.aspnet_Memberships.DeleteAllOnSubmit(memberships);
275        db.aspnet_Users.DeleteAllOnSubmit(users);
276        db.aspnet_Roles.DeleteAllOnSubmit(roles);
277        db.aspnet_Applications.DeleteOnSubmit(application);
278        db.SubmitChanges();
279      }
280    }
281
282    public Application GetApplication(Guid id) {
283      using (UserManagementDataContext db = new UserManagementDataContext()) {
284        var application = db.aspnet_Applications.Where(x => x.ApplicationId == id).FirstOrDefault();
285        return Convert.ToDto(application);
286      }
287    }
288
289    public IEnumerable<Application> GetApplications() {
290      List<Application> applicationList = new List<Application>();
291      using (UserManagementDataContext db = new UserManagementDataContext()) {
292        var apps = db.aspnet_Applications.ToList<aspnet_Application>();
293        foreach (aspnet_Application app in apps) {
294          applicationList.Add(Convert.ToDto(app));
295        }
296      }
297      return applicationList;
298    }
299    #endregion
300  }
301}
Note: See TracBrowser for help on using the repository browser.