Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1196 added setpassword to the model

File size: 11.4 KB
RevLine 
[5257]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;
[4584]23using System.Collections.Generic;
24using System.Linq;
25using System.ServiceModel;
26using HeuristicLab.Services.Authentication.DataTransfer;
27using HeuristicLab.Services.Authentication.DataAccess;
28using System.Data.Linq;
[4647]29using System.Diagnostics;
[4584]30
[5257]31namespace HeuristicLab.Services.Authentication {
32  [ServiceBehavior(IncludeExceptionDetailInFaults = true)]
33  public class AuthenticationService : IAuthenticationService {
[4926]34
[5257]35    #region User
[4590]36
[5257]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    }
[4926]44
[5257]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    }
[4584]51
[4647]52
[5257]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    }
[4726]59
[4647]60
[5257]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;
[4926]72        }
[5257]73      }
74      return Guid.Empty;
[4647]75
[5257]76    }
[4979]77
[5257]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      }
[4979]88
[5257]89    }
[4647]90
91
[5257]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    }
[4647]100
101
[5257]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    }
[4647]111
112
113
[5257]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);
[4926]120        }
[5257]121        return userList;
122      }
123    }
[4584]124
[4647]125
[5257]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;
[4926]132        }
[5257]133        return isInRole;
134      }
135    }
[4647]136
137
[5257]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    }
[4647]145
146
[5350]147
[5257]148    public User ResetPassword(string applicationName, string userName, string password) {
[4584]149
[5257]150      string salt = "";
[5350]151      int format = 1; //Password format (0=Plaintext, 1=Hashed, 2=Encrypted)
[4647]152
[5257]153      using (UserManagementDataContext db = new UserManagementDataContext()) {
[5350]154        db.aspnet_Membership_SetPassword(applicationName, userName, password, salt, DateTime.UtcNow, format);
[5257]155        return null;
156      }
157    }
[4740]158
[5257]159    #endregion
[4740]160
[5257]161    #region Role
[4647]162
[5257]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    }
[4588]169
[5257]170    public IEnumerable<Role> GetRoles() {
[4979]171
[5257]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));
[4979]177        }
[5257]178        return roleList;
179      }
[4979]180
[5257]181    }
[4979]182
[5257]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));
[4979]189        }
[5257]190        return roleList;
191      }
192    }
[4979]193
[5257]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;
[4979]203        }
[5257]204      }
205      return Guid.Empty;
206    }
[4979]207
[5257]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    }
[4979]217
[5257]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);
[4979]224        }
[5257]225        return roleList;
226      }
227    }
[4979]228
[5257]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    }
[4979]236
[5257]237    #endregion
[4979]238
[5257]239    #region Application
[4979]240
[5257]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;
[4926]250        }
[5257]251      }
252      return Guid.Empty;
253    }
[4647]254
[5257]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    }
[4979]262
[4647]263
[5257]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);
[4926]273        }
[5257]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    }
[4647]281
[5257]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    }
[4647]288
[5257]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));
[4926]295        }
[5257]296      }
297      return applicationList;
[4926]298    }
[5257]299    #endregion
300  }
[4584]301}
Note: See TracBrowser for help on using the repository browser.