using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Security.Principal;
using System.Threading;
namespace System.Security.Permissions
{
/// Allows checks against the active principal (see ) using the language constructs defined for both declarative and imperative security actions. This class cannot be inherited.
// Token: 0x0200055C RID: 1372
[ComVisible(true)]
[Serializable]
public sealed class PrincipalPermission : IBuiltInPermission, IUnrestrictedPermission, IPermission, ISecurityEncodable
{
/// Initializes a new instance of the class with the specified .
/// One of the values.
/// The parameter is not a valid .
// Token: 0x06003218 RID: 12824 RVA: 0x000ABBC4 File Offset: 0x000A9DC4
public PrincipalPermission(PermissionState state)
{
this.principals = new ArrayList();
if (CodeAccessPermission.CheckPermissionState(state, true) == PermissionState.Unrestricted)
{
PrincipalPermission.PrincipalInfo principalInfo = new PrincipalPermission.PrincipalInfo(null, null, true);
this.principals.Add(principalInfo);
}
}
/// Initializes a new instance of the class for the specified and .
/// The name of the object's user.
/// The role of the object's user (for example, Administrator).
// Token: 0x06003219 RID: 12825 RVA: 0x000ABC08 File Offset: 0x000A9E08
public PrincipalPermission(string name, string role)
: this(name, role, true)
{
}
/// Initializes a new instance of the class for the specified , , and authentication status.
/// The name of the object's user.
/// The role of the object's user (for example, Administrator).
/// true to signify that the user is authenticated; otherwise, false.
// Token: 0x0600321A RID: 12826 RVA: 0x000ABC14 File Offset: 0x000A9E14
public PrincipalPermission(string name, string role, bool isAuthenticated)
{
this.principals = new ArrayList();
PrincipalPermission.PrincipalInfo principalInfo = new PrincipalPermission.PrincipalInfo(name, role, isAuthenticated);
this.principals.Add(principalInfo);
}
// Token: 0x0600321B RID: 12827 RVA: 0x000ABC48 File Offset: 0x000A9E48
internal PrincipalPermission(ArrayList principals)
{
this.principals = (ArrayList)principals.Clone();
}
// Token: 0x0600321C RID: 12828 RVA: 0x000ABC64 File Offset: 0x000A9E64
int IBuiltInPermission.GetTokenIndex()
{
return 8;
}
/// Creates and returns an identical copy of the current permission.
/// A copy of the current permission.
// Token: 0x0600321D RID: 12829 RVA: 0x000ABC68 File Offset: 0x000A9E68
public IPermission Copy()
{
return new PrincipalPermission(this.principals);
}
/// Determines at run time whether the current principal matches the principal specified by the current permission.
/// The current principal does not pass the security check for the principal specified by the current permission.-or- The current is null.
// Token: 0x0600321E RID: 12830 RVA: 0x000ABC78 File Offset: 0x000A9E78
public void Demand()
{
IPrincipal currentPrincipal = Thread.CurrentPrincipal;
if (currentPrincipal == null)
{
throw new SecurityException("no Principal");
}
if (this.principals.Count > 0)
{
bool flag = false;
foreach (object obj in this.principals)
{
PrincipalPermission.PrincipalInfo principalInfo = (PrincipalPermission.PrincipalInfo)obj;
if ((principalInfo.Name == null || principalInfo.Name == currentPrincipal.Identity.Name) && (principalInfo.Role == null || currentPrincipal.IsInRole(principalInfo.Role)) && ((principalInfo.IsAuthenticated && currentPrincipal.Identity.IsAuthenticated) || !principalInfo.IsAuthenticated))
{
flag = true;
break;
}
}
if (!flag)
{
throw new SecurityException("Demand for principal refused.");
}
}
}
/// Reconstructs a permission with a specified state from an XML encoding.
/// The XML encoding to use to reconstruct the permission.
/// The parameter is null.
/// The parameter is not a valid permission element.-or- The parameter's version number is not valid.
// Token: 0x0600321F RID: 12831 RVA: 0x000ABD90 File Offset: 0x000A9F90
public void FromXml(SecurityElement elem)
{
this.CheckSecurityElement(elem, "elem", 1, 1);
this.principals.Clear();
if (elem.Children != null)
{
foreach (object obj in elem.Children)
{
SecurityElement securityElement = (SecurityElement)obj;
if (securityElement.Tag != "Identity")
{
throw new ArgumentException("not IPermission/Identity");
}
string text = securityElement.Attribute("ID");
string text2 = securityElement.Attribute("Role");
string text3 = securityElement.Attribute("Authenticated");
bool flag = false;
if (text3 != null)
{
try
{
flag = bool.Parse(text3);
}
catch
{
}
}
PrincipalPermission.PrincipalInfo principalInfo = new PrincipalPermission.PrincipalInfo(text, text2, flag);
this.principals.Add(principalInfo);
}
}
}
/// Creates and returns a permission that is the intersection of the current permission and the specified permission.
/// A new permission that represents the intersection of the current permission and the specified permission. This new permission will be null if the intersection is empty.
/// A permission to intersect with the current permission. It must be of the same type as the current permission.
/// The parameter is not null and is not an instance of the same class as the current permission.
// Token: 0x06003220 RID: 12832 RVA: 0x000ABEB4 File Offset: 0x000AA0B4
public IPermission Intersect(IPermission target)
{
PrincipalPermission principalPermission = this.Cast(target);
if (principalPermission == null)
{
return null;
}
if (this.IsUnrestricted())
{
return principalPermission.Copy();
}
if (principalPermission.IsUnrestricted())
{
return this.Copy();
}
PrincipalPermission principalPermission2 = new PrincipalPermission(PermissionState.None);
foreach (object obj in this.principals)
{
PrincipalPermission.PrincipalInfo principalInfo = (PrincipalPermission.PrincipalInfo)obj;
foreach (object obj2 in principalPermission.principals)
{
PrincipalPermission.PrincipalInfo principalInfo2 = (PrincipalPermission.PrincipalInfo)obj2;
if (principalInfo.IsAuthenticated == principalInfo2.IsAuthenticated)
{
string text = null;
if (principalInfo.Name == principalInfo2.Name || principalInfo2.Name == null)
{
text = principalInfo.Name;
}
else if (principalInfo.Name == null)
{
text = principalInfo2.Name;
}
string text2 = null;
if (principalInfo.Role == principalInfo2.Role || principalInfo2.Role == null)
{
text2 = principalInfo.Role;
}
else if (principalInfo.Role == null)
{
text2 = principalInfo2.Role;
}
if (text != null || text2 != null)
{
PrincipalPermission.PrincipalInfo principalInfo3 = new PrincipalPermission.PrincipalInfo(text, text2, principalInfo.IsAuthenticated);
principalPermission2.principals.Add(principalInfo3);
}
}
}
}
return (principalPermission2.principals.Count <= 0) ? null : principalPermission2;
}
/// Determines whether the current permission is a subset of the specified permission.
/// true if the current permission is a subset of the specified permission; otherwise, false.
/// A permission that is to be tested for the subset relationship. This permission must be of the same type as the current permission.
/// The parameter is an object that is not of the same type as the current permission.
// Token: 0x06003221 RID: 12833 RVA: 0x000AC0A0 File Offset: 0x000AA2A0
public bool IsSubsetOf(IPermission target)
{
PrincipalPermission principalPermission = this.Cast(target);
if (principalPermission == null)
{
return this.IsEmpty();
}
if (this.IsUnrestricted())
{
return principalPermission.IsUnrestricted();
}
if (principalPermission.IsUnrestricted())
{
return true;
}
foreach (object obj in this.principals)
{
PrincipalPermission.PrincipalInfo principalInfo = (PrincipalPermission.PrincipalInfo)obj;
bool flag = false;
foreach (object obj2 in principalPermission.principals)
{
PrincipalPermission.PrincipalInfo principalInfo2 = (PrincipalPermission.PrincipalInfo)obj2;
if ((principalInfo.Name == principalInfo2.Name || principalInfo2.Name == null) && (principalInfo.Role == principalInfo2.Role || principalInfo2.Role == null) && principalInfo.IsAuthenticated == principalInfo2.IsAuthenticated)
{
flag = true;
}
}
if (!flag)
{
return false;
}
}
return true;
}
/// Returns a value indicating whether the current permission is unrestricted.
/// true if the current permission is unrestricted; otherwise, false.
// Token: 0x06003222 RID: 12834 RVA: 0x000AC20C File Offset: 0x000AA40C
public bool IsUnrestricted()
{
foreach (object obj in this.principals)
{
PrincipalPermission.PrincipalInfo principalInfo = (PrincipalPermission.PrincipalInfo)obj;
if (principalInfo.Name == null && principalInfo.Role == null && principalInfo.IsAuthenticated)
{
return true;
}
}
return false;
}
/// Creates and returns a string representing the current permission.
/// A representation of the current permission.
// Token: 0x06003223 RID: 12835 RVA: 0x000AC2A0 File Offset: 0x000AA4A0
public override string ToString()
{
return this.ToXml().ToString();
}
/// Creates an XML encoding of the permission and its current state.
/// An XML encoding of the permission, including any state information.
// Token: 0x06003224 RID: 12836 RVA: 0x000AC2B0 File Offset: 0x000AA4B0
public SecurityElement ToXml()
{
SecurityElement securityElement = new SecurityElement("Permission");
Type type = base.GetType();
securityElement.AddAttribute("class", type.FullName + ", " + type.Assembly.ToString().Replace('"', '\''));
securityElement.AddAttribute("version", 1.ToString());
foreach (object obj in this.principals)
{
PrincipalPermission.PrincipalInfo principalInfo = (PrincipalPermission.PrincipalInfo)obj;
SecurityElement securityElement2 = new SecurityElement("Identity");
if (principalInfo.Name != null)
{
securityElement2.AddAttribute("ID", principalInfo.Name);
}
if (principalInfo.Role != null)
{
securityElement2.AddAttribute("Role", principalInfo.Role);
}
if (principalInfo.IsAuthenticated)
{
securityElement2.AddAttribute("Authenticated", "true");
}
securityElement.AddChild(securityElement2);
}
return securityElement;
}
/// Creates a permission that is the union of the current permission and the specified permission.
/// A new permission that represents the union of the current permission and the specified permission.
/// A permission to combine with the current permission. It must be of the same type as the current permission.
/// The parameter is an object that is not of the same type as the current permission.
// Token: 0x06003225 RID: 12837 RVA: 0x000AC3DC File Offset: 0x000AA5DC
public IPermission Union(IPermission other)
{
PrincipalPermission principalPermission = this.Cast(other);
if (principalPermission == null)
{
return this.Copy();
}
if (this.IsUnrestricted() || principalPermission.IsUnrestricted())
{
return new PrincipalPermission(PermissionState.Unrestricted);
}
PrincipalPermission principalPermission2 = new PrincipalPermission(this.principals);
foreach (object obj in principalPermission.principals)
{
PrincipalPermission.PrincipalInfo principalInfo = (PrincipalPermission.PrincipalInfo)obj;
principalPermission2.principals.Add(principalInfo);
}
return principalPermission2;
}
/// Determines whether the specified object is equal to the current .
/// true if the specified is equal to the current object; otherwise, false.
/// The object to compare with the current .
// Token: 0x06003226 RID: 12838 RVA: 0x000AC494 File Offset: 0x000AA694
[ComVisible(false)]
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
PrincipalPermission principalPermission = obj as PrincipalPermission;
if (principalPermission == null)
{
return false;
}
if (this.principals.Count != principalPermission.principals.Count)
{
return false;
}
foreach (object obj2 in this.principals)
{
PrincipalPermission.PrincipalInfo principalInfo = (PrincipalPermission.PrincipalInfo)obj2;
bool flag = false;
foreach (object obj3 in principalPermission.principals)
{
PrincipalPermission.PrincipalInfo principalInfo2 = (PrincipalPermission.PrincipalInfo)obj3;
if ((principalInfo.Name == principalInfo2.Name || principalInfo2.Name == null) && (principalInfo.Role == principalInfo2.Role || principalInfo2.Role == null) && principalInfo.IsAuthenticated == principalInfo2.IsAuthenticated)
{
flag = true;
break;
}
}
if (!flag)
{
return false;
}
}
return true;
}
/// Gets a hash code for the object that is suitable for use in hashing algorithms and data structures such as a hash table.
/// A hash code for the current object.
// Token: 0x06003227 RID: 12839 RVA: 0x000AC608 File Offset: 0x000AA808
[ComVisible(false)]
public override int GetHashCode()
{
return base.GetHashCode();
}
// Token: 0x06003228 RID: 12840 RVA: 0x000AC610 File Offset: 0x000AA810
private PrincipalPermission Cast(IPermission target)
{
if (target == null)
{
return null;
}
PrincipalPermission principalPermission = target as PrincipalPermission;
if (principalPermission == null)
{
CodeAccessPermission.ThrowInvalidPermission(target, typeof(PrincipalPermission));
}
return principalPermission;
}
// Token: 0x06003229 RID: 12841 RVA: 0x000AC644 File Offset: 0x000AA844
private bool IsEmpty()
{
return this.principals.Count == 0;
}
// Token: 0x0600322A RID: 12842 RVA: 0x000AC654 File Offset: 0x000AA854
internal int CheckSecurityElement(SecurityElement se, string parameterName, int minimumVersion, int maximumVersion)
{
if (se == null)
{
throw new ArgumentNullException(parameterName);
}
if (se.Tag != "Permission")
{
string text = string.Format(Locale.GetText("Invalid tag {0}"), se.Tag);
throw new ArgumentException(text, parameterName);
}
int num = minimumVersion;
string text2 = se.Attribute("version");
if (text2 != null)
{
try
{
num = int.Parse(text2);
}
catch (Exception ex)
{
string text3 = Locale.GetText("Couldn't parse version from '{0}'.");
text3 = string.Format(text3, text2);
throw new ArgumentException(text3, parameterName, ex);
}
}
if (num < minimumVersion || num > maximumVersion)
{
string text4 = Locale.GetText("Unknown version '{0}', expected versions between ['{1}','{2}'].");
text4 = string.Format(text4, num, minimumVersion, maximumVersion);
throw new ArgumentException(text4, parameterName);
}
return num;
}
// Token: 0x04001496 RID: 5270
private const int version = 1;
// Token: 0x04001497 RID: 5271
private ArrayList principals;
// Token: 0x0200055D RID: 1373
internal class PrincipalInfo
{
// Token: 0x0600322B RID: 12843 RVA: 0x000AC744 File Offset: 0x000AA944
public PrincipalInfo(string name, string role, bool isAuthenticated)
{
this._name = name;
this._role = role;
this._isAuthenticated = isAuthenticated;
}
// Token: 0x170009D7 RID: 2519
// (get) Token: 0x0600322C RID: 12844 RVA: 0x000AC764 File Offset: 0x000AA964
public string Name
{
get
{
return this._name;
}
}
// Token: 0x170009D8 RID: 2520
// (get) Token: 0x0600322D RID: 12845 RVA: 0x000AC76C File Offset: 0x000AA96C
public string Role
{
get
{
return this._role;
}
}
// Token: 0x170009D9 RID: 2521
// (get) Token: 0x0600322E RID: 12846 RVA: 0x000AC774 File Offset: 0x000AA974
public bool IsAuthenticated
{
get
{
return this._isAuthenticated;
}
}
// Token: 0x04001498 RID: 5272
private string _name;
// Token: 0x04001499 RID: 5273
private string _role;
// Token: 0x0400149A RID: 5274
private bool _isAuthenticated;
}
}
}