-
Notifications
You must be signed in to change notification settings - Fork 10
/
PermissionManager.cs
160 lines (130 loc) · 5.03 KB
/
PermissionManager.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
156
157
158
159
160
using System;
using System.Collections.Generic;
using System.Text;
using Watson.ORM.Sqlite;
using Watson.ORM.Core;
using ExpressionTree;
namespace GateKeeper
{
/// <summary>
/// Permission manager.
/// </summary>
public class PermissionManager
{
#region Public-Members
#endregion
#region Internal-Members
internal ResourceManager Resources { get; set; } = null;
internal RoleManager Roles { get; set; } = null;
internal UserManager Users { get; set; } = null;
internal UserRoleManager UserRoles { get; set; } = null;
#endregion
#region Private-Members
private WatsonORM _ORM = null;
#endregion
#region Constructors-and-Factories
/// <summary>
/// Instantiate.
/// </summary>
/// <param name="orm">ORM.</param>
public PermissionManager(WatsonORM orm)
{
_ORM = orm ?? throw new ArgumentNullException(nameof(orm));
}
#endregion
#region Public-Methods
/// <summary>
/// Add.
/// </summary>
/// <param name="permission">Permission.</param>
/// <returns>Object.</returns>
public Permission Add(Permission permission)
{
if (permission == null) throw new ArgumentNullException(nameof(permission));
if (ExistsByName(permission.Name))
throw new ArgumentException("An item with the same key has already been added.");
return _ORM.Insert<Permission>(permission);
}
/// <summary>
/// Remove.
/// </summary>
/// <param name="permission">Permission.</param>
public void Remove(Permission permission)
{
if (permission == null) throw new ArgumentNullException(nameof(permission));
if (!ExistsByName(permission.Name))
throw new KeyNotFoundException("The specified key was not found.");
_ORM.Delete<Permission>(permission);
}
/// <summary>
/// Remove all permissions for a given resource.
/// </summary>
/// <param name="resource">Resource.</param>
public void RemoveResourcePermissions(Resource resource)
{
if (resource == null) throw new ArgumentNullException(nameof(resource));
Expr e = new Expr(_ORM.GetColumnName<Permission>(nameof(Permission.ResourceGUID)), OperatorEnum.Equals, resource.GUID);
_ORM.DeleteMany<Permission>(e);
}
/// <summary>
/// Remove by name.
/// </summary>
/// <param name="name">Name.</param>
public void RemoveByName(string name)
{
if (String.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
if (!ExistsByName(name))
throw new KeyNotFoundException("The specified key was not found.");
Permission p = GetFirstByName(name);
if (p == null)
throw new KeyNotFoundException("The specified key was not found.");
_ORM.Delete<Permission>(p);
}
/// <summary>
/// Retrieve all.
/// </summary>
/// <returns>List.</returns>
public List<Permission> All()
{
Expr e = new Expr(_ORM.GetColumnName<Permission>(nameof(Permission.Id)), OperatorEnum.GreaterThan, 0);
return _ORM.SelectMany<Permission>(e);
}
/// <summary>
/// Retrieve first record by name.
/// </summary>
/// <param name="name">Name.</param>
/// <returns>Object.</returns>
public Permission GetFirstByName(string name)
{
if (String.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
Expr e = new Expr(_ORM.GetColumnName<Permission>(nameof(Permission.Name)), OperatorEnum.Equals, name);
Permission p = _ORM.SelectFirst<Permission>(e);
return p;
}
/// <summary>
/// Retrieve permissions associated with a resource.
/// </summary>
/// <param name="resource">Resource.</param>
/// <returns>List.</returns>
public List<Permission> GetByResource(Resource resource)
{
if (resource == null) throw new ArgumentNullException(nameof(resource));
Expr e = new Expr(_ORM.GetColumnName<Permission>(nameof(Permission.ResourceGUID)), OperatorEnum.Equals, resource.GUID);
return _ORM.SelectMany<Permission>(e);
}
/// <summary>
/// Check existence by name.
/// </summary>
/// <param name="name">Name.</param>
/// <returns>True if exists.</returns>
public bool ExistsByName(string name)
{
if (String.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
Expr e = new Expr(_ORM.GetColumnName<Permission>(nameof(Permission.Name)), OperatorEnum.Equals, name);
return _ORM.Exists<Permission>(e);
}
#endregion
#region Private-Methods
#endregion
}
}