Merge pull request #2377 from joelmartinez/docs-multiassembly-extension-fix
[mono.git] / mcs / class / referencesource / mscorlib / system / security / permissions / siteidentitypermission.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 // SiteIdentityPermission.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 SiteString = System.Security.Util.SiteString;
18     using System.Text;
19     using System.Collections;
20     using System.Collections.Generic;
21     using System.Globalization;
22     using System.Runtime.Serialization;
23
24 [System.Runtime.InteropServices.ComVisible(true)]
25     [Serializable]
26     sealed public class SiteIdentityPermission : CodeAccessPermission, IBuiltInPermission
27     {
28         //------------------------------------------------------
29         //
30         // PRIVATE STATE DATA
31         //
32         //------------------------------------------------------
33         [OptionalField(VersionAdded = 2)]
34         private bool m_unrestricted;
35         [OptionalField(VersionAdded = 2)]        
36         private SiteString[] m_sites;
37
38 #if FEATURE_REMOTING
39         // This field will be populated only for non X-AD scenarios where we create a XML-ised string of the Permission
40         [OptionalField(VersionAdded = 2)]
41         private String m_serializedPermission; 
42
43         //  This field is legacy info from v1.x and is never used in v2.0 and beyond: purely for serialization purposes
44         private SiteString m_site;
45
46         [OnDeserialized]
47         private void OnDeserialized(StreamingContext ctx)
48         {
49             // v2.0 and beyond XML case
50             if (m_serializedPermission != null)
51             {
52                 FromXml(SecurityElement.FromString(m_serializedPermission));
53                 m_serializedPermission = null;
54             }
55             else if (m_site != null) //v1.x case where we read the m_site value
56             {
57                 m_unrestricted = false;
58                 m_sites = new SiteString[1];
59                 m_sites[0] = m_site;
60                 m_site = null;
61             }
62         }
63
64         [OnSerializing]
65         private void OnSerializing(StreamingContext ctx)
66         {
67
68             if ((ctx.State & ~(StreamingContextStates.Clone|StreamingContextStates.CrossAppDomain)) != 0)
69             {
70                 m_serializedPermission = ToXml().ToString(); //for the v2 and beyond case
71                 if (m_sites != null && m_sites.Length == 1) // for the v1.x case
72                     m_site = m_sites[0];
73                 
74             }
75         }   
76         [OnSerialized]
77         private void OnSerialized(StreamingContext ctx)
78         {
79             if ((ctx.State & ~(StreamingContextStates.Clone|StreamingContextStates.CrossAppDomain)) != 0)
80             {
81                 m_serializedPermission = null;
82                 m_site = null;
83             }
84         }
85 #endif // FEATURE_REMOTING
86
87         //------------------------------------------------------
88         //
89         // PUBLIC CONSTRUCTORS
90         //
91         //------------------------------------------------------
92         
93        
94         public SiteIdentityPermission(PermissionState state)
95         {
96             if (state == PermissionState.Unrestricted)
97             {
98                 m_unrestricted = true;
99             }
100             else if (state == PermissionState.None)
101             {
102                 m_unrestricted = false;
103             }
104             else
105             {
106                 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
107             }
108         }
109         
110         public SiteIdentityPermission( String site )
111         {
112             Site = site;
113         }
114         
115         //------------------------------------------------------
116         //
117         // PUBLIC ACCESSOR METHODS
118         //
119         //------------------------------------------------------
120
121         public String Site
122         {
123             set
124             {
125                 m_unrestricted = false;
126                 m_sites = new SiteString[1];
127                 m_sites[0] = new SiteString( value );
128             }
129
130             get
131             {
132                 if(m_sites == null)
133                     return "";
134                 if(m_sites.Length == 1)
135                     return m_sites[0].ToString();
136                 throw new NotSupportedException(Environment.GetResourceString("NotSupported_AmbiguousIdentity"));
137             }
138         } 
139
140         //------------------------------------------------------
141         //
142         // PRIVATE AND PROTECTED HELPERS FOR ACCESSORS AND CONSTRUCTORS
143         //
144         //------------------------------------------------------
145
146         //------------------------------------------------------
147         //
148         // CODEACCESSPERMISSION IMPLEMENTATION
149         //
150         //------------------------------------------------------
151
152         //------------------------------------------------------
153         //
154         // IPERMISSION IMPLEMENTATION
155         //
156         //------------------------------------------------------
157
158
159         public override IPermission Copy()
160         {
161             SiteIdentityPermission perm = new SiteIdentityPermission( PermissionState.None );
162             perm.m_unrestricted = this.m_unrestricted;
163             if (this.m_sites != null)
164             {
165                 perm.m_sites = new SiteString[this.m_sites.Length];
166                 int n;
167                 for(n = 0; n < this.m_sites.Length; n++)
168                     perm.m_sites[n] = (SiteString)this.m_sites[n].Copy();
169             }
170             return perm;
171         }
172         
173         public override bool IsSubsetOf(IPermission target)
174         {
175             if (target == null)
176             {
177                 if(m_unrestricted)
178                     return false;
179                 if(m_sites == null)
180                     return true;
181                 if(m_sites.Length == 0)
182                     return true;
183                 return false;
184             }
185             SiteIdentityPermission that = target as SiteIdentityPermission;
186             if(that == null)
187                 throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
188             if(that.m_unrestricted)
189                 return true;
190             if(m_unrestricted)
191                 return false;
192             if(this.m_sites != null)
193             {
194                 foreach(SiteString ssThis in this.m_sites)
195                 {
196                     bool bOK = false;
197                     if(that.m_sites != null)
198                     {
199                         foreach(SiteString ssThat in that.m_sites)
200                         {
201                             if(ssThis.IsSubsetOf(ssThat))
202                             {
203                                 bOK = true;
204                                 break;
205                             }
206                         }
207                     }
208                     if(!bOK)
209                         return false;           
210                 }
211             }
212             return true;
213         }
214         
215         public override IPermission Intersect(IPermission target)
216         {
217             if (target == null)
218                 return null;
219             SiteIdentityPermission that = target as SiteIdentityPermission;
220             if(that == null)
221                 throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
222             if(this.m_unrestricted && that.m_unrestricted)
223             {
224                 SiteIdentityPermission res = new SiteIdentityPermission(PermissionState.None);
225                 res.m_unrestricted = true;
226                 return res;
227             }
228             if(this.m_unrestricted)
229                 return that.Copy();
230             if(that.m_unrestricted)
231                 return this.Copy();
232             if(this.m_sites == null || that.m_sites == null || this.m_sites.Length == 0 || that.m_sites.Length == 0)
233                 return null;
234             List<SiteString> alSites = new List<SiteString>();
235             foreach(SiteString ssThis in this.m_sites)
236             {
237                 foreach(SiteString ssThat in that.m_sites)
238                 {
239                     SiteString ssInt = (SiteString)ssThis.Intersect(ssThat);
240                     if(ssInt != null)
241                         alSites.Add(ssInt);
242                 }
243             }
244             if(alSites.Count == 0)
245                 return null;
246             SiteIdentityPermission result = new SiteIdentityPermission(PermissionState.None);
247             result.m_sites = alSites.ToArray();
248             return result;
249         }
250         
251         public override IPermission Union(IPermission target)
252         {
253             if (target == null)
254             {
255                 if((this.m_sites == null || this.m_sites.Length == 0) && !this.m_unrestricted)
256                     return null;
257                 return this.Copy();
258             }
259             SiteIdentityPermission that = target as SiteIdentityPermission;
260             if(that == null)
261                 throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
262             if(this.m_unrestricted || that.m_unrestricted)
263             {
264                 SiteIdentityPermission res = new SiteIdentityPermission(PermissionState.None);
265                 res.m_unrestricted = true;
266                 return res;
267             }
268             if (this.m_sites == null || this.m_sites.Length == 0)
269             {
270                 if(that.m_sites == null || that.m_sites.Length == 0)
271                     return null;
272                 return that.Copy();
273             }
274             if(that.m_sites == null || that.m_sites.Length == 0)
275                 return this.Copy();
276             List<SiteString> alSites = new List<SiteString>();
277             foreach(SiteString ssThis in this.m_sites)
278                 alSites.Add(ssThis);
279             foreach(SiteString ssThat in that.m_sites)
280             {
281                 bool bDupe = false;
282                 foreach(SiteString ss in alSites)
283                 {
284                     if(ssThat.Equals(ss))
285                     {
286                         bDupe = true;
287                         break;
288                     }
289                 }
290                 if(!bDupe)
291                     alSites.Add(ssThat);
292             }
293             SiteIdentityPermission result = new SiteIdentityPermission(PermissionState.None);
294             result.m_sites = alSites.ToArray();
295             return result;
296         }
297
298 #if FEATURE_CAS_POLICY
299         public override void FromXml(SecurityElement esd)
300         {
301             m_unrestricted = false;
302             m_sites = null;
303             CodeAccessPermission.ValidateElement( esd, this );
304             String unr = esd.Attribute( "Unrestricted" );
305             if(unr != null && String.Compare(unr, "true", StringComparison.OrdinalIgnoreCase) == 0)
306             {
307                 m_unrestricted = true;
308                 return;
309             }
310             String elem = esd.Attribute( "Site" );
311             List<SiteString> al = new List<SiteString>();
312             if(elem != null)
313                 al.Add(new SiteString( elem ));
314             ArrayList alChildren = esd.Children;
315             if(alChildren != null)
316             {
317                 foreach(SecurityElement child in alChildren)
318                 {
319                     elem = child.Attribute( "Site" );
320                     if(elem != null)
321                         al.Add(new SiteString( elem ));
322                 }
323             }
324             if(al.Count != 0)
325                 m_sites = al.ToArray();
326         }
327
328         public override SecurityElement ToXml()
329         {
330             SecurityElement esd = CodeAccessPermission.CreatePermissionElement( this, "System.Security.Permissions.SiteIdentityPermission" );
331             if (m_unrestricted)
332                 esd.AddAttribute( "Unrestricted", "true" );
333             else if (m_sites != null)
334             {
335                 if (m_sites.Length == 1)
336                     esd.AddAttribute( "Site", m_sites[0].ToString() );
337                 else
338                 {
339                     int n;
340                     for(n = 0; n < m_sites.Length; n++)
341                     {
342                         SecurityElement child = new SecurityElement("Site");
343                         child.AddAttribute( "Site", m_sites[n].ToString() );
344                         esd.AddChild(child);
345                     }
346                 }
347             }
348             return esd;
349         }
350 #endif // FEATURE_CAS_POLICY
351
352         /// <internalonly/>
353         int IBuiltInPermission.GetTokenIndex()
354         {
355             return SiteIdentityPermission.GetTokenIndex();
356         }
357
358         internal static int GetTokenIndex()
359         {
360             return BuiltInPermissionIndex.SiteIdentityPermissionIndex;
361         }
362     }
363 }