2 // System.Web.Configuration.AuthorizationConfig
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
7 // (C) 2003 Ximian, Inc (http://www.ximian.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;
33 using System.Security.Principal;
36 namespace System.Web.Configuration
38 class AuthorizationConfig
40 AuthorizationConfig parent;
43 internal AuthorizationConfig (object parent)
45 this.parent = parent as AuthorizationConfig;
48 static string [] SplitAndTrim (string s)
50 if (s == null || s == "")
53 string [] all = s.Split (',');
54 for (int i = 0; i < all.Length; i++)
55 all [i] = all [i].Trim ();
60 static bool CheckWildcards (string [] values)
65 foreach (string s in values) {
66 if (s == null || s.Length == 1)
69 if (s.IndexOf ('?') != -1 || s.IndexOf ('*') != -1)
76 bool Add (bool allow, string users, string roles, string verbs)
78 string [] allUsers = SplitAndTrim (users);
79 string [] allRoles = SplitAndTrim (roles);
80 string [] allVerbs = SplitAndTrim (verbs);
81 if (!CheckWildcards (allUsers) || !CheckWildcards (allRoles))
85 list = new ArrayList ();
87 list.Add (new UserData (allow, allUsers, allRoles, allVerbs));
91 internal bool Allow (string users, string roles, string verbs)
93 return Add (true, users, roles, verbs);
96 internal bool Deny (string users, string roles, string verbs)
98 return Add (false, users, roles, verbs);
101 internal bool IsValidUser (IPrincipal user, string verb)
108 return parent.IsValidUser (user, verb);
113 foreach (UserData data in list) {
114 if (data.Verbs != null && !data.CheckVerb (verb))
117 if ((data.Users !=null && data.CheckUser(user.Identity.Name)) ||
118 (data.Roles != null && data.CheckRole(user)))
123 return parent.IsValidUser (user, verb);
131 public string [] Users;
132 public string [] Roles;
133 public string [] Verbs;
135 public UserData (bool allow, string [] users, string [] roles, string [] verbs)
143 public bool CheckUser (string user)
145 foreach (string u in Users) {
146 if (String.Compare (u, user, true) == 0 ||
148 (u == "?" && user == ""))
155 public bool CheckRole (IPrincipal user)
157 foreach (string r in Roles) {
158 if (user.IsInRole (r))
165 public bool CheckVerb (string verb)
167 foreach (string u in Verbs) {
168 if (String.Compare (u, verb, true) == 0)