/
PermissionDisplay.cs
76 lines (67 loc) · 3.1 KB
/
PermissionDisplay.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
// Copyright (c) 2018 Jon P Smith, GitHub: JonPSmith, web: http://www.thereformedprogrammer.net/
// Licensed under MIT license. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace PermissionParts
{
public class PermissionDisplay
{
public PermissionDisplay(string groupName, string name, string description, Permissions permission,
string moduleName)
{
Permission = permission;
GroupName = groupName;
ShortName = name ?? throw new ArgumentNullException(nameof(name));
Description = description ?? throw new ArgumentNullException(nameof(description));
ModuleName = moduleName;
}
/// <summary>
/// GroupName, which groups permissions working in the same area
/// </summary>
public string GroupName { get; private set; }
/// <summary>
/// ShortName of the permission - often says what it does, e.g. Read
/// </summary>
public string ShortName { get; private set; }
/// <summary>
/// Long description of what action this permission allows
/// </summary>
public string Description { get; private set; }
/// <summary>
/// Gives the actual permission
/// </summary>
public Permissions Permission { get; private set; }
/// <summary>
/// Contains an optional paidForModule that this feature is linked to
/// </summary>
public string ModuleName { get; private set; }
/// <summary>
/// This returns
/// </summary>
/// <returns></returns>
public static List<PermissionDisplay> GetPermissionsToDisplay(Type enumType)
{
var result = new List<PermissionDisplay>();
foreach (var permissionName in Enum.GetNames(enumType))
{
var member = enumType.GetMember(permissionName);
//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
var obsoleteAttribute = member[0].GetCustomAttribute<ObsoleteAttribute>();
if (obsoleteAttribute != null)
continue;
//If there is no DisplayAttribute then the Enum is not used
var displayAttribute = member[0].GetCustomAttribute<DisplayAttribute>();
if (displayAttribute == null)
continue;
//Gets the optional PaidForModule that a permission can be linked to
var moduleAttribute = member[0].GetCustomAttribute<LinkedToModuleAttribute>();
var permission = (Permissions)Enum.Parse(enumType, permissionName, false);
result.Add(new PermissionDisplay(displayAttribute.GroupName, displayAttribute.Name,
displayAttribute.Description, permission, moduleAttribute?.PaidForModule.ToString()));
}
return result;
}
}
}