AuthorizationRule.cs 6.7 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. #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. [MonoTODO]
  126. protected override void Reset (ConfigurationElement parentElement)
  127. {
  128. base.Reset (parentElement);
  129. }
  130. [MonoTODO]
  131. protected override void ResetModified ()
  132. {
  133. base.ResetModified ();
  134. }
  135. protected override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
  136. {
  137. PreSerialize (writer);
  138. writer.WriteStartElement (action == AuthorizationRuleAction.Allow ? "allow" : "deny");
  139. if (Roles.Count > 0)
  140. writer.WriteAttributeString ("roles", Roles.ToString());
  141. if (Users.Count > 0)
  142. writer.WriteAttributeString ("users", Users.ToString());
  143. if (Verbs.Count > 0)
  144. writer.WriteAttributeString ("verbs", Verbs.ToString());
  145. writer.WriteEndElement ();
  146. return true;
  147. }
  148. [MonoTODO]
  149. protected override void SetReadOnly ()
  150. {
  151. base.SetReadOnly();
  152. }
  153. [MonoTODO]
  154. protected override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
  155. {
  156. base.Unmerge (sourceElement, parentElement, saveMode);
  157. }
  158. public AuthorizationRuleAction Action {
  159. get { return action; }
  160. set { action = value; }
  161. }
  162. [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
  163. [ConfigurationProperty ("roles")]
  164. public StringCollection Roles {
  165. get { return (StringCollection) base [rolesProp];}
  166. }
  167. [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
  168. [ConfigurationProperty ("users")]
  169. public StringCollection Users {
  170. get { return (StringCollection) base [usersProp];}
  171. }
  172. [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
  173. [ConfigurationProperty ("verbs")]
  174. public StringCollection Verbs {
  175. get { return (StringCollection) base [verbsProp];}
  176. }
  177. protected override ConfigurationPropertyCollection Properties {
  178. get { return properties; }
  179. }
  180. internal bool CheckVerb (string verb)
  181. {
  182. foreach (string v in Verbs) {
  183. if (String.Compare (v, verb, true) == 0)
  184. return true;
  185. }
  186. return false;
  187. }
  188. internal bool CheckUser (string user)
  189. {
  190. foreach (string u in Users) {
  191. if (String.Compare (u, user, true) == 0 ||
  192. u == "*" ||
  193. (u == "?" && user == ""))
  194. return true;
  195. }
  196. return false;
  197. }
  198. internal bool CheckRole (IPrincipal user)
  199. {
  200. foreach (string r in Roles) {
  201. if (user.IsInRole (r))
  202. return true;
  203. }
  204. return false;
  205. }
  206. }
  207. }
  208. #endif