-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathRbac.cs
235 lines (198 loc) · 8.66 KB
/
Rbac.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#region Copyright Notice
/* Copyright (c) 2017, Deb'jyoti Das - [email protected]
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are not permitted.Neither the name of the
'Deb'jyoti Das' nor the names of its contributors may be used
to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY Deb'jyoti Das 'AS IS' AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL Debjyoti OR Deb'jyoti OR Debojyoti Das OR Eyedia BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#region Developer Information
/*
Author - Debjyoti Das ([email protected])
Created - 11/12/2017 3:31:31 PM
Description -
Modified By -
Description -
*/
#endregion Developer Information
#endregion Copyright Notice
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;
namespace Eyedia.Aarbac.Framework
{
public partial class Rbac : RbacBase, IRbac, IDisposable
{
public int RbacId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public string ConnectionString { get; internal set; }
public string MetaDataRbac { get; internal set; }
public string MetaDataEntitlements { get; internal set; }
public int Version { get; set; }
private byte[] PassKey;
public StringWriterTrace Trace { get; private set; }
public RbacUser User { get; private set; }
public bool IsAuthenticated { get; private set; }
public Rbac()
{
//this is only to satisfy serializers
this.Trace = new StringWriterTrace();
}
/// <summary>
/// This instance is allowed to call only from test bed - todo security
/// </summary>
/// <param name="userName">The logged in user name</param>
/// <param name="rbacName">The overridden rbac name, empty will use user's default</param>
/// <param name="roleName">The overriden role name, empty will use user's default</param>
public Rbac(string userName, string rbacName, string roleName)
{
//no cache will be implemented
DataManager.Manager manager = new DataManager.Manager(false);
RbacUser user = new RbacUser(userName);
if (!string.IsNullOrEmpty(roleName))
user.Role = new RbacRole(roleName); //override role
if (user.Role == null)
RbacException.Raise(string.Format("The role '{0}' is not found!", roleName), RbacExceptionCategories.Repository);
Rbac dbRbac = null;
if (!string.IsNullOrEmpty(rbacName))
dbRbac = manager.GetRbac(rbacName); //override rbac
else
dbRbac = manager.GetRbac(user.Role.RbacId);
if (dbRbac == null)
RbacException.Raise(string.Format("The rbac '{0}' was not defined yet!", rbacName), RbacExceptionCategories.Repository);
dbRbac.User = user;
Assign(dbRbac);
}
public Rbac(string userName)
{
Rbac rbacFromCache = RbacCache.Instance.GetContext(userName);
if (rbacFromCache == null)
{
RbacUser user = new RbacUser(userName);
Rbac dbRbac = new DataManager.Manager(false).GetRbac(user.Role.RbacId);
dbRbac.User = user;
Assign(dbRbac);
RbacCache.Instance.Contexts.Add(userName, this);
}
else
{
Assign(rbacFromCache);
}
}
public Rbac CreateNew(string rbacName, string description, string connectionString, string metaDataEntitlement)
{
DataManager.Manager manager = new DataManager.Manager(false);
if (manager.GetRbac(rbacName) != null)
RbacException.Raise(string.Format("'{0}' already exists! Please provide a different name.", rbacName), RbacExceptionCategories.Repository);
Rbac newRbac = new Rbac();
newRbac.Name = rbacName;
newRbac.Description = description;
newRbac.ConnectionString = connectionString;
N("Generating meta data...");
newRbac.MetaDataRbac = new RbacMetaData().Generate(newRbac.ConnectionString);
N("Done!", LogMessageTypes.Success);
N("Saving your rbac instance...");
Rbac rbac = manager.AddOrUpdate(newRbac);
N("Done!", LogMessageTypes.Success);
return rbac;
}
/// <summary>
/// This will refresh the rule from the meta data, byt merging latest changes from the database
/// </summary>
public void Refresh()
{
DataManager.Manager manager = new DataManager.Manager(false);
List<RbacRole> roles = manager.GetRoles(this.RbacId);
foreach (RbacRole role in roles)
{
role.MetaDataRbac = new RbacMetaData().Merge(ConnectionString, role.MetaDataRbac);
manager.AddOrUpdate(role);
}
}
private void Assign(Rbac rbac)
{
this.User = rbac.User;
this.RbacId = rbac.RbacId;
this.Name = rbac.Name;
this.Description = rbac.Description;
this.ConnectionString = rbac.ConnectionString;
this.MetaDataRbac = rbac.MetaDataRbac;
this.MetaDataEntitlements = rbac.MetaDataEntitlements;
this.Version = rbac.Version;
this.MetaDataEntitlements = rbac.MetaDataEntitlements;
this.Trace = new StringWriterTrace();
}
public void Save()
{
new DataManager.Manager().AddOrUpdate(this);
}
public bool Authenticate(string password)
{
IsAuthenticated = new DataManager.Manager().Authenticate(this.RbacId, password);
if (!IsAuthenticated)
RbacException.Raise("Incorrect password!");
return IsAuthenticated;
}
public RbacUser Authenticate(string userName, string password)
{
RbacUser user = GetUser(userName);
if (user == null)
RbacException.Raise("User name was not found!");
user = new DataManager.Manager().Authenticate(userName, password);
if (user == null)
RbacException.Raise("Incorrect password!");
return user;
}
public RbacRole CreateRole(string roleName, string roleDescription, string metaDataRbac, string metaDataEntitlements)
{
return new RbacRole(this.RbacId, roleName, roleDescription, metaDataRbac, metaDataEntitlements);
}
public void Export(string fileName)
{
Rbac rbac = new DataManager.Manager(false).GetRbac(this.Name);
RbacEngineWeb wRbac = new RbacEngineWeb(rbac);
StreamWriter sw = new StreamWriter(fileName);
var s = new System.Xml.Serialization.XmlSerializer(wRbac.GetType());
s.Serialize(sw, wRbac);
sw.Close();
}
#region Helpers
int indentLevel = 0;
public void WriteTrace(string message)
{
string prefix = string.Empty;
Trace.Write(new string('\t', indentLevel) + prefix + message);
}
public override string ToString()
{
return Trace.ToString();
}
public void Dispose()
{
if (this.Trace != null)
{
this.Trace.Close();
this.Trace.Dispose();
}
}
#endregion Helpers
}
}