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