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