Avoid a .cctor on the initialization path.
[mono.git] / mcs / class / corlib / System.Security.Permissions / SecurityPermission.cs
1 //
2 // System.Security.Permissions.SecurityPermission.cs
3 //
4 // Authors:
5 //      Dan Lewis (dihlewis@yahoo.co.uk)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2002
9 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.Globalization;
33 using System.Runtime.InteropServices;
34
35 namespace System.Security.Permissions {
36
37         [ComVisible (true)]
38         [Serializable]
39         public sealed class SecurityPermission :
40                 CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
41
42                 private const int version = 1;
43
44                 private SecurityPermissionFlag flags;
45
46                 // constructors
47
48                 public SecurityPermission (PermissionState state)
49                 {
50                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
51                                 flags = SecurityPermissionFlag.AllFlags;
52                         else
53                                 flags = SecurityPermissionFlag.NoFlags;
54                 }
55
56                 public SecurityPermission (SecurityPermissionFlag flag) 
57                 {
58                         // reuse validation by the Flags property
59                         Flags = flag;
60                 }
61
62                 public SecurityPermissionFlag Flags {
63                         get { return flags; }
64                         set {
65                                 if ((value & SecurityPermissionFlag.AllFlags) != value) {
66                                         string msg = String.Format (Locale.GetText ("Invalid flags {0}"), value);
67                                         throw new ArgumentException (msg, "SecurityPermissionFlag");
68                                 }
69                                 flags = value;
70                         }
71                 }
72
73                 // IUnrestrictedPermission
74                 public bool IsUnrestricted () 
75                 {
76                         return (flags == SecurityPermissionFlag.AllFlags);
77                 }
78
79                 public override IPermission Copy () 
80                 {
81                         return new SecurityPermission (flags);
82                 }
83
84                 public override IPermission Intersect (IPermission target) 
85                 {
86                         SecurityPermission sp = Cast (target);
87                         if (sp == null)
88                                 return null;
89                         if (IsEmpty () || sp.IsEmpty ())
90                                 return null;
91
92                         if (this.IsUnrestricted () && sp.IsUnrestricted ())
93                                 return new SecurityPermission (PermissionState.Unrestricted);
94                         if (this.IsUnrestricted ())
95                                 return sp.Copy ();
96                         if (sp.IsUnrestricted ())
97                                 return this.Copy ();
98
99                         SecurityPermissionFlag f = flags & sp.flags;
100                         if (f == SecurityPermissionFlag.NoFlags)
101                                 return null;
102                         else
103                                 return new SecurityPermission (f);
104                 }
105
106                 public override IPermission Union (IPermission target) 
107                 {
108                         SecurityPermission sp = Cast (target);
109                         if (sp == null)
110                                 return this.Copy ();
111
112                         if (this.IsUnrestricted () || sp.IsUnrestricted ())
113                                 return new SecurityPermission (PermissionState.Unrestricted);
114                         
115                         return new SecurityPermission (flags | sp.flags);
116                 }
117
118                 public override bool IsSubsetOf (IPermission target) 
119                 {
120                         SecurityPermission sp = Cast (target);
121                         if (sp == null) 
122                                 return IsEmpty ();
123
124                         if (sp.IsUnrestricted ())
125                                 return true;
126                         if (this.IsUnrestricted ())
127                                 return false;
128
129                         return ((flags & ~sp.flags) == 0);
130                 }
131
132                 public override void FromXml (SecurityElement esd) 
133                 {
134                         // General validation in CodeAccessPermission
135                         CheckSecurityElement (esd, "esd", version, version);
136                         // Note: we do not (yet) care about the return value 
137                         // as we only accept version 1 (min/max values)
138
139                         if (IsUnrestricted (esd)) {
140                                 flags = SecurityPermissionFlag.AllFlags;
141                         }
142                         else {
143                                 string f = esd.Attribute ("Flags");
144                                 if (f == null) {
145                                         flags = SecurityPermissionFlag.NoFlags;
146                                 }
147                                 else {
148                                         flags = (SecurityPermissionFlag) Enum.Parse (
149                                                 typeof (SecurityPermissionFlag), f);
150                                 }
151                         }
152                 }
153
154                 public override SecurityElement ToXml () 
155                 {
156                         SecurityElement e = Element (version);
157                         if (IsUnrestricted ())
158                                 e.AddAttribute ("Unrestricted", "true");
159                         else
160                                 e.AddAttribute ("Flags", flags.ToString ());
161                         return e;
162                 }
163
164                 // IBuiltInPermission
165                 int IBuiltInPermission.GetTokenIndex ()
166                 {
167                         return (int) BuiltInToken.Security;
168                 }
169
170                 // helpers
171
172                 private bool IsEmpty ()
173                 {
174                         return (flags == SecurityPermissionFlag.NoFlags);
175                 }
176
177                 private SecurityPermission Cast (IPermission target)
178                 {
179                         if (target == null)
180                                 return null;
181
182                         SecurityPermission sp = (target as SecurityPermission);
183                         if (sp == null) {
184                                 ThrowInvalidPermission (target, typeof (SecurityPermission));
185                         }
186
187                         return sp;
188                 }
189         }
190 }