2004-08-03 Sebastien Pouliot <sebastien@ximian.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;
32 using System.Globalization;
33 using System.Security;
34
35 namespace System.Security.Permissions {
36
37         [Serializable]
38         public sealed class ZoneIdentityPermission : CodeAccessPermission, IBuiltInPermission {
39
40                 private SecurityZone zone;
41
42                 public ZoneIdentityPermission (PermissionState state)
43                 {
44                         switch (state) {
45                                 case PermissionState.None:
46                                         zone = SecurityZone.NoZone;
47                                         break;
48                                 case PermissionState.Unrestricted:
49                                         throw new ArgumentException (Locale.GetText (
50                                                 "unrestricted not allowed"));
51                                 default:
52                                         throw new ArgumentException (Locale.GetText (
53                                                 "invalid state"));
54                         }
55                 }
56
57                 public ZoneIdentityPermission (SecurityZone zone)
58                 {
59                         // also needs the validations
60                         SecurityZone = zone;
61                 }
62
63                 public override IPermission Copy ()
64                 {
65                         return new ZoneIdentityPermission (zone);
66                 }
67
68                 public override bool IsSubsetOf (IPermission target)
69                 {
70                         if (target == null)
71                                 return zone == SecurityZone.NoZone;
72
73                         ZoneIdentityPermission zip = (target as ZoneIdentityPermission);
74                         if (zip == null) {
75                                 throw new ArgumentException (Locale.GetText (
76                                         "Invalid permission"));
77                         }
78
79                         return (zone == zip.zone);
80                 }
81
82                 public override IPermission Union (IPermission target)
83                 {
84                         if (target == null)
85                                 return (zone == SecurityZone.NoZone) ? null : Copy ();
86
87                         ZoneIdentityPermission zip = (target as ZoneIdentityPermission);
88                         if (zip == null) {
89                                 throw new ArgumentException (Locale.GetText (
90                                         "Invalid permission"));
91                         }
92
93                         if (zone == zip.zone || zip.zone == SecurityZone.NoZone)
94                                 return Copy ();
95
96                         if (zone == SecurityZone.NoZone)
97                                 return zip.Copy ();
98 #if NET_2_0
99                         throw new ArgumentException (Locale.GetText (
100                                 "Union impossible"));
101 #else
102                         return null;
103 #endif
104                 }
105
106                 public override IPermission Intersect (IPermission target)
107                 {
108                         if (target == null || zone == SecurityZone.NoZone)
109                                 return null;
110
111                         ZoneIdentityPermission zip = (target as ZoneIdentityPermission);
112                         if (zip == null) {
113                                 throw new ArgumentException (Locale.GetText (
114                                         "Invalid permission"));
115                         }
116
117                         if (zone == zip.zone)
118                                 return Copy ();
119
120                         return null;
121                 }
122
123                 public override void FromXml (SecurityElement esd)
124                 {
125                         if (esd == null)
126                                 throw new ArgumentException ("esd");
127
128                         if (esd.Attribute ("version") != "1") {
129                                 throw new ArgumentException (Locale.GetText (
130                                         "version attributte is wrong"));
131                         }
132                                 
133                         string zoneName = esd.Attribute ("Zone");
134                         zone = (SecurityZone) Enum.Parse (typeof (SecurityZone), zoneName);
135                 }
136
137                 public override SecurityElement ToXml ()
138                 {
139                         SecurityElement se = new SecurityElement ("IPermission");
140                         Type t = GetType ();
141                         se.AddAttribute ("class", t.FullName + ", " + t.Module.Assembly.FullName);
142                         se.AddAttribute ("version", "1");
143                         se.AddAttribute ("Zone", zone.ToString ());
144
145                         return se;
146                 }
147
148                 public SecurityZone SecurityZone {
149                         get { return zone; }
150                         set {
151                                 if (!Enum.IsDefined (typeof (SecurityZone), value)) {
152                                         throw new ArgumentException (Locale.GetText (
153                                                 "invalid zone"));
154                                 }
155                                 zone = value;
156                         }
157                 }
158
159                 // IBuiltInPermission
160                 int IBuiltInPermission.GetTokenIndex ()
161                 {
162                         return 13;
163                 }
164         }
165 }
166