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;
41 namespace System.Web.Configuration {
43 public sealed class AuthorizationRule : ConfigurationElement
45 static ConfigurationProperty rolesProp;
46 static ConfigurationProperty usersProp;
47 static ConfigurationProperty verbsProp;
48 static ConfigurationPropertyCollection properties;
50 AuthorizationRuleAction action;
51 ConfigurationSaveMode saveMode = ConfigurationSaveMode.Full;
53 static AuthorizationRule ()
55 rolesProp = new ConfigurationProperty ("roles", typeof (StringCollection), null,
56 PropertyHelper.CommaDelimitedStringCollectionConverter,
57 PropertyHelper.DefaultValidator,
58 ConfigurationPropertyOptions.None);
59 usersProp = new ConfigurationProperty ("users", typeof (StringCollection), null,
60 PropertyHelper.CommaDelimitedStringCollectionConverter,
61 PropertyHelper.DefaultValidator,
62 ConfigurationPropertyOptions.None);
63 verbsProp = new ConfigurationProperty ("verbs", typeof (StringCollection), null,
64 PropertyHelper.CommaDelimitedStringCollectionConverter,
65 PropertyHelper.DefaultValidator,
66 ConfigurationPropertyOptions.None);
67 properties = new ConfigurationPropertyCollection ();
69 properties.Add (rolesProp);
70 properties.Add (usersProp);
71 properties.Add (verbsProp);
74 public AuthorizationRule (AuthorizationRuleAction action)
77 base[rolesProp] = new CommaDelimitedStringCollection ();
78 base[usersProp] = new CommaDelimitedStringCollection ();
79 base[verbsProp] = new CommaDelimitedStringCollection ();
82 public override bool Equals (object obj)
84 AuthorizationRule auth = obj as AuthorizationRule;
88 if (action != auth.Action)
91 if (Roles.Count != auth.Roles.Count
92 || Users.Count != auth.Users.Count
93 || Verbs.Count != auth.Verbs.Count)
98 for (i = 0; i < Roles.Count; i ++)
99 if (Roles[i] != auth.Roles[i])
102 for (i = 0; i < Users.Count; i ++)
103 if (Users[i] != auth.Users[i])
106 for (i = 0; i < Verbs.Count; i ++)
107 if (Verbs[i] != auth.Verbs[i])
113 public override int GetHashCode ()
115 int hashCode = (int)action;
118 for (i = 0; i < Roles.Count; i ++)
119 hashCode += Roles[i].GetHashCode();
121 for (i = 0; i < Users.Count; i ++)
122 hashCode += Users[i].GetHashCode();
124 for (i = 0; i < Verbs.Count; i ++)
125 hashCode += Verbs[i].GetHashCode();
130 [MonoTODO ("Not implemented")]
131 protected internal override bool IsModified ()
133 if (((CommaDelimitedStringCollection)Roles).IsModified || ((CommaDelimitedStringCollection)Users).IsModified || ((CommaDelimitedStringCollection)Verbs).IsModified)
141 if (Roles.Count == 0 && Users.Count == 0)
142 throw new ConfigurationErrorsException ("You must supply either a list of users or roles when creating an AuthorizationRule");
145 protected override void PostDeserialize ()
147 base.PostDeserialize();
152 protected override void PreSerialize (XmlWriter writer)
154 base.PreSerialize (writer);
159 protected internal override void Reset (ConfigurationElement parentElement)
161 AuthorizationRule r = (AuthorizationRule)parentElement;
164 base.Reset (parentElement);
167 protected internal override void ResetModified ()
169 base.ResetModified ();
172 protected internal override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
174 if (saveMode != ConfigurationSaveMode.Full && !IsModified ())
177 PreSerialize (writer);
179 writer.WriteStartElement (action == AuthorizationRuleAction.Allow ? "allow" : "deny");
181 writer.WriteAttributeString ("roles", Roles.ToString());
183 writer.WriteAttributeString ("users", Users.ToString());
185 writer.WriteAttributeString ("verbs", Verbs.ToString());
187 writer.WriteEndElement ();
192 protected internal override void SetReadOnly ()
197 protected internal override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
199 base.Unmerge (sourceElement, parentElement, saveMode);
200 this.saveMode = saveMode;
202 AuthorizationRule source = sourceElement as AuthorizationRule;
204 this.action = source.Action;
207 public AuthorizationRuleAction Action {
208 get { return action; }
209 set { action = value; }
212 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
213 [ConfigurationProperty ("roles")]
214 public StringCollection Roles {
215 get { return (StringCollection) base [rolesProp];}
218 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
219 [ConfigurationProperty ("users")]
220 public StringCollection Users {
221 get { return (StringCollection) base [usersProp];}
224 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
225 [ConfigurationProperty ("verbs")]
226 public StringCollection Verbs {
227 get { return (StringCollection) base [verbsProp];}
230 protected internal override ConfigurationPropertyCollection Properties {
231 get { return properties; }
235 internal bool CheckVerb (string verb)
237 foreach (string v in Verbs) {
238 if (String.Compare (v, verb, true, Helpers.InvariantCulture) == 0)
244 internal bool CheckUser (string user)
246 foreach (string u in Users) {
247 if (String.Compare (u, user, true, Helpers.InvariantCulture) == 0 ||
249 (u == "?" && user == ""))
255 internal bool CheckRole (IPrincipal user)
257 foreach (string r in Roles) {
258 if (user.IsInRole (r))