(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / corlib / System.Security.Permissions / SecurityPermission.cs
blob9f970097c6991c463196993b286c810318d5746b
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)
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.
32 using System.Globalization;
34 namespace System.Security.Permissions {
36 [Serializable]
37 public sealed class SecurityPermission :
38 CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
40 private const int version = 1;
42 private SecurityPermissionFlag flags;
44 // constructors
46 public SecurityPermission (PermissionState state)
48 if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
49 flags = SecurityPermissionFlag.AllFlags;
50 else
51 flags = SecurityPermissionFlag.NoFlags;
54 public SecurityPermission (SecurityPermissionFlag flags)
56 // reuse validation by the Flags property
57 Flags = flags;
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");
67 flags = value;
71 // IUnrestrictedPermission
72 public bool IsUnrestricted ()
74 return (flags == SecurityPermissionFlag.AllFlags);
77 public override IPermission Copy ()
79 return new SecurityPermission (flags);
82 public override IPermission Intersect (IPermission target)
84 SecurityPermission sp = Cast (target);
85 if (sp == null)
86 return null;
87 if (IsEmpty () || sp.IsEmpty ())
88 return null;
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 ();
97 SecurityPermissionFlag f = flags & sp.flags;
98 if (f == SecurityPermissionFlag.NoFlags)
99 return null;
100 else
101 return new SecurityPermission (f);
104 public override IPermission Union (IPermission target)
106 SecurityPermission sp = Cast (target);
107 if (sp == null)
108 return this.Copy ();
110 if (this.IsUnrestricted () || sp.IsUnrestricted ())
111 return new SecurityPermission (PermissionState.Unrestricted);
113 return new SecurityPermission (flags | sp.flags);
116 public override bool IsSubsetOf (IPermission target)
118 SecurityPermission sp = Cast (target);
119 if (sp == null)
120 return IsEmpty ();
122 if (sp.IsUnrestricted ())
123 return true;
124 if (this.IsUnrestricted ())
125 return false;
127 return ((flags & ~sp.flags) == 0);
130 public override void FromXml (SecurityElement e)
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)
137 if (IsUnrestricted (e)) {
138 flags = SecurityPermissionFlag.AllFlags;
140 else {
141 string f = e.Attribute ("Flags");
142 if (f == null) {
143 flags = SecurityPermissionFlag.NoFlags;
145 else {
146 flags = (SecurityPermissionFlag) Enum.Parse (
147 typeof (SecurityPermissionFlag), f);
152 public override SecurityElement ToXml ()
154 SecurityElement e = Element (version);
155 if (IsUnrestricted ())
156 e.AddAttribute ("Unrestricted", "true");
157 else
158 e.AddAttribute ("Flags", flags.ToString ());
159 return e;
162 // IBuiltInPermission
163 int IBuiltInPermission.GetTokenIndex ()
165 return (int) BuiltInToken.Security;
168 // helpers
170 private bool IsEmpty ()
172 return (flags == SecurityPermissionFlag.NoFlags);
175 private SecurityPermission Cast (IPermission target)
177 if (target == null)
178 return null;
180 SecurityPermission sp = (target as SecurityPermission);
181 if (sp == null) {
182 ThrowInvalidPermission (target, typeof (SecurityPermission));
185 return sp;