Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / mscorlib / system / security / permissions / siteidentitypermission.cs
bloba6c530be87e8ef26a343692bb51f86574a4fdffd
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 // SiteIdentityPermission.cs
7 //
8 // <OWNER>Microsoft</OWNER>
9 //
11 namespace System.Security.Permissions
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;
24 [System.Runtime.InteropServices.ComVisible(true)]
25 [Serializable]
26 sealed public class SiteIdentityPermission : CodeAccessPermission, IBuiltInPermission
28 //------------------------------------------------------
30 // PRIVATE STATE DATA
32 //------------------------------------------------------
33 [OptionalField(VersionAdded = 2)]
34 private bool m_unrestricted;
35 [OptionalField(VersionAdded = 2)]
36 private SiteString[] m_sites;
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;
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;
46 [OnDeserialized]
47 private void OnDeserialized(StreamingContext ctx)
49 // v2.0 and beyond XML case
50 if (m_serializedPermission != null)
52 FromXml(SecurityElement.FromString(m_serializedPermission));
53 m_serializedPermission = null;
55 else if (m_site != null) //v1.x case where we read the m_site value
57 m_unrestricted = false;
58 m_sites = new SiteString[1];
59 m_sites[0] = m_site;
60 m_site = null;
64 [OnSerializing]
65 private void OnSerializing(StreamingContext ctx)
68 if ((ctx.State & ~(StreamingContextStates.Clone|StreamingContextStates.CrossAppDomain)) != 0)
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];
76 [OnSerialized]
77 private void OnSerialized(StreamingContext ctx)
79 if ((ctx.State & ~(StreamingContextStates.Clone|StreamingContextStates.CrossAppDomain)) != 0)
81 m_serializedPermission = null;
82 m_site = null;
85 #endif // FEATURE_REMOTING
87 //------------------------------------------------------
89 // PUBLIC CONSTRUCTORS
91 //------------------------------------------------------
94 public SiteIdentityPermission(PermissionState state)
96 if (state == PermissionState.Unrestricted)
98 m_unrestricted = true;
100 else if (state == PermissionState.None)
102 m_unrestricted = false;
104 else
106 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
110 public SiteIdentityPermission( String site )
112 Site = site;
115 //------------------------------------------------------
117 // PUBLIC ACCESSOR METHODS
119 //------------------------------------------------------
121 public String Site
125 m_unrestricted = false;
126 m_sites = new SiteString[1];
127 m_sites[0] = new SiteString( value );
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"));
140 //------------------------------------------------------
142 // PRIVATE AND PROTECTED HELPERS FOR ACCESSORS AND CONSTRUCTORS
144 //------------------------------------------------------
146 //------------------------------------------------------
148 // CODEACCESSPERMISSION IMPLEMENTATION
150 //------------------------------------------------------
152 //------------------------------------------------------
154 // IPERMISSION IMPLEMENTATION
156 //------------------------------------------------------
159 public override IPermission Copy()
161 SiteIdentityPermission perm = new SiteIdentityPermission( PermissionState.None );
162 perm.m_unrestricted = this.m_unrestricted;
163 if (this.m_sites != null)
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();
170 return perm;
173 public override bool IsSubsetOf(IPermission target)
175 if (target == null)
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;
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)
194 foreach(SiteString ssThis in this.m_sites)
196 bool bOK = false;
197 if(that.m_sites != null)
199 foreach(SiteString ssThat in that.m_sites)
201 if(ssThis.IsSubsetOf(ssThat))
203 bOK = true;
204 break;
208 if(!bOK)
209 return false;
212 return true;
215 public override IPermission Intersect(IPermission target)
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)
224 SiteIdentityPermission res = new SiteIdentityPermission(PermissionState.None);
225 res.m_unrestricted = true;
226 return res;
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)
237 foreach(SiteString ssThat in that.m_sites)
239 SiteString ssInt = (SiteString)ssThis.Intersect(ssThat);
240 if(ssInt != null)
241 alSites.Add(ssInt);
244 if(alSites.Count == 0)
245 return null;
246 SiteIdentityPermission result = new SiteIdentityPermission(PermissionState.None);
247 result.m_sites = alSites.ToArray();
248 return result;
251 public override IPermission Union(IPermission target)
253 if (target == null)
255 if((this.m_sites == null || this.m_sites.Length == 0) && !this.m_unrestricted)
256 return null;
257 return this.Copy();
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)
264 SiteIdentityPermission res = new SiteIdentityPermission(PermissionState.None);
265 res.m_unrestricted = true;
266 return res;
268 if (this.m_sites == null || this.m_sites.Length == 0)
270 if(that.m_sites == null || that.m_sites.Length == 0)
271 return null;
272 return that.Copy();
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)
281 bool bDupe = false;
282 foreach(SiteString ss in alSites)
284 if(ssThat.Equals(ss))
286 bDupe = true;
287 break;
290 if(!bDupe)
291 alSites.Add(ssThat);
293 SiteIdentityPermission result = new SiteIdentityPermission(PermissionState.None);
294 result.m_sites = alSites.ToArray();
295 return result;
298 #if FEATURE_CAS_POLICY
299 public override void FromXml(SecurityElement esd)
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)
307 m_unrestricted = true;
308 return;
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)
317 foreach(SecurityElement child in alChildren)
319 elem = child.Attribute( "Site" );
320 if(elem != null)
321 al.Add(new SiteString( elem ));
324 if(al.Count != 0)
325 m_sites = al.ToArray();
328 public override SecurityElement ToXml()
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)
335 if (m_sites.Length == 1)
336 esd.AddAttribute( "Site", m_sites[0].ToString() );
337 else
339 int n;
340 for(n = 0; n < m_sites.Length; n++)
342 SecurityElement child = new SecurityElement("Site");
343 child.AddAttribute( "Site", m_sites[n].ToString() );
344 esd.AddChild(child);
348 return esd;
350 #endif // FEATURE_CAS_POLICY
352 /// <internalonly/>
353 int IBuiltInPermission.GetTokenIndex()
355 return SiteIdentityPermission.GetTokenIndex();
358 internal static int GetTokenIndex()
360 return BuiltInPermissionIndex.SiteIdentityPermissionIndex;