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