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 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
;
34 namespace System
.Security
.Permissions
{
37 public sealed class SecurityPermission
:
38 CodeAccessPermission
, IUnrestrictedPermission
, IBuiltInPermission
{
40 private const int version
= 1;
42 private SecurityPermissionFlag flags
;
46 public SecurityPermission (PermissionState state
)
48 if (CheckPermissionState (state
, true) == PermissionState
.Unrestricted
)
49 flags
= SecurityPermissionFlag
.AllFlags
;
51 flags
= SecurityPermissionFlag
.NoFlags
;
54 public SecurityPermission (SecurityPermissionFlag flags
)
56 // reuse validation by the Flags property
60 public SecurityPermissionFlag Flags
{
63 if ((value & SecurityPermissionFlag
.AllFlags
) != value) {
64 string msg
= String
.Format (Locale
.GetText ("Invalid flags {0}"), value);
65 throw new ArgumentException (msg
, "SecurityPermissionFlag");
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
);
87 if (IsEmpty () || sp
.IsEmpty ())
90 if (this.IsUnrestricted () && sp
.IsUnrestricted ())
91 return new SecurityPermission (PermissionState
.Unrestricted
);
92 if (this.IsUnrestricted ())
94 if (sp
.IsUnrestricted ())
97 SecurityPermissionFlag f
= flags
& sp
.flags
;
98 if (f
== SecurityPermissionFlag
.NoFlags
)
101 return new SecurityPermission (f
);
104 public override IPermission
Union (IPermission target
)
106 SecurityPermission sp
= Cast (target
);
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
);
122 if (sp
.IsUnrestricted ())
124 if (this.IsUnrestricted ())
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
;
141 string f
= e
.Attribute ("Flags");
143 flags
= SecurityPermissionFlag
.NoFlags
;
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");
158 e
.AddAttribute ("Flags", flags
.ToString ());
162 // IBuiltInPermission
163 int IBuiltInPermission
.GetTokenIndex ()
165 return (int) BuiltInToken
.Security
;
170 private bool IsEmpty ()
172 return (flags
== SecurityPermissionFlag
.NoFlags
);
175 private SecurityPermission
Cast (IPermission target
)
180 SecurityPermission sp
= (target
as SecurityPermission
);
182 ThrowInvalidPermission (target
, typeof (SecurityPermission
));