-
Notifications
You must be signed in to change notification settings - Fork 65
/
AuthorizationRepository.cs
592 lines (526 loc) · 20.7 KB
/
AuthorizationRepository.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Criterion;
using Rhino.Security.Impl;
using Rhino.Security.Impl.Util;
using Rhino.Security.Interfaces;
using Rhino.Security.Model;
using System.Linq;
namespace Rhino.Security.Services
{
/// <summary>
/// Allows to edit the security information of the
/// system
/// </summary>
public class AuthorizationRepository : IAuthorizationRepository
{
private readonly ISession session;
/// <summary>
/// Initializes a new instance of the <see cref="AuthorizationRepository"/> class.
/// </summary>
public AuthorizationRepository(ISession session)
{
this.session = session;
}
#region IAuthorizationRepository Members
/// <summary>
/// Creates a new users group.
/// </summary>
/// <param name="name">The name of the new group.</param>
public virtual UsersGroup CreateUsersGroup(string name)
{
var ug = new UsersGroup {Name = name};
session.Save(ug);
return ug;
}
/// <summary>
/// Creates the users group as a child of <paramref name="parentGroupName"/>.
/// </summary>
/// <param name="parentGroupName">Name of the parent group.</param>
/// <param name="usersGroupName">Name of the users group.</param>
/// <returns></returns>
public virtual UsersGroup CreateChildUserGroupOf(string parentGroupName, string usersGroupName)
{
UsersGroup parent = GetUsersGroupByName(parentGroupName);
Guard.Against<ArgumentException>(parent == null,
"Parent users group '" + parentGroupName + "' does not exists");
UsersGroup group = CreateUsersGroup(usersGroupName);
group.Parent = parent;
group.AllParents.AddAll(parent.AllParents);
group.AllParents.Add(parent);
parent.DirectChildren.Add(group);
parent.AllChildren.Add(group);
return group;
}
/// <summary>
/// Creates the entity group as a child of <paramref name="parentGroupName"/>.
/// </summary>
/// <param name="parentGroupName">Name of the parent group.</param>
/// <param name="usersGroupName">Name of the users group.</param>
/// <returns></returns>
public virtual EntitiesGroup CreateChildEntityGroupOf(string parentGroupName, string usersGroupName)
{
EntitiesGroup parent = GetEntitiesGroupByName(parentGroupName);
Guard.Against<ArgumentException>(parent == null,
"Parent users group '" + parentGroupName + "' does not exists");
EntitiesGroup group = CreateEntitiesGroup(usersGroupName);
group.Parent = parent;
group.AllParents.AddAll(parent.AllParents);
group.AllParents.Add(parent);
parent.DirectChildren.Add(group);
parent.AllChildren.Add(group);
return group;
}
/// <summary>
/// temporary string
/// </summary>
/// <param name="usersGroupName">Name of the users group.</param>
public virtual void RemoveUsersGroup(string usersGroupName)
{
UsersGroup group = GetUsersGroupByName(usersGroupName);
if (group == null)
return;
Guard.Against(group.DirectChildren.Count != 0, "Cannot remove users group '"+usersGroupName+"' because is has child groups. Remove those groups and try again.");
session.CreateQuery("delete Permission p where p.UsersGroup = :group")
.SetEntity("group", group)
.ExecuteUpdate();
// we have to do this in order to ensure that we play
// nicely with the second level cache and collection removals
if (group.Parent!=null)
{
group.Parent.DirectChildren.Remove(group);
}
foreach (UsersGroup parent in group.AllParents)
{
parent.AllChildren.Remove(group);
}
group.AllParents.Clear();
group.Users.Clear();
session.Delete(group);
}
///<summary>
/// Renames an existing users group
///</summary>
///<param name="usersGroupName">The name of the usersgroup to rename</param>
///<param name="newName">The new name of the usersgroup</param>
///<returns>The renamed group</returns>
public virtual UsersGroup RenameUsersGroup(string usersGroupName, string newName)
{
UsersGroup group = GetUsersGroupByName(usersGroupName);
Guard.Against(group == null, "There is no users group named: " + usersGroupName);
group.Name = newName;
session.Save(group);
return group;
}
/// <summary>
/// Removes the specified entities group.
/// Will also delete all permissions that are associated with this group.
/// </summary>
/// <param name="entitesGroupName">Name of the entites group.</param>
public virtual void RemoveEntitiesGroup(string entitesGroupName)
{
EntitiesGroup group = GetEntitiesGroupByName(entitesGroupName);
if(group==null)
return;
Guard.Against(group.DirectChildren.Count != 0, "Cannot remove entity group '" + entitesGroupName + "' because is has child groups. Remove those groups and try again.");
session.CreateQuery("delete Permission p where p.EntitiesGroup = :group")
.SetEntity("group", group)
.ExecuteUpdate();
// we have to do this in order to ensure that we play
// nicely with the second level cache and collection removals
if (group.Parent != null)
{
group.Parent.DirectChildren.Remove(group);
}
foreach (EntitiesGroup parent in group.AllParents)
{
parent.AllChildren.Remove(group);
}
group.AllParents.Clear();
group.Entities.Clear();
session.Delete(group);
}
/// <summary>
/// Removes the specified operation.
/// Will also delete all permissions for this operation
/// </summary>
/// <param name="operationName">The operation N ame.</param>
public virtual void RemoveOperation(string operationName)
{
Operation operation = GetOperationByName(operationName);
if(operation==null)
return;
Guard.Against(operation.Children.Count != 0, "Cannot remove operation '"+operationName+"' because it has child operations. Remove those operations and try again.");
session.CreateQuery("delete Permission p where p.Operation = :operation")
.SetEntity("operation", operation)
.ExecuteUpdate();
// so we can play safely with the 2nd level cache & collections
if(operation.Parent!=null)
{
operation.Parent.Children.Remove(operation);
}
session.Delete(operation);
}
/// <summary>
/// Gets the ancestry association of a user with the named users group.
/// This allows to track how a user is associated to a group through
/// their ancestry.
/// </summary>
/// <param name="user">The user.</param>
/// <param name="usersGroupName">Name of the users group.</param>
/// <returns></returns>
public virtual UsersGroup[] GetAncestryAssociation(IUser user, string usersGroupName)
{
UsersGroup desiredGroup = GetUsersGroupByName(usersGroupName);
ICollection<UsersGroup> directGroups =
SecurityCriterions.DirectUsersGroups((user))
.GetExecutableCriteria(session)
.SetCacheable(true)
.List<UsersGroup>();
if (directGroups.Contains(desiredGroup))
{
return new[] {desiredGroup};
}
// as a nice benefit, this does an eager load of all the groups in the hierarchy
// in an efficient way, so we don't have SELECT N + 1 here, nor do we need
// to load the Users collection (which may be very large) to check if we are associated
// directly or not
UsersGroup[] associatedGroups = GetAssociatedUsersGroupFor(user);
if (Array.IndexOf(associatedGroups, desiredGroup) == -1)
{
return new UsersGroup[0];
}
// now we need to find out the path to it
List<UsersGroup> shortest = new List<UsersGroup>();
foreach (UsersGroup usersGroup in associatedGroups)
{
List<UsersGroup> path = new List<UsersGroup>();
UsersGroup current = usersGroup;
while (current != null && current != desiredGroup)
{
path.Add(current);
current = current.Parent;
}
if (current != null)
path.Add(current);
// Valid paths are those that are contains the desired group
// and start in one of the groups that are directly associated
// with the user
if (path.Contains(desiredGroup) && directGroups.Contains(path[0]))
{
shortest = Min(shortest, path);
}
}
return shortest.ToArray();
}
/// <summary>
/// Gets the ancestry association of an entity with the named entity group.
/// This allows to track how an entity is associated to a group through
/// their ancestry.
/// </summary>
/// <param name="entity">The entity.</param>
/// <param name="entityGroupName">Name of the entity group.</param>
/// <returns></returns>
public virtual EntitiesGroup[] GetAncestryAssociationOfEntity<TEntity>(TEntity entity, string entityGroupName) where TEntity : class
{
EntitiesGroup desiredGroup = GetEntitiesGroupByName(entityGroupName);
ICollection<EntitiesGroup> directGroups =
SecurityCriterions.DirectEntitiesGroups(entity)
.GetExecutableCriteria(session)
.SetCacheable(true)
.List<EntitiesGroup>();
if (directGroups.Contains(desiredGroup))
{
return new[] { desiredGroup };
}
// as a nice benefit, this does an eager load of all the groups in the hierarchy
// in an efficient way, so we don't have SELECT N + 1 here, nor do we need
// to load the Entities collection (which may be very large) to check if we are associated
// directly or not
EntitiesGroup[] associatedGroups = GetAssociatedEntitiesGroupsFor(entity);
if (Array.IndexOf(associatedGroups, desiredGroup) == -1)
{
return new EntitiesGroup[0];
}
// now we need to find out the path to it
List<EntitiesGroup> shortest = new List<EntitiesGroup>();
foreach (EntitiesGroup entitiesGroup in associatedGroups)
{
List<EntitiesGroup> path = new List<EntitiesGroup>();
EntitiesGroup current = entitiesGroup;
while (current != null && current != desiredGroup)
{
path.Add(current);
current = current.Parent;
}
if (current != null)
path.Add(current);
// Valid paths are those that are contains the desired group
// and start in one of the groups that are directly associated
// with the user
if (path.Contains(desiredGroup) && directGroups.Contains(path[0]))
{
shortest = Min(shortest, path);
}
}
return shortest.ToArray();
}
/// <summary>
/// Creates a new entities group.
/// </summary>
/// <param name="name">The name of the new group.</param>
public virtual EntitiesGroup CreateEntitiesGroup(string name)
{
EntitiesGroup eg = new EntitiesGroup {Name = name};
session.Save(eg);
return eg;
}
///<summary>
/// Renames an existing entities group
///</summary>
///<param name="entitiesGroupName">The name of the entities group to rename</param>
///<param name="newName">The new name of the entities group</param>
///<returns>The renamed group</returns>
public virtual EntitiesGroup RenameEntitiesGroup(string entitiesGroupName, string newName)
{
EntitiesGroup group = GetEntitiesGroupByName(entitiesGroupName);
Guard.Against(group == null, "There is no entities group named: " + entitiesGroupName);
group.Name = newName;
session.Save(group);
return group;
}
/// <summary>
/// Gets the associated users group for the specified user.
/// </summary>
/// <param name="user">The user.</param>
public virtual UsersGroup[] GetAssociatedUsersGroupFor(IUser user)
{
ICollection<UsersGroup> usersGroups =
SecurityCriterions.AllGroups(user)
.GetExecutableCriteria(session)
.AddOrder(Order.Asc("Name"))
.SetCacheable(true)
.List<UsersGroup>();
return usersGroups.ToArray();
}
/// <summary>
/// Gets the users group by its name
/// </summary>
/// <param name="groupName">Name of the group.</param>
public virtual UsersGroup GetUsersGroupByName(string groupName)
{
return session.CreateCriteria<UsersGroup>()
.Add(Restrictions.Eq("Name", groupName))
.SetCacheable(true)
.UniqueResult<UsersGroup>();
}
/// <summary>
/// Gets the entities group by its groupName
/// </summary>
/// <param name="groupName">The name of the group.</param>
public virtual EntitiesGroup GetEntitiesGroupByName(string groupName)
{
return session.CreateCriteria<EntitiesGroup>()
.Add(Restrictions.Eq("Name", groupName))
.SetCacheable(true)
.UniqueResult<EntitiesGroup>();
}
/// <summary>
/// Gets the groups the specified entity is associated with
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="entity">The entity.</param>
/// <returns></returns>
public virtual EntitiesGroup[] GetAssociatedEntitiesGroupsFor<TEntity>(TEntity entity) where TEntity : class
{
ICollection<EntitiesGroup> entitiesGroups =
SecurityCriterions.AllGroups(entity)
.GetExecutableCriteria(session)
.AddOrder(Order.Asc("Name"))
.SetCacheable(true)
.List<EntitiesGroup>();
return entitiesGroups.ToArray();
}
/// <summary>
/// Associates the entity with a group with the specified name
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="entity">The entity.</param>
/// <param name="groupName">Name of the group.</param>
public virtual void AssociateEntityWith<TEntity>(TEntity entity, string groupName) where TEntity : class
{
EntitiesGroup entitiesGroup = GetEntitiesGroupByName(groupName);
Guard.Against<ArgumentException>(entitiesGroup == null, "There is no entities group named: " + groupName);
AssociateEntityWith(entity, entitiesGroup);
}
/// <summary>
/// Associates the entity with the specified group
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="entity">The entity.</param>
/// <param name="entitiesGroup">The entities group.</param>
public void AssociateEntityWith<TEntity>(TEntity entity, EntitiesGroup entitiesGroup) where TEntity : class
{
Guid key = Security.ExtractKey(entity);
EntityReference reference = GetOrCreateEntityReference<TEntity>(key);
entitiesGroup.Entities.Add(reference);
}
/// <summary>
/// Associates the user with a group with the specified name
/// </summary>
/// <param name="user">The user.</param>
/// <param name="groupName">Name of the group.</param>
public virtual void AssociateUserWith(IUser user, string groupName)
{
UsersGroup group = GetUsersGroupByName(groupName);
Guard.Against(group == null, "There is no users group named: " + groupName);
AssociateUserWith(user, group);
}
/// <summary>
/// Associates the user with a group with the specified name
/// </summary>
/// <param name="user">The user.</param>
/// <param name="group">The group.</param>
public void AssociateUserWith(IUser user, UsersGroup group)
{
group.Users.Add(user);
}
/// <summary>
/// Creates the operation with the given name
/// </summary>
/// <param name="operationName">Name of the operation.</param>
/// <returns></returns>
public virtual Operation CreateOperation(string operationName)
{
Guard.Against<ArgumentException>(string.IsNullOrEmpty(operationName), "operationName must have a value");
Guard.Against<ArgumentException>(operationName[0] != '/', "Operation names must start with '/'");
Operation op = new Operation {Name = operationName};
string parentOperationName = Strings.GetParentOperationName(operationName);
if (parentOperationName != string.Empty) //we haven't got to the root
{
Operation parentOperation = GetOperationByName(parentOperationName);
if (parentOperation == null)
parentOperation = CreateOperation(parentOperationName);
op.Parent = parentOperation;
parentOperation.Children.Add(op);
}
session.Save(op);
return op;
}
/// <summary>
/// Gets the operation by the specified name
/// </summary>
/// <param name="operationName">Name of the operation.</param>
/// <returns></returns>
public virtual Operation GetOperationByName(string operationName)
{
return session.CreateCriteria<Operation>()
.Add(Restrictions.Eq("Name", operationName))
.SetCacheable(true)
.UniqueResult<Operation>();
}
/// <summary>
/// Removes the user from the specified group
/// </summary>
/// <param name="user">The user.</param>
/// <param name="usersGroupName">Name of the users group.</param>
public void DetachUserFromGroup(IUser user, string usersGroupName)
{
UsersGroup group = GetUsersGroupByName(usersGroupName);
Guard.Against(group == null, "There is no users group named: " + usersGroupName);
group.Users.Remove(user);
}
/// <summary>
/// Removes the entities from the specified group
/// </summary>
/// <typeparam name="TEntity">The type of the entity.</typeparam>
/// <param name="entity">The entity.</param>
/// <param name="entitiesGroupName">Name of the entities group.</param>
public void DetachEntityFromGroup<TEntity>(TEntity entity, string entitiesGroupName) where TEntity : class
{
EntitiesGroup entitiesGroup = GetEntitiesGroupByName(entitiesGroupName);
Guard.Against<ArgumentException>(entitiesGroup == null,
"There is no entities group named: " + entitiesGroupName);
Guid key = Security.ExtractKey(entity);
EntityReference reference = GetOrCreateEntityReference<TEntity>(key);
entitiesGroup.Entities.Remove(reference);
}
/// <summary>
/// Removes the user from rhino security.
/// This does NOT delete the user itself, merely reset all the
/// information that rhino security knows about it.
/// It also allows it to be removed by external API without violating
/// FK constraints
/// </summary>
/// <param name="user">The user.</param>
public void RemoveUser(IUser user)
{
ICollection<UsersGroup> groups =
SecurityCriterions.DirectUsersGroups((user))
.GetExecutableCriteria(session)
.SetCacheable(true)
.List<UsersGroup>();
foreach (UsersGroup group in groups)
{
group.Users.Remove(user);
}
session.CreateQuery("delete Permission p where p.User = :user")
.SetEntity("user", user)
.ExecuteUpdate();
}
/// <summary>
/// Removes the specified permission.
/// </summary>
/// <param name="permission">The permission.</param>
public void RemovePermission(Permission permission)
{
session.Delete(permission);
}
#endregion
private static List<UsersGroup> Min(List<UsersGroup> first, List<UsersGroup> second)
{
if (first.Count == 0)
return second;
if (first.Count <= second.Count)
return first;
return second;
}
private static List<EntitiesGroup> Min(List<EntitiesGroup> first, List<EntitiesGroup> second)
{
if (first.Count == 0)
return second;
if (first.Count <= second.Count)
return first;
return second;
}
private EntityReference GetOrCreateEntityReference<TEntity>(Guid key)
{
EntityReference reference = session.CreateCriteria<EntityReference>()
.Add(Restrictions.Eq("EntitySecurityKey", key))
.SetCacheable(true)
.UniqueResult<EntityReference>();
if (reference == null)
{
reference = new EntityReference();
reference.EntitySecurityKey = key;
reference.Type = GetOrCreateEntityType<TEntity>();
session.Save(reference);
}
return reference;
}
private EntityType GetOrCreateEntityType<TEntity>()
{
EntityType entityType = session.CreateCriteria<EntityType>()
.Add(Restrictions.Eq("Name", typeof (TEntity).FullName))
.SetCacheable(true)
.UniqueResult<EntityType>();
if (entityType == null)
{
entityType = new EntityType {Name = typeof (TEntity).FullName};
session.Save(entityType);
}
return entityType;
}
}
}