AuthorizationRule.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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.Security.Principal;
  32. using System.Configuration;
  33. using System.ComponentModel;
  34. using System.Xml;
  35. using System.Globalization;
  36. #if NET_2_0
  37. namespace System.Web.Configuration {
  38. public sealed class AuthorizationRule : ConfigurationElement
  39. {
  40. static ConfigurationProperty rolesProp;
  41. static ConfigurationProperty usersProp;
  42. static ConfigurationProperty verbsProp;
  43. static ConfigurationPropertyCollection properties;
  44. AuthorizationRuleAction action;
  45. static AuthorizationRule ()
  46. {
  47. rolesProp = new ConfigurationProperty ("roles", typeof (StringCollection), null,
  48. PropertyHelper.CommaDelimitedStringCollectionConverter,
  49. PropertyHelper.DefaultValidator,
  50. ConfigurationPropertyOptions.None);
  51. usersProp = new ConfigurationProperty ("users", typeof (StringCollection), null,
  52. PropertyHelper.CommaDelimitedStringCollectionConverter,
  53. PropertyHelper.DefaultValidator,
  54. ConfigurationPropertyOptions.None);
  55. verbsProp = new ConfigurationProperty ("verbs", typeof (StringCollection), null,
  56. PropertyHelper.CommaDelimitedStringCollectionConverter,
  57. PropertyHelper.DefaultValidator,
  58. ConfigurationPropertyOptions.None);
  59. properties = new ConfigurationPropertyCollection ();
  60. properties.Add (rolesProp);
  61. properties.Add (usersProp);
  62. properties.Add (verbsProp);
  63. }
  64. public AuthorizationRule (AuthorizationRuleAction action)
  65. {
  66. this.action = action;
  67. base[rolesProp] = new CommaDelimitedStringCollection ();
  68. base[usersProp] = new CommaDelimitedStringCollection ();
  69. base[verbsProp] = new CommaDelimitedStringCollection ();
  70. }
  71. public override bool Equals (object obj)
  72. {
  73. AuthorizationRule auth = obj as AuthorizationRule;
  74. if (auth == null)
  75. return false;
  76. if (action != auth.Action)
  77. return false;
  78. if (Roles.Count != auth.Roles.Count
  79. || Users.Count != auth.Users.Count
  80. || Verbs.Count != auth.Verbs.Count)
  81. return false;
  82. int i;
  83. for (i = 0; i < Roles.Count; i ++)
  84. if (Roles[i] != auth.Roles[i])
  85. return false;
  86. for (i = 0; i < Users.Count; i ++)
  87. if (Users[i] != auth.Users[i])
  88. return false;
  89. for (i = 0; i < Verbs.Count; i ++)
  90. if (Verbs[i] != auth.Verbs[i])
  91. return false;
  92. return true;
  93. }
  94. public override int GetHashCode ()
  95. {
  96. int hashCode = (int)action;
  97. int i;
  98. for (i = 0; i < Roles.Count; i ++)
  99. hashCode += Roles[i].GetHashCode();
  100. for (i = 0; i < Users.Count; i ++)
  101. hashCode += Users[i].GetHashCode();
  102. for (i = 0; i < Verbs.Count; i ++)
  103. hashCode += Verbs[i].GetHashCode();
  104. return hashCode;
  105. }
  106. [MonoTODO ("Not implemented")]
  107. protected override bool IsModified ()
  108. {
  109. throw new NotImplementedException ();
  110. }
  111. void VerifyData ()
  112. {
  113. if (Roles.Count == 0 && Users.Count == 0)
  114. throw new ConfigurationErrorsException ("You must supply either a list of users or roles when creating an AuthorizationRule");
  115. }
  116. protected override void PostDeserialize ()
  117. {
  118. base.PostDeserialize();
  119. VerifyData ();
  120. }
  121. protected override void PreSerialize (XmlWriter writer)
  122. {
  123. base.PreSerialize (writer);
  124. VerifyData ();
  125. }
  126. protected override void Reset (ConfigurationElement parentElement)
  127. {
  128. AuthorizationRule r = (AuthorizationRule)parentElement;
  129. Action = r.Action;
  130. base.Reset (parentElement);
  131. }
  132. protected override void ResetModified ()
  133. {
  134. base.ResetModified ();
  135. }
  136. protected override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
  137. {
  138. PreSerialize (writer);
  139. writer.WriteStartElement (action == AuthorizationRuleAction.Allow ? "allow" : "deny");
  140. if (Roles.Count > 0)
  141. writer.WriteAttributeString ("roles", Roles.ToString());
  142. if (Users.Count > 0)
  143. writer.WriteAttributeString ("users", Users.ToString());
  144. if (Verbs.Count > 0)
  145. writer.WriteAttributeString ("verbs", Verbs.ToString());
  146. writer.WriteEndElement ();
  147. return true;
  148. }
  149. protected override void SetReadOnly ()
  150. {
  151. base.SetReadOnly();
  152. }
  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. internal bool CheckVerb (string verb)
  180. {
  181. foreach (string v in Verbs) {
  182. if (String.Compare (v, verb, true, CultureInfo.InvariantCulture) == 0)
  183. return true;
  184. }
  185. return false;
  186. }
  187. internal bool CheckUser (string user)
  188. {
  189. foreach (string u in Users) {
  190. if (String.Compare (u, user, true, CultureInfo.InvariantCulture) == 0 ||
  191. u == "*" ||
  192. (u == "?" && user == ""))
  193. return true;
  194. }
  195. return false;
  196. }
  197. internal bool CheckRole (IPrincipal user)
  198. {
  199. foreach (string r in Roles) {
  200. if (user.IsInRole (r))
  201. return true;
  202. }
  203. return false;
  204. }
  205. }
  206. }
  207. #endif