Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Security.Core/3.2/SecurityManager.cs @ 2285

Last change on this file since 2285 was 2020, checked in by svonolfe, 15 years ago

Added GetUserByLogin method for the SecurityManager (#532)

File size: 14.3 KB
RevLine 
[1409]1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.Security.Contracts.Interfaces;
6using HeuristicLab.Security.Contracts.BusinessObjects;
7using HeuristicLab.Security.DataAccess;
8using HeuristicLab.PluginInfrastructure;
[1624]9using HeuristicLab.DataAccess.Interfaces;
[1738]10using System.ServiceModel;
[1409]11
12namespace HeuristicLab.Security.Core {
13  public class SecurityManager : ISecurityManager {
14
[1724]15    private static ISessionFactory factory = ServiceLocator.GetSessionFactory();
[1409]16
[1624]17    private ISession session;
[1409]18
[1624]19   /// <summary>
20    /// Add new user.
21    /// </summary>
22    /// <param name="user"></param>
23    /// <returns></returns>
[1409]24    public User AddNewUser(User user) {
[1624]25      try {
26        session = factory.GetSessionForCurrentThread();
27
28        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
29        if (user != null)
30          userAdapter.Update(user);
31
32        return user;
33      }
[1738]34      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]35      finally {
36        if (session != null)
37          session.EndSession();
38      }
[1409]39    }
40
[1624]41    /// <summary>
42    /// Update user.
43    /// </summary>
44    /// <param name="user"></param>
45    /// <returns></returns>
[1409]46    public User UpdateUser(User user) {
[1624]47      try {
48        session = factory.GetSessionForCurrentThread();
49        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
50
51        if (user != null)
52          userAdapter.Update(user);
53        return user;
54      }
[1738]55      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]56      finally {
57        if (session != null)
58          session.EndSession();
59      }
[1409]60    }
61
[1624]62   /// <summary>
63    /// Remove user.
64   /// </summary>
65   /// <param name="userId"></param>
66   /// <returns></returns>
[1409]67    public bool RemoveUser(Guid userId) {
[1624]68      try {
69        session = factory.GetSessionForCurrentThread();
70        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
71        User user = userAdapter.GetById(userId);
72
73        if (user != null)
74          return userAdapter.Delete(user);
75        return false;
76      }
[1738]77      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]78      finally {
79        if (session != null)
80          session.EndSession();
81      }
[1409]82    }
83
[1624]84    /// <summary>
85    /// Gets all Users.
86    /// </summary>
87    /// <returns></returns>
88    public ICollection<User> GetAllUsers() {
89      try {
90        session = factory.GetSessionForCurrentThread();
91        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
92
93        return userAdapter.GetAll();
94      }
[1738]95      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]96      finally {
97        if (session != null)
98          session.EndSession();
99      }         
[1409]100    }
101
[1624]102    /// <summary>
103    /// Gets user by name.
104    /// </summary>
105    /// <param name="name"></param>
106    /// <returns></returns>
107    public User GetUserByName(string name) {
108      try {
109        session = factory.GetSessionForCurrentThread();
110        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
111
112        return userAdapter.GetByName(name);
113      }
[1738]114      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]115      finally {
116        if (session != null)
117          session.EndSession();
118      } 
[1409]119    }
120
[1624]121    /// <summary>
[2020]122    /// Gets user by his login.
123    /// </summary>
124    /// <param name="login"></param>
125    /// <returns></returns>
126    public User GetUserByLogin(string login) {
127      try {
128        session = factory.GetSessionForCurrentThread();
129        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
130
131        return userAdapter.GetByLogin(login);
132      }
133      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
134      finally {
135        if (session != null)
136          session.EndSession();
137      }
138    }
139
140    /// <summary>
[1624]141    /// Add new user group.
142    /// </summary>
143    /// <param name="group"></param>
144    /// <returns></returns>
145    public UserGroup AddNewUserGroup(UserGroup userGroup) {
146      try {
147        session = factory.GetSessionForCurrentThread();
148
149        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
150        if (userGroup != null)
151          userGroupAdapter.Update(userGroup);
152
153        return userGroup;
154      }
[1738]155      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]156      finally {
157        if (session != null)
158          session.EndSession();
159      }
[1409]160    }
161
[1624]162    /// <summary>
163    /// Update user group.
164    /// </summary>
165    /// <param name="group"></param>
166    /// <returns></returns>
167    public UserGroup UpdateUserGroup(UserGroup userGroup) {
168      try {
169        session = factory.GetSessionForCurrentThread();
170
171        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
172        if (userGroup != null)
173          userGroupAdapter.Update(userGroup);
174
175        return userGroup;
176      }
[1738]177      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]178      finally {
179        if (session != null)
180          session.EndSession();
181      }
[1409]182    }
183
[1624]184    /// <summary>
185    /// Remove user group.
186    /// </summary>
187    /// <param name="groupId"></param>
188    /// <returns></returns>
189    public bool RemoveUserGroup(Guid userGroupId) {
190      try {
191        session = factory.GetSessionForCurrentThread();
192
193        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
194        UserGroup userGroup = userGroupAdapter.GetById(userGroupId);   
195
196        if (userGroup != null)
197          return userGroupAdapter.Delete(userGroup);
198        return false;
199      }
[1738]200      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]201      finally {
202        if (session != null)
203          session.EndSession();
204      }
[1409]205    }
206
[1624]207    /// <summary>
208    /// Gets all UserGroups.
209    /// </summary>
210    /// <returns></returns>
211    public ICollection<UserGroup> GetAllUserGroups() {
212      try {
213        session = factory.GetSessionForCurrentThread();
214        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
215
216        return userGroupAdapter.GetAll();
217       }
[1738]218      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]219      finally {
220        if (session != null)
221          session.EndSession();
222      }
223    }
224
225    /// <summary>
226    /// Gets UserGroup by name.
227    /// </summary>
228    /// <param name="name"></param>
229    /// <returns></returns>
230    public UserGroup GetUserGroupByName(string name) {
231      try {
232        session = factory.GetSessionForCurrentThread();
233        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
234
235        return userGroupAdapter.GetByName(name);
236      }
[1738]237      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]238      finally {
239        if (session != null)
240          session.EndSession();
241      }
242    }
243
244    /// <summary>
245    /// Updates a PermissionOwner.
246    /// </summary>
247    /// <param name="permissionOwner"></param>
248    /// <returns></returns>
249    public PermissionOwner UpdatePermissionOwner(PermissionOwner permissionOwner) {
250      try {
251        session = factory.GetSessionForCurrentThread();
252
253        IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
254        if (permissionOwner != null)
255          permOwnerAdapter.Update(permissionOwner);
256
257        return permissionOwner;
258      }
[1738]259      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]260      finally {
261        if (session != null)
262          session.EndSession();
263      }
264    }
265
266    /// <summary>
267    /// Add permission owner to group.
268    /// </summary>
269    /// <param name="userGroupId"></param>
270    /// <param name="permissionOwnerId"></param>
271    /// <returns></returns>
272    public bool AddPermissionOwnerToGroup(Guid userGroupId, Guid permissionOwnerId) {
273      try {
274        session = factory.GetSessionForCurrentThread();
275        ITransaction transaction = session.BeginTransaction();
276
277        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
278        UserGroup userGroup = userGroupAdapter.GetById(userGroupId);
279
280        IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
281        PermissionOwner permissionOwner = permOwnerAdapter.GetById(permissionOwnerId);
282       
283        if ((userGroup != null) && (permissionOwner != null)) {
284          userGroup.Members.Add(permissionOwner);
285          userGroupAdapter.Update(userGroup);
286          transaction.Commit();
287          return true;
288        }
289        return false;
290      }
[1738]291      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]292      finally {
293        if (session != null)
294          session.EndSession();
295      }
296    }
297
298
299    /// <summary>
300    /// Remove permission owner from group.
301    /// </summary>
302    /// <param name="groupId"></param>
303    /// <param name="permissionOwnerId"></param>
304    /// <returns></returns>
305    public bool RemovePermissionOwnerFromGroup(Guid userGroupId, Guid permissionOwnerId) {
306      try {
307        session = factory.GetSessionForCurrentThread();
308        ITransaction transaction = session.BeginTransaction();
309
310        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
311        UserGroup userGroup = userGroupAdapter.GetById(userGroupId);
312
313        IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
314        PermissionOwner permissionOwner = permOwnerAdapter.GetById(permissionOwnerId);
315
316        if ((userGroup != null) && (permissionOwner != null)) {
317          userGroup.Members.Add(permissionOwner);
318          userGroupAdapter.Delete(userGroup);
319          transaction.Commit();
320          return true;
321        }
322        return false;
323      }
[1738]324      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]325      finally {
326        if (session != null)
327          session.EndSession();
328      }
329    }
330
331    /// <summary>
332    /// Grant permission.
333    /// </summary>
334    /// <param name="permissionOwnerId"></param>
335    /// <param name="permissionId"></param>
336    /// <param name="entityId"></param>
337    /// <returns></returns>
[1409]338    public bool GrantPermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
[1624]339      try {
340        session = factory.GetSessionForCurrentThread();                         
341        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
342
343        return permissionAdapter.grantPermission(permissionOwnerId, permissionId, entityId);
344      }
[1738]345      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]346      finally {
347        if (session != null)
348          session.EndSession();
349      }
[1409]350    }
351
[1624]352    /// <summary>
353    /// Gets Permission by ID.
354    /// </summary>
355    /// <param name="permissionId"></param>
356    /// <returns></returns>
357    public Permission GetPermissionById(Guid permissionId) {
358      try {
359        session = factory.GetSessionForCurrentThread();
360
361        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
362        return permissionAdapter.GetById(permissionId);
363      }
[1738]364      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]365      finally {
366        if (session != null)
367          session.EndSession();
368      }
369    }
370
371    /// <summary>
372    /// Revoke permission.
373    /// </summary>
374    /// <param name="permissionOwnerId"></param>
375    /// <param name="permissionId"></param>
376    /// <param name="entityId"></param>
377    /// <returns></returns>
[1409]378    public bool RevokePermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
[1624]379      try {
380        session = factory.GetSessionForCurrentThread();     
381        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
382 
383        return permissionAdapter.revokePermission(permissionOwnerId, permissionId, entityId);
384      }
[1738]385      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1624]386      finally {
387        if (session != null)
388          session.EndSession();
389      }
[1409]390    }
[1729]391
392    public Permission AddPermission(Permission permission) {
393      try {
394        session = factory.GetSessionForCurrentThread();
395        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
396
397        if (permission != null) {
398          permissionAdapter.Update(permission);
399
400          return permission;
401        } else
402          return null;
403      }
[1738]404      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1729]405      finally {
406        if (session != null)
407          session.EndSession();
408      }
409    }
410
411    public bool RemovePermission(Guid permissionId) {
412      try {
413        session = factory.GetSessionForCurrentThread();
414        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
415
416        Permission permission = permissionAdapter.GetById(permissionId);
417        if (permission != null)
418          return permissionAdapter.Delete(permission);
419        else
420          return false;
421      }
[1738]422      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1729]423      finally {
424        if (session != null)
425          session.EndSession();
426      }
427    }
428
429    public Permission UpdatePermission(Permission permission) {
430      try {
431        session = factory.GetSessionForCurrentThread();
432        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
433
434        if(permission != null) {
435          permissionAdapter.Update(permission);
436          return permission;
437        }
438        else
439          return null;
440      }
[1738]441      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
[1729]442      finally {
443        if (session != null)
444          session.EndSession();
445      }
446    }
[1409]447  }
448}
Note: See TracBrowser for help on using the repository browser.