-
Notifications
You must be signed in to change notification settings - Fork 10
/
RoleManager.cs
141 lines (111 loc) · 3.9 KB
/
RoleManager.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
using System;
using System.Collections.Generic;
using System.Text;
using Watson.ORM.Sqlite;
using Watson.ORM.Core;
using ExpressionTree;
namespace GateKeeper
{
/// <summary>
/// Role manager.
/// </summary>
public class RoleManager
{
#region Public-Members
#endregion
#region Internal-Members
internal PermissionManager Permissions { get; set; } = null;
internal ResourceManager Resources { 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 RoleManager(WatsonORM orm)
{
_ORM = orm ?? throw new ArgumentNullException(nameof(orm));
}
#endregion
#region Public-Methods
/// <summary>
/// Add.
/// </summary>
/// <param name="role">Role.</param>
/// <returns>Object.</returns>
public Role Add(Role role)
{
if (role == null) throw new ArgumentNullException(nameof(role));
if (ExistsByName(role.Name))
throw new ArgumentException("An item with the same key has already been added.");
return _ORM.Insert<Role>(role);
}
/// <summary>
/// Remove.
/// </summary>
/// <param name="role">Role.</param>
public void Remove(Role role)
{
if (role == null) throw new ArgumentNullException(nameof(role));
if (!ExistsByName(role.Name))
throw new KeyNotFoundException("The specified key was not found.");
UserRoles.RemoveUserRolesByRole(role);
_ORM.Delete<Role>(role);
}
/// <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.");
Role r = GetFirstByName(name);
if (r == null)
throw new KeyNotFoundException("The specified key was not found.");
UserRoles.RemoveUserRolesByRole(r);
_ORM.Delete<Role>(r);
}
/// <summary>
/// Retrieve all.
/// </summary>
/// <returns>List.</returns>
public List<Role> All()
{
Expr e = new Expr(_ORM.GetColumnName<Role>(nameof(Role.Id)), OperatorEnum.GreaterThan, 0);
return _ORM.SelectMany<Role>(e);
}
/// <summary>
/// Retrieve first record by name.
/// </summary>
/// <param name="name">Name.</param>
/// <returns>Object.</returns>
public Role GetFirstByName(string name)
{
if (String.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
Expr e = new Expr(_ORM.GetColumnName<Role>(nameof(Role.Name)), OperatorEnum.Equals, name);
Role r = _ORM.SelectFirst<Role>(e);
return r;
}
/// <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<Role>(nameof(Role.Name)), OperatorEnum.Equals, name);
return _ORM.Exists<Role>(e);
}
#endregion
#region Private-Methods
#endregion
}
}