PermissionDictionary.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using Abp;
  4. using Abp.Authorization;
  5. namespace IwbZero.Authorization.Permissions
  6. {
  7. public class IwbPermissionDictionary : Dictionary<string, Permission>
  8. {
  9. /// <summary>
  10. /// Adds all child permissions of current permissions recursively.
  11. /// </summary>
  12. public void AddAllPermissions()
  13. {
  14. foreach (var permission in Values.ToList())
  15. {
  16. AddPermissionRecursively(permission);
  17. }
  18. }
  19. /// <summary>
  20. /// Adds a permission and it's all child permissions to dictionary.
  21. /// </summary>
  22. /// <param name="permission">Permission to be added</param>
  23. private void AddPermissionRecursively(Permission permission)
  24. {
  25. //Prevent multiple adding of same named permission.
  26. if (TryGetValue(permission.Name, out var existingPermission))
  27. {
  28. if (existingPermission != permission)
  29. {
  30. throw new AbpInitializationException("Duplicate permission name detected for " + permission.Name);
  31. }
  32. }
  33. else
  34. {
  35. this[permission.Name] = permission;
  36. }
  37. //Add child permissions (recursive call)
  38. foreach (var childPermission in permission.Children)
  39. {
  40. AddPermissionRecursively(childPermission);
  41. }
  42. }
  43. }
  44. }