2004-09-29 Ben Maurer <bmaurer@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.Configuration / AuthorizationConfigHandler.cs
1 //
2 // System.Web.Configuration.AuthorizationConfigHandler
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //
7 // (C) 2003 Ximian, Inc (http://www.ximian.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;
33 using System.Configuration;
34 using System.Xml;
35
36 namespace System.Web.Configuration
37 {
38         class AuthorizationConfigHandler : IConfigurationSectionHandler
39         {
40                 public object Create (object parent, object context, XmlNode section)
41                 {
42                         AuthorizationConfig config = new AuthorizationConfig (parent);
43
44                         if (section.Attributes != null && section.Attributes.Count != 0)
45                                 ThrowException ("Unrecognized attribute", section);
46
47                         XmlNodeList authNodes = section.ChildNodes;
48                         foreach (XmlNode child in authNodes) {
49                                 XmlNodeType ntype = child.NodeType;
50                                 if (ntype != XmlNodeType.Element)
51                                         continue;
52                                 
53                                 string childName = child.Name;
54                                 bool allow = (childName == "allow");
55                                 bool deny = (childName == "deny");
56                                 if (!allow && !deny)
57                                         ThrowException ("Element name must be 'allow' or 'deny'.", child);
58
59                                 string users = AttValue ("users", child);
60                                 string roles = AttValue ("roles", child);
61                                 if (users == null && roles == null)
62                                         ThrowException ("At least 'users' or 'roles' must be present.", child);
63
64                                 string verbs = AttValue ("verbs", child);
65                                 if (child.Attributes != null && child.Attributes.Count != 0)
66                                         ThrowException ("Unrecognized attribute.", child);
67
68                                 bool added;
69                                 if (allow)
70                                         added = config.Allow (users, roles, verbs);
71                                 else
72                                         added = config.Deny (users, roles, verbs);
73
74                                 if (!added)
75                                         ThrowException ("User and role names cannot contain '?' or '*'.", child);
76                         }
77
78                         return config;
79                 }
80
81                 // A few methods to save some typing
82                 static string AttValue (string name, XmlNode node, bool optional)
83                 {
84                         return HandlersUtil.ExtractAttributeValue (name, node, optional);
85                 }
86
87                 static string AttValue (string name, XmlNode node)
88                 {
89                         return HandlersUtil.ExtractAttributeValue (name, node, true);
90                 }
91
92                 static void ThrowException (string message, XmlNode node)
93                 {
94                         HandlersUtil.ThrowException (message, node);
95                 }
96                 //
97
98         }
99 }
100