1 using System.Diagnostics.Contracts;
4 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // SiteMembershipCondition.cs
9 // <OWNER>Microsoft</OWNER>
11 // Implementation of membership condition for zones
14 namespace System.Security.Policy {
17 using SecurityManager = System.Security.SecurityManager;
18 using SiteString = System.Security.Util.SiteString;
19 using PermissionSet = System.Security.PermissionSet;
20 using SecurityElement = System.Security.SecurityElement;
21 using System.Collections;
22 using System.Globalization;
25 [System.Runtime.InteropServices.ComVisible(true)]
26 sealed public class SiteMembershipCondition : IMembershipCondition, IConstantMembershipCondition, IReportMatchMembershipCondition
29 //------------------------------------------------------
33 //------------------------------------------------------
35 private SiteString m_site;
36 private SecurityElement m_element;
38 //------------------------------------------------------
40 // PUBLIC CONSTRUCTORS
42 //------------------------------------------------------
44 internal SiteMembershipCondition()
49 public SiteMembershipCondition( String site )
52 throw new ArgumentNullException( "site" );
53 Contract.EndContractBlock();
55 m_site = new SiteString( site );
58 //------------------------------------------------------
60 // PUBLIC ACCESSOR METHODS
62 //------------------------------------------------------
70 throw new ArgumentNullException( "value" );
71 Contract.EndContractBlock();
73 m_site = new SiteString( value );
78 if (m_site == null && m_element != null)
82 return m_site.ToString();
88 //------------------------------------------------------
90 // IMEMBERSHIPCONDITION IMPLEMENTATION
92 //------------------------------------------------------
94 public bool Check( Evidence evidence )
96 object usedEvidence = null;
97 return (this as IReportMatchMembershipCondition).Check(evidence, out usedEvidence);
100 bool IReportMatchMembershipCondition.Check(Evidence evidence, out object usedEvidence)
104 if (evidence == null)
107 Site site = evidence.GetHostEvidence<Site>();
110 if (m_site == null && m_element != null)
115 if (site.GetSiteString().IsSubsetOf(this.m_site))
124 public IMembershipCondition Copy()
126 if (m_site == null && m_element != null)
129 return new SiteMembershipCondition( m_site.ToString() );
133 public SecurityElement ToXml()
135 return ToXml( null );
138 public void FromXml( SecurityElement e )
143 public SecurityElement ToXml( PolicyLevel level )
145 if (m_site == null && m_element != null)
148 SecurityElement root = new SecurityElement( "IMembershipCondition" );
149 System.Security.Util.XMLUtil.AddClassAttribute( root, this.GetType(), "System.Security.Policy.SiteMembershipCondition" );
150 // If you hit this assert then most likely you are trying to change the name of this class.
151 // This is ok as long as you change the hard coded string above and change the assert below.
152 Contract.Assert( this.GetType().FullName.Equals( "System.Security.Policy.SiteMembershipCondition" ), "Class name changed!" );
154 root.AddAttribute( "version", "1" );
157 root.AddAttribute( "Site", m_site.ToString() );
162 public void FromXml( SecurityElement e, PolicyLevel level )
165 throw new ArgumentNullException("e");
167 if (!e.Tag.Equals( "IMembershipCondition" ))
169 throw new ArgumentException( Environment.GetResourceString( "Argument_MembershipConditionElement" ) );
171 Contract.EndContractBlock();
180 private void ParseSite()
184 if (m_element == null)
187 String elSite = m_element.Attribute( "Site" );
189 throw new ArgumentException( Environment.GetResourceString( "Argument_SiteCannotBeNull" ) );
191 m_site = new SiteString( elSite );
196 public override bool Equals( Object o )
198 SiteMembershipCondition that = (o as SiteMembershipCondition);
202 if (this.m_site == null && this.m_element != null)
204 if (that.m_site == null && that.m_element != null)
207 if( Equals (this.m_site, that.m_site ))
215 public override int GetHashCode()
217 if (m_site == null && m_element != null)
222 return m_site.GetHashCode();
226 return typeof( SiteMembershipCondition ).GetHashCode();
230 public override String ToString()
232 if (m_site == null && m_element != null)
236 return String.Format( CultureInfo.CurrentCulture, Environment.GetResourceString( "Site_ToStringArg" ), m_site );
238 return Environment.GetResourceString( "Site_ToString" );