2 // System.Web.Configuration.AuthorizationRule
5 // Chris Toshok (toshok@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections.Specialized;
33 using System.Security.Principal;
34 using System.Configuration;
35 using System.ComponentModel;
37 using System.Globalization;
38 using System.Web.Util;
42 namespace System.Web.Configuration {
44 public sealed class AuthorizationRule : ConfigurationElement
46 static ConfigurationProperty rolesProp;
47 static ConfigurationProperty usersProp;
48 static ConfigurationProperty verbsProp;
49 static ConfigurationPropertyCollection properties;
51 AuthorizationRuleAction action;
52 ConfigurationSaveMode saveMode = ConfigurationSaveMode.Full;
54 static AuthorizationRule ()
56 rolesProp = new ConfigurationProperty ("roles", typeof (StringCollection), null,
57 PropertyHelper.CommaDelimitedStringCollectionConverter,
58 PropertyHelper.DefaultValidator,
59 ConfigurationPropertyOptions.None);
60 usersProp = new ConfigurationProperty ("users", typeof (StringCollection), null,
61 PropertyHelper.CommaDelimitedStringCollectionConverter,
62 PropertyHelper.DefaultValidator,
63 ConfigurationPropertyOptions.None);
64 verbsProp = new ConfigurationProperty ("verbs", typeof (StringCollection), null,
65 PropertyHelper.CommaDelimitedStringCollectionConverter,
66 PropertyHelper.DefaultValidator,
67 ConfigurationPropertyOptions.None);
68 properties = new ConfigurationPropertyCollection ();
70 properties.Add (rolesProp);
71 properties.Add (usersProp);
72 properties.Add (verbsProp);
75 public AuthorizationRule (AuthorizationRuleAction action)
78 base[rolesProp] = new CommaDelimitedStringCollection ();
79 base[usersProp] = new CommaDelimitedStringCollection ();
80 base[verbsProp] = new CommaDelimitedStringCollection ();
83 public override bool Equals (object obj)
85 AuthorizationRule auth = obj as AuthorizationRule;
89 if (action != auth.Action)
92 if (Roles.Count != auth.Roles.Count
93 || Users.Count != auth.Users.Count
94 || Verbs.Count != auth.Verbs.Count)
99 for (i = 0; i < Roles.Count; i ++)
100 if (Roles[i] != auth.Roles[i])
103 for (i = 0; i < Users.Count; i ++)
104 if (Users[i] != auth.Users[i])
107 for (i = 0; i < Verbs.Count; i ++)
108 if (Verbs[i] != auth.Verbs[i])
114 public override int GetHashCode ()
116 int hashCode = (int)action;
119 for (i = 0; i < Roles.Count; i ++)
120 hashCode += Roles[i].GetHashCode();
122 for (i = 0; i < Users.Count; i ++)
123 hashCode += Users[i].GetHashCode();
125 for (i = 0; i < Verbs.Count; i ++)
126 hashCode += Verbs[i].GetHashCode();
131 [MonoTODO ("Not implemented")]
132 protected internal override bool IsModified ()
134 if (((CommaDelimitedStringCollection)Roles).IsModified || ((CommaDelimitedStringCollection)Users).IsModified || ((CommaDelimitedStringCollection)Verbs).IsModified)
142 if (Roles.Count == 0 && Users.Count == 0)
143 throw new ConfigurationErrorsException ("You must supply either a list of users or roles when creating an AuthorizationRule");
146 protected override void PostDeserialize ()
148 base.PostDeserialize();
153 protected override void PreSerialize (XmlWriter writer)
155 base.PreSerialize (writer);
160 protected internal override void Reset (ConfigurationElement parentElement)
162 AuthorizationRule r = (AuthorizationRule)parentElement;
165 base.Reset (parentElement);
168 protected internal override void ResetModified ()
170 base.ResetModified ();
173 protected internal override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
175 if (saveMode != ConfigurationSaveMode.Full && !IsModified ())
178 PreSerialize (writer);
180 writer.WriteStartElement (action == AuthorizationRuleAction.Allow ? "allow" : "deny");
182 writer.WriteAttributeString ("roles", Roles.ToString());
184 writer.WriteAttributeString ("users", Users.ToString());
186 writer.WriteAttributeString ("verbs", Verbs.ToString());
188 writer.WriteEndElement ();
193 protected internal override void SetReadOnly ()
198 protected internal override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
200 base.Unmerge (sourceElement, parentElement, saveMode);
201 this.saveMode = saveMode;
203 AuthorizationRule source = sourceElement as AuthorizationRule;
205 this.action = source.Action;
208 public AuthorizationRuleAction Action {
209 get { return action; }
210 set { action = value; }
213 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
214 [ConfigurationProperty ("roles")]
215 public StringCollection Roles {
216 get { return (StringCollection) base [rolesProp];}
219 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
220 [ConfigurationProperty ("users")]
221 public StringCollection Users {
222 get { return (StringCollection) base [usersProp];}
225 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
226 [ConfigurationProperty ("verbs")]
227 public StringCollection Verbs {
228 get { return (StringCollection) base [verbsProp];}
231 protected internal override ConfigurationPropertyCollection Properties {
232 get { return properties; }
236 internal bool CheckVerb (string verb)
238 foreach (string v in Verbs) {
239 if (String.Compare (v, verb, true, Helpers.InvariantCulture) == 0)
245 internal bool CheckUser (string user)
247 foreach (string u in Users) {
248 if (String.Compare (u, user, true, Helpers.InvariantCulture) == 0 ||
250 (u == "?" && user == ""))
256 internal bool CheckRole (IPrincipal user)
258 foreach (string r in Roles) {
259 if (user.IsInRole (r))