2 // System.Security.Permissions.ZoneIdentityPermission
5 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Globalization;
32 using System.Runtime.InteropServices;
34 namespace System.Security.Permissions {
40 public sealed class ZoneIdentityPermission : CodeAccessPermission, IBuiltInPermission {
42 private const int version = 1;
44 private SecurityZone zone;
46 public ZoneIdentityPermission (PermissionState state)
48 // false == do not allow Unrestricted for Identity Permissions
49 CheckPermissionState (state, false);
51 zone = SecurityZone.NoZone;
54 public ZoneIdentityPermission (SecurityZone zone)
56 // also needs the validations
60 public override IPermission Copy ()
62 return new ZoneIdentityPermission (zone);
65 public override bool IsSubsetOf (IPermission target)
67 ZoneIdentityPermission zip = Cast (target);
69 return (zone == SecurityZone.NoZone);
71 return ((zone == SecurityZone.NoZone) || (zone == zip.zone));
74 public override IPermission Union (IPermission target)
76 ZoneIdentityPermission zip = Cast (target);
78 return (zone == SecurityZone.NoZone) ? null : Copy ();
80 if (zone == zip.zone || zip.zone == SecurityZone.NoZone)
83 if (zone == SecurityZone.NoZone)
86 throw new ArgumentException (Locale.GetText (
93 public override IPermission Intersect (IPermission target)
95 ZoneIdentityPermission zip = Cast (target);
96 if (zip == null || zone == SecurityZone.NoZone)
105 public override void FromXml (SecurityElement esd)
107 // General validation in CodeAccessPermission
108 CheckSecurityElement (esd, "esd", version, version);
109 // Note: we do not (yet) care about the return value
110 // as we only accept version 1 (min/max values)
112 string zoneName = esd.Attribute ("Zone");
113 if (zoneName == null)
114 zone = SecurityZone.NoZone;
116 zone = (SecurityZone) Enum.Parse (typeof (SecurityZone), zoneName);
119 public override SecurityElement ToXml ()
121 SecurityElement se = Element (version);
122 if (zone != SecurityZone.NoZone)
123 se.AddAttribute ("Zone", zone.ToString ());
127 public SecurityZone SecurityZone {
130 if (!Enum.IsDefined (typeof (SecurityZone), value)) {
131 string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
132 throw new ArgumentException (msg, "SecurityZone");
138 // IBuiltInPermission
139 int IBuiltInPermission.GetTokenIndex ()
141 return (int) BuiltInToken.ZoneIdentity;
146 private ZoneIdentityPermission Cast (IPermission target)
151 ZoneIdentityPermission zip = (target as ZoneIdentityPermission);
153 ThrowInvalidPermission (target, typeof (ZoneIdentityPermission));