PermissionDisplay.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright (c) 2018 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
  2. // Licensed under MIT license. See License.txt in the project root for license information.
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Reflection;
  5. namespace Hotline.Permissions
  6. {
  7. public class PermissionDisplay
  8. {
  9. public PermissionDisplay(string groupName, string name, string description, EPermission permission,
  10. string moduleName)
  11. {
  12. Permission = permission;
  13. GroupName = groupName;
  14. ShortName = name ?? throw new ArgumentNullException(nameof(name));
  15. Description = description ?? throw new ArgumentNullException(nameof(description));
  16. ModuleName = moduleName;
  17. }
  18. /// <summary>
  19. /// GroupName, which groups permissions working in the same area
  20. /// </summary>
  21. public string GroupName { get; private set; }
  22. /// <summary>
  23. /// ShortName of the permission - often says what it does, e.g. Read
  24. /// </summary>
  25. public string ShortName { get; private set; }
  26. /// <summary>
  27. /// Long description of what action this permission allows
  28. /// </summary>
  29. public string Description { get; private set; }
  30. /// <summary>
  31. /// Gives the actual permission
  32. /// </summary>
  33. public EPermission Permission { get; private set; }
  34. /// <summary>
  35. /// Contains an optional paidForModule that this feature is linked to
  36. /// </summary>
  37. public string ModuleName { get; private set; }
  38. public string Menu { get; private set; }
  39. /// <summary>
  40. /// This returns
  41. /// </summary>
  42. /// <returns></returns>
  43. public static List<PermissionDisplay> GetPermissionsToDisplay(Type enumType)
  44. {
  45. var result = new List<PermissionDisplay>();
  46. foreach (var permissionName in Enum.GetNames(enumType))
  47. {
  48. var member = enumType.GetMember(permissionName);
  49. //This allows you to obsolete a permission and it won't be shown as a possible option, but is still there so you won't reuse the number
  50. var obsoleteAttribute = member[0].GetCustomAttribute<ObsoleteAttribute>();
  51. if (obsoleteAttribute != null)
  52. continue;
  53. //If there is no DisplayAttribute then the Enum is not used
  54. var displayAttribute = member[0].GetCustomAttribute<DisplayAttribute>();
  55. if (displayAttribute == null)
  56. continue;
  57. //Gets the optional PaidForModule that a permission can be linked to
  58. var moduleAttribute = member[0].GetCustomAttribute<LinkedToModuleAttribute>();
  59. var permission = (EPermission)Enum.Parse(enumType, permissionName, false);
  60. result.Add(new PermissionDisplay(displayAttribute.GroupName, displayAttribute.Name,
  61. displayAttribute.Description, permission, moduleAttribute?.PaidForModule.ToString()));
  62. }
  63. return result;
  64. }
  65. }
  66. }