/
PrincipalPermission.cs
305 lines (262 loc) · 10.1 KB
/
PrincipalPermission.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Generic;
using System.Security.Principal;
using System.Threading;
namespace System.Security.Permissions
{
#if NETCOREAPP
[Obsolete(Obsoletions.CodeAccessSecurityMessage, DiagnosticId = Obsoletions.CodeAccessSecurityDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
#endif
public sealed class PrincipalPermission : IPermission, ISecurityEncodable, IUnrestrictedPermission
{
private IDRole[] _idArray;
public PrincipalPermission(PermissionState state)
{
if (state == PermissionState.Unrestricted)
{
_idArray = new IDRole[] { new IDRole(true, null, null) };
}
else if (state == PermissionState.None)
{
_idArray = new IDRole[] { new IDRole(false, string.Empty, string.Empty) };
}
else
{
throw new ArgumentException(SR.Argument_InvalidPermissionState);
}
}
public PrincipalPermission(string name, string role)
{
_idArray = new IDRole[] { new IDRole(true, name, role) };
}
public PrincipalPermission(string name, string role, bool isAuthenticated)
{
_idArray = new IDRole[] { new IDRole(isAuthenticated, name, role) };
}
private PrincipalPermission(IDRole[] array)
{
_idArray = array;
}
private bool IsEmpty()
{
foreach (IDRole idRole in _idArray)
{
if (idRole.ID == null || idRole.ID.Length != 0 || idRole.Role == null || idRole.Role.Length != 0 || idRole.Authenticated)
return false;
}
return true;
}
private bool VerifyType(IPermission perm)
{
// if perm is null, then obviously not of the same type
return (perm != null) && (perm.GetType() == GetType());
}
public bool IsUnrestricted()
{
foreach (IDRole idRole in _idArray)
{
if (idRole.ID != null || idRole.Role != null || !idRole.Authenticated)
return false;
}
return true;
}
public bool IsSubsetOf(IPermission target)
{
if (target == null)
{
return IsEmpty();
}
else if (!VerifyType(target))
{
throw new ArgumentException(SR.Format(SR.Argument_WrongType, GetType().FullName), nameof(target));
}
PrincipalPermission operand = (PrincipalPermission)target;
if (operand.IsUnrestricted())
{
return true;
}
else if (IsUnrestricted())
{
return false;
}
foreach (IDRole idRole in _idArray)
{
bool foundMatch = false;
foreach (IDRole operandIdRole in operand._idArray)
{
if ((operandIdRole.Authenticated == idRole.Authenticated) &&
(operandIdRole.ID == null || (idRole.ID != null && idRole.ID.Equals(operandIdRole.ID))) &&
(operandIdRole.Role == null || (idRole.Role != null && idRole.Role.Equals(operandIdRole.Role))))
{
foundMatch = true;
break;
}
}
if (!foundMatch)
return false;
}
return true;
}
public IPermission Intersect(IPermission target)
{
if (target == null)
{
return null;
}
else if (!VerifyType(target))
{
throw new ArgumentException(SR.Format(SR.Argument_WrongType, GetType().FullName), nameof(target));
}
else if (IsUnrestricted())
{
return target.Copy();
}
PrincipalPermission operand = (PrincipalPermission)target;
if (operand.IsUnrestricted())
{
return Copy();
}
List<IDRole> idroles = null;
foreach (IDRole idRole in _idArray)
{
foreach (IDRole operandIdRole in operand._idArray)
{
if (operandIdRole.Authenticated == idRole.Authenticated)
{
string newID = string.Empty;
string newRole = string.Empty;
bool newAuthenticated = operandIdRole.Authenticated;
bool addToNewIDRoles = false;
if (operandIdRole.ID == null || idRole.ID == null || idRole.ID.Equals(operandIdRole.ID))
{
newID = operandIdRole.ID ?? idRole.ID;
addToNewIDRoles = true;
}
if (operandIdRole.Role == null || idRole.Role == null || idRole.Role.Equals(operandIdRole.Role))
{
newRole = operandIdRole.Role ?? idRole.Role;
addToNewIDRoles = true;
}
if (addToNewIDRoles)
{
idroles ??= new List<IDRole>();
idroles.Add(new IDRole(newAuthenticated, newID, newRole));
}
}
}
}
return (idroles == null) ? null : new PrincipalPermission(idroles.ToArray());
}
public IPermission Union(IPermission other)
{
if (other == null)
{
return Copy();
}
else if (!VerifyType(other))
{
throw new ArgumentException(SR.Format(SR.Argument_WrongType, GetType().FullName), nameof(other));
}
PrincipalPermission operand = (PrincipalPermission)other;
if (IsUnrestricted() || operand.IsUnrestricted())
{
return new PrincipalPermission(PermissionState.Unrestricted);
}
IDRole[] idrolesArray = new IDRole[_idArray.Length + operand._idArray.Length];
Array.Copy(_idArray, idrolesArray, _idArray.Length);
Array.Copy(operand._idArray, 0, idrolesArray, _idArray.Length, operand._idArray.Length);
return new PrincipalPermission(idrolesArray);
}
public override bool Equals(object obj)
{
IPermission perm = obj as IPermission;
if (obj != null && perm == null)
return false;
if (!IsSubsetOf(perm))
return false;
if (perm != null && !perm.IsSubsetOf(this))
return false;
return true;
}
public override int GetHashCode()
{
int hash = 0;
foreach (IDRole idRole in _idArray)
hash += idRole.GetHashCode();
return hash;
}
public IPermission Copy()
{
return new PrincipalPermission(_idArray);
}
public void Demand()
{
IPrincipal principal = Thread.CurrentPrincipal;
if (principal == null)
throw new SecurityException(SR.Security_PrincipalPermission);
if (_idArray == null)
return;
// A demand passes when the grant satisfies all entries.
foreach (IDRole idRole in _idArray)
{
// If the demand is authenticated, we need to check the identity and role
if (!idRole.Authenticated)
{
return;
}
else if (principal.Identity.IsAuthenticated &&
(idRole.ID == null || string.Equals(principal.Identity.Name, idRole.ID, StringComparison.OrdinalIgnoreCase)))
{
if (idRole.Role == null || principal.IsInRole(idRole.Role))
return;
}
}
throw new SecurityException(SR.Security_PrincipalPermission);
}
public SecurityElement ToXml()
{
SecurityElement root = new SecurityElement("IPermission");
string typename = "System.Security.Permissions.PrincipalPermission";
root.AddAttribute("class", typename + ", " + GetType().Module.Assembly.FullName.Replace('\"', '\''));
root.AddAttribute("version", "1");
if (_idArray != null)
{
foreach (IDRole idRole in _idArray)
{
root.AddChild(idRole.ToXml());
}
}
return root;
}
public void FromXml(SecurityElement elem)
{
if (elem is null)
{
throw new ArgumentNullException(nameof(elem));
}
if (elem.Tag == null || !elem.Tag.Equals("Permission") && !elem.Tag.Equals("IPermission"))
throw new ArgumentException(SR.Argument_NotAPermissionElement);
string version = elem.Attribute("version");
if (version == null || (version != null && !version.Equals("1")))
throw new ArgumentException(SR.Argument_InvalidXMLBadVersion);
if (elem.Children != null && elem.Children.Count != 0)
{
int numChildren = elem.Children.Count;
int count = 0;
_idArray = new IDRole[numChildren];
foreach (object curr in elem.Children)
{
_idArray[count++] = new IDRole((SecurityElement)curr);
}
}
else
_idArray = Array.Empty<IDRole>();
}
public override string ToString()
{
return ToXml().ToString();
}
}
}