-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPermission.cs
155 lines (123 loc) · 4.62 KB
/
Permission.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
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace PermissionsWithBitArray
{
public class Permission
{
private enum PermissionValue
{
ListSuppliers,
GetSupplier,
AddSupplier,
UpdateSupplier,
ListUsers,
GetUser,
AddUser,
UpdateUser
}
private static int PermissionValuesCount { get; } = Enum.GetValues(typeof(PermissionValue)).Length;
private static readonly Dictionary<string, Permission> permissionsDictionary;
static Permission()
{
permissionsDictionary = new Dictionary<string, Permission>();
var type = typeof(Permission);
var props = type.GetProperties();
foreach(var p in props)
{
var v = p.GetValue(null, null);
if(v is Permission)
{
var permission = v as Permission;
permissionsDictionary.Add(p.Name, permission);
}
}
}
#region Suppliers
public static Permission ListSuppliers { get; } = new Permission(PermissionValue.ListSuppliers);
public static Permission GetSupplier { get; } = new Permission(PermissionValue.GetSupplier);
public static Permission AddSupplier { get; } = new Permission(PermissionValue.AddSupplier);
public static Permission UpdateSupplier { get; } = new Permission(PermissionValue.UpdateSupplier);
public static Permission Supplier { get; } = ListSuppliers | GetSupplier | AddSupplier | UpdateSupplier;
#endregion
#region Users
public static Permission ListUsers { get; } = new Permission(PermissionValue.ListUsers);
public static Permission GetUser { get; } = new Permission(PermissionValue.GetUser);
public static Permission AddUser { get; } = new Permission(PermissionValue.AddUser);
public static Permission UpdateUser { get; } = new Permission(PermissionValue.UpdateUser);
public static Permission User { get; } = ListUsers | GetUser | AddUser | UpdateUser;
#endregion
public static Permission All { get; } = Supplier | User;
public static Permission operator |(Permission left, Permission right)
{
var permission = new Permission(left);
permission.Or(right);
return permission;
}
public static Permission operator &(Permission left, Permission right)
{
var permission = new Permission(left);
permission.And(right);
return permission;
}
public static Permission operator !(Permission permission)
{
var invertedPermission = new Permission(permission);
return invertedPermission.Not();
}
public static Permission FromString(string str)
{
return permissionsDictionary.ContainsKey(str) ? permissionsDictionary[str] : null;
}
private readonly BitArray bitArray;
public Permission()
{
bitArray = new BitArray(PermissionValuesCount);
}
public Permission(Permission permission)
{
bitArray = new BitArray(permission.bitArray);
}
private Permission(PermissionValue permissionValue) : this()
{
bitArray.Set((int)permissionValue, true);
}
public Permission Or(Permission value)
{
bitArray.Or(value.bitArray);
return this;
}
public Permission And(Permission value)
{
bitArray.And(value.bitArray);
return this;
}
public Permission Not()
{
bitArray.Not();
return this;
}
public bool Has(Permission value)
{
for (int i = 0; i < bitArray.Count; i++)
{
if (value.bitArray.Get(i) && bitArray.Get(i) != value.bitArray.Get(i))
{
return false;
}
}
return true;
}
public override string ToString()
{
var stringBuilder = new StringBuilder();
for(int i = 0; i < bitArray.Count; i++)
{
var bit = bitArray.Get(i);
stringBuilder.Append(bit ? "1" : "0");
}
return stringBuilder.ToString();
}
}
}