2007-11-30 Zoltan Varga <vargaz@gmail.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-2005 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 using System.Runtime.InteropServices;
33
34 namespace System.Security.Permissions {
35
36 #if NET_2_0
37         [ComVisible (true)]
38 #endif
39         [Serializable]
40         public sealed class ZoneIdentityPermission : CodeAccessPermission, IBuiltInPermission {
41
42                 private const int version = 1;
43
44                 private SecurityZone zone;
45
46                 public ZoneIdentityPermission (PermissionState state)
47                 {
48                         // false == do not allow Unrestricted for Identity Permissions
49                         CheckPermissionState (state, false);
50                         // default values
51                         zone = SecurityZone.NoZone;
52                 }
53
54                 public ZoneIdentityPermission (SecurityZone zone)
55                 {
56                         // also needs the validations
57                         SecurityZone = zone;
58                 }
59
60                 public override IPermission Copy ()
61                 {
62                         return new ZoneIdentityPermission (zone);
63                 }
64
65                 public override bool IsSubsetOf (IPermission target)
66                 {
67                         ZoneIdentityPermission zip = Cast (target);
68                         if (zip == null)
69                                 return (zone == SecurityZone.NoZone);
70
71                         return ((zone == SecurityZone.NoZone) || (zone == zip.zone));
72                 }
73
74                 public override IPermission Union (IPermission target)
75                 {
76                         ZoneIdentityPermission zip = Cast (target);
77                         if (zip == null)
78                                 return (zone == SecurityZone.NoZone) ? null : Copy ();
79
80                         if (zone == zip.zone || zip.zone == SecurityZone.NoZone)
81                                 return Copy ();
82
83                         if (zone == SecurityZone.NoZone)
84                                 return zip.Copy ();
85 #if NET_2_0
86                         throw new ArgumentException (Locale.GetText (
87                                 "Union impossible"));
88 #else
89                         return null;
90 #endif
91                 }
92
93                 public override IPermission Intersect (IPermission target)
94                 {
95                         ZoneIdentityPermission zip = Cast (target);
96                         if (zip == null || zone == SecurityZone.NoZone)
97                                 return null;
98
99                         if (zone == zip.zone)
100                                 return Copy ();
101
102                         return null;
103                 }
104
105                 public override void FromXml (SecurityElement esd)
106                 {
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)
111
112                         string zoneName = esd.Attribute ("Zone");
113                         if (zoneName == null)
114                                 zone = SecurityZone.NoZone;
115                         else
116                                 zone = (SecurityZone) Enum.Parse (typeof (SecurityZone), zoneName);
117                 }
118
119                 public override SecurityElement ToXml ()
120                 {
121                         SecurityElement se = Element (version);
122                         if (zone != SecurityZone.NoZone)
123                                 se.AddAttribute ("Zone", zone.ToString ());
124                         return se;
125                 }
126
127                 public SecurityZone SecurityZone {
128                         get { return zone; }
129                         set {
130                                 if (!Enum.IsDefined (typeof (SecurityZone), value)) {
131                                         string msg = String.Format (Locale.GetText ("Invalid enum {0}"), value);
132                                         throw new ArgumentException (msg, "SecurityZone");
133                                 }
134                                 zone = value;
135                         }
136                 }
137
138                 // IBuiltInPermission
139                 int IBuiltInPermission.GetTokenIndex ()
140                 {
141                         return (int) BuiltInToken.ZoneIdentity;
142                 }
143
144                 // helpers
145
146                 private ZoneIdentityPermission Cast (IPermission target)
147                 {
148                         if (target == null)
149                                 return null;
150
151                         ZoneIdentityPermission zip = (target as ZoneIdentityPermission);
152                         if (zip == null) {
153                                 ThrowInvalidPermission (target, typeof (ZoneIdentityPermission));
154                         }
155
156                         return zip;
157                 }
158         }
159 }
160