Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Privileges.cs
2using System.Threading.Tasks;
6
8{
12 public class Privileges
13 {
14 private static readonly Dictionary<string, Privilege> privileges = new Dictionary<string, Privilege>();
15 private static readonly MultiReadSingleWriteObject synchObj = new MultiReadSingleWriteObject(typeof(Privileges));
16
23 public static async Task<Privilege> GetPrivilege(string PrivilegeId)
24 {
25 await synchObj.BeginWrite();
26 try
27 {
28 return await GetPrivilegeLocked(PrivilegeId);
29 }
30 finally
31 {
32 await synchObj.EndWrite();
33 }
34 }
35
36 private static async Task<Privilege> GetPrivilegeLocked(string PrivilegeId)
37 {
38 if (privileges.TryGetValue(PrivilegeId, out Privilege Privilege))
39 return Privilege;
40
41 Privilege = await Database.FindFirstDeleteRest<Privilege>(new FilterFieldEqualTo("FullId", PrivilegeId));
42 if (Privilege is null)
43 {
44 string ParentId;
45 string LocalId;
46 int i = PrivilegeId.LastIndexOf('.');
47
48 if (i < 0)
49 {
50 ParentId = string.Empty;
51 LocalId = PrivilegeId;
52 }
53 else
54 {
55 ParentId = PrivilegeId.Substring(0, i);
56 LocalId = PrivilegeId.Substring(i + 1);
57 }
58
59 Privilege = new Privilege()
60 {
61 ParentFullId = ParentId,
62 LocalId = LocalId,
63 FullId = PrivilegeId,
64 Description = string.Empty
65 };
66
67 await Database.Insert(Privilege);
68 }
69
70 if (!string.IsNullOrEmpty(Privilege.ParentFullId))
71 Privilege.Parent = await GetPrivilegeLocked(Privilege.ParentFullId);
72
73 privileges[PrivilegeId] = Privilege;
74
75 return Privilege;
76 }
77
81 public static async Task LoadAll()
82 {
83 await synchObj.BeginWrite();
84 try
85 {
86 privileges.Clear();
87
88 IEnumerable<Privilege> Privileges = await Database.Find<Privilege>();
89
90 foreach (Privilege Privilege in Privileges)
91 privileges[Privilege.FullId] = Privilege;
92
93 foreach (Privilege Privilege in Privileges)
94 {
95 if (!string.IsNullOrEmpty(Privilege.ParentFullId))
96 Privilege.Parent = await GetPrivilegeLocked(Privilege.ParentFullId);
97 }
98 }
99 finally
100 {
101 await synchObj.EndWrite();
102 }
103 }
104 }
105}
Static interface for database persistence. In order to work, a database provider has to be assigned t...
Definition: Database.cs:21
static Task< IEnumerable< object > > Find(string Collection, params string[] SortOrder)
Finds objects in a given collection.
Definition: Database.cs:238
static async Task Insert(object Object)
Inserts an object into the default collection of the database.
Definition: Database.cs:97
This filter selects objects that have a named field equal to a given value.
Represents an object that allows single concurrent writers but multiple concurrent readers....
Corresponds to a privilege in the system.
Definition: Privilege.cs:16
Privilege Parent
Parent privilege.
Definition: Privilege.cs:83
string ParentFullId
Full Privilege ID of parent privilege. If the empty string, privilege is a root privilege.
Definition: Privilege.cs:45
string FullId
Full Privilege ID. Corresponds to the concatenation of ancestor IDs with the local ID,...
Definition: Privilege.cs:63
Maintains the collection of all privileges in the system.
Definition: Privileges.cs:13
static async Task LoadAll()
Loads all privileges
Definition: Privileges.cs:81
static async Task< Privilege > GetPrivilege(string PrivilegeId)
Gets the Privilege object corresponding to a full Privilege ID.
Definition: Privileges.cs:23