AuthorizationRule.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //
  2. // System.Web.Configuration.AuthorizationRule
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections.Specialized;
  31. using System.Configuration;
  32. using System.ComponentModel;
  33. using System.Xml;
  34. #if NET_2_0
  35. namespace System.Web.Configuration {
  36. public sealed class AuthorizationRule : ConfigurationElement
  37. {
  38. static ConfigurationProperty rolesProp;
  39. static ConfigurationProperty usersProp;
  40. static ConfigurationProperty verbsProp;
  41. static ConfigurationPropertyCollection properties;
  42. AuthorizationRuleAction action;
  43. static AuthorizationRule ()
  44. {
  45. rolesProp = new ConfigurationProperty ("roles", typeof (StringCollection), null,
  46. PropertyHelper.CommaDelimitedStringCollectionConverter,
  47. PropertyHelper.DefaultValidator,
  48. ConfigurationPropertyOptions.None);
  49. usersProp = new ConfigurationProperty ("users", typeof (StringCollection), null,
  50. PropertyHelper.CommaDelimitedStringCollectionConverter,
  51. PropertyHelper.DefaultValidator,
  52. ConfigurationPropertyOptions.None);
  53. verbsProp = new ConfigurationProperty ("verbs", typeof (StringCollection), null,
  54. PropertyHelper.CommaDelimitedStringCollectionConverter,
  55. PropertyHelper.DefaultValidator,
  56. ConfigurationPropertyOptions.None);
  57. properties = new ConfigurationPropertyCollection ();
  58. properties.Add (rolesProp);
  59. properties.Add (usersProp);
  60. properties.Add (verbsProp);
  61. }
  62. public AuthorizationRule (AuthorizationRuleAction action)
  63. {
  64. this.action = action;
  65. base[rolesProp] = new CommaDelimitedStringCollection ();
  66. base[usersProp] = new CommaDelimitedStringCollection ();
  67. base[verbsProp] = new CommaDelimitedStringCollection ();
  68. }
  69. public override bool Equals (object obj)
  70. {
  71. AuthorizationRule auth = obj as AuthorizationRule;
  72. if (auth == null)
  73. return false;
  74. if (action != auth.Action)
  75. return false;
  76. if (Roles.Count != auth.Roles.Count
  77. || Users.Count != auth.Users.Count
  78. || Verbs.Count != auth.Verbs.Count)
  79. return false;
  80. int i;
  81. for (i = 0; i < Roles.Count; i ++)
  82. if (Roles[i] != auth.Roles[i])
  83. return false;
  84. for (i = 0; i < Users.Count; i ++)
  85. if (Users[i] != auth.Users[i])
  86. return false;
  87. for (i = 0; i < Verbs.Count; i ++)
  88. if (Verbs[i] != auth.Verbs[i])
  89. return false;
  90. return true;
  91. }
  92. public override int GetHashCode ()
  93. {
  94. int hashCode = (int)action;
  95. int i;
  96. for (i = 0; i < Roles.Count; i ++)
  97. hashCode += Roles[i].GetHashCode();
  98. for (i = 0; i < Users.Count; i ++)
  99. hashCode += Users[i].GetHashCode();
  100. for (i = 0; i < Verbs.Count; i ++)
  101. hashCode += Verbs[i].GetHashCode();
  102. return hashCode;
  103. }
  104. [MonoTODO]
  105. protected override bool IsModified ()
  106. {
  107. throw new NotImplementedException ();
  108. }
  109. void VerifyData ()
  110. {
  111. if (Roles.Count == 0 && Users.Count == 0)
  112. throw new ConfigurationErrorsException ("You must supply either a list of users or roles when creating an AuthorizationRule");
  113. }
  114. protected override void PostDeserialize ()
  115. {
  116. base.PostDeserialize();
  117. VerifyData ();
  118. }
  119. protected override void PreSerialize (XmlWriter writer)
  120. {
  121. base.PreSerialize (writer);
  122. VerifyData ();
  123. }
  124. [MonoTODO]
  125. protected override void Reset (ConfigurationElement parentElement)
  126. {
  127. base.Reset (parentElement);
  128. }
  129. [MonoTODO]
  130. protected override void ResetModified ()
  131. {
  132. base.ResetModified ();
  133. }
  134. protected override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
  135. {
  136. PreSerialize (writer);
  137. writer.WriteStartElement (action == AuthorizationRuleAction.Allow ? "allow" : "deny");
  138. if (Roles.Count > 0)
  139. writer.WriteAttributeString ("roles", Roles.ToString());
  140. if (Users.Count > 0)
  141. writer.WriteAttributeString ("users", Users.ToString());
  142. if (Verbs.Count > 0)
  143. writer.WriteAttributeString ("verbs", Verbs.ToString());
  144. writer.WriteEndElement ();
  145. return true;
  146. }
  147. [MonoTODO]
  148. protected override void SetReadOnly ()
  149. {
  150. base.SetReadOnly();
  151. }
  152. [MonoTODO]
  153. protected override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
  154. {
  155. base.Unmerge (sourceElement, parentElement, saveMode);
  156. }
  157. public AuthorizationRuleAction Action {
  158. get { return action; }
  159. set { action = value; }
  160. }
  161. [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
  162. [ConfigurationProperty ("roles")]
  163. public StringCollection Roles {
  164. get { return (StringCollection) base [rolesProp];}
  165. }
  166. [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
  167. [ConfigurationProperty ("users")]
  168. public StringCollection Users {
  169. get { return (StringCollection) base [usersProp];}
  170. }
  171. [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
  172. [ConfigurationProperty ("verbs")]
  173. public StringCollection Verbs {
  174. get { return (StringCollection) base [verbsProp];}
  175. }
  176. protected override ConfigurationPropertyCollection Properties {
  177. get { return properties; }
  178. }
  179. }
  180. }
  181. #endif