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

Last change on this file since 1724 was 1724, checked in by svonolfe, 12 years ago

Finished security server (#537)

File size: 10.7 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;
10
11namespace HeuristicLab.Security.Core {
12  public class SecurityManager : ISecurityManager {
13
14    private static ISessionFactory factory = ServiceLocator.GetSessionFactory();
15
16    private ISession session;
17
18   /// <summary>
19    /// Add new user.
20    /// </summary>
21    /// <param name="user"></param>
22    /// <returns></returns>
23    public User AddNewUser(User user) {
24      try {
25        session = factory.GetSessionForCurrentThread();
26
27        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
28        if (user != null)
29          userAdapter.Update(user);
30
31        return user;
32      }
33      finally {
34        if (session != null)
35          session.EndSession();
36      }
37    }
38
39    /// <summary>
40    /// Update user.
41    /// </summary>
42    /// <param name="user"></param>
43    /// <returns></returns>
44    public User UpdateUser(User user) {
45      try {
46        session = factory.GetSessionForCurrentThread();
47        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
48
49        if (user != null)
50          userAdapter.Update(user);
51        return user;
52      }
53      finally {
54        if (session != null)
55          session.EndSession();
56      }
57    }
58
59   /// <summary>
60    /// Remove user.
61   /// </summary>
62   /// <param name="userId"></param>
63   /// <returns></returns>
64    public bool RemoveUser(Guid userId) {
65      try {
66        session = factory.GetSessionForCurrentThread();
67        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
68        User user = userAdapter.GetById(userId);
69
70        if (user != null)
71          return userAdapter.Delete(user);
72        return false;
73      }
74      finally {
75        if (session != null)
76          session.EndSession();
77      }
78    }
79
80    /// <summary>
81    /// Gets all Users.
82    /// </summary>
83    /// <returns></returns>
84    public ICollection<User> GetAllUsers() {
85      try {
86        session = factory.GetSessionForCurrentThread();
87        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
88
89        return userAdapter.GetAll();
90      }
91      finally {
92        if (session != null)
93          session.EndSession();
94      }         
95    }
96
97    /// <summary>
98    /// Gets user by name.
99    /// </summary>
100    /// <param name="name"></param>
101    /// <returns></returns>
102    public User GetUserByName(string name) {
103      try {
104        session = factory.GetSessionForCurrentThread();
105        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
106
107        return userAdapter.GetByName(name);
108      }
109      finally {
110        if (session != null)
111          session.EndSession();
112      } 
113    }
114
115    /// <summary>
116    /// Add new user group.
117    /// </summary>
118    /// <param name="group"></param>
119    /// <returns></returns>
120    public UserGroup AddNewUserGroup(UserGroup userGroup) {
121      try {
122        session = factory.GetSessionForCurrentThread();
123
124        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
125        if (userGroup != null)
126          userGroupAdapter.Update(userGroup);
127
128        return userGroup;
129      }
130      finally {
131        if (session != null)
132          session.EndSession();
133      }
134    }
135
136    /// <summary>
137    /// Update user group.
138    /// </summary>
139    /// <param name="group"></param>
140    /// <returns></returns>
141    public UserGroup UpdateUserGroup(UserGroup userGroup) {
142      try {
143        session = factory.GetSessionForCurrentThread();
144
145        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
146        if (userGroup != null)
147          userGroupAdapter.Update(userGroup);
148
149        return userGroup;
150      }
151      finally {
152        if (session != null)
153          session.EndSession();
154      }
155    }
156
157    /// <summary>
158    /// Remove user group.
159    /// </summary>
160    /// <param name="groupId"></param>
161    /// <returns></returns>
162    public bool RemoveUserGroup(Guid userGroupId) {
163      try {
164        session = factory.GetSessionForCurrentThread();
165
166        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
167        UserGroup userGroup = userGroupAdapter.GetById(userGroupId);   
168
169        if (userGroup != null)
170          return userGroupAdapter.Delete(userGroup);
171        return false;
172      }
173      finally {
174        if (session != null)
175          session.EndSession();
176      }
177    }
178
179    /// <summary>
180    /// Gets all UserGroups.
181    /// </summary>
182    /// <returns></returns>
183    public ICollection<UserGroup> GetAllUserGroups() {
184      try {
185        session = factory.GetSessionForCurrentThread();
186        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
187
188        return userGroupAdapter.GetAll();
189       }
190      finally {
191        if (session != null)
192          session.EndSession();
193      }
194    }
195
196    /// <summary>
197    /// Gets UserGroup by name.
198    /// </summary>
199    /// <param name="name"></param>
200    /// <returns></returns>
201    public UserGroup GetUserGroupByName(string name) {
202      try {
203        session = factory.GetSessionForCurrentThread();
204        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
205
206        return userGroupAdapter.GetByName(name);
207      }
208      finally {
209        if (session != null)
210          session.EndSession();
211      }
212    }
213
214    /// <summary>
215    /// Updates a PermissionOwner.
216    /// </summary>
217    /// <param name="permissionOwner"></param>
218    /// <returns></returns>
219    public PermissionOwner UpdatePermissionOwner(PermissionOwner permissionOwner) {
220      try {
221        session = factory.GetSessionForCurrentThread();
222
223        IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
224        if (permissionOwner != null)
225          permOwnerAdapter.Update(permissionOwner);
226
227        return permissionOwner;
228      }
229      finally {
230        if (session != null)
231          session.EndSession();
232      }
233    }
234
235    /// <summary>
236    /// Add permission owner to group.
237    /// </summary>
238    /// <param name="userGroupId"></param>
239    /// <param name="permissionOwnerId"></param>
240    /// <returns></returns>
241    public bool AddPermissionOwnerToGroup(Guid userGroupId, Guid permissionOwnerId) {
242      try {
243        session = factory.GetSessionForCurrentThread();
244        ITransaction transaction = session.BeginTransaction();
245
246        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
247        UserGroup userGroup = userGroupAdapter.GetById(userGroupId);
248
249        IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
250        PermissionOwner permissionOwner = permOwnerAdapter.GetById(permissionOwnerId);
251       
252        if ((userGroup != null) && (permissionOwner != null)) {
253          userGroup.Members.Add(permissionOwner);
254          userGroupAdapter.Update(userGroup);
255          transaction.Commit();
256          return true;
257        }
258        return false;
259      }
260      finally {
261        if (session != null)
262          session.EndSession();
263      }
264    }
265
266
267    /// <summary>
268    /// Remove permission owner from group.
269    /// </summary>
270    /// <param name="groupId"></param>
271    /// <param name="permissionOwnerId"></param>
272    /// <returns></returns>
273    public bool RemovePermissionOwnerFromGroup(Guid userGroupId, Guid permissionOwnerId) {
274      try {
275        session = factory.GetSessionForCurrentThread();
276        ITransaction transaction = session.BeginTransaction();
277
278        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
279        UserGroup userGroup = userGroupAdapter.GetById(userGroupId);
280
281        IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
282        PermissionOwner permissionOwner = permOwnerAdapter.GetById(permissionOwnerId);
283
284        if ((userGroup != null) && (permissionOwner != null)) {
285          userGroup.Members.Add(permissionOwner);
286          userGroupAdapter.Delete(userGroup);
287          transaction.Commit();
288          return true;
289        }
290        return false;
291      }
292      finally {
293        if (session != null)
294          session.EndSession();
295      }
296    }
297
298    /// <summary>
299    /// Grant permission.
300    /// </summary>
301    /// <param name="permissionOwnerId"></param>
302    /// <param name="permissionId"></param>
303    /// <param name="entityId"></param>
304    /// <returns></returns>
305    public bool GrantPermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
306      try {
307        session = factory.GetSessionForCurrentThread();                         
308        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
309
310        return permissionAdapter.grantPermission(permissionOwnerId, permissionId, entityId);
311      }
312      finally {
313        if (session != null)
314          session.EndSession();
315      }
316    }
317
318    /// <summary>
319    /// Gets Permission by ID.
320    /// </summary>
321    /// <param name="permissionId"></param>
322    /// <returns></returns>
323    public Permission GetPermissionById(Guid permissionId) {
324      try {
325        session = factory.GetSessionForCurrentThread();
326
327        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
328        return permissionAdapter.GetById(permissionId);
329      }
330      finally {
331        if (session != null)
332          session.EndSession();
333      }
334    }
335
336    /// <summary>
337    /// Revoke permission.
338    /// </summary>
339    /// <param name="permissionOwnerId"></param>
340    /// <param name="permissionId"></param>
341    /// <param name="entityId"></param>
342    /// <returns></returns>
343    public bool RevokePermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
344      try {
345        session = factory.GetSessionForCurrentThread();     
346        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
347 
348        return permissionAdapter.revokePermission(permissionOwnerId, permissionId, entityId);
349      }
350      finally {
351        if (session != null)
352          session.EndSession();
353      }
354    }
355 
356  }
357}
Note: See TracBrowser for help on using the repository browser.