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 //
9 
10 //
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.
29 //
30 
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;
39 
40 
41 namespace System.Web.Configuration {
42 
43 	public sealed class AuthorizationRule : ConfigurationElement
44 	{
45 		static ConfigurationProperty rolesProp;
46 		static ConfigurationProperty usersProp;
47 		static ConfigurationProperty verbsProp;
48 		static ConfigurationPropertyCollection properties;
49 
50 		AuthorizationRuleAction action;
51 		ConfigurationSaveMode saveMode = ConfigurationSaveMode.Full;
52 
AuthorizationRule()53 		static AuthorizationRule ()
54 		{
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 ();
68 
69 			properties.Add (rolesProp);
70 			properties.Add (usersProp);
71 			properties.Add (verbsProp);
72 		}
73 
AuthorizationRule(AuthorizationRuleAction action)74 		public AuthorizationRule (AuthorizationRuleAction action)
75 		{
76 			this.action = action;
77 			base[rolesProp] = new CommaDelimitedStringCollection ();
78 			base[usersProp] = new CommaDelimitedStringCollection ();
79 			base[verbsProp] = new CommaDelimitedStringCollection ();
80 		}
81 
Equals(object obj)82 		public override bool Equals (object obj)
83 		{
84 			AuthorizationRule auth = obj as AuthorizationRule;
85 			if (auth == null)
86 				return false;
87 
88 			if (action != auth.Action)
89 				return false;
90 
91 			if (Roles.Count != auth.Roles.Count
92 			    || Users.Count != auth.Users.Count
93 			    || Verbs.Count != auth.Verbs.Count)
94 				return false;
95 
96 			int i;
97 
98 			for (i = 0; i < Roles.Count; i ++)
99 				if (Roles[i] != auth.Roles[i])
100 					return false;
101 
102 			for (i = 0; i < Users.Count; i ++)
103 				if (Users[i] != auth.Users[i])
104 					return false;
105 
106 			for (i = 0; i < Verbs.Count; i ++)
107 				if (Verbs[i] != auth.Verbs[i])
108 					return false;
109 
110 			return true;
111 		}
112 
GetHashCode()113 		public override int GetHashCode ()
114 		{
115 			int hashCode = (int)action;
116 			int i;
117 
118 			for (i = 0; i < Roles.Count; i ++)
119 				hashCode += Roles[i].GetHashCode();
120 
121 			for (i = 0; i < Users.Count; i ++)
122 				hashCode += Users[i].GetHashCode();
123 
124 			for (i = 0; i < Verbs.Count; i ++)
125 				hashCode += Verbs[i].GetHashCode();
126 
127 			return hashCode;
128 		}
129 
130 		[MonoTODO ("Not implemented")]
IsModified()131 		protected internal override bool IsModified ()
132 		{
133 			if (((CommaDelimitedStringCollection)Roles).IsModified || ((CommaDelimitedStringCollection)Users).IsModified || ((CommaDelimitedStringCollection)Verbs).IsModified)
134 				return true;
135 
136 			return false;
137 		}
138 
VerifyData()139 		void VerifyData ()
140 		{
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");
143 		}
144 
PostDeserialize()145 		protected override void PostDeserialize ()
146 		{
147 			base.PostDeserialize();
148 
149 			VerifyData ();
150 		}
151 
PreSerialize(XmlWriter writer)152 		protected override void PreSerialize (XmlWriter writer)
153 		{
154 			base.PreSerialize (writer);
155 
156 			VerifyData ();
157 		}
158 
Reset(ConfigurationElement parentElement)159 		protected internal override void Reset (ConfigurationElement parentElement)
160 		{
161 			AuthorizationRule r = (AuthorizationRule)parentElement;
162 			Action = r.Action;
163 
164 			base.Reset (parentElement);
165 		}
166 
ResetModified()167 		protected internal override void ResetModified ()
168 		{
169 			base.ResetModified ();
170 		}
171 
SerializeElement(XmlWriter writer, bool serializeCollectionKey)172 		protected internal override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
173 		{
174 			if (saveMode != ConfigurationSaveMode.Full && !IsModified ())
175 				return true;
176 
177 			PreSerialize (writer);
178 
179 			writer.WriteStartElement (action == AuthorizationRuleAction.Allow ? "allow" : "deny");
180 			if (Roles.Count > 0)
181 				writer.WriteAttributeString ("roles", Roles.ToString());
182 			if (Users.Count > 0)
183 				writer.WriteAttributeString ("users", Users.ToString());
184 			if (Verbs.Count > 0)
185 				writer.WriteAttributeString ("verbs", Verbs.ToString());
186 
187 			writer.WriteEndElement ();
188 
189 			return true;
190 		}
191 
SetReadOnly()192 		protected internal override void SetReadOnly ()
193 		{
194 			base.SetReadOnly();
195 		}
196 
Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)197 		protected internal override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
198 		{
199 			base.Unmerge (sourceElement, parentElement, saveMode);
200 			this.saveMode = saveMode;
201 
202 			AuthorizationRule source = sourceElement as AuthorizationRule;
203 			if (source != null)
204 				this.action = source.Action;
205 		}
206 
207 		public AuthorizationRuleAction Action {
208 			get { return action; }
209 			set { action = value; }
210 		}
211 
212 		[TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
213 		[ConfigurationProperty ("roles")]
214 		public StringCollection Roles {
215 			get { return (StringCollection) base [rolesProp];}
216 		}
217 
218 		[TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
219 		[ConfigurationProperty ("users")]
220 		public StringCollection Users {
221 			get { return (StringCollection) base [usersProp];}
222 		}
223 
224 		[TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
225 		[ConfigurationProperty ("verbs")]
226 		public StringCollection Verbs {
227 			get { return (StringCollection) base [verbsProp];}
228 		}
229 
230 		protected internal override ConfigurationPropertyCollection Properties {
231 			get { return properties; }
232 		}
233 
234 
CheckVerb(string verb)235 		internal bool CheckVerb (string verb)
236 		{
237 			foreach (string v in Verbs) {
238 				if (String.Compare (v, verb, true, Helpers.InvariantCulture) == 0)
239 					return true;
240 			}
241 			return false;
242 		}
243 
CheckUser(string user)244 		internal bool CheckUser (string user)
245 		{
246 			foreach (string u in Users) {
247 				if (String.Compare (u, user, true, Helpers.InvariantCulture) == 0 ||
248 				    u == "*" ||
249 				    (u == "?" && user == ""))
250 					return true;
251 			}
252 			return false;
253 		}
254 
CheckRole(IPrincipal user)255 		internal bool CheckRole (IPrincipal user)
256 		{
257 			foreach (string r in Roles) {
258 				if (user.IsInRole (r))
259 					return true;
260 			}
261 			return false;
262 		}
263 
264 	}
265 
266 }
267 
268 
269