3 // Copyright (c) Microsoft Corporation. All rights reserved.
6 // SiteIdentityPermission.cs
8 // <OWNER>[....]</OWNER>
11 namespace System.Security.Permissions
14 #if FEATURE_CAS_POLICY
15 using SecurityElement = System.Security.SecurityElement;
16 #endif // FEATURE_CAS_POLICY
17 using SiteString = System.Security.Util.SiteString;
19 using System.Collections;
20 using System.Collections.Generic;
21 using System.Globalization;
22 using System.Runtime.Serialization;
24 [System.Runtime.InteropServices.ComVisible(true)]
26 sealed public class SiteIdentityPermission : CodeAccessPermission, IBuiltInPermission
28 //------------------------------------------------------
32 //------------------------------------------------------
33 [OptionalField(VersionAdded = 2)]
34 private bool m_unrestricted;
35 [OptionalField(VersionAdded = 2)]
36 private SiteString[] m_sites;
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;
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];
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
77 private void OnSerialized(StreamingContext ctx)
79 if ((ctx.State & ~(StreamingContextStates.Clone|StreamingContextStates.CrossAppDomain)) != 0)
81 m_serializedPermission = 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;
106 throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPermissionState"));
110 public SiteIdentityPermission( String site )
115 //------------------------------------------------------
117 // PUBLIC ACCESSOR METHODS
119 //------------------------------------------------------
125 m_unrestricted = false;
126 m_sites = new SiteString[1];
127 m_sites[0] = new SiteString( value );
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];
167 for(n = 0; n < this.m_sites.Length; n++)
168 perm.m_sites[n] = (SiteString)this.m_sites[n].Copy();
173 public override bool IsSubsetOf(IPermission target)
181 if(m_sites.Length == 0)
185 SiteIdentityPermission that = target as SiteIdentityPermission;
187 throw new ArgumentException(Environment.GetResourceString("Argument_WrongType", this.GetType().FullName));
188 if(that.m_unrestricted)
192 if(this.m_sites != null)
194 foreach(SiteString ssThis in this.m_sites)
197 if(that.m_sites != null)
199 foreach(SiteString ssThat in that.m_sites)
201 if(ssThis.IsSubsetOf(ssThat))
215 public override IPermission Intersect(IPermission target)
219 SiteIdentityPermission that = target as SiteIdentityPermission;
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;
228 if(this.m_unrestricted)
230 if(that.m_unrestricted)
232 if(this.m_sites == null || that.m_sites == null || this.m_sites.Length == 0 || that.m_sites.Length == 0)
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);
244 if(alSites.Count == 0)
246 SiteIdentityPermission result = new SiteIdentityPermission(PermissionState.None);
247 result.m_sites = alSites.ToArray();
251 public override IPermission Union(IPermission target)
255 if((this.m_sites == null || this.m_sites.Length == 0) && !this.m_unrestricted)
259 SiteIdentityPermission that = target as SiteIdentityPermission;
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;
268 if (this.m_sites == null || this.m_sites.Length == 0)
270 if(that.m_sites == null || that.m_sites.Length == 0)
274 if(that.m_sites == null || that.m_sites.Length == 0)
276 List<SiteString> alSites = new List<SiteString>();
277 foreach(SiteString ssThis in this.m_sites)
279 foreach(SiteString ssThat in that.m_sites)
282 foreach(SiteString ss in alSites)
284 if(ssThat.Equals(ss))
293 SiteIdentityPermission result = new SiteIdentityPermission(PermissionState.None);
294 result.m_sites = alSites.ToArray();
298 #if FEATURE_CAS_POLICY
299 public override void FromXml(SecurityElement esd)
301 m_unrestricted = false;
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;
310 String elem = esd.Attribute( "Site" );
311 List<SiteString> al = new List<SiteString>();
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" );
321 al.Add(new SiteString( elem ));
325 m_sites = al.ToArray();
328 public override SecurityElement ToXml()
330 SecurityElement esd = CodeAccessPermission.CreatePermissionElement( this, "System.Security.Permissions.SiteIdentityPermission" );
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() );
340 for(n = 0; n < m_sites.Length; n++)
342 SecurityElement child = new SecurityElement("Site");
343 child.AddAttribute( "Site", m_sites[n].ToString() );
350 #endif // FEATURE_CAS_POLICY
353 int IBuiltInPermission.GetTokenIndex()
355 return SiteIdentityPermission.GetTokenIndex();
358 internal static int GetTokenIndex()
360 return BuiltInPermissionIndex.SiteIdentityPermissionIndex;