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
|
//------------------------------------------------------------------------------
// <copyright file="PnrpPermission.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//------------------------------------------------------------------------------
namespace System.Net.PeerToPeer
{
using System.Security;
using System.Security.Permissions;
using System.Globalization;
/// <remarks>
/// PnrpPermission atrribute
/// </remarks>
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor |
AttributeTargets.Class | AttributeTargets.Struct |
AttributeTargets.Assembly,
AllowMultiple = true, Inherited = false)]
[Serializable()]
public sealed class PnrpPermissionAttribute : CodeAccessSecurityAttribute
{
/// <summary>
/// Just call base constructor
/// </summary>
/// <param name="action"></param>
public PnrpPermissionAttribute(SecurityAction action) : base(action) { }
/// <summary>
/// As required by the SecurityAttribute class.
/// </summary>
/// <returns></returns>
public override IPermission CreatePermission() {
if (Unrestricted) {
return new PnrpPermission(PermissionState.Unrestricted);
}
else {
return new PnrpPermission(PermissionState.None);
}
}
}
/// <remarks>
/// Currently we only support two levels - Unrestrictred or none
/// </remarks>
[Serializable]
public sealed class PnrpPermission : CodeAccessPermission, IUnrestrictedPermission
{
private bool m_noRestriction;
internal static readonly PnrpPermission UnrestrictedPnrpPermission = new PnrpPermission(PermissionState.Unrestricted);
/// <summary>
/// <para>
/// Creates a new instance of the <see cref='System.Net.PeerToPeer.PnrpPermission'/>
/// class that passes all demands or that fails all demands.
/// </para>
/// </summary>
public PnrpPermission(PermissionState state)
{
m_noRestriction = (state == PermissionState.Unrestricted);
}
internal PnrpPermission(bool free)
{
m_noRestriction = free;
}
// IUnrestrictedPermission interface methods
/// <summary>
/// <para>
/// Checks the overall permission state of the object.
/// </para>
/// </summary>
public bool IsUnrestricted()
{
return m_noRestriction;
}
// IPermission interface methods
/// <summary>
/// <para>
/// Creates a copy of a <see cref='System.Net.PeerToPeer.PnrpPermission'/> instance.
/// </para>
/// </summary>
public override IPermission Copy()
{
return new PnrpPermission(m_noRestriction);
}
/// <summary>
/// <para>Returns the logical union between two <see cref='System.Net.PeerToPeer.PnrpPermission'/> instances.</para>
/// </summary>
public override IPermission Union(IPermission target)
{
// Pattern suggested by Security engine
if (target == null) {
return this.Copy();
}
PnrpPermission other = target as PnrpPermission;
if (other == null) {
throw new ArgumentException( SR.GetString(SR.PnrpPermission_CantUnionWithNonPnrpPermission), "target");
}
return new PnrpPermission(m_noRestriction || other.m_noRestriction);
}
/// <summary>
/// <para>Returns the logical intersection between two <see cref='System.Net.PeerToPeer.PnrpPermission'/> instances.</para>
/// </summary>
public override IPermission Intersect(IPermission target)
{
// Pattern suggested by Security engine
if (target == null) {
return null;
}
PnrpPermission other = target as PnrpPermission;
if (other == null) {
throw new ArgumentException(SR.GetString(SR.PnrpPermission_CantIntersectWithNonPnrpPermission), "target");
}
// return null if resulting permission is restricted and empty
// Hence, the only way for a bool permission will be.
if (this.m_noRestriction && other.m_noRestriction) {
return new PnrpPermission(true);
}
return null;
}
/// <summary>
/// <para>Compares two <see cref='System.Net.PeerToPeer.PnrpPermission'/> instances.</para>
/// </summary>
public override bool IsSubsetOf(IPermission target)
{
// Pattern suggested by Security engine
if (target == null) {
return m_noRestriction == false;
}
PnrpPermission other = target as PnrpPermission;
if (other == null) {
throw new ArgumentException(SR.GetString(SR.PnrpPermission_TargetNotAPnrpPermission), "target");
}
//Here is the matrix of result based on m_noRestriction for me and she
// me.noRestriction she.noRestriction me.isSubsetOf(she)
// 0 0 1
// 0 1 1
// 1 0 0
// 1 1 1
return (!m_noRestriction || other.m_noRestriction);
}
/// <summary>
/// Cinstrcy from a security element
/// </summary>
/// <param name="securityElement"></param>
public override void FromXml(SecurityElement e)
{
if (e == null) {
throw new ArgumentNullException(SR.GetString(SR.InvalidSecurityElem));
}
// SecurityElement must be a permission element
if (!e.Tag.Equals("IPermission")) {
throw new ArgumentException(SR.GetString(SR.InvalidSecurityElem), "securityElement");
}
string className = e.Attribute("class");
// SecurityElement must be a permission element for this type
if (className == null) {
throw new ArgumentException(SR.GetString(SR.InvalidSecurityElem), "securityElement");
}
if (className.IndexOf(this.GetType().FullName, StringComparison.Ordinal) < 0) {
throw new ArgumentException(SR.GetString(SR.InvalidSecurityElem), "securityElement");
}
string str = e.Attribute("Unrestricted");
m_noRestriction = (str != null ? (0 == string.Compare(str, "true", StringComparison.OrdinalIgnoreCase)) : false);
}
/// <summary>
/// Copyto a security element
/// </summary>
/// <returns></returns>
public override SecurityElement ToXml()
{
SecurityElement securityElement = new SecurityElement("IPermission");
securityElement.AddAttribute("class", this.GetType().FullName + ", " + this.GetType().Module.Assembly.FullName.Replace('\"', '\''));
securityElement.AddAttribute("version", "1");
if (m_noRestriction) {
securityElement.AddAttribute("Unrestricted", "true");
}
return securityElement;
}
} // class PnrpPermission
} // namespace System.Net.PeerToPeer
|