コード例 #1
0
        private void ValidatePscxPath(string parameterName, PscxPathAttribute pathAttrib, PscxPathInfo pscxPath)
        {
            WriteDebug(String.Format("ValidatePscxPath: parameter {0} ; pscxPath {1}", parameterName, pscxPath));

            if (pscxPath.IsValid == false)
            {
                // todo: localize
                string description = String.Format(
                    "The path '{0}' supplied for {1} is invalid.",
                    pscxPath, parameterName);

                // path syntax error
                OnPscxPathError(parameterName, description, PscxPathState.Invalid, pscxPath);
            }

            PscxPathType pathType = PscxPathType.Unknown;

            // explicit true or unset
            if (pathAttrib.ShouldExist || (pathAttrib._shouldExist == null))
            {
                if (pathAttrib.ShouldExist)
                {
                    // explicit true, so check existance
                    ValidateExists(parameterName, pscxPath, ref pathType);
                }
                else
                {
                    // shouldexist not specified, so grab path type via invokeprovider
                    pathType = this.InvokeProvider.Item.IsContainer(pscxPath.ToString())
                                   ? PscxPathType.Container : PscxPathType.Leaf;
                }

                PscxPathType expectedPathType = pathAttrib.PathType;

                // do we have a path type constraint?
                if (expectedPathType != PscxPathType.None)
                {
                    ValidatePathType(parameterName, pscxPath, pathType, expectedPathType);
                }
            }
            else // shouldexist explicit false
            {
                // NOTE: for Pscx developers
                Trace.Assert(pathAttrib.PathType == PscxPathType.None,
                             String.Format(
                                 "Pscx Developer Error: {0}\n\nIf a PathType constraint is placed on a parameter, " +
                                 "ShouldExist cannot be explicitly set to false. Remove " +
                                 "the ShouldExist condition from the attribute, or set it " +
                                 "explicitly to true.\n\nIf you are seeing this message " +
                                 "as a Pscx end-user, please log an issue on " +
                                 "http://www.codeplex.com/powershellcx", CmdletName));
            }

            // any provider constraints defined on the PscxPath attribute,
            // or on a Type-level ProviderConstraint attribute?
            if ((pathAttrib.ProviderTypes != null) || (_defaultProviderConstraint != null))
            {
                CheckProviderConstraints(pscxPath, pathAttrib, parameterName);
            }
        }
コード例 #2
0
        private void ValidatePscxPaths()
        {
            _boundPaths = new List <Pair <PropertyInfo, PscxPathAttribute> >();

            var visitor = new Visitor(this);

            visitor.VisitType(GetType());

            foreach (var boundPath in _boundPaths)
            {
                PropertyInfo parameter = boundPath.First;

                string parameterName = parameter.Name;

                // retrieve [PscxPath] attribute
                PscxPathAttribute pathAttrib = boundPath.Second;

                // get property value
                object value = parameter.GetValue(this, null);

                if ((value != null) &&
                    // allow runtime modification from derived classes
                    OnValidatePscxPath(parameterName, pathAttrib))
                {
                    WriteDebug("Validating " + parameterName);
                    PscxPathInfo[] pscxPaths = null;

                    // may be array
                    if (value is Array)
                    {
                        pscxPaths = value as PscxPathInfo[];
                    }
                    else
                    {
                        var pscxPath = value as PscxPathInfo;
                        if (pscxPath != null)
                        {
                            pscxPaths = new PscxPathInfo[] { pscxPath };
                        }
                    }

                    if (pscxPaths != null)
                    {
                        foreach (PscxPathInfo pscxPath in pscxPaths)
                        {
                            ValidatePscxPath(parameterName, pathAttrib, pscxPath);
                        }
                    }
                }
                else
                {
                    WriteDebug("Skipping " + parameterName);
                }
            }
        }
コード例 #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="pathAttrib"></param>
        /// <param name="pscxPath"></param>
        protected void CheckProviderConstraints(PscxPathInfo pscxPath, PscxPathAttribute pathAttrib, string parameterName)
        {
            WriteDebug("CheckProviderConstraints");

            // get cmdlet-level policy.
            ProviderConstraintPolicy effectivePolicy = this._defaultPolicy;

            if (pathAttrib != null && (pathAttrib.ConstraintPolicy != ProviderConstraintPolicy.Default))
            {
                effectivePolicy = pathAttrib.ConstraintPolicy;
            }

            var violations  = new Collection <Pair <PscxPathInfo, Type> >();
            var constraints = new List <Type>();

            if (_defaultProviderConstraint != null)
            {
                constraints.AddRange(_defaultProviderConstraint.ProviderTypes);
            }

            if (pathAttrib != null && (pathAttrib.ProviderTypes != null))
            {
                constraints.AddRange(pathAttrib.ProviderTypes);
            }

            // TODO: localize
            string description = String.Format(
                "The path '{0}' supplied for {1} is not contained in a provider compatible with {2}.",
                pscxPath, parameterName, this.CmdletName);

            bool constrained = false;

            Type providerType = pscxPath.Provider.ImplementingType;

            foreach (Type typeConstraint in constraints)
            {
                if (!typeConstraint.IsAssignableFrom(providerType))
                {
                    constrained = true;

                    if (effectivePolicy == ProviderConstraintPolicy.EnforceAll)
                    {
                        // enforcing all, so notify now.

                        // write verbose reason for fail
                        WriteVerbose(String.Format("The constraint check for the interface or base class '{0}' against the provider '{1}' failed.", typeConstraint,
                                                   pscxPath.Provider.Name));

                        // terminating error
                        OnPscxPathError(parameterName, description,
                                        PscxPathState.ProviderConstraintFailure, pscxPath);
                    }
                    else
                    {
                        // enforcing only one; a subsequent check may pass.
                        violations.Add(new Pair <PscxPathInfo, Type>(pscxPath, typeConstraint));
                    }
                }
                else
                {
                    constrained = false;

                    if (effectivePolicy == ProviderConstraintPolicy.EnforceOne)
                    {
                        // we passed at least one, so stop checking.
                        break;
                    }
                }
            }

            // if all checks failed (and enforcing all), then fail.
            if (constrained && (effectivePolicy == ProviderConstraintPolicy.EnforceOne))
            {
                foreach (Pair <PscxPathInfo, Type> violation in violations)
                {
                    // write out verbose reason for failure.
                    WriteVerbose(String.Format("The constraint check for the interface or base class '{0}' against the provider '{1}' failed.", violation.Second,
                                               pscxPath.Provider.Name));
                }

                // terminating error
                OnPscxPathError(parameterName, description, PscxPathState.ProviderConstraintFailure, pscxPath);
            }
        }
コード例 #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="parameterName"></param>
        /// <param name="pathAttrib"></param>
        /// <param name="pscxPath"></param>
        protected void CheckProviderConstraints(PscxPathInfo pscxPath, PscxPathAttribute pathAttrib, string parameterName)
        {
            WriteDebug("CheckProviderConstraints");

            // get cmdlet-level policy.
            ProviderConstraintPolicy effectivePolicy = this._defaultPolicy;

            if (pathAttrib != null && (pathAttrib.ConstraintPolicy != ProviderConstraintPolicy.Default))
            {
                effectivePolicy = pathAttrib.ConstraintPolicy;
            }

            var violations = new Collection<Pair<PscxPathInfo, Type>>();
            var constraints = new List<Type>();

            if (_defaultProviderConstraint != null)
            {
                constraints.AddRange(_defaultProviderConstraint.ProviderTypes);
            }

            if (pathAttrib != null && (pathAttrib.ProviderTypes != null))
            {
                constraints.AddRange(pathAttrib.ProviderTypes);
            }

            // TODO: localize
            string description = String.Format(
                "The path '{0}' supplied for {1} is not contained in a provider compatible with {2}.",
                pscxPath, parameterName, this.CmdletName);

            bool constrained = false;

            Type providerType = pscxPath.Provider.ImplementingType;

            foreach (Type typeConstraint in constraints)
            {
                if (!typeConstraint.IsAssignableFrom(providerType))
                {
                    constrained = true;

                    if (effectivePolicy == ProviderConstraintPolicy.EnforceAll)
                    {
                        // enforcing all, so notify now.

                        // write verbose reason for fail
                        WriteVerbose(String.Format("The constraint check for the interface or base class '{0}' against the provider '{1}' failed.", typeConstraint,
                                                   pscxPath.Provider.Name));

                        // terminating error
                        OnPscxPathError(parameterName, description,
                                        PscxPathState.ProviderConstraintFailure, pscxPath);
                    }
                    else
                    {
                        // enforcing only one; a subsequent check may pass.
                        violations.Add(new Pair<PscxPathInfo, Type>(pscxPath, typeConstraint));
                    }
                }
                else
                {
                    constrained = false;

                    if (effectivePolicy == ProviderConstraintPolicy.EnforceOne)
                    {
                        // we passed at least one, so stop checking.
                        break;
                    }
                }
            }

            // if all checks failed (and enforcing all), then fail.
            if (constrained && (effectivePolicy == ProviderConstraintPolicy.EnforceOne))
            {
                foreach (Pair<PscxPathInfo, Type> violation in violations)
                {
                    // write out verbose reason for failure.
                    WriteVerbose(String.Format("The constraint check for the interface or base class '{0}' against the provider '{1}' failed.", violation.Second,
                                               pscxPath.Provider.Name));
                }

                // terminating error
                OnPscxPathError(parameterName, description, PscxPathState.ProviderConstraintFailure, pscxPath);
            }
        }
コード例 #5
0
        private void ValidatePscxPath(string parameterName, PscxPathAttribute pathAttrib, PscxPathInfo pscxPath)
        {
            WriteDebug(String.Format("ValidatePscxPath: parameter {0} ; pscxPath {1}", parameterName, pscxPath));

            if (pscxPath.IsValid == false)
            {
                // todo: localize
                string description = String.Format(
                    "The path '{0}' supplied for {1} is invalid.",
                    pscxPath, parameterName);

                // path syntax error
                OnPscxPathError(parameterName, description, PscxPathState.Invalid, pscxPath);
            }

            PscxPathType pathType = PscxPathType.Unknown;

            // explicit true or unset
            if (pathAttrib.ShouldExist || (pathAttrib._shouldExist == null))
            {                
                if (pathAttrib.ShouldExist)
                {
                    // explicit true, so check existance
                    ValidateExists(parameterName, pscxPath, ref pathType);
                }
                else
                {
                    // shouldexist not specified, so grab path type via invokeprovider
                    pathType = this.InvokeProvider.Item.IsContainer(pscxPath.ToString())
                                   ? PscxPathType.Container : PscxPathType.Leaf;
                }

                PscxPathType expectedPathType = pathAttrib.PathType;

                // do we have a path type constraint?
                if (expectedPathType != PscxPathType.None)
                {
                    ValidatePathType(parameterName, pscxPath, pathType, expectedPathType);
                }
            }
            else // shouldexist explicit false
            {
                // NOTE: for Pscx developers
                Trace.Assert(pathAttrib.PathType == PscxPathType.None,
                    String.Format(
                             "Pscx Developer Error: {0}\n\nIf a PathType constraint is placed on a parameter, " +
                             "ShouldExist cannot be explicitly set to false. Remove " +
                             "the ShouldExist condition from the attribute, or set it " +
                             "explicitly to true.\n\nIf you are seeing this message " +
                             "as a Pscx end-user, please log an issue on " +
                             "http://www.codeplex.com/powershellcx", CmdletName));
            }

            // any provider constraints defined on the PscxPath attribute,
            // or on a Type-level ProviderConstraint attribute?
            if ((pathAttrib.ProviderTypes != null) || (_defaultProviderConstraint != null))
            {
                CheckProviderConstraints(pscxPath, pathAttrib, parameterName);
            }
        }