mscorlib(4.0.0.0) API with additions
PrincipalPermission.cs
1 using System.Collections;
3 using System.Reflection;
7 using System.Security.Util;
8 using System.Threading;
9 
11 {
13  [Serializable]
14  [ComVisible(true)]
15  public sealed class PrincipalPermission : IPermission, ISecurityEncodable, IUnrestrictedPermission, IBuiltInPermission
16  {
17  private IDRole[] m_array;
18 
23  {
24  switch (state)
25  {
26  case PermissionState.Unrestricted:
27  m_array = new IDRole[1];
28  m_array[0] = new IDRole();
29  m_array[0].m_authenticated = true;
30  m_array[0].m_id = null;
31  m_array[0].m_role = null;
32  break;
33  case PermissionState.None:
34  m_array = new IDRole[1];
35  m_array[0] = new IDRole();
36  m_array[0].m_authenticated = false;
37  m_array[0].m_id = "";
38  m_array[0].m_role = "";
39  break;
40  default:
41  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
42  }
43  }
44 
48  public PrincipalPermission(string name, string role)
49  {
50  m_array = new IDRole[1];
51  m_array[0] = new IDRole();
52  m_array[0].m_authenticated = true;
53  m_array[0].m_id = name;
54  m_array[0].m_role = role;
55  }
56 
62  public PrincipalPermission(string name, string role, bool isAuthenticated)
63  {
64  m_array = new IDRole[1];
65  m_array[0] = new IDRole();
66  m_array[0].m_authenticated = isAuthenticated;
67  m_array[0].m_id = name;
68  m_array[0].m_role = role;
69  }
70 
71  private PrincipalPermission(IDRole[] array)
72  {
73  m_array = array;
74  }
75 
76  private bool IsEmpty()
77  {
78  for (int i = 0; i < m_array.Length; i++)
79  {
80  if (m_array[i].m_id == null || !m_array[i].m_id.Equals("") || m_array[i].m_role == null || !m_array[i].m_role.Equals("") || m_array[i].m_authenticated)
81  {
82  return false;
83  }
84  }
85  return true;
86  }
87 
88  private bool VerifyType(IPermission perm)
89  {
90  if (perm == null || perm.GetType() != GetType())
91  {
92  return false;
93  }
94  return true;
95  }
96 
100  public bool IsUnrestricted()
101  {
102  for (int i = 0; i < m_array.Length; i++)
103  {
104  if (m_array[i].m_id != null || m_array[i].m_role != null || !m_array[i].m_authenticated)
105  {
106  return false;
107  }
108  }
109  return true;
110  }
111 
117  public bool IsSubsetOf(IPermission target)
118  {
119  if (target == null)
120  {
121  return IsEmpty();
122  }
123  try
124  {
125  PrincipalPermission principalPermission = (PrincipalPermission)target;
126  if (principalPermission.IsUnrestricted())
127  {
128  return true;
129  }
130  if (IsUnrestricted())
131  {
132  return false;
133  }
134  for (int i = 0; i < m_array.Length; i++)
135  {
136  bool flag = false;
137  for (int j = 0; j < principalPermission.m_array.Length; j++)
138  {
139  if (principalPermission.m_array[j].m_authenticated == m_array[i].m_authenticated && (principalPermission.m_array[j].m_id == null || (m_array[i].m_id != null && m_array[i].m_id.Equals(principalPermission.m_array[j].m_id))) && (principalPermission.m_array[j].m_role == null || (m_array[i].m_role != null && m_array[i].m_role.Equals(principalPermission.m_array[j].m_role))))
140  {
141  flag = true;
142  break;
143  }
144  }
145  if (!flag)
146  {
147  return false;
148  }
149  }
150  return true;
151  }
152  catch (InvalidCastException)
153  {
154  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
155  }
156  }
157 
163  {
164  if (target == null)
165  {
166  return null;
167  }
168  if (!VerifyType(target))
169  {
170  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
171  }
172  if (IsUnrestricted())
173  {
174  return target.Copy();
175  }
176  PrincipalPermission principalPermission = (PrincipalPermission)target;
177  if (principalPermission.IsUnrestricted())
178  {
179  return Copy();
180  }
181  List<IDRole> list = null;
182  for (int i = 0; i < m_array.Length; i++)
183  {
184  for (int j = 0; j < principalPermission.m_array.Length; j++)
185  {
186  if (principalPermission.m_array[j].m_authenticated != m_array[i].m_authenticated)
187  {
188  continue;
189  }
190  if (principalPermission.m_array[j].m_id == null || m_array[i].m_id == null || m_array[i].m_id.Equals(principalPermission.m_array[j].m_id))
191  {
192  if (list == null)
193  {
194  list = new List<IDRole>();
195  }
196  IDRole iDRole = new IDRole();
197  iDRole.m_id = ((principalPermission.m_array[j].m_id == null) ? m_array[i].m_id : principalPermission.m_array[j].m_id);
198  if (principalPermission.m_array[j].m_role == null || m_array[i].m_role == null || m_array[i].m_role.Equals(principalPermission.m_array[j].m_role))
199  {
200  iDRole.m_role = ((principalPermission.m_array[j].m_role == null) ? m_array[i].m_role : principalPermission.m_array[j].m_role);
201  }
202  else
203  {
204  iDRole.m_role = "";
205  }
206  iDRole.m_authenticated = principalPermission.m_array[j].m_authenticated;
207  list.Add(iDRole);
208  }
209  else if (principalPermission.m_array[j].m_role == null || m_array[i].m_role == null || m_array[i].m_role.Equals(principalPermission.m_array[j].m_role))
210  {
211  if (list == null)
212  {
213  list = new List<IDRole>();
214  }
215  IDRole iDRole2 = new IDRole();
216  iDRole2.m_id = "";
217  iDRole2.m_role = ((principalPermission.m_array[j].m_role == null) ? m_array[i].m_role : principalPermission.m_array[j].m_role);
218  iDRole2.m_authenticated = principalPermission.m_array[j].m_authenticated;
219  list.Add(iDRole2);
220  }
221  }
222  }
223  if (list == null)
224  {
225  return null;
226  }
227  IDRole[] array = new IDRole[list.Count];
228  IEnumerator enumerator = list.GetEnumerator();
229  int num = 0;
230  while (enumerator.MoveNext())
231  {
232  array[num++] = (IDRole)enumerator.Current;
233  }
234  return new PrincipalPermission(array);
235  }
236 
242  {
243  if (other == null)
244  {
245  return Copy();
246  }
247  if (!VerifyType(other))
248  {
249  throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", GetType().FullName));
250  }
251  PrincipalPermission principalPermission = (PrincipalPermission)other;
252  if (IsUnrestricted() || principalPermission.IsUnrestricted())
253  {
254  return new PrincipalPermission(PermissionState.Unrestricted);
255  }
256  int num = m_array.Length + principalPermission.m_array.Length;
257  IDRole[] array = new IDRole[num];
258  int i;
259  for (i = 0; i < m_array.Length; i++)
260  {
261  array[i] = m_array[i];
262  }
263  for (int j = 0; j < principalPermission.m_array.Length; j++)
264  {
265  array[i + j] = principalPermission.m_array[j];
266  }
267  return new PrincipalPermission(array);
268  }
269 
274  [ComVisible(false)]
275  public override bool Equals(object obj)
276  {
277  IPermission permission = obj as IPermission;
278  if (obj != null && permission == null)
279  {
280  return false;
281  }
282  if (!IsSubsetOf(permission))
283  {
284  return false;
285  }
286  if (permission != null && !permission.IsSubsetOf(this))
287  {
288  return false;
289  }
290  return true;
291  }
292 
295  [ComVisible(false)]
296  public override int GetHashCode()
297  {
298  int num = 0;
299  for (int i = 0; i < m_array.Length; i++)
300  {
301  num += m_array[i].GetHashCode();
302  }
303  return num;
304  }
305 
308  public IPermission Copy()
309  {
310  return new PrincipalPermission(m_array);
311  }
312 
313  [SecurityCritical]
314  private void ThrowSecurityException()
315  {
316  AssemblyName assemblyName = null;
317  Evidence evidence = null;
318  PermissionSet.s_fullTrust.Assert();
319  try
320  {
321  Assembly callingAssembly = Assembly.GetCallingAssembly();
322  assemblyName = callingAssembly.GetName();
323  if (callingAssembly != Assembly.GetExecutingAssembly())
324  {
325  evidence = callingAssembly.Evidence;
326  }
327  }
328  catch
329  {
330  }
331  PermissionSet.RevertAssert();
332  throw new SecurityException(Environment.GetResourceString("Security_PrincipalPermission"), assemblyName, null, null, null, SecurityAction.Demand, this, this, evidence);
333  }
334 
337  [SecuritySafeCritical]
338  public void Demand()
339  {
340  IPrincipal principal = null;
341  new SecurityPermission(SecurityPermissionFlag.ControlPrincipal).Assert();
342  principal = Thread.CurrentPrincipal;
343  if (principal == null)
344  {
345  ThrowSecurityException();
346  }
347  if (m_array == null)
348  {
349  return;
350  }
351  int num = m_array.Length;
352  bool flag = false;
353  for (int i = 0; i < num; i++)
354  {
355  if (m_array[i].m_authenticated)
356  {
357  IIdentity identity = principal.Identity;
358  if (identity.IsAuthenticated && (m_array[i].m_id == null || string.Compare(identity.Name, m_array[i].m_id, StringComparison.OrdinalIgnoreCase) == 0))
359  {
360  if (m_array[i].m_role == null)
361  {
362  flag = true;
363  }
364  else
365  {
366  WindowsPrincipal windowsPrincipal = principal as WindowsPrincipal;
367  flag = ((windowsPrincipal == null || !(m_array[i].Sid != null)) ? principal.IsInRole(m_array[i].m_role) : windowsPrincipal.IsInRole(m_array[i].Sid));
368  }
369  if (flag)
370  {
371  break;
372  }
373  }
374  continue;
375  }
376  flag = true;
377  break;
378  }
379  if (!flag)
380  {
381  ThrowSecurityException();
382  }
383  }
384 
388  {
389  SecurityElement securityElement = new SecurityElement("IPermission");
390  XMLUtil.AddClassAttribute(securityElement, GetType(), "System.Security.Permissions.PrincipalPermission");
391  securityElement.AddAttribute("version", "1");
392  int num = m_array.Length;
393  for (int i = 0; i < num; i++)
394  {
395  securityElement.AddChild(m_array[i].ToXml());
396  }
397  return securityElement;
398  }
399 
404  public void FromXml(SecurityElement elem)
405  {
406  CodeAccessPermission.ValidateElement(elem, this);
407  if (elem.InternalChildren != null && elem.InternalChildren.Count != 0)
408  {
409  int count = elem.InternalChildren.Count;
410  int num = 0;
411  m_array = new IDRole[count];
412  IEnumerator enumerator = elem.Children.GetEnumerator();
413  while (enumerator.MoveNext())
414  {
415  IDRole iDRole = new IDRole();
416  iDRole.FromXml((SecurityElement)enumerator.Current);
417  m_array[num++] = iDRole;
418  }
419  }
420  else
421  {
422  m_array = new IDRole[0];
423  }
424  }
425 
428  public override string ToString()
429  {
430  return ToXml().ToString();
431  }
432 
433  int IBuiltInPermission.GetTokenIndex()
434  {
435  return GetTokenIndex();
436  }
437 
438  internal static int GetTokenIndex()
439  {
440  return 8;
441  }
442  }
443 }
static Assembly GetExecutingAssembly()
Gets the assembly that contains the code that is currently executing.
Definition: Assembly.cs:799
static IPrincipal CurrentPrincipal
Gets or sets the thread's current principal (for role-based security).
Definition: Thread.cs:297
Enables code to check the Windows group membership of a Windows user.
Allows a permission to expose an unrestricted state.
void FromXml(SecurityElement elem)
Reconstructs a permission with a specified state from an XML encoding.
Describes a set of security permissions applied to code. This class cannot be inherited.
PrincipalPermission(string name, string role)
Initializes a new instance of the T:System.Security.Permissions.PrincipalPermission class for the spe...
bool MoveNext()
Advances the enumerator to the next element of the collection.
override bool IsInRole(string role)
Determines whether the current principal belongs to the Windows user group with the specified name.
static Assembly GetCallingAssembly()
Returns the T:System.Reflection.Assembly of the method that invoked the currently executing method.
Definition: Assembly.cs:810
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
Defines the basic functionality of a principal object.
Definition: IPrincipal.cs:8
IPermission Union(IPermission other)
Creates a permission that is the union of the current permission and the specified permission.
override string ToString()
Creates and returns a string representing the current permission.
virtual AssemblyName GetName()
Gets an T:System.Reflection.AssemblyName for this assembly.
Definition: Assembly.cs:832
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
void Demand()
Determines at run time whether the current principal matches the principal specified by the current p...
Definition: __Canon.cs:3
The exception that is thrown for invalid casting or explicit conversion.
SecurityElement ToXml()
Creates an XML encoding of the permission and its current state.
IPermission Copy()
Creates and returns an identical copy of the current permission.
override int GetHashCode()
Gets a hash code for the T:System.Security.Permissions.PrincipalPermission object that is suitable fo...
bool IsUnrestricted()
Returns a value indicating whether the current permission is unrestricted.
bool IsSubsetOf(IPermission target)
Determines whether the current permission is a subset of the specified permission.
void AddChild(SecurityElement child)
Adds a child element to the XML element.
SecurityAction
Specifies the security actions that can be performed using declarative security.
PrincipalPermission(PermissionState state)
Initializes a new instance of the T:System.Security.Permissions.PrincipalPermission class with the sp...
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
bool IsAuthenticated
Gets a value that indicates whether the user has been authenticated.
Definition: IIdentity.cs:33
IIdentity Identity
Gets the identity of the current principal.
Definition: IPrincipal.cs:14
Represents a collection that can contain many different types of permissions.
Represents the XML object model for encoding security objects. This class cannot be inherited.
IPermission Intersect(IPermission target)
Creates and returns a permission that is the intersection of the current permission and the specified...
Defines the methods that convert permission object state to and from XML element representation.
object Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:15
Represents an assembly, which is a reusable, versionable, and self-describing building block of a com...
Definition: Assembly.cs:22
Defines the underlying structure of all code access permissions.
Defines methods implemented by permission types.
Definition: IPermission.cs:7
virtual Evidence Evidence
Gets the evidence for this assembly.
Definition: Assembly.cs:107
Describes an assembly's unique identity in full.
Definition: AssemblyName.cs:19
bool IsSubsetOf(IPermission target)
Determines whether the current permission is a subset of the specified permission.
The exception that is thrown when one of the arguments provided to a method is not valid.
bool IsInRole(string role)
Determines whether the current principal belongs to the specified role.
override bool Equals(object obj)
Determines whether the specified T:System.Security.Permissions.PrincipalPermission object is equal to...
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition: List.cs:14
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
Defines the set of information that constitutes input to security policy decisions....
Definition: Evidence.cs:17
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
Specifies that the class can be serialized.
ArrayList Children
Gets or sets the array of child elements of the XML element.
virtual IEnumerator GetEnumerator()
Returns an enumerator for the entire T:System.Collections.ArrayList.
Definition: ArrayList.cs:2615
Defines the basic functionality of an identity object.
Definition: IIdentity.cs:8
Allows checks against the active principal (see T:System.Security.Principal.IPrincipal) using the lan...
void Assert()
Declares that the calling code can access the resource protected by a permission demand through the c...
string Name
Gets the name of the current user.
Definition: IIdentity.cs:14
void Assert()
Declares that the calling code can access the resource protected by a permission demand through the c...
IPermission Copy()
Creates and returns an identical copy of the current permission.
SecurityPermissionFlag
Specifies access flags for the security permission object.
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
override string ToString()
Produces a string representation of an XML element and its constituent attributes,...
PrincipalPermission(string name, string role, bool isAuthenticated)
Initializes a new instance of the T:System.Security.Permissions.PrincipalPermission class for the spe...
Creates and controls a thread, sets its priority, and gets its status.
Definition: Thread.cs:18