System.Drawing: added email to icon and test file headers
[mono.git] / mcs / class / System / System.Web / AspNetHostingPermission.cs
1 //
2 // System.Web.AspNetHostingPermission.cs
3 //
4 // Authors:
5 //      Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 #if NET_1_1
31
32 using System.Security;
33 using System.Security.Permissions;
34
35 namespace System.Web {
36
37         [Serializable]
38         public sealed class AspNetHostingPermission : CodeAccessPermission, IUnrestrictedPermission {
39
40                 private const int version = 1;
41
42                 private AspNetHostingPermissionLevel _level;
43
44                 public AspNetHostingPermission (AspNetHostingPermissionLevel level)
45                 {
46                         // use the property to get the enum validation
47                         Level = level;
48                 }
49
50                 public AspNetHostingPermission (PermissionState state)
51                 {
52                         if (PermissionHelper.CheckPermissionState (state, true) == PermissionState.Unrestricted)
53                                 _level = AspNetHostingPermissionLevel.Unrestricted;
54                         else
55                                 _level = AspNetHostingPermissionLevel.None;
56                 }
57
58                 public AspNetHostingPermissionLevel Level {
59                         get { return _level; }
60                         set {
61                                 if ((value < AspNetHostingPermissionLevel.None) || (value > AspNetHostingPermissionLevel.Unrestricted)) {
62                                         string msg = Locale.GetText ("Invalid enum {0}.");
63                                         throw new ArgumentException (String.Format (msg, value), "Level");
64                                 }
65                                 _level = value;
66                         }
67                 }
68
69                 public bool IsUnrestricted ()
70                 {
71                         return (_level == AspNetHostingPermissionLevel.Unrestricted);
72                 }
73
74                 public override IPermission Copy ()
75                 {
76                         // note: no need to handle unrestricted here
77                         return new AspNetHostingPermission (_level);
78                 }
79
80                 public override void FromXml (SecurityElement securityElement)
81                 {
82                         PermissionHelper.CheckSecurityElement (securityElement, "securityElement", version, version);
83                         if (securityElement.Tag != "IPermission") {
84                                 string msg = Locale.GetText ("Invalid tag '{0}' for permission.");
85                                 throw new ArgumentException (String.Format (msg, securityElement.Tag), "securityElement");
86                         }
87                         if (securityElement.Attribute ("version") == null) {
88                                 string msg = Locale.GetText ("Missing version attribute.");
89                                 throw new ArgumentException (msg, "securityElement");
90                         }
91
92                         if (PermissionHelper.IsUnrestricted (securityElement)) {
93                                 // in case it's get fixed later...
94                                 _level = AspNetHostingPermissionLevel.Unrestricted;
95                         }
96                         else {
97                                 string level = securityElement.Attribute ("Level");
98                                 if (level != null) {
99                                         _level = (AspNetHostingPermissionLevel) Enum.Parse (
100                                                 typeof (AspNetHostingPermissionLevel), level);
101                                 }
102                                 else
103                                         _level = AspNetHostingPermissionLevel.None;
104                         }
105                 }
106
107                 public override SecurityElement ToXml ()
108                 {
109                         SecurityElement se = PermissionHelper.Element (typeof (AspNetHostingPermission), version);
110 #if NET_2_0
111                         if (IsUnrestricted ())
112                                 se.AddAttribute ("Unrestricted", "true"); // FDBK15156 fixed in 2.0 RC
113 #endif
114                         se.AddAttribute ("Level", _level.ToString ());
115                         return se;
116                 }
117
118                 public override IPermission Intersect (IPermission target)
119                 {
120                         AspNetHostingPermission anhp = Cast (target);
121                         if (anhp == null)
122                                 return null;
123
124                         return new AspNetHostingPermission ((_level <= anhp.Level) ? _level : anhp.Level);
125                 }
126
127                 public override bool IsSubsetOf (IPermission target)
128                 {
129                         AspNetHostingPermission anhp = Cast (target);
130                         if (anhp == null)
131                                 return IsEmpty ();
132                         return (_level <= anhp._level);
133                 }
134
135                 public override IPermission Union (IPermission target)
136                 {
137                         AspNetHostingPermission anhp = Cast (target);
138                         if (anhp == null)
139                                 return Copy ();
140                         return new AspNetHostingPermission ((_level > anhp.Level) ? _level : anhp.Level);
141                 }
142
143                 // Internal helpers methods
144
145                 private bool IsEmpty ()
146                 {
147                         return (_level == AspNetHostingPermissionLevel.None);
148                 }
149
150                 private AspNetHostingPermission Cast (IPermission target)
151                 {
152                         if (target == null)
153                                 return null;
154
155                         AspNetHostingPermission anhp = (target as AspNetHostingPermission);
156                         if (anhp == null) {
157                                 PermissionHelper.ThrowInvalidPermission (target, typeof (AspNetHostingPermission));
158                         }
159
160                         return anhp;
161                 }
162         }
163 }
164
165 #endif