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