Merge pull request #2377 from joelmartinez/docs-multiassembly-extension-fix
[mono.git] / mcs / class / referencesource / mscorlib / system / security / permissions / zoneidentitypermission.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // ZoneIdentityPermission.cs
7 // 
8 // <OWNER>[....]</OWNER>
9 // 
10
11 namespace System.Security.Permissions
12 {
13     using System;
14 #if FEATURE_CAS_POLICY
15     using SecurityElement = System.Security.SecurityElement;
16 #endif // FEATURE_CAS_POLICY
17     using System.Globalization;
18     using System.Runtime.Serialization;
19     using System.Collections;
20     using System.Collections.Generic;
21     using System.Diagnostics.Contracts;
22
23 [System.Runtime.InteropServices.ComVisible(true)]
24     [Serializable]
25     sealed public class ZoneIdentityPermission : CodeAccessPermission, IBuiltInPermission
26     {
27         //------------------------------------------------------
28         //
29         // PRIVATE STATE DATA
30         //
31         //------------------------------------------------------
32
33         // Zone            Enum       Flag
34         // -----           -----      -----
35         // NoZone          -1         0x00
36         // MyComputer       0         0x01  (1 << 0)
37         // Intranet         1         0x02  (1 << 1)
38         // Trusted          2         0x04  (1 << 2)
39         // Internet         3         0x08  (1 << 3)
40         // Untrusted        4         0x10  (1 << 4)
41
42         private const uint AllZones = 0x1f;
43         [OptionalField(VersionAdded = 2)]
44         private uint m_zones;
45
46 #if FEATURE_REMOTING
47         // This field will be populated only for non X-AD scenarios where we create a XML-ised string of the Permission
48         [OptionalField(VersionAdded = 2)]
49         private String m_serializedPermission; 
50
51         //  This field is legacy info from v1.x and is never used in v2.0 and beyond: purely for serialization purposes
52         private SecurityZone m_zone = SecurityZone.NoZone;
53
54         [OnDeserialized]
55         private void OnDeserialized(StreamingContext ctx)
56         {
57             if ((ctx.State & ~(StreamingContextStates.Clone|StreamingContextStates.CrossAppDomain)) != 0)
58             {
59                 // v2.0 and beyond XML case
60                 if (m_serializedPermission != null)
61                 {
62                     FromXml(SecurityElement.FromString(m_serializedPermission));
63                     m_serializedPermission = null;
64                 }
65                 else //v1.x case where we read the m_zone value
66                 {
67                     SecurityZone = m_zone;
68                     m_zone = SecurityZone.NoZone;
69                 }
70             }
71
72
73         }
74
75         [OnSerializing]
76         private void OnSerializing(StreamingContext ctx)
77         {
78
79             if ((ctx.State & ~(StreamingContextStates.Clone|StreamingContextStates.CrossAppDomain)) != 0)
80             {
81                 m_serializedPermission = ToXml().ToString(); //for the v2 and beyond case
82                 m_zone = SecurityZone;
83                 
84             }
85         }   
86         [OnSerialized]
87         private void OnSerialized(StreamingContext ctx)
88         {
89             if ((ctx.State & ~(StreamingContextStates.Clone|StreamingContextStates.CrossAppDomain)) != 0)
90             {
91                 m_serializedPermission = null;
92                 m_zone = SecurityZone.NoZone;
93             }
94         }
95 #endif // FEATURE_REMOTING
96
97         //------------------------------------------------------
98         //
99         // PUBLIC CONSTRUCTORS
100         //
101         //------------------------------------------------------
102
103         public ZoneIdentityPermission(PermissionState state)
104         {
105             if (state == PermissionState.Unrestricted)
106             {
107                 m_zones = AllZones;
108             }
109             else if (state == PermissionState.None)
110             {
111                 m_zones = 0;
112             }
113             else
114             {
115                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
116             }
117         }
118
119         public ZoneIdentityPermission( SecurityZone zone )
120         {
121             this.SecurityZone = zone;
122         }
123
124         internal ZoneIdentityPermission( uint zones )
125         {
126             m_zones = (zones & AllZones);
127         }
128
129         // Internal function to append all the Zone in this permission to the input ArrayList
130         internal void AppendZones(ArrayList zoneList)
131         {
132             int nEnum = 0;
133             uint nFlag;
134             for(nFlag = 1; nFlag < AllZones; nFlag <<= 1)
135             {
136                 if((m_zones & nFlag) != 0)
137                 {
138                     zoneList.Add((SecurityZone)nEnum);
139                 }
140                 nEnum++;
141             }
142         }
143
144         //------------------------------------------------------
145         //
146         // PUBLIC ACCESSOR METHODS
147         //
148         //------------------------------------------------------
149
150         public SecurityZone SecurityZone
151         {
152             set
153             {
154                 VerifyZone( value );
155                 if(value == SecurityZone.NoZone)
156                     m_zones = 0;
157                 else
158                     m_zones = (uint)1 << (int)value;
159             }
160
161             get
162             {
163                 SecurityZone z = SecurityZone.NoZone;
164                 int nEnum = 0;
165                 uint nFlag;
166                 for(nFlag = 1; nFlag < AllZones; nFlag <<= 1)
167                 {
168                     if((m_zones & nFlag) != 0)
169                     {
170                         if(z == SecurityZone.NoZone)
171                             z = (SecurityZone)nEnum;
172                         else
173                             return SecurityZone.NoZone;
174                     }
175                     nEnum++;
176                 }
177                 return z;
178             }
179         }
180
181         //------------------------------------------------------
182         //
183         // PRIVATE AND PROTECTED HELPERS FOR ACCESSORS AND CONSTRUCTORS
184         //
185         //------------------------------------------------------
186
187         private static void VerifyZone( SecurityZone zone )
188         {
189             if (zone < SecurityZone.NoZone || zone > SecurityZone.Untrusted)
190             {
191                 throw new ArgumentException( Environment.GetResourceString("Argument_IllegalZone") );
192             }
193             Contract.EndContractBlock();
194         }
195
196
197         //------------------------------------------------------
198         //
199         // CODEACCESSPERMISSION IMPLEMENTATION
200         //
201         //------------------------------------------------------
202
203         //------------------------------------------------------
204         //
205         // IPERMISSION IMPLEMENTATION
206         //
207         //------------------------------------------------------
208
209
210         public override IPermission Copy()
211         {
212             return new ZoneIdentityPermission(this.m_zones);
213         }
214
215         public override bool IsSubsetOf(IPermission target)
216         {
217             if (target == null)
218                 return this.m_zones == 0;
219
220             ZoneIdentityPermission that = target as ZoneIdentityPermission;
221             if (that == null)
222                 throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
223             return (this.m_zones & that.m_zones) == this.m_zones;
224         }
225
226         public override IPermission Intersect(IPermission target)
227         {
228             if (target == null)
229                 return null;
230
231             ZoneIdentityPermission that = target as ZoneIdentityPermission;
232             if (that == null)
233                 throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
234             uint newZones = this.m_zones & that.m_zones;
235             if(newZones == 0)
236                 return null;
237             return new ZoneIdentityPermission(newZones);
238         }
239
240         public override IPermission Union(IPermission target)
241         {
242             if (target == null)
243                 return this.m_zones != 0 ? this.Copy() : null;
244
245             ZoneIdentityPermission that = target as ZoneIdentityPermission;
246             if (that == null)
247                 throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
248             return new ZoneIdentityPermission(this.m_zones | that.m_zones);
249         }
250
251 #if FEATURE_CAS_POLICY
252         public override SecurityElement ToXml()
253         {
254             SecurityElement esd = CodeAccessPermission.CreatePermissionElement( this, "System.Security.Permissions.ZoneIdentityPermission" );
255             if (SecurityZone != SecurityZone.NoZone)
256             {
257                 esd.AddAttribute( "Zone", Enum.GetName( typeof( SecurityZone ), this.SecurityZone ) );
258             }
259             else
260             {
261                 int nEnum = 0;
262                 uint nFlag;
263                 for(nFlag = 1; nFlag < AllZones; nFlag <<= 1)
264                 {
265                     if((m_zones & nFlag) != 0)
266                     {
267                         SecurityElement child = new SecurityElement("Zone");
268                         child.AddAttribute( "Zone", Enum.GetName( typeof( SecurityZone ), (SecurityZone)nEnum ) );
269                         esd.AddChild(child);
270                     }
271                     nEnum++;
272                 }
273             }
274             return esd;
275         }
276
277         public override void FromXml(SecurityElement esd)
278         {
279             m_zones = 0;
280             CodeAccessPermission.ValidateElement( esd, this );
281             String eZone = esd.Attribute( "Zone" );
282             if (eZone != null)
283                 SecurityZone = (SecurityZone)Enum.Parse( typeof( SecurityZone ), eZone );
284             if(esd.Children != null)
285             {
286                 foreach(SecurityElement child in esd.Children)
287                 {
288                     eZone = child.Attribute( "Zone" );
289                     int enm = (int)Enum.Parse( typeof( SecurityZone ), eZone );
290                     if(enm == (int)SecurityZone.NoZone)
291                         continue;
292                     m_zones |= ((uint)1 << enm);
293                 }
294             }
295         }
296 #endif // FEATURE_CAS_POLICY
297
298         /// <internalonly/>
299         int IBuiltInPermission.GetTokenIndex()
300         {
301             return ZoneIdentityPermission.GetTokenIndex();
302         }
303
304         internal static int GetTokenIndex()
305         {
306             return BuiltInPermissionIndex.ZoneIdentityPermissionIndex;
307         }
308
309     }
310 }