2005-06-05 Peter Bartok <pbartok@novell.com>
[mono.git] / mcs / class / corlib / System.Security.Permissions / ZoneIdentityPermission.cs
1 //
2 // System.Security.Permissions.ZoneIdentityPermission
3 //
4 // Authors:
5 //      Gonzalo Paniagua Javier (gonzalo@ximian.com)
6 //      Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // (C) 2002 Ximian, Inc (http://www.ximian.com)
9 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
10 //
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:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
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.
29 //
30
31 using System.Globalization;
32
33 namespace System.Security.Permissions {
34
35         [Serializable]
36         public sealed class ZoneIdentityPermission : CodeAccessPermission, IBuiltInPermission {
37
38                 private const int version = 1;
39
40                 private SecurityZone zone;
41
42                 public ZoneIdentityPermission (PermissionState state)
43                 {
44                         // false == do not allow Unrestricted for Identity Permissions
45                         CheckPermissionState (state, false);
46                         // default values
47                         zone = SecurityZone.NoZone;
48                 }
49
50                 public ZoneIdentityPermission (SecurityZone zone)
51                 {
52                         // also needs the validations
53                         SecurityZone = zone;
54                 }
55
56                 public override IPermission Copy ()
57                 {
58                         return new ZoneIdentityPermission (zone);
59                 }
60
61                 public override bool IsSubsetOf (IPermission target)
62                 {
63                         ZoneIdentityPermission zip = Cast (target);
64                         if (zip == null)
65                                 return (zone == SecurityZone.NoZone);
66
67                         return ((zone == SecurityZone.NoZone) || (zone == zip.zone));
68                 }
69
70                 public override IPermission Union (IPermission target)
71                 {
72                         ZoneIdentityPermission zip = Cast (target);
73                         if (zip == null)
74                                 return (zone == SecurityZone.NoZone) ? null : Copy ();
75
76                         if (zone == zip.zone || zip.zone == SecurityZone.NoZone)
77                                 return Copy ();
78
79                         if (zone == SecurityZone.NoZone)
80                                 return zip.Copy ();
81 #if NET_2_0
82                         throw new ArgumentException (Locale.GetText (
83                                 "Union impossible"));
84 #else
85                         return null;
86 #endif
87                 }
88
89                 public override IPermission Intersect (IPermission target)
90                 {
91                         ZoneIdentityPermission zip = Cast (target);
92                         if (zip == null || zone == SecurityZone.NoZone)
93                                 return null;
94
95                         if (zone == zip.zone)
96                                 return Copy ();
97
98                         return null;
99                 }
100
101                 public override void FromXml (SecurityElement esd)
102                 {
103                         // General validation in CodeAccessPermission
104                         CheckSecurityElement (esd, "esd", version, version);
105                         // Note: we do not (yet) care about the return value 
106                         // as we only accept version 1 (min/max values)
107
108                         string zoneName = esd.Attribute ("Zone");
109                         if (zoneName == null)
110                                 zone = SecurityZone.NoZone;
111                         else
112                                 zone = (SecurityZone) Enum.Parse (typeof (SecurityZone), zoneName);
113                 }
114
115                 public override SecurityElement ToXml ()
116                 {
117                         SecurityElement se = Element (version);
118                         if (zone != SecurityZone.NoZone)
119                                 se.AddAttribute ("Zone", zone.ToString ());
120                         return se;
121                 }
122
123                 public SecurityZone SecurityZone {
124                         get { return zone; }
125                         set {
126                                 if (!Enum.IsDefined (typeof (SecurityZone), value)) {
127                                         string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
128                                         throw new ArgumentException (msg, "SecurityZone");
129                                 }
130                                 zone = value;
131                         }
132                 }
133
134                 // IBuiltInPermission
135                 int IBuiltInPermission.GetTokenIndex ()
136                 {
137                         return (int) BuiltInToken.ZoneIdentity;
138                 }
139
140                 // helpers
141
142                 private ZoneIdentityPermission Cast (IPermission target)
143                 {
144                         if (target == null)
145                                 return null;
146
147                         ZoneIdentityPermission zip = (target as ZoneIdentityPermission);
148                         if (zip == null) {
149                                 ThrowInvalidPermission (target, typeof (ZoneIdentityPermission));
150                         }
151
152                         return zip;
153                 }
154         }
155 }
156