In corlib/System.Runtime.InteropServices:
[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 ("Not implemented")]
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                 protected override void ResetModified ()
163                 {
164                         base.ResetModified ();
165                 }
166
167                 protected override bool SerializeElement (XmlWriter writer, bool serializeCollectionKey)
168                 {
169                         PreSerialize (writer);
170
171                         writer.WriteStartElement (action == AuthorizationRuleAction.Allow ? "allow" : "deny");
172                         if (Roles.Count > 0)
173                                 writer.WriteAttributeString ("roles", Roles.ToString());
174                         if (Users.Count > 0)
175                                 writer.WriteAttributeString ("users", Users.ToString());
176                         if (Verbs.Count > 0)
177                                 writer.WriteAttributeString ("verbs", Verbs.ToString());
178
179                         writer.WriteEndElement ();
180
181                         return true;
182                 }
183
184                 protected override void SetReadOnly ()
185                 {
186                         base.SetReadOnly();
187                 }
188
189                 protected override void Unmerge (ConfigurationElement sourceElement, ConfigurationElement parentElement, ConfigurationSaveMode saveMode)
190                 {
191                         base.Unmerge (sourceElement, parentElement, saveMode);
192                 }
193
194                 public AuthorizationRuleAction Action {
195                         get { return action; }
196                         set { action = value; }
197                 }
198
199                 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
200                 [ConfigurationProperty ("roles")]
201                 public StringCollection Roles {
202                         get { return (StringCollection) base [rolesProp];}
203                 }
204
205                 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
206                 [ConfigurationProperty ("users")]
207                 public StringCollection Users {
208                         get { return (StringCollection) base [usersProp];}
209                 }
210
211                 [TypeConverter (typeof (CommaDelimitedStringCollectionConverter))]
212                 [ConfigurationProperty ("verbs")]
213                 public StringCollection Verbs {
214                         get { return (StringCollection) base [verbsProp];}
215                 }
216
217                 protected override ConfigurationPropertyCollection Properties {
218                         get { return properties; }
219                 }
220
221
222                 internal bool CheckVerb (string verb)
223                 {
224                         foreach (string v in Verbs) {
225                                 if (String.Compare (v, verb, true) == 0)
226                                         return true;
227                         }
228                         return false;
229                 }
230
231                 internal bool CheckUser (string user)
232                 {
233                         foreach (string u in Users) {
234                                 if (String.Compare (u, user, true) == 0 ||
235                                     u == "*" ||
236                                     (u == "?" && user == ""))
237                                         return true;
238                         }
239                         return false;
240                 }
241
242                 internal bool CheckRole (IPrincipal user)
243                 {
244                         foreach (string r in Roles) {
245                                 if (user.IsInRole (r))
246                                         return true;
247                         }
248                         return false;
249                 }
250
251         }
252
253 }
254
255 #endif
256