2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[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 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
34 namespace System.Security.Permissions {
35         
36         [Serializable]
37         public sealed class SecurityPermission :
38                 CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
39
40                 private const int version = 1;
41
42                 private SecurityPermissionFlag flags;
43
44                 // constructors
45
46                 public SecurityPermission (PermissionState state)
47                 {
48                         if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
49                                 flags = SecurityPermissionFlag.AllFlags;
50                         else
51                                 flags = SecurityPermissionFlag.NoFlags;
52                 }
53
54                 public SecurityPermission (SecurityPermissionFlag flags) 
55                 {
56                         // reuse validation by the Flags property
57                         Flags = flags;
58                 }
59
60                 public SecurityPermissionFlag Flags {
61                         get { return flags; }
62                         set {
63                                 if ((value & SecurityPermissionFlag.AllFlags) != value) {
64                                         string msg = String.Format (Locale.GetText ("Invalid flags {0}"), value);
65                                         throw new ArgumentException (msg, "SecurityPermissionFlag");
66                                 }
67                                 flags = value;
68                         }
69                 }
70
71                 // IUnrestrictedPermission
72                 public bool IsUnrestricted () 
73                 {
74                         return (flags == SecurityPermissionFlag.AllFlags);
75                 }
76
77                 public override IPermission Copy () 
78                 {
79                         return new SecurityPermission (flags);
80                 }
81
82                 public override IPermission Intersect (IPermission target) 
83                 {
84                         SecurityPermission sp = Cast (target);
85                         if (sp == null)
86                                 return null;
87                         if (IsEmpty () || sp.IsEmpty ())
88                                 return null;
89
90                         if (this.IsUnrestricted () && sp.IsUnrestricted ())
91                                 return new SecurityPermission (PermissionState.Unrestricted);
92                         if (this.IsUnrestricted ())
93                                 return sp.Copy ();
94                         if (sp.IsUnrestricted ())
95                                 return this.Copy ();
96
97                         SecurityPermissionFlag f = flags & sp.flags;
98                         if (f == SecurityPermissionFlag.NoFlags)
99                                 return null;
100                         else
101                                 return new SecurityPermission (f);
102                 }
103
104                 public override IPermission Union (IPermission target) 
105                 {
106                         SecurityPermission sp = Cast (target);
107                         if (sp == null)
108                                 return this.Copy ();
109
110                         if (this.IsUnrestricted () || sp.IsUnrestricted ())
111                                 return new SecurityPermission (PermissionState.Unrestricted);
112                         
113                         return new SecurityPermission (flags | sp.flags);
114                 }
115
116                 public override bool IsSubsetOf (IPermission target) 
117                 {
118                         SecurityPermission sp = Cast (target);
119                         if (sp == null) 
120                                 return IsEmpty ();
121
122                         if (sp.IsUnrestricted ())
123                                 return true;
124                         if (this.IsUnrestricted ())
125                                 return false;
126
127                         return ((flags & ~sp.flags) == 0);
128                 }
129
130                 public override void FromXml (SecurityElement e) 
131                 {
132                         // General validation in CodeAccessPermission
133                         CheckSecurityElement (e, "e", version, version);
134                         // Note: we do not (yet) care about the return value 
135                         // as we only accept version 1 (min/max values)
136
137                         if (IsUnrestricted (e)) {
138                                 flags = SecurityPermissionFlag.AllFlags;
139                         }
140                         else {
141                                 string f = e.Attribute ("Flags");
142                                 if (f == null) {
143                                         flags = SecurityPermissionFlag.NoFlags;
144                                 }
145                                 else {
146                                         flags = (SecurityPermissionFlag) Enum.Parse (
147                                                 typeof (SecurityPermissionFlag), f);
148                                 }
149                         }
150                 }
151
152                 public override SecurityElement ToXml () 
153                 {
154                         SecurityElement e = Element (version);
155                         if (IsUnrestricted ())
156                                 e.AddAttribute ("Unrestricted", "true");
157                         else
158                                 e.AddAttribute ("Flags", flags.ToString ());
159                         return e;
160                 }
161
162                 // IBuiltInPermission
163                 int IBuiltInPermission.GetTokenIndex ()
164                 {
165                         return (int) BuiltInToken.Security;
166                 }
167
168                 // helpers
169
170                 private bool IsEmpty ()
171                 {
172                         return (flags == SecurityPermissionFlag.NoFlags);
173                 }
174
175                 private SecurityPermission Cast (IPermission target)
176                 {
177                         if (target == null)
178                                 return null;
179
180                         SecurityPermission sp = (target as SecurityPermission);
181                         if (sp == null) {
182                                 ThrowInvalidPermission (target, typeof (SecurityPermission));
183                         }
184
185                         return sp;
186                 }
187         }
188 }