2 // System.Security.Permissions.SecurityPermission.cs
5 // Dan Lewis (dihlewis@yahoo.co.uk)
6 // Sebastien Pouliot <sebastien@ximian.com>
9 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
10 // Copyright (C) 2004-2005 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:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
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;
33 using System.Runtime.InteropServices;
35 namespace System.Security.Permissions {
39 public sealed class SecurityPermission :
40 CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
42 private const int version = 1;
44 private SecurityPermissionFlag flags;
48 public SecurityPermission (PermissionState state)
50 if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
51 flags = SecurityPermissionFlag.AllFlags;
53 flags = SecurityPermissionFlag.NoFlags;
56 public SecurityPermission (SecurityPermissionFlag flag)
58 // reuse validation by the Flags property
62 public SecurityPermissionFlag Flags {
65 if ((value & SecurityPermissionFlag.AllFlags) != value) {
66 string msg = String.Format (Locale.GetText ("Invalid flags {0}"), value);
67 throw new ArgumentException (msg, "SecurityPermissionFlag");
73 // IUnrestrictedPermission
74 public bool IsUnrestricted ()
76 return (flags == SecurityPermissionFlag.AllFlags);
79 public override IPermission Copy ()
81 return new SecurityPermission (flags);
84 public override IPermission Intersect (IPermission target)
86 SecurityPermission sp = Cast (target);
89 if (IsEmpty () || sp.IsEmpty ())
92 if (this.IsUnrestricted () && sp.IsUnrestricted ())
93 return new SecurityPermission (PermissionState.Unrestricted);
94 if (this.IsUnrestricted ())
96 if (sp.IsUnrestricted ())
99 SecurityPermissionFlag f = flags & sp.flags;
100 if (f == SecurityPermissionFlag.NoFlags)
103 return new SecurityPermission (f);
106 public override IPermission Union (IPermission target)
108 SecurityPermission sp = Cast (target);
112 if (this.IsUnrestricted () || sp.IsUnrestricted ())
113 return new SecurityPermission (PermissionState.Unrestricted);
115 return new SecurityPermission (flags | sp.flags);
118 public override bool IsSubsetOf (IPermission target)
120 SecurityPermission sp = Cast (target);
124 if (sp.IsUnrestricted ())
126 if (this.IsUnrestricted ())
129 return ((flags & ~sp.flags) == 0);
132 public override void FromXml (SecurityElement esd)
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)
139 if (IsUnrestricted (esd)) {
140 flags = SecurityPermissionFlag.AllFlags;
143 string f = esd.Attribute ("Flags");
145 flags = SecurityPermissionFlag.NoFlags;
148 flags = (SecurityPermissionFlag) Enum.Parse (
149 typeof (SecurityPermissionFlag), f);
154 public override SecurityElement ToXml ()
156 SecurityElement e = Element (version);
157 if (IsUnrestricted ())
158 e.AddAttribute ("Unrestricted", "true");
160 e.AddAttribute ("Flags", flags.ToString ());
164 // IBuiltInPermission
165 int IBuiltInPermission.GetTokenIndex ()
167 return (int) BuiltInToken.Security;
172 private bool IsEmpty ()
174 return (flags == SecurityPermissionFlag.NoFlags);
177 private SecurityPermission Cast (IPermission target)
182 SecurityPermission sp = (target as SecurityPermission);
184 ThrowInvalidPermission (target, typeof (SecurityPermission));