forked from jasontaylordev/flexible-aspnetcore-authorization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Permissions.dib
93 lines (64 loc) · 2.67 KB
/
Permissions.dib
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
#!csharp
[Flags]
public enum Permissions
{
None = 0, // 00000
A = 1, // 00001
B = 2, // 00010
C = 4, // 00100
D = 8, // 01000
E = 16, // 10000
All = ~None // 11111
}
// Permissions are stored using a C# flags enum
// Applying the attribute indicates the enum can be treated as a bit field, i.e. a set of flags.
// Note: Values are powers of 2, e.g. 2, 4, 8, 16, ...
// ~None - The operator ~ produces a complement of its operand by reversing each bit,
// So ~00000 = 11111 = A, B, C, D, E
var userPermissions = Permissions.A | Permissions.C; // 00001 + 00100 = 00101 = 5
var requiredPermissions = Permissions.A | Permissions.B; // 00001 + 00010 = 00011 = 3
#!csharp
// Output permissions (as string)
Console.WriteLine($"User Permissions: {userPermissions}");
#!csharp
Console.WriteLine($"Required Permissions: {requiredPermissions}");
#!csharp
// Output permissions (as int)
Console.WriteLine($"User Permissions: {(int)userPermissions}");
Console.WriteLine($"Required Permissions: {(int)requiredPermissions}");
#!csharp
// Check permissions
Console.WriteLine($"User Permissions: {userPermissions}");
Console.WriteLine($"Required Permissions: {requiredPermissions}");
Console.WriteLine($"Authorised: {((requiredPermissions & userPermissions) != 0)}");
#!csharp
// Add permission (with logical OR operator)
userPermissions |= Permissions.B;
// Output permissions (as string)
Console.WriteLine($"User Permissions: {userPermissions}");
#!csharp
// Remove permission (with logical XOR operator)
userPermissions ^= Permissions.B;
// Output permissions (as string)
Console.WriteLine($"User Permissions: {userPermissions}");
#!csharp
requiredPermissions = Permissions.A | Permissions.B;
// Create a policy name
// [Authorize(Permissions.A | Permission.B)]
private const string PolicyPrefix = "Permissions";
var policyName = $"{PolicyPrefix}{(int)requiredPermissions}";
// Output policy name
Console.WriteLine($"Required Permissions: {requiredPermissions} ({(int)requiredPermissions})");
Console.WriteLine($"Policy Name: '{policyName}'");
#!csharp
// Update required permissions and policy name
requiredPermissions = Permissions.A | Permissions.B | Permissions.C;
policyName = $"{PolicyPrefix}{(int)requiredPermissions}";
// Output policy name
Console.WriteLine($"Required Permissions: {requiredPermissions} ({(int)requiredPermissions})");
Console.WriteLine($"Policy Name: '{policyName}'");
#!csharp
// Get permissions based on policy name
var permissionsValue = int.Parse(policyName[PolicyPrefix.Length..]);
Console.WriteLine($"Policy Name: {policyName}");
Console.WriteLine($"Required Permissions: {(Permissions)permissionsValue}");