FileCodeGroup.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / whidbey / netfxsp / ndp / clr / src / BCL / System / Security / Policy / FileCodeGroup.cs / 3 / FileCodeGroup.cs

                            // ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 

// 
// FileCodeGroup.cs 
//
// Representation for code groups used for the policy mechanism. 
//

namespace System.Security.Policy {
    using System; 
    using System.Collections;
    using System.Globalization; 
    using System.Security.Permissions; 
    using System.Security.Util;
    using System.Runtime.Serialization; 
    using System.Runtime.Versioning;

    [Serializable]
    [System.Runtime.InteropServices.ComVisible(true)] 
    sealed public class FileCodeGroup : CodeGroup, IUnionSemanticCodeGroup {
        private FileIOPermissionAccess m_access; 
 
        internal FileCodeGroup() : base() {}
 
        public FileCodeGroup(IMembershipCondition membershipCondition, FileIOPermissionAccess access)
            : base(membershipCondition, (PolicyStatement)null) {
            m_access = access;
        } 

        [ResourceExposure(ResourceScope.Machine)] 
        [ResourceConsumption(ResourceScope.Machine)] 
        public override PolicyStatement Resolve(Evidence evidence) {
            if (evidence == null) 
                throw new ArgumentNullException("evidence");

            object usedEvidence = null;
 
            if (PolicyManager.CheckMembershipCondition(MembershipCondition, evidence, out usedEvidence)) {
                PolicyStatement thisPolicy = CalculateAssemblyPolicy(evidence); 
 
                // If any delay-evidence was used to generate this grant set, then we need to keep track of
                // that for potentially later forcing it to be verified. 
                IDelayEvaluatedEvidence delayEvidence = usedEvidence as IDelayEvaluatedEvidence;
                bool delayEvidenceNeedsVerification = delayEvidence != null && !delayEvidence.IsVerified;
                if (delayEvidenceNeedsVerification) {
                    thisPolicy.AddDependentEvidence(delayEvidence); 
                }
 
                bool foundExclusiveChild = false; 
                IEnumerator enumerator = this.Children.GetEnumerator();
                while (enumerator.MoveNext() && !foundExclusiveChild) { 
                    PolicyStatement childPolicy = PolicyManager.ResolveCodeGroup(enumerator.Current as CodeGroup,
                                                                                 evidence);

                    if (childPolicy != null) { 
                        thisPolicy.InplaceUnion(childPolicy);
 
                        if ((childPolicy.Attributes & PolicyStatementAttribute.Exclusive) == PolicyStatementAttribute.Exclusive) { 
                            foundExclusiveChild = true;
                        } 
                    }
                }

                return thisPolicy; 
            }
            else { 
                return null; 
            }
        } 

        /// 
        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)] 
        PolicyStatement IUnionSemanticCodeGroup.InternalResolve(Evidence evidence) {
            if (evidence == null) 
                throw new ArgumentNullException("evidence"); 

            if (this.MembershipCondition.Check(evidence)) { 
                return CalculateAssemblyPolicy(evidence);
            }

            return null; 
        }
 
        public override CodeGroup ResolveMatchingCodeGroups(Evidence evidence) { 
            if (evidence == null)
                throw new ArgumentNullException("evidence"); 

            if (this.MembershipCondition.Check(evidence)) {
                CodeGroup retGroup = this.Copy();
                retGroup.Children = new ArrayList(); 
                IEnumerator enumerator = this.Children.GetEnumerator();
                while (enumerator.MoveNext()) { 
                    CodeGroup matchingGroups = ((CodeGroup)enumerator.Current).ResolveMatchingCodeGroups(evidence); 
                    // If the child has a policy, we are done.
                    if (matchingGroups != null) 
                        retGroup.AddChild(matchingGroups);
                }
                return retGroup;
            } 
            else {
                return null; 
            } 
        }
 
        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        internal PolicyStatement CalculatePolicy(Url url) {
            URLString urlString = url.GetURLString(); 
            if (String.Compare(urlString.Scheme, "file", StringComparison.OrdinalIgnoreCase) != 0)
                return null; 
 
            string directory = urlString.GetDirectoryName();
            PermissionSet permSet = new PermissionSet(PermissionState.None); 
            permSet.SetPermission(new FileIOPermission(m_access, System.IO.Path.GetFullPath(directory)));

            return new PolicyStatement(permSet, PolicyStatementAttribute.Nothing);
        } 

        [ResourceExposure(ResourceScope.Machine)] 
        [ResourceConsumption(ResourceScope.Machine)] 
        private PolicyStatement CalculateAssemblyPolicy(Evidence evidence) {
            PolicyStatement thisPolicy = null; 

            IEnumerator evidenceEnumerator = evidence.GetHostEnumerator();
            while (evidenceEnumerator.MoveNext()) {
                Url url = evidenceEnumerator.Current as Url; 
                if (url != null)
                    thisPolicy = CalculatePolicy(url); 
            } 

            if (thisPolicy == null) 
                thisPolicy = new PolicyStatement(new PermissionSet(false), PolicyStatementAttribute.Nothing);
            return thisPolicy;
        }
 
        public override CodeGroup Copy() {
            FileCodeGroup group = new FileCodeGroup(this.MembershipCondition, this.m_access); 
            group.Name = this.Name; 
            group.Description = this.Description;
 
            IEnumerator enumerator = this.Children.GetEnumerator();
            while (enumerator.MoveNext()) {
                group.AddChild((CodeGroup)enumerator.Current);
            } 
            return group;
        } 
 
        public override string MergeLogic {
            get { 
                return Environment.GetResourceString("MergeLogic_Union");
            }
        }
 
        public override string PermissionSetName {
            get { 
                return String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("FileCodeGroup_PermissionSet"), XMLUtil.BitFieldEnumToString(typeof(FileIOPermissionAccess), m_access)); 
            }
        } 

        public override string AttributeString {
            get {
                return null; 
            }
        } 
 
        protected override void CreateXml(SecurityElement element, PolicyLevel level) {
            element.AddAttribute("Access", XMLUtil.BitFieldEnumToString(typeof(FileIOPermissionAccess), m_access)); 
        }

        protected override void ParseXml(SecurityElement e, PolicyLevel level) {
            string access = e.Attribute("Access"); 
            if (access != null)
                m_access = (FileIOPermissionAccess) Enum.Parse(typeof(FileIOPermissionAccess), access); 
            else 
                m_access = FileIOPermissionAccess.NoAccess;
        } 

        public override bool Equals(Object o) {
            FileCodeGroup that = (o as FileCodeGroup);
            if (that != null && base.Equals(that)) { 
                if (this.m_access == that.m_access)
                    return true; 
            } 
            return false;
        } 

        public override int GetHashCode() {
            return base.GetHashCode() + m_access.GetHashCode();
        } 

        internal override string GetTypeName() { 
            return "System.Security.Policy.FileCodeGroup"; 
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// ==++== 
//
//   Copyright (c) Microsoft Corporation.  All rights reserved.
//
// ==--== 

// 
// FileCodeGroup.cs 
//
// Representation for code groups used for the policy mechanism. 
//

namespace System.Security.Policy {
    using System; 
    using System.Collections;
    using System.Globalization; 
    using System.Security.Permissions; 
    using System.Security.Util;
    using System.Runtime.Serialization; 
    using System.Runtime.Versioning;

    [Serializable]
    [System.Runtime.InteropServices.ComVisible(true)] 
    sealed public class FileCodeGroup : CodeGroup, IUnionSemanticCodeGroup {
        private FileIOPermissionAccess m_access; 
 
        internal FileCodeGroup() : base() {}
 
        public FileCodeGroup(IMembershipCondition membershipCondition, FileIOPermissionAccess access)
            : base(membershipCondition, (PolicyStatement)null) {
            m_access = access;
        } 

        [ResourceExposure(ResourceScope.Machine)] 
        [ResourceConsumption(ResourceScope.Machine)] 
        public override PolicyStatement Resolve(Evidence evidence) {
            if (evidence == null) 
                throw new ArgumentNullException("evidence");

            object usedEvidence = null;
 
            if (PolicyManager.CheckMembershipCondition(MembershipCondition, evidence, out usedEvidence)) {
                PolicyStatement thisPolicy = CalculateAssemblyPolicy(evidence); 
 
                // If any delay-evidence was used to generate this grant set, then we need to keep track of
                // that for potentially later forcing it to be verified. 
                IDelayEvaluatedEvidence delayEvidence = usedEvidence as IDelayEvaluatedEvidence;
                bool delayEvidenceNeedsVerification = delayEvidence != null && !delayEvidence.IsVerified;
                if (delayEvidenceNeedsVerification) {
                    thisPolicy.AddDependentEvidence(delayEvidence); 
                }
 
                bool foundExclusiveChild = false; 
                IEnumerator enumerator = this.Children.GetEnumerator();
                while (enumerator.MoveNext() && !foundExclusiveChild) { 
                    PolicyStatement childPolicy = PolicyManager.ResolveCodeGroup(enumerator.Current as CodeGroup,
                                                                                 evidence);

                    if (childPolicy != null) { 
                        thisPolicy.InplaceUnion(childPolicy);
 
                        if ((childPolicy.Attributes & PolicyStatementAttribute.Exclusive) == PolicyStatementAttribute.Exclusive) { 
                            foundExclusiveChild = true;
                        } 
                    }
                }

                return thisPolicy; 
            }
            else { 
                return null; 
            }
        } 

        /// 
        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)] 
        PolicyStatement IUnionSemanticCodeGroup.InternalResolve(Evidence evidence) {
            if (evidence == null) 
                throw new ArgumentNullException("evidence"); 

            if (this.MembershipCondition.Check(evidence)) { 
                return CalculateAssemblyPolicy(evidence);
            }

            return null; 
        }
 
        public override CodeGroup ResolveMatchingCodeGroups(Evidence evidence) { 
            if (evidence == null)
                throw new ArgumentNullException("evidence"); 

            if (this.MembershipCondition.Check(evidence)) {
                CodeGroup retGroup = this.Copy();
                retGroup.Children = new ArrayList(); 
                IEnumerator enumerator = this.Children.GetEnumerator();
                while (enumerator.MoveNext()) { 
                    CodeGroup matchingGroups = ((CodeGroup)enumerator.Current).ResolveMatchingCodeGroups(evidence); 
                    // If the child has a policy, we are done.
                    if (matchingGroups != null) 
                        retGroup.AddChild(matchingGroups);
                }
                return retGroup;
            } 
            else {
                return null; 
            } 
        }
 
        [ResourceExposure(ResourceScope.Machine)]
        [ResourceConsumption(ResourceScope.Machine)]
        internal PolicyStatement CalculatePolicy(Url url) {
            URLString urlString = url.GetURLString(); 
            if (String.Compare(urlString.Scheme, "file", StringComparison.OrdinalIgnoreCase) != 0)
                return null; 
 
            string directory = urlString.GetDirectoryName();
            PermissionSet permSet = new PermissionSet(PermissionState.None); 
            permSet.SetPermission(new FileIOPermission(m_access, System.IO.Path.GetFullPath(directory)));

            return new PolicyStatement(permSet, PolicyStatementAttribute.Nothing);
        } 

        [ResourceExposure(ResourceScope.Machine)] 
        [ResourceConsumption(ResourceScope.Machine)] 
        private PolicyStatement CalculateAssemblyPolicy(Evidence evidence) {
            PolicyStatement thisPolicy = null; 

            IEnumerator evidenceEnumerator = evidence.GetHostEnumerator();
            while (evidenceEnumerator.MoveNext()) {
                Url url = evidenceEnumerator.Current as Url; 
                if (url != null)
                    thisPolicy = CalculatePolicy(url); 
            } 

            if (thisPolicy == null) 
                thisPolicy = new PolicyStatement(new PermissionSet(false), PolicyStatementAttribute.Nothing);
            return thisPolicy;
        }
 
        public override CodeGroup Copy() {
            FileCodeGroup group = new FileCodeGroup(this.MembershipCondition, this.m_access); 
            group.Name = this.Name; 
            group.Description = this.Description;
 
            IEnumerator enumerator = this.Children.GetEnumerator();
            while (enumerator.MoveNext()) {
                group.AddChild((CodeGroup)enumerator.Current);
            } 
            return group;
        } 
 
        public override string MergeLogic {
            get { 
                return Environment.GetResourceString("MergeLogic_Union");
            }
        }
 
        public override string PermissionSetName {
            get { 
                return String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("FileCodeGroup_PermissionSet"), XMLUtil.BitFieldEnumToString(typeof(FileIOPermissionAccess), m_access)); 
            }
        } 

        public override string AttributeString {
            get {
                return null; 
            }
        } 
 
        protected override void CreateXml(SecurityElement element, PolicyLevel level) {
            element.AddAttribute("Access", XMLUtil.BitFieldEnumToString(typeof(FileIOPermissionAccess), m_access)); 
        }

        protected override void ParseXml(SecurityElement e, PolicyLevel level) {
            string access = e.Attribute("Access"); 
            if (access != null)
                m_access = (FileIOPermissionAccess) Enum.Parse(typeof(FileIOPermissionAccess), access); 
            else 
                m_access = FileIOPermissionAccess.NoAccess;
        } 

        public override bool Equals(Object o) {
            FileCodeGroup that = (o as FileCodeGroup);
            if (that != null && base.Equals(that)) { 
                if (this.m_access == that.m_access)
                    return true; 
            } 
            return false;
        } 

        public override int GetHashCode() {
            return base.GetHashCode() + m_access.GetHashCode();
        } 

        internal override string GetTypeName() { 
            return "System.Security.Policy.FileCodeGroup"; 
        }
    } 
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK