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