2 // System.Security.Permissions.SecurityPermission.cs
5 // Dan Lewis (dihlewis@yahoo.co.uk)
6 // Sebastien Pouliot (spouliot@motus.com)
9 // Portions (C) 2003 Motus Technologies Inc. (http://www.motus.com)
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
36 using System.Globalization;
38 namespace System.Security.Permissions {
41 public sealed class SecurityPermission :
42 CodeAccessPermission, IUnrestrictedPermission, IBuiltInPermission {
44 private SecurityPermissionFlag flags;
48 public SecurityPermission (PermissionState state)
50 if (state == PermissionState.Unrestricted)
51 flags = SecurityPermissionFlag.AllFlags;
53 flags = SecurityPermissionFlag.NoFlags;
56 public SecurityPermission (SecurityPermissionFlag flags)
61 public SecurityPermissionFlag Flags {
63 set { flags = value; }
66 public bool IsUnrestricted ()
68 return (flags == SecurityPermissionFlag.AllFlags);
71 public override IPermission Copy ()
73 return new SecurityPermission (flags);
76 internal SecurityPermission Cast (IPermission target)
78 SecurityPermission perm = (target as SecurityPermission);
80 throw new ArgumentException ("wrong type for target");
84 public override IPermission Intersect (IPermission target)
89 SecurityPermission perm = Cast (target);
90 if (this.IsUnrestricted () && perm.IsUnrestricted ())
91 return new SecurityPermission (PermissionState.Unrestricted);
92 if (this.IsUnrestricted ())
94 if (perm.IsUnrestricted ())
96 return new SecurityPermission (flags & perm.flags);
99 public override IPermission Union (IPermission target)
104 SecurityPermission perm = Cast (target);
105 if (this.IsUnrestricted () || perm.IsUnrestricted ())
106 return new SecurityPermission (PermissionState.Unrestricted);
108 return new SecurityPermission (flags | perm.flags);
111 public override bool IsSubsetOf (IPermission target)
114 return (flags == SecurityPermissionFlag.NoFlags);
116 SecurityPermission perm = Cast (target);
117 if (perm.IsUnrestricted ())
119 if (this.IsUnrestricted ())
122 return ((flags & ~perm.flags) == 0);
125 public override void FromXml (SecurityElement e)
128 throw new ArgumentNullException (
129 Locale.GetText ("The argument is null."));
131 if (e.Attribute ("class") != GetType ().AssemblyQualifiedName)
132 throw new ArgumentException (
133 Locale.GetText ("The argument is not valid"));
135 if (e.Attribute ("version") != "1")
136 throw new ArgumentException (
137 Locale.GetText ("The argument is not valid"));
139 flags = (SecurityPermissionFlag) Enum.Parse (
140 typeof (SecurityPermissionFlag), e.Attribute ("Flags"));
143 public override SecurityElement ToXml ()
145 SecurityElement e = new SecurityElement ("IPermission");
146 e.AddAttribute ("class", GetType ().AssemblyQualifiedName);
147 e.AddAttribute ("version", "1");
149 e.AddAttribute ("Flags", flags.ToString ());
154 // IBuiltInPermission
155 int IBuiltInPermission.GetTokenIndex ()