mscorlib(4.0.0.0) API with additions
PolicyStatement.cs
1 using System.Collections;
6 using System.Security.Util;
7 using System.Text;
8 
10 {
12  [Serializable]
13  [ComVisible(true)]
15  {
16  internal PermissionSet m_permSet;
17 
18  [NonSerialized]
19  private List<IDelayEvaluatedEvidence> m_dependentEvidence;
20 
21  internal PolicyStatementAttribute m_attributes;
22 
26  {
27  get
28  {
29  lock (this)
30  {
31  return m_permSet.Copy();
32  }
33  }
34  set
35  {
36  lock (this)
37  {
38  if (value == null)
39  {
40  m_permSet = new PermissionSet(fUnrestricted: false);
41  }
42  else
43  {
44  m_permSet = value.Copy();
45  }
46  }
47  }
48  }
49 
53  {
54  get
55  {
56  return m_attributes;
57  }
58  set
59  {
60  if (ValidProperties(value))
61  {
62  m_attributes = value;
63  }
64  }
65  }
66 
69  public string AttributeString
70  {
71  get
72  {
73  StringBuilder stringBuilder = new StringBuilder();
74  bool flag = true;
75  if (GetFlag(1))
76  {
77  stringBuilder.Append("Exclusive");
78  flag = false;
79  }
80  if (GetFlag(2))
81  {
82  if (!flag)
83  {
84  stringBuilder.Append(" ");
85  }
86  stringBuilder.Append("LevelFinal");
87  }
88  return stringBuilder.ToString();
89  }
90  }
91 
92  internal IEnumerable<IDelayEvaluatedEvidence> DependentEvidence => m_dependentEvidence.AsReadOnly();
93 
94  internal bool HasDependentEvidence
95  {
96  get
97  {
98  if (m_dependentEvidence != null)
99  {
100  return m_dependentEvidence.Count > 0;
101  }
102  return false;
103  }
104  }
105 
106  internal PolicyStatement()
107  {
108  m_permSet = null;
109  m_attributes = PolicyStatementAttribute.Nothing;
110  }
111 
115  : this(permSet, PolicyStatementAttribute.Nothing)
116  {
117  }
118 
123  {
124  if (permSet == null)
125  {
126  m_permSet = new PermissionSet(fUnrestricted: false);
127  }
128  else
129  {
130  m_permSet = permSet.Copy();
131  }
132  if (ValidProperties(attributes))
133  {
134  m_attributes = attributes;
135  }
136  }
137 
138  private PolicyStatement(PermissionSet permSet, PolicyStatementAttribute attributes, bool copy)
139  {
140  if (permSet != null)
141  {
142  if (copy)
143  {
144  m_permSet = permSet.Copy();
145  }
146  else
147  {
148  m_permSet = permSet;
149  }
150  }
151  else
152  {
153  m_permSet = new PermissionSet(fUnrestricted: false);
154  }
155  m_attributes = attributes;
156  }
157 
158  internal void SetPermissionSetNoCopy(PermissionSet permSet)
159  {
160  m_permSet = permSet;
161  }
162 
163  internal PermissionSet GetPermissionSetNoCopy()
164  {
165  lock (this)
166  {
167  return m_permSet;
168  }
169  }
170 
174  {
175  PolicyStatement policyStatement = new PolicyStatement(m_permSet, Attributes, copy: true);
176  if (HasDependentEvidence)
177  {
178  policyStatement.m_dependentEvidence = new List<IDelayEvaluatedEvidence>(m_dependentEvidence);
179  }
180  return policyStatement;
181  }
182 
183  private static bool ValidProperties(PolicyStatementAttribute attributes)
184  {
185  if ((attributes & ~(PolicyStatementAttribute.Exclusive | PolicyStatementAttribute.LevelFinal)) == PolicyStatementAttribute.Nothing)
186  {
187  return true;
188  }
189  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidFlag"));
190  }
191 
192  private bool GetFlag(int flag)
193  {
194  return (flag & (int)m_attributes) != 0;
195  }
196 
197  internal void AddDependentEvidence(IDelayEvaluatedEvidence dependentEvidence)
198  {
199  if (m_dependentEvidence == null)
200  {
201  m_dependentEvidence = new List<IDelayEvaluatedEvidence>();
202  }
203  m_dependentEvidence.Add(dependentEvidence);
204  }
205 
206  internal void InplaceUnion(PolicyStatement childPolicy)
207  {
208  if ((Attributes & childPolicy.Attributes & PolicyStatementAttribute.Exclusive) == PolicyStatementAttribute.Exclusive)
209  {
210  throw new PolicyException(Environment.GetResourceString("Policy_MultipleExclusive"));
211  }
212  if (childPolicy.HasDependentEvidence)
213  {
214  bool flag = m_permSet.IsSubsetOf(childPolicy.GetPermissionSetNoCopy()) && !childPolicy.GetPermissionSetNoCopy().IsSubsetOf(m_permSet);
215  if (HasDependentEvidence | flag)
216  {
217  if (m_dependentEvidence == null)
218  {
219  m_dependentEvidence = new List<IDelayEvaluatedEvidence>();
220  }
221  m_dependentEvidence.AddRange(childPolicy.DependentEvidence);
222  }
223  }
224  if ((childPolicy.Attributes & PolicyStatementAttribute.Exclusive) == PolicyStatementAttribute.Exclusive)
225  {
226  m_permSet = childPolicy.GetPermissionSetNoCopy();
227  Attributes = childPolicy.Attributes;
228  }
229  else
230  {
231  m_permSet.InplaceUnion(childPolicy.GetPermissionSetNoCopy());
232  Attributes |= childPolicy.Attributes;
233  }
234  }
235 
239  {
240  return ToXml(null);
241  }
242 
247  public void FromXml(SecurityElement et)
248  {
249  FromXml(et, null);
250  }
251 
256  {
257  return ToXml(level, useInternal: false);
258  }
259 
260  internal SecurityElement ToXml(PolicyLevel level, bool useInternal)
261  {
262  SecurityElement securityElement = new SecurityElement("PolicyStatement");
263  securityElement.AddAttribute("version", "1");
264  if (m_attributes != 0)
265  {
266  securityElement.AddAttribute("Attributes", XMLUtil.BitFieldEnumToString(typeof(PolicyStatementAttribute), m_attributes));
267  }
268  lock (this)
269  {
270  if (m_permSet == null)
271  {
272  return securityElement;
273  }
274  if (!(m_permSet is NamedPermissionSet))
275  {
276  if (!useInternal)
277  {
278  securityElement.AddChild(m_permSet.ToXml());
279  return securityElement;
280  }
281  securityElement.AddChild(m_permSet.InternalToXml());
282  return securityElement;
283  }
284  NamedPermissionSet namedPermissionSet = (NamedPermissionSet)m_permSet;
285  if (level != null && level.GetNamedPermissionSet(namedPermissionSet.Name) != null)
286  {
287  securityElement.AddAttribute("PermissionSetName", namedPermissionSet.Name);
288  return securityElement;
289  }
290  if (!useInternal)
291  {
292  securityElement.AddChild(namedPermissionSet.ToXml());
293  return securityElement;
294  }
295  securityElement.AddChild(namedPermissionSet.InternalToXml());
296  return securityElement;
297  }
298  }
299 
305  [SecuritySafeCritical]
306  public void FromXml(SecurityElement et, PolicyLevel level)
307  {
308  FromXml(et, level, allowInternalOnly: false);
309  }
310 
311  [SecurityCritical]
312  internal void FromXml(SecurityElement et, PolicyLevel level, bool allowInternalOnly)
313  {
314  if (et == null)
315  {
316  throw new ArgumentNullException("et");
317  }
318  if (!et.Tag.Equals("PolicyStatement"))
319  {
320  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidXMLElement"), "PolicyStatement", GetType().FullName));
321  }
322  m_attributes = PolicyStatementAttribute.Nothing;
323  string text = et.Attribute("Attributes");
324  if (text != null)
325  {
326  m_attributes = (PolicyStatementAttribute)Enum.Parse(typeof(PolicyStatementAttribute), text);
327  }
328  lock (this)
329  {
330  m_permSet = null;
331  if (level != null)
332  {
333  string text2 = et.Attribute("PermissionSetName");
334  if (text2 != null)
335  {
336  m_permSet = level.GetNamedPermissionSetInternal(text2);
337  if (m_permSet == null)
338  {
339  m_permSet = new PermissionSet(PermissionState.None);
340  }
341  }
342  }
343  if (m_permSet == null)
344  {
345  SecurityElement securityElement = et.SearchForChildByTag("PermissionSet");
346  if (securityElement == null)
347  {
348  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidXML"));
349  }
350  string text3 = securityElement.Attribute("class");
351  if (text3 != null && (text3.Equals("NamedPermissionSet") || text3.Equals("System.Security.NamedPermissionSet")))
352  {
353  m_permSet = new NamedPermissionSet("DefaultName", PermissionState.None);
354  }
355  else
356  {
357  m_permSet = new PermissionSet(PermissionState.None);
358  }
359  try
360  {
361  m_permSet.FromXml(securityElement, allowInternalOnly, ignoreTypeLoadFailures: true);
362  }
363  catch
364  {
365  }
366  }
367  if (m_permSet == null)
368  {
369  m_permSet = new PermissionSet(PermissionState.None);
370  }
371  }
372  }
373 
374  [SecurityCritical]
375  internal void FromXml(SecurityDocument doc, int position, PolicyLevel level, bool allowInternalOnly)
376  {
377  if (doc == null)
378  {
379  throw new ArgumentNullException("doc");
380  }
381  if (!doc.GetTagForElement(position).Equals("PolicyStatement"))
382  {
383  throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Argument_InvalidXMLElement"), "PolicyStatement", GetType().FullName));
384  }
385  m_attributes = PolicyStatementAttribute.Nothing;
386  string attributeForElement = doc.GetAttributeForElement(position, "Attributes");
387  if (attributeForElement != null)
388  {
389  m_attributes = (PolicyStatementAttribute)Enum.Parse(typeof(PolicyStatementAttribute), attributeForElement);
390  }
391  lock (this)
392  {
393  m_permSet = null;
394  if (level != null)
395  {
396  string attributeForElement2 = doc.GetAttributeForElement(position, "PermissionSetName");
397  if (attributeForElement2 != null)
398  {
399  m_permSet = level.GetNamedPermissionSetInternal(attributeForElement2);
400  if (m_permSet == null)
401  {
402  m_permSet = new PermissionSet(PermissionState.None);
403  }
404  }
405  }
406  if (m_permSet == null)
407  {
408  ArrayList childrenPositionForElement = doc.GetChildrenPositionForElement(position);
409  int num = -1;
410  for (int i = 0; i < childrenPositionForElement.Count; i++)
411  {
412  if (doc.GetTagForElement((int)childrenPositionForElement[i]).Equals("PermissionSet"))
413  {
414  num = (int)childrenPositionForElement[i];
415  }
416  }
417  if (num == -1)
418  {
419  throw new ArgumentException(Environment.GetResourceString("Argument_InvalidXML"));
420  }
421  string attributeForElement3 = doc.GetAttributeForElement(num, "class");
422  if (attributeForElement3 != null && (attributeForElement3.Equals("NamedPermissionSet") || attributeForElement3.Equals("System.Security.NamedPermissionSet")))
423  {
424  m_permSet = new NamedPermissionSet("DefaultName", PermissionState.None);
425  }
426  else
427  {
428  m_permSet = new PermissionSet(PermissionState.None);
429  }
430  m_permSet.FromXml(doc, num, allowInternalOnly);
431  }
432  if (m_permSet == null)
433  {
434  m_permSet = new PermissionSet(PermissionState.None);
435  }
436  }
437  }
438 
443  [ComVisible(false)]
444  public override bool Equals(object obj)
445  {
446  PolicyStatement policyStatement = obj as PolicyStatement;
447  if (policyStatement == null)
448  {
449  return false;
450  }
451  if (m_attributes != policyStatement.m_attributes)
452  {
453  return false;
454  }
455  if (!object.Equals(m_permSet, policyStatement.m_permSet))
456  {
457  return false;
458  }
459  return true;
460  }
461 
464  [ComVisible(false)]
465  public override int GetHashCode()
466  {
467  int num = (int)m_attributes;
468  if (m_permSet != null)
469  {
470  num ^= m_permSet.GetHashCode();
471  }
472  return num;
473  }
474  }
475 }
The exception that is thrown when a null reference (Nothing in Visual Basic) is passed to a method th...
int Count
Gets the number of elements contained in the T:System.Collections.Generic.List`1.
Definition: List.cs:296
unsafe override string ToString()
Converts the value of this instance to a T:System.String.
PolicyStatement Copy()
Creates an equivalent copy of the current policy statement.
PolicyStatement(PermissionSet permSet, PolicyStatementAttribute attributes)
Initializes a new instance of the T:System.Security.Policy.PolicyStatement class with the specified T...
PermissionSet PermissionSet
Gets or sets the T:System.Security.PermissionSet of the policy statement.
ReadOnlyCollection< T > AsReadOnly()
Returns a read-only T:System.Collections.ObjectModel.ReadOnlyCollection`1 wrapper for the current col...
Definition: List.cs:553
Represents the security policy levels for the common language runtime. This class cannot be inherited...
Definition: PolicyLevel.cs:15
virtual int Count
Gets the number of elements actually contained in the T:System.Collections.ArrayList.
Definition: ArrayList.cs:2255
Definition: __Canon.cs:3
PolicyStatementAttribute
Defines special attribute flags for security policy on code groups.
SecurityElement SearchForChildByTag(string tag)
Finds a child by its tag name.
string Tag
Gets or sets the tag name of an XML element.
PolicyStatement(PermissionSet permSet)
Initializes a new instance of the T:System.Security.Policy.PolicyStatement class with the specified T...
Represents the statement of a T:System.Security.Policy.CodeGroup describing the permissions and other...
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
void AddChild(SecurityElement child)
Adds a child element to the XML element.
void Add(T item)
Adds an object to the end of the T:System.Collections.Generic.List`1.
Definition: List.cs:510
Provides information about, and means to manipulate, the current environment and platform....
Definition: Environment.cs:21
StringBuilder Append(char value, int repeatCount)
Appends a specified number of copies of the string representation of a Unicode character to this inst...
Represents a collection that can contain many different types of permissions.
override bool Equals(object obj)
Determines whether the specified T:System.Security.Policy.PolicyStatement object is equal to the curr...
Provides the base class for enumerations.
Definition: Enum.cs:14
Represents the XML object model for encoding security objects. This class cannot be inherited.
Defines the methods that convert permission object state to and from XML element representation.
override int GetHashCode()
Gets a hash code for the T:System.Security.Policy.PolicyStatement object that is suitable for use in ...
string AttributeString
Gets a string representation of the attributes of the policy statement.
void AddRange(IEnumerable< T > collection)
Adds the elements of the specified collection to the end of the T:System.Collections....
Definition: List.cs:545
Represents a mutable string of characters. This class cannot be inherited.To browse the ....
SecurityElement ToXml()
Creates an XML encoding of the security object and its current state.
static CultureInfo CurrentCulture
Gets or sets the T:System.Globalization.CultureInfo object that represents the culture used by the cu...
Definition: CultureInfo.cs:120
The exception that is thrown when one of the arguments provided to a method is not valid.
Supports the methods that convert permission object state to and from an XML element representation.
Attribute can be applied to an enumeration.
PermissionState
Specifies whether a permission should have all or no access to resources at creation.
void AddAttribute(string name, string value)
Adds a name/value attribute to an XML element.
Specifies that the class can be serialized.
void FromXml(SecurityElement et)
Reconstructs a security object with a given state from an XML encoding.
void FromXml(SecurityElement et, PolicyLevel level)
Reconstructs a security object with a given state from an XML encoding.
virtual PermissionSet Copy()
Creates a copy of the T:System.Security.PermissionSet.
Provides information about a specific culture (called a locale for unmanaged code development)....
Definition: CultureInfo.cs:16
NamedPermissionSet GetNamedPermissionSet(string name)
Returns the T:System.Security.NamedPermissionSet in the current policy level with the specified name.
Definition: PolicyLevel.cs:561
SecurityElement ToXml(PolicyLevel level)
Creates an XML encoding of the security object and its current state.
PolicyStatementAttribute Attributes
Gets or sets the attributes of the policy statement.
Implements the T:System.Collections.IList interface using an array whose size is dynamically increase...
Definition: ArrayList.cs:14