2010-03-02 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.Web / System.Web.Configuration_2.0 / AuthorizationRule.cs
blobc2e064af73f94ce9c3f803ff4a35792380a6a152
1 //
2 // System.Web.Configuration.AuthorizationRule
3 //
4 // Authors:
5 // Chris Toshok (toshok@ximian.com)
6 //
7 // (C) 2005 Novell, Inc (http://www.novell.com)
8 //
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:
18 //
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 //
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.
31 using System;
32 using System.Collections.Specialized;
33 using System.Security.Principal;
34 using System.Configuration;
35 using System.ComponentModel;
36 using System.Xml;
37 using System.Globalization;
38 using System.Web.Util;
40 #if NET_2_0
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;
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)
76 this.action = 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;
85 if (auth == null)
86 return false;
88 if (action != auth.Action)
89 return false;
91 if (Roles.Count != auth.Roles.Count
92 || Users.Count != auth.Users.Count
93 || Verbs.Count != auth.Verbs.Count)
94 return false;
96 int i;
98 for (i = 0; i < Roles.Count; i ++)
99 if (Roles[i] != auth.Roles[i])
100 return false;
102 for (i = 0; i < Users.Count; i ++)
103 if (Users[i] != auth.Users[i])
104 return false;
106 for (i = 0; i < Verbs.Count; i ++)
107 if (Verbs[i] != auth.Verbs[i])
108 return false;
110 return true;
113 public override int GetHashCode ()
115 int hashCode = (int)action;
116 int i;
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();
127 return hashCode;
130 [MonoTODO ("Not implemented")]
131 protected override bool IsModified ()
133 throw new NotImplementedException ();
136 void VerifyData ()
138 if (Roles.Count == 0 && Users.Count == 0)
139 throw new ConfigurationErrorsException ("You must supply either a list of users or roles when creating an AuthorizationRule");
142 protected override void PostDeserialize ()
144 base.PostDeserialize();
146 VerifyData ();
149 protected override void PreSerialize (XmlWriter writer)
151 base.PreSerialize (writer);
153 VerifyData ();
156 protected override void Reset (ConfigurationElement parentElement)
158 AuthorizationRule r = (AuthorizationRule)parentElement;
159 Action = r.Action;
161 base.Reset (parentElement);
164 protected override void ResetModified ()
166 base.ResetModified ();
169 protected override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
171 PreSerialize (writer);
173 writer.WriteStartElement (action == AuthorizationRuleAction.Allow ? "allow" : "deny");
174 if (Roles.Count > 0)
175 writer.WriteAttributeString ("roles", Roles.ToString());
176 if (Users.Count > 0)
177 writer.WriteAttributeString ("users", Users.ToString());
178 if (Verbs.Count > 0)
179 writer.WriteAttributeString ("verbs", Verbs.ToString());
181 writer.WriteEndElement ();
183 return true;
186 protected override void SetReadOnly ()
188 base.SetReadOnly();
191 protected override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
193 base.Unmerge (sourceElement, parentElement, saveMode);
196 public AuthorizationRuleAction Action {
197 get { return action; }
198 set { action = value; }
201 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
202 [ConfigurationProperty ("roles")]
203 public StringCollection Roles {
204 get { return (StringCollection) base [rolesProp];}
207 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
208 [ConfigurationProperty ("users")]
209 public StringCollection Users {
210 get { return (StringCollection) base [usersProp];}
213 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
214 [ConfigurationProperty ("verbs")]
215 public StringCollection Verbs {
216 get { return (StringCollection) base [verbsProp];}
219 protected override ConfigurationPropertyCollection Properties {
220 get { return properties; }
224 internal bool CheckVerb (string verb)
226 foreach (string v in Verbs) {
227 if (String.Compare (v, verb, true, Helpers.InvariantCulture) == 0)
228 return true;
230 return false;
233 internal bool CheckUser (string user)
235 foreach (string u in Users) {
236 if (String.Compare (u, user, true, Helpers.InvariantCulture) == 0 ||
237 u == "*" ||
238 (u == "?" && user == ""))
239 return true;
241 return false;
244 internal bool CheckRole (IPrincipal user)
246 foreach (string r in Roles) {
247 if (user.IsInRole (r))
248 return true;
250 return false;
257 #endif