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 {
42 public sealed class SecurityPermission :
43 CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
45 private const int version = 1;
47 private SecurityPermissionFlag flags;
51 public SecurityPermission (PermissionState state)
53 if (CheckPermissionState (state, true) == PermissionState.Unrestricted)
54 flags = SecurityPermissionFlag.AllFlags;
56 flags = SecurityPermissionFlag.NoFlags;
59 public SecurityPermission (SecurityPermissionFlag flags)
61 // reuse validation by the Flags property
65 public SecurityPermissionFlag Flags {
68 if ((value & SecurityPermissionFlag.AllFlags) != value) {
69 string msg = String.Format (Locale.GetText ("Invalid flags {0}"), value);
70 throw new ArgumentException (msg, "SecurityPermissionFlag");
76 // IUnrestrictedPermission
77 public bool IsUnrestricted ()
79 return (flags == SecurityPermissionFlag.AllFlags);
82 public override IPermission Copy ()
84 return new SecurityPermission (flags);
87 public override IPermission Intersect (IPermission target)
89 SecurityPermission sp = Cast (target);
92 if (IsEmpty () || sp.IsEmpty ())
95 if (this.IsUnrestricted () && sp.IsUnrestricted ())
96 return new SecurityPermission (PermissionState.Unrestricted);
97 if (this.IsUnrestricted ())
99 if (sp.IsUnrestricted ())
102 SecurityPermissionFlag f = flags & sp.flags;
103 if (f == SecurityPermissionFlag.NoFlags)
106 return new SecurityPermission (f);
109 public override IPermission Union (IPermission target)
111 SecurityPermission sp = Cast (target);
115 if (this.IsUnrestricted () || sp.IsUnrestricted ())
116 return new SecurityPermission (PermissionState.Unrestricted);
118 return new SecurityPermission (flags | sp.flags);
121 public override bool IsSubsetOf (IPermission target)
123 SecurityPermission sp = Cast (target);
127 if (sp.IsUnrestricted ())
129 if (this.IsUnrestricted ())
132 return ((flags & ~sp.flags) == 0);
135 public override void FromXml (SecurityElement e)
137 // General validation in CodeAccessPermission
138 CheckSecurityElement (e, "e", version, version);
139 // Note: we do not (yet) care about the return value
140 // as we only accept version 1 (min/max values)
142 if (IsUnrestricted (e)) {
143 flags = SecurityPermissionFlag.AllFlags;
146 string f = e.Attribute ("Flags");
148 flags = SecurityPermissionFlag.NoFlags;
151 flags = (SecurityPermissionFlag) Enum.Parse (
152 typeof (SecurityPermissionFlag), f);
157 public override SecurityElement ToXml ()
159 SecurityElement e = Element (version);
160 if (IsUnrestricted ())
161 e.AddAttribute ("Unrestricted", "true");
163 e.AddAttribute ("Flags", flags.ToString ());
167 // IBuiltInPermission
168 int IBuiltInPermission.GetTokenIndex ()
170 return (int) BuiltInToken.Security;
175 private bool IsEmpty ()
177 return (flags == SecurityPermissionFlag.NoFlags);
180 private SecurityPermission Cast (IPermission target)
185 SecurityPermission sp = (target as SecurityPermission);
187 ThrowInvalidPermission (target, typeof (SecurityPermission));