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);
155 protected override void Reset (ConfigurationElement parentElement)
157 base.Reset (parentElement);
161 protected override void ResetModified ()
163 base.ResetModified ();
166 protected override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
168 PreSerialize (writer);
170 writer.WriteStartElement (action == AuthorizationRuleAction.Allow ? "allow" : "deny");
172 writer.WriteAttributeString ("roles", Roles.ToString());
174 writer.WriteAttributeString ("users", Users.ToString());
176 writer.WriteAttributeString ("verbs", Verbs.ToString());
178 writer.WriteEndElement ();
184 protected override void SetReadOnly ()
190 protected override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
192 base.Unmerge (sourceElement, parentElement, saveMode);
195 public AuthorizationRuleAction Action {
196 get { return action; }
197 set { action = value; }
200 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
201 [ConfigurationProperty ("roles")]
202 public StringCollection Roles {
203 get { return (StringCollection) base [rolesProp];}
206 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
207 [ConfigurationProperty ("users")]
208 public StringCollection Users {
209 get { return (StringCollection) base [usersProp];}
212 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
213 [ConfigurationProperty ("verbs")]
214 public StringCollection Verbs {
215 get { return (StringCollection) base [verbsProp];}
218 protected override ConfigurationPropertyCollection Properties {
219 get { return properties; }
223 internal bool CheckVerb (string verb)
225 foreach (string v in Verbs) {
232 internal bool CheckUser (string user)
234 foreach (string u in Users) {
241 internal bool CheckRole (IPrincipal user)
243 foreach (string r in Roles) {
244 if (user.IsInRole (r))