AuthorizationRule.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. }
  66. public override bool Equals (object obj)
  67. {
  68. AuthorizationRule auth = obj as AuthorizationRule;
  69. if (auth == null)
  70. return false;
  71. if (action != auth.Action)
  72. return false;
  73. if (Roles.Count != auth.Roles.Count
  74. || Users.Count != auth.Users.Count
  75. || Verbs.Count != auth.Verbs.Count)
  76. return false;
  77. int i;
  78. for (i = 0; i < Roles.Count; i ++)
  79. if (Roles[i] != auth.Roles[i])
  80. return false;
  81. for (i = 0; i < Users.Count; i ++)
  82. if (Users[i] != auth.Users[i])
  83. return false;
  84. for (i = 0; i < Verbs.Count; i ++)
  85. if (Verbs[i] != auth.Verbs[i])
  86. return false;
  87. return true;
  88. }
  89. public override int GetHashCode ()
  90. {
  91. int hashCode = (int)action;
  92. int i;
  93. for (i = 0; i < Roles.Count; i ++)
  94. hashCode += Roles[i].GetHashCode();
  95. for (i = 0; i < Users.Count; i ++)
  96. hashCode += Users[i].GetHashCode();
  97. for (i = 0; i < Verbs.Count; i ++)
  98. hashCode += Verbs[i].GetHashCode();
  99. return hashCode;
  100. }
  101. [MonoTODO]
  102. protected override bool IsModified ()
  103. {
  104. throw new NotImplementedException ();
  105. }
  106. [MonoTODO]
  107. protected override void PostDeserialize ()
  108. {
  109. base.PostDeserialize();
  110. }
  111. [MonoTODO]
  112. protected override void PreSerialize (XmlWriter writer)
  113. {
  114. base.PreSerialize (writer);
  115. }
  116. [MonoTODO]
  117. protected override void Reset (ConfigurationElement parentElement)
  118. {
  119. base.Reset (parentElement);
  120. }
  121. [MonoTODO]
  122. protected override void ResetModified ()
  123. {
  124. base.ResetModified ();
  125. }
  126. [MonoTODO]
  127. protected override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
  128. {
  129. bool ret = base.SerializeElement (writer, serializeCollectionKey);
  130. /* XXX more here? .. */
  131. return ret;
  132. }
  133. [MonoTODO]
  134. protected override void SetReadOnly ()
  135. {
  136. base.SetReadOnly();
  137. }
  138. [MonoTODO]
  139. protected override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
  140. {
  141. base.Unmerge (sourceElement, parentElement, saveMode);
  142. }
  143. public AuthorizationRuleAction Action {
  144. get { return action; }
  145. set { action = value; }
  146. }
  147. [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
  148. [ConfigurationProperty ("roles")]
  149. public StringCollection Roles {
  150. get { return (StringCollection) base [rolesProp];}
  151. }
  152. [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
  153. [ConfigurationProperty ("users")]
  154. public StringCollection Users {
  155. get { return (StringCollection) base [usersProp];}
  156. }
  157. [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
  158. [ConfigurationProperty ("verbs")]
  159. public StringCollection Verbs {
  160. get { return (StringCollection) base [verbsProp];}
  161. }
  162. protected override ConfigurationPropertyCollection Properties {
  163. get { return properties; }
  164. }
  165. }
  166. }
  167. #endif