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;
40 namespace System.Web.Configuration {
42 public sealed class AuthorizationRule : ConfigurationElement
44 static ConfigurationProperty rolesProp;
45 static ConfigurationProperty usersProp;
46 static ConfigurationProperty verbsProp;
47 static ConfigurationPropertyCollection properties;
49 AuthorizationRuleAction action;
51 static AuthorizationRule ()
53 rolesProp = new ConfigurationProperty ("roles", typeof (StringCollection), null,
54 PropertyHelper.CommaDelimitedStringCollectionConverter,
55 PropertyHelper.DefaultValidator,
56 ConfigurationPropertyOptions.None);
57 usersProp = new ConfigurationProperty ("users", typeof (StringCollection), null,
58 PropertyHelper.CommaDelimitedStringCollectionConverter,
59 PropertyHelper.DefaultValidator,
60 ConfigurationPropertyOptions.None);
61 verbsProp = new ConfigurationProperty ("verbs", typeof (StringCollection), null,
62 PropertyHelper.CommaDelimitedStringCollectionConverter,
63 PropertyHelper.DefaultValidator,
64 ConfigurationPropertyOptions.None);
65 properties = new ConfigurationPropertyCollection ();
67 properties.Add (rolesProp);
68 properties.Add (usersProp);
69 properties.Add (verbsProp);
72 public AuthorizationRule (AuthorizationRuleAction action)
75 base[rolesProp] = new CommaDelimitedStringCollection ();
76 base[usersProp] = new CommaDelimitedStringCollection ();
77 base[verbsProp] = new CommaDelimitedStringCollection ();
80 public override bool Equals (object obj)
82 AuthorizationRule auth = obj as AuthorizationRule;
86 if (action != auth.Action)
89 if (Roles.Count != auth.Roles.Count
90 || Users.Count != auth.Users.Count
91 || Verbs.Count != auth.Verbs.Count)
96 for (i = 0; i < Roles.Count; i ++)
97 if (Roles[i] != auth.Roles[i])
100 for (i = 0; i < Users.Count; i ++)
101 if (Users[i] != auth.Users[i])
104 for (i = 0; i < Verbs.Count; i ++)
105 if (Verbs[i] != auth.Verbs[i])
111 public override int GetHashCode ()
113 int hashCode = (int)action;
116 for (i = 0; i < Roles.Count; i ++)
117 hashCode += Roles[i].GetHashCode();
119 for (i = 0; i < Users.Count; i ++)
120 hashCode += Users[i].GetHashCode();
122 for (i = 0; i < Verbs.Count; i ++)
123 hashCode += Verbs[i].GetHashCode();
129 protected override bool IsModified ()
131 throw new NotImplementedException ();
136 if (Roles.Count == 0 && Users.Count == 0)
137 throw new ConfigurationErrorsException ("You must supply either a list of users or roles when creating an AuthorizationRule");
140 protected override void PostDeserialize ()
142 base.PostDeserialize();
147 protected override void PreSerialize (XmlWriter writer)
149 base.PreSerialize (writer);
154 protected override void Reset (ConfigurationElement parentElement)
156 AuthorizationRule r = (AuthorizationRule)parentElement;
159 base.Reset (parentElement);
163 protected override void ResetModified ()
165 base.ResetModified ();
168 protected override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
170 PreSerialize (writer);
172 writer.WriteStartElement (action == AuthorizationRuleAction.Allow ? "allow" : "deny");
174 writer.WriteAttributeString ("roles", Roles.ToString());
176 writer.WriteAttributeString ("users", Users.ToString());
178 writer.WriteAttributeString ("verbs", Verbs.ToString());
180 writer.WriteEndElement ();
186 protected override void SetReadOnly ()
192 protected override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
194 base.Unmerge (sourceElement, parentElement, saveMode);
197 public AuthorizationRuleAction Action {
198 get { return action; }
199 set { action = value; }
202 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
203 [ConfigurationProperty ("roles")]
204 public StringCollection Roles {
205 get { return (StringCollection) base [rolesProp];}
208 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
209 [ConfigurationProperty ("users")]
210 public StringCollection Users {
211 get { return (StringCollection) base [usersProp];}
214 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
215 [ConfigurationProperty ("verbs")]
216 public StringCollection Verbs {
217 get { return (StringCollection) base [verbsProp];}
220 protected override ConfigurationPropertyCollection Properties {
221 get { return properties; }
225 internal bool CheckVerb (string verb)
227 foreach (string v in Verbs) {
228 if (String.Compare (v, verb, true) == 0)
234 internal bool CheckUser (string user)
236 foreach (string u in Users) {
237 if (String.Compare (u, user, true) == 0 ||
239 (u == "?" && user == ""))
245 internal bool CheckRole (IPrincipal user)
247 foreach (string r in Roles) {
248 if (user.IsInRole (r))