AuthorizationRule.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #if NET_2_0
  36. namespace System.Web.Configuration {
  37. public sealed class AuthorizationRule : ConfigurationElement
  38. {
  39. static ConfigurationProperty rolesProp;
  40. static ConfigurationProperty usersProp;
  41. static ConfigurationProperty verbsProp;
  42. static ConfigurationPropertyCollection properties;
  43. AuthorizationRuleAction action;
  44. static AuthorizationRule ()
  45. {
  46. rolesProp = new ConfigurationProperty ("roles", typeof (StringCollection), null,
  47. PropertyHelper.CommaDelimitedStringCollectionConverter,
  48. PropertyHelper.DefaultValidator,
  49. ConfigurationPropertyOptions.None);
  50. usersProp = new ConfigurationProperty ("users", typeof (StringCollection), null,
  51. PropertyHelper.CommaDelimitedStringCollectionConverter,
  52. PropertyHelper.DefaultValidator,
  53. ConfigurationPropertyOptions.None);
  54. verbsProp = new ConfigurationProperty ("verbs", typeof (StringCollection), null,
  55. PropertyHelper.CommaDelimitedStringCollectionConverter,
  56. PropertyHelper.DefaultValidator,
  57. ConfigurationPropertyOptions.None);
  58. properties = new ConfigurationPropertyCollection ();
  59. properties.Add (rolesProp);
  60. properties.Add (usersProp);
  61. properties.Add (verbsProp);
  62. }
  63. public AuthorizationRule (AuthorizationRuleAction action)
  64. {
  65. this.action = action;
  66. base[rolesProp] = new CommaDelimitedStringCollection ();
  67. base[usersProp] = new CommaDelimitedStringCollection ();
  68. base[verbsProp] = new CommaDelimitedStringCollection ();
  69. }
  70. public override bool Equals (object obj)
  71. {
  72. AuthorizationRule auth = obj as AuthorizationRule;
  73. if (auth == null)
  74. return false;
  75. if (action != auth.Action)
  76. return false;
  77. if (Roles.Count != auth.Roles.Count
  78. || Users.Count != auth.Users.Count
  79. || Verbs.Count != auth.Verbs.Count)
  80. return false;
  81. int i;
  82. for (i = 0; i < Roles.Count; i ++)
  83. if (Roles[i] != auth.Roles[i])
  84. return false;
  85. for (i = 0; i < Users.Count; i ++)
  86. if (Users[i] != auth.Users[i])
  87. return false;
  88. for (i = 0; i < Verbs.Count; i ++)
  89. if (Verbs[i] != auth.Verbs[i])
  90. return false;
  91. return true;
  92. }
  93. public override int GetHashCode ()
  94. {
  95. int hashCode = (int)action;
  96. int i;
  97. for (i = 0; i < Roles.Count; i ++)
  98. hashCode += Roles[i].GetHashCode();
  99. for (i = 0; i < Users.Count; i ++)
  100. hashCode += Users[i].GetHashCode();
  101. for (i = 0; i < Verbs.Count; i ++)
  102. hashCode += Verbs[i].GetHashCode();
  103. return hashCode;
  104. }
  105. [MonoTODO]
  106. protected override bool IsModified ()
  107. {
  108. throw new NotImplementedException ();
  109. }
  110. void VerifyData ()
  111. {
  112. if (Roles.Count == 0 && Users.Count == 0)
  113. throw new ConfigurationErrorsException ("You must supply either a list of users or roles when creating an AuthorizationRule");
  114. }
  115. protected override void PostDeserialize ()
  116. {
  117. base.PostDeserialize();
  118. VerifyData ();
  119. }
  120. protected override void PreSerialize (XmlWriter writer)
  121. {
  122. base.PreSerialize (writer);
  123. VerifyData ();
  124. }
  125. protected override void Reset (ConfigurationElement parentElement)
  126. {
  127. AuthorizationRule r = (AuthorizationRule)parentElement;
  128. Action = r.Action;
  129. base.Reset (parentElement);
  130. }
  131. [MonoTODO]
  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. [MonoTODO]
  150. protected override void SetReadOnly ()
  151. {
  152. base.SetReadOnly();
  153. }
  154. [MonoTODO]
  155. protected override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
  156. {
  157. base.Unmerge (sourceElement, parentElement, saveMode);
  158. }
  159. public AuthorizationRuleAction Action {
  160. get { return action; }
  161. set { action = value; }
  162. }
  163. [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
  164. [ConfigurationProperty ("roles")]
  165. public StringCollection Roles {
  166. get { return (StringCollection) base [rolesProp];}
  167. }
  168. [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
  169. [ConfigurationProperty ("users")]
  170. public StringCollection Users {
  171. get { return (StringCollection) base [usersProp];}
  172. }
  173. [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
  174. [ConfigurationProperty ("verbs")]
  175. public StringCollection Verbs {
  176. get { return (StringCollection) base [verbsProp];}
  177. }
  178. protected override ConfigurationPropertyCollection Properties {
  179. get { return properties; }
  180. }
  181. internal bool CheckVerb (string verb)
  182. {
  183. foreach (string v in Verbs) {
  184. if (String.Compare (v, verb, true) == 0)
  185. return true;
  186. }
  187. return false;
  188. }
  189. internal bool CheckUser (string user)
  190. {
  191. foreach (string u in Users) {
  192. if (String.Compare (u, user, true) == 0 ||
  193. u == "*" ||
  194. (u == "?" && user == ""))
  195. return true;
  196. }
  197. return false;
  198. }
  199. internal bool CheckRole (IPrincipal user)
  200. {
  201. foreach (string r in Roles) {
  202. if (user.IsInRole (r))
  203. return true;
  204. }
  205. return false;
  206. }
  207. }
  208. }
  209. #endif