mscorlib(4.0.0.0) API with additions
FileCodeGroup.cs
1 using System.Collections;
2 using System.IO;
5 using System.Security.Util;
6 
8 {
10  [Serializable]
11  [ComVisible(true)]
12  public sealed class FileCodeGroup : CodeGroup, IUnionSemanticCodeGroup
13  {
14  private FileIOPermissionAccess m_access;
15 
18  public override string MergeLogic => Environment.GetResourceString("MergeLogic_Union");
19 
22  public override string PermissionSetName => Environment.GetResourceString("FileCodeGroup_PermissionSet", XMLUtil.BitFieldEnumToString(typeof(FileIOPermissionAccess), m_access));
23 
26  public override string AttributeString => null;
27 
28  internal FileCodeGroup()
29  {
30  }
31 
37  public FileCodeGroup(IMembershipCondition membershipCondition, FileIOPermissionAccess access)
38  : base(membershipCondition, (PolicyStatement)null)
39  {
40  m_access = access;
41  }
42 
48  [SecuritySafeCritical]
49  public override PolicyStatement Resolve(Evidence evidence)
50  {
51  if (evidence == null)
52  {
53  throw new ArgumentNullException("evidence");
54  }
55  object usedEvidence = null;
56  if (PolicyManager.CheckMembershipCondition(base.MembershipCondition, evidence, out usedEvidence))
57  {
58  PolicyStatement policyStatement = CalculateAssemblyPolicy(evidence);
59  IDelayEvaluatedEvidence delayEvaluatedEvidence = usedEvidence as IDelayEvaluatedEvidence;
60  if (delayEvaluatedEvidence != null && !delayEvaluatedEvidence.IsVerified)
61  {
62  policyStatement.AddDependentEvidence(delayEvaluatedEvidence);
63  }
64  bool flag = false;
65  IEnumerator enumerator = base.Children.GetEnumerator();
66  while (enumerator.MoveNext() && !flag)
67  {
68  PolicyStatement policyStatement2 = PolicyManager.ResolveCodeGroup(enumerator.Current as CodeGroup, evidence);
69  if (policyStatement2 != null)
70  {
71  policyStatement.InplaceUnion(policyStatement2);
72  if ((policyStatement2.Attributes & PolicyStatementAttribute.Exclusive) == PolicyStatementAttribute.Exclusive)
73  {
74  flag = true;
75  }
76  }
77  }
78  return policyStatement;
79  }
80  return null;
81  }
82 
83  PolicyStatement IUnionSemanticCodeGroup.InternalResolve(Evidence evidence)
84  {
85  if (evidence == null)
86  {
87  throw new ArgumentNullException("evidence");
88  }
89  if (base.MembershipCondition.Check(evidence))
90  {
91  return CalculateAssemblyPolicy(evidence);
92  }
93  return null;
94  }
95 
100  public override CodeGroup ResolveMatchingCodeGroups(Evidence evidence)
101  {
102  if (evidence == null)
103  {
104  throw new ArgumentNullException("evidence");
105  }
106  if (base.MembershipCondition.Check(evidence))
107  {
108  CodeGroup codeGroup = Copy();
109  codeGroup.Children = new ArrayList();
110  IEnumerator enumerator = base.Children.GetEnumerator();
111  while (enumerator.MoveNext())
112  {
113  CodeGroup codeGroup2 = ((CodeGroup)enumerator.Current).ResolveMatchingCodeGroups(evidence);
114  if (codeGroup2 != null)
115  {
116  codeGroup.AddChild(codeGroup2);
117  }
118  }
119  return codeGroup;
120  }
121  return null;
122  }
123 
124  internal PolicyStatement CalculatePolicy(Url url)
125  {
126  URLString uRLString = url.GetURLString();
127  if (string.Compare(uRLString.Scheme, "file", StringComparison.OrdinalIgnoreCase) != 0)
128  {
129  return null;
130  }
131  string directoryName = uRLString.GetDirectoryName();
132  PermissionSet permissionSet = new PermissionSet(PermissionState.None);
133  permissionSet.SetPermission(new FileIOPermission(m_access, Path.GetFullPath(directoryName)));
134  return new PolicyStatement(permissionSet, PolicyStatementAttribute.Nothing);
135  }
136 
137  private PolicyStatement CalculateAssemblyPolicy(Evidence evidence)
138  {
139  PolicyStatement policyStatement = null;
140  Url hostEvidence = evidence.GetHostEvidence<Url>();
141  if (hostEvidence != null)
142  {
143  policyStatement = CalculatePolicy(hostEvidence);
144  }
145  if (policyStatement == null)
146  {
147  policyStatement = new PolicyStatement(new PermissionSet(fUnrestricted: false), PolicyStatementAttribute.Nothing);
148  }
149  return policyStatement;
150  }
151 
154  public override CodeGroup Copy()
155  {
156  FileCodeGroup fileCodeGroup = new FileCodeGroup(base.MembershipCondition, m_access);
157  fileCodeGroup.Name = base.Name;
158  fileCodeGroup.Description = base.Description;
159  IEnumerator enumerator = base.Children.GetEnumerator();
160  while (enumerator.MoveNext())
161  {
162  fileCodeGroup.AddChild((CodeGroup)enumerator.Current);
163  }
164  return fileCodeGroup;
165  }
166 
167  protected override void CreateXml(SecurityElement element, PolicyLevel level)
168  {
169  element.AddAttribute("Access", XMLUtil.BitFieldEnumToString(typeof(FileIOPermissionAccess), m_access));
170  }
171 
172  protected override void ParseXml(SecurityElement e, PolicyLevel level)
173  {
174  string text = e.Attribute("Access");
175  if (text != null)
176  {
177  m_access = (FileIOPermissionAccess)Enum.Parse(typeof(FileIOPermissionAccess), text);
178  }
179  else
180  {
181  m_access = FileIOPermissionAccess.NoAccess;
182  }
183  }
184 
189  public override bool Equals(object o)
190  {
191  FileCodeGroup fileCodeGroup = o as FileCodeGroup;
192  if (fileCodeGroup != null && base.Equals((object)fileCodeGroup) && m_access == fileCodeGroup.m_access)
193  {
194  return true;
195  }
196  return false;
197  }
198 
201  public override int GetHashCode()
202  {
203  return base.GetHashCode() + m_access.GetHashCode();
204  }
205 
206  internal override string GetTypeName()
207  {
208  return "System.Security.Policy.FileCodeGroup";
209  }
210  }
211 }
abstract CodeGroup ResolveMatchingCodeGroups(Evidence evidence)
When overridden in a derived class, resolves matching code groups.
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
FileIOPermissionAccess
Specifies the type of file access requested.
bool MoveNext()
Advances the enumerator to the next element of the collection.
override CodeGroup ResolveMatchingCodeGroups(Evidence evidence)
Resolves matching code groups.
StringComparison
Specifies the culture, case, and sort rules to be used by certain overloads of the M:System....
Represents the security policy levels for the common language runtime. This class cannot be inherited...
Definition: PolicyLevel.cs:15
Definition: __Canon.cs:3
PolicyStatementAttribute
Defines special attribute flags for security policy on code groups.
string Name
Gets or sets the name of the code group.
Definition: CodeGroup.cs:128
IPermission SetPermission(IPermission perm)
Sets a permission to the T:System.Security.PermissionSet, replacing any existing permission of the sa...
Provides the URL from which a code assembly originates as evidence for policy evaluation....
Definition: Url.cs:10
Represents the statement of a T:System.Security.Policy.CodeGroup describing the permissions and other...
void AddChild(CodeGroup group)
Adds a child code group to the current code group.
Definition: CodeGroup.cs:240
static object Parse(Type enumType, string value)
Converts the string representation of the name or numeric value of one or more enumerated constants t...
Definition: Enum.cs:298
override string PermissionSetName
Gets the name of the named permission set for the code group.
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
Grants permission to manipulate files located in the code assemblies to code assemblies that match th...
IList Children
Gets or sets an ordered list of the child code groups of a code group.
Definition: CodeGroup.cs:32
Represents a collection that can contain many different types of permissions.
Defines the test to determine whether a code assembly is a member of a code group.
Provides the base class for enumerations.
Definition: Enum.cs:14
Represents the XML object model for encoding security objects. This class cannot be inherited.
override string AttributeString
Gets a string representation of the attributes of the policy statement for the code group.
override void ParseXml(SecurityElement e, PolicyLevel level)
When overridden in a derived class, reconstructs properties and internal state specific to a derived ...
string Description
Gets or sets the description of the code group.
Definition: CodeGroup.cs:142
object Current
Gets the element in the collection at the current position of the enumerator.
Definition: IEnumerator.cs:15
Represents the abstract base class from which all implementations of code groups must derive.
Definition: CodeGroup.cs:11
override void CreateXml(SecurityElement element, PolicyLevel level)
When overridden in a derived class, serializes properties and internal state specific to a derived co...
override CodeGroup Copy()
Makes a deep copy of the current code group.
PolicyStatement PolicyStatement
Gets or sets the policy statement associated with the code group.
Definition: CodeGroup.cs:99
override string MergeLogic
Gets the merge logic.
override int GetHashCode()
Gets the hash code of the current code group.
static string GetFullPath(string path)
Returns the absolute path for the specified path string.
Definition: Path.cs:446
FileCodeGroup(IMembershipCondition membershipCondition, FileIOPermissionAccess access)
Initializes a new instance of the T:System.Security.Policy.FileCodeGroup class.
override PolicyStatement Resolve(Evidence evidence)
Resolves policy for the code group and its descendants for a set of evidence.
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.
override bool Equals(object o)
Determines whether the specified code group is equivalent to the current code group.
Controls the ability to access files and folders. This class cannot be inherited.
PolicyStatementAttribute Attributes
Gets or sets the attributes of the policy statement.
Supports a simple iteration over a non-generic collection.
Definition: IEnumerator.cs:9
Performs operations on T:System.String instances that contain file or directory path information....
Definition: Path.cs:13
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14