2 // System.Web.Configuration.AuthorizationRule
5 // Chris Toshok (toshok@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System
.Collections
.Specialized
;
33 using System
.Security
.Principal
;
34 using System
.Configuration
;
35 using System
.ComponentModel
;
37 using System
.Globalization
;
38 using System
.Web
.Util
;
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
)
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
;
88 if (action
!= auth
.Action
)
91 if (Roles
.Count
!= auth
.Roles
.Count
92 || Users
.Count
!= auth
.Users
.Count
93 || Verbs
.Count
!= auth
.Verbs
.Count
)
98 for (i
= 0; i
< Roles
.Count
; i
++)
99 if (Roles
[i
] != auth
.Roles
[i
])
102 for (i
= 0; i
< Users
.Count
; i
++)
103 if (Users
[i
] != auth
.Users
[i
])
106 for (i
= 0; i
< Verbs
.Count
; i
++)
107 if (Verbs
[i
] != auth
.Verbs
[i
])
113 public override int GetHashCode ()
115 int hashCode
= (int)action
;
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();
130 [MonoTODO ("Not implemented")]
131 protected override bool IsModified ()
133 throw new NotImplementedException ();
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();
149 protected override void PreSerialize (XmlWriter writer
)
151 base.PreSerialize (writer
);
156 protected override void Reset (ConfigurationElement parentElement
)
158 AuthorizationRule r
= (AuthorizationRule
)parentElement
;
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");
175 writer
.WriteAttributeString ("roles", Roles
.ToString());
177 writer
.WriteAttributeString ("users", Users
.ToString());
179 writer
.WriteAttributeString ("verbs", Verbs
.ToString());
181 writer
.WriteEndElement ();
186 protected override void 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)
233 internal bool CheckUser (string user
)
235 foreach (string u
in Users
) {
236 if (String
.Compare (u
, user
, true, Helpers
.InvariantCulture
) == 0 ||
238 (u
== "?" && user
== ""))
244 internal bool CheckRole (IPrincipal user
)
246 foreach (string r
in Roles
) {
247 if (user
.IsInRole (r
))