Free cookie consent management tool by TermsFeed Policy Generator

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

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

Added GetUserByLogin method for the SecurityManager (#532)

File size: 14.3 KB
Line 
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;
9using HeuristicLab.DataAccess.Interfaces;
10using System.ServiceModel;
11
12namespace HeuristicLab.Security.Core {
13  public class SecurityManager : ISecurityManager {
14
15    private static ISessionFactory factory = ServiceLocator.GetSessionFactory();
16
17    private ISession session;
18
19   /// <summary>
20    /// Add new user.
21    /// </summary>
22    /// <param name="user"></param>
23    /// <returns></returns>
24    public User AddNewUser(User user) {
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      }
34      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
35      finally {
36        if (session != null)
37          session.EndSession();
38      }
39    }
40
41    /// <summary>
42    /// Update user.
43    /// </summary>
44    /// <param name="user"></param>
45    /// <returns></returns>
46    public User UpdateUser(User user) {
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      }
55      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
56      finally {
57        if (session != null)
58          session.EndSession();
59      }
60    }
61
62   /// <summary>
63    /// Remove user.
64   /// </summary>
65   /// <param name="userId"></param>
66   /// <returns></returns>
67    public bool RemoveUser(Guid userId) {
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      }
77      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
78      finally {
79        if (session != null)
80          session.EndSession();
81      }
82    }
83
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      }
95      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
96      finally {
97        if (session != null)
98          session.EndSession();
99      }         
100    }
101
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      }
114      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
115      finally {
116        if (session != null)
117          session.EndSession();
118      } 
119    }
120
121    /// <summary>
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>
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      }
155      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
156      finally {
157        if (session != null)
158          session.EndSession();
159      }
160    }
161
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      }
177      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
178      finally {
179        if (session != null)
180          session.EndSession();
181      }
182    }
183
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      }
200      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
201      finally {
202        if (session != null)
203          session.EndSession();
204      }
205    }
206
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       }
218      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
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      }
237      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
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      }
259      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
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      }
291      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
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      }
324      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
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>
338    public bool GrantPermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
339      try {
340        session = factory.GetSessionForCurrentThread();                         
341        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
342
343        return permissionAdapter.grantPermission(permissionOwnerId, permissionId, entityId);
344      }
345      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
346      finally {
347        if (session != null)
348          session.EndSession();
349      }
350    }
351
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      }
364      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
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>
378    public bool RevokePermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
379      try {
380        session = factory.GetSessionForCurrentThread();     
381        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
382 
383        return permissionAdapter.revokePermission(permissionOwnerId, permissionId, entityId);
384      }
385      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
386      finally {
387        if (session != null)
388          session.EndSession();
389      }
390    }
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      }
404      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
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      }
422      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
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      }
441      catch (Exception ex) { throw new FaultException("Server: " + ex.Message); }
442      finally {
443        if (session != null)
444          session.EndSession();
445      }
446    }
447  }
448}
Note: See TracBrowser for help on using the repository browser.