CodeGroup.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // System.Security.Policy.CodeGroup
  2. //
  3. // Author(s):
  4. // Nick Drochak ([email protected])
  5. //
  6. // (C) 2001 Nick Drochak, All rights reserved.
  7. using System.Security.Policy;
  8. using System.Security.Permissions;
  9. using System.Collections;
  10. using System; // for MonoTODO attribute
  11. namespace System.Security.Policy
  12. {
  13. [Serializable]
  14. public abstract class CodeGroup
  15. {
  16. PolicyStatement m_policy = null;
  17. IMembershipCondition m_membershipCondition = null;
  18. string m_description = null;
  19. string m_name = null;
  20. ArrayList m_children = new ArrayList();
  21. PolicyLevel m_level;
  22. public CodeGroup(IMembershipCondition membershipCondition,
  23. PolicyStatement policy)
  24. {
  25. if (null == membershipCondition)
  26. throw new ArgumentNullException("Value cannot be null.");
  27. m_policy = policy;
  28. m_membershipCondition = membershipCondition;
  29. }
  30. public abstract CodeGroup Copy();
  31. public abstract string MergeLogic {get;}
  32. public abstract PolicyStatement Resolve( Evidence evidence);
  33. public abstract CodeGroup ResolveMatchingCodeGroups(Evidence evidence);
  34. public PolicyStatement PolicyStatement
  35. {
  36. get
  37. {
  38. return m_policy;
  39. }
  40. set
  41. {
  42. m_policy = value;
  43. }
  44. }
  45. public string Description
  46. {
  47. get
  48. {
  49. return m_description;
  50. }
  51. set
  52. {
  53. m_description = value;
  54. }
  55. }
  56. public IMembershipCondition MembershipCondition
  57. {
  58. get
  59. {
  60. return m_membershipCondition;
  61. }
  62. set
  63. {
  64. if (null == value)
  65. throw new ArgumentException("Value cannot be null");
  66. m_membershipCondition = value;
  67. }
  68. }
  69. public string Name
  70. {
  71. get
  72. {
  73. return m_name;
  74. }
  75. set
  76. {
  77. m_name = value;
  78. }
  79. }
  80. public IList Children
  81. {
  82. get
  83. {
  84. return m_children;
  85. }
  86. set
  87. {
  88. if (null == value)
  89. throw new ArgumentException("Value cannot be null");
  90. m_children = new ArrayList(value);
  91. }
  92. }
  93. public virtual string AttributeString
  94. {
  95. get
  96. {
  97. if (null != m_policy)
  98. return m_policy.AttributeString;
  99. return null;
  100. }
  101. }
  102. public virtual string PermissionSetName
  103. {
  104. get
  105. {
  106. if (m_policy.PermissionSet is Security.NamedPermissionSet)
  107. return ((NamedPermissionSet)(m_policy.PermissionSet)).Name;
  108. return null;
  109. }
  110. }
  111. public void AddChild(CodeGroup group)
  112. {
  113. if (null == group)
  114. throw new ArgumentNullException("The group parameter cannot be null");
  115. m_children.Add(group);
  116. }
  117. public override bool Equals(object o)
  118. {
  119. if (!(o is CodeGroup))
  120. return false;
  121. return Equals((CodeGroup)o, false);
  122. }
  123. public bool Equals(CodeGroup cg, bool compareChildren)
  124. {
  125. if (cg.Name != this.Name)
  126. return false;
  127. if (cg.Description != this.Description)
  128. return false;
  129. if (cg.MembershipCondition != this.MembershipCondition)
  130. return false;
  131. if (compareChildren)
  132. {
  133. int childCount = cg.Children.Count;
  134. if (this.Children.Count != childCount)
  135. return false;
  136. for (int index = 0; index < childCount; index++)
  137. {
  138. // LAMESPEC: are we supposed to check child equality recursively?
  139. // The docs imply 'no' but it seems natural to do a 'deep' compare.
  140. // Will check the children's children, and so-on unless we find out that
  141. // we shouldn't
  142. if (!((CodeGroup)(this.Children[index])).Equals((CodeGroup)(cg.Children[index]), true))
  143. return false;
  144. }
  145. }
  146. return true;
  147. }
  148. public void RemoveChild(CodeGroup group)
  149. {
  150. if (!m_children.Contains(group))
  151. throw new ArgumentException();
  152. m_children.Remove(group);
  153. }
  154. [MonoTODO]
  155. public override int GetHashCode()
  156. {
  157. return 42;
  158. }
  159. public void FromXml(SecurityElement e)
  160. {
  161. FromXml(e, (PolicyLevel)null);
  162. }
  163. [MonoTODO]
  164. public void FromXml(SecurityElement e, PolicyLevel level )
  165. {
  166. if (null == e)
  167. throw new ArgumentNullException("e");
  168. // Not sure what might be serialized in this XML, so just do the strings for now
  169. // and null's for everything else
  170. m_children = null;
  171. m_policy = null;
  172. m_membershipCondition = null;
  173. m_name = e.Attribute("Name");
  174. m_description = e.Attribute("Description");
  175. // seems like we might need this to Resolve() in subclasses
  176. m_level = level;
  177. ParseXml(e, level);
  178. }
  179. protected virtual void ParseXml(SecurityElement e, PolicyLevel level)
  180. {
  181. }
  182. public SecurityElement ToXml()
  183. {
  184. return ToXml(null);
  185. }
  186. [MonoTODO("Not sure what to do with PolicyLevel parameter")]
  187. public SecurityElement ToXml(PolicyLevel level)
  188. {
  189. SecurityElement e = new SecurityElement("CodeGroup");
  190. e.AddAttribute("class", this.GetType().AssemblyQualifiedName);
  191. e.AddAttribute("version", "1");
  192. if (null != Name)
  193. e.AddAttribute("Name", Name);
  194. if (null != Description)
  195. e.AddAttribute("Description", Description);
  196. if (null != MembershipCondition)
  197. e.AddChild(MembershipCondition.ToXml());
  198. if (null != PolicyStatement)
  199. e.AddChild(PolicyStatement.PermissionSet.ToXml());
  200. foreach (CodeGroup child in Children)
  201. e.AddChild(child.ToXml());
  202. CreateXml(e, level);
  203. return e;
  204. }
  205. protected virtual void CreateXml(SecurityElement element, PolicyLevel level)
  206. {
  207. }
  208. } // public abstract class CodeGroup
  209. } // namespace System.Security.Policy