Example #1
0
        /// <summary>
        /// Determines if the item can be renamed. Derived classes that need
        /// to perform a check should override this method.
        /// </summary>
        /// <param name="item">
        /// The item to verify if it can be renamed.
        /// </param>
        /// <returns>
        /// true if the item can be renamed or false otherwise.
        /// </returns>
        internal override bool CanRenameItem(object item)
        {
            bool result = false;

            AliasInfo aliasInfo = item as AliasInfo;

            if (aliasInfo != null)
            {
                if ((aliasInfo.Options & ScopedItemOptions.Constant) != 0 ||
                    ((aliasInfo.Options & ScopedItemOptions.ReadOnly) != 0 && !Force))
                {
                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                            aliasInfo.Name,
                            SessionStateCategory.Alias,
                            "CannotRenameAlias",
                            SessionStateStrings.CannotRenameAlias);

                    throw e;
                }

                result = true;
            }

            return(result);
        }
Example #2
0
        internal override bool CanRenameItem(object item)
        {
            bool flag = false;

            switch (item)
            {
            case FunctionInfo functionInfo:
                if ((functionInfo.Options & ScopedItemOptions.Constant) != ScopedItemOptions.None || (functionInfo.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None && !(bool)this.Force)
                {
                    SessionStateUnauthorizedAccessException unauthorizedAccessException = new SessionStateUnauthorizedAccessException(functionInfo.Name, SessionStateCategory.Function, "CannotRenameFunction");
                    FunctionProvider.tracer.TraceException((Exception)unauthorizedAccessException);
                    throw unauthorizedAccessException;
                }
                flag = true;
                break;

            case FilterInfo filterInfo:
                if ((filterInfo.Options & ScopedItemOptions.Constant) != ScopedItemOptions.None || (filterInfo.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None && !(bool)this.Force)
                {
                    SessionStateUnauthorizedAccessException unauthorizedAccessException = new SessionStateUnauthorizedAccessException(filterInfo.Name, SessionStateCategory.Filter, "CannotRenameFilter");
                    FunctionProvider.tracer.TraceException((Exception)unauthorizedAccessException);
                    throw unauthorizedAccessException;
                }
                flag = true;
                break;
            }
            FunctionProvider.tracer.WriteLine("result = {0}", (object)flag);
            return(flag);
        }
Example #3
0
        } // GetValueOfItem

        /// <summary>
        /// Determines if the item can be renamed. Derived classes that need
        /// to perform a check should override this method.
        /// </summary>
        ///
        /// <param name="item">
        /// The item to verify if it can be renamed.
        /// </param>
        ///
        /// <returns>
        /// true if the item can be renamed or false otherwise.
        /// </returns>
        ///
        internal override bool CanRenameItem(object item)
        {
            bool result = false;

            PSVariable variable = item as PSVariable;

            if (variable != null)
            {
                if ((variable.Options & ScopedItemOptions.Constant) != 0 ||
                    ((variable.Options & ScopedItemOptions.ReadOnly) != 0 && !Force))
                {
                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                            variable.Name,
                            SessionStateCategory.Variable,
                            "CannotRenameVariable",
                            SessionStateStrings.CannotRenameVariable);

                    throw e;
                }

                result = true;
            }

            return(result);
        }
        /// <summary>
        /// Determines if the item can be renamed. Derived classes that need
        /// to perform a check should override this method.
        /// </summary>
        /// <param name="item">
        /// The item to verify if it can be renamed.
        /// </param>
        /// <returns>
        /// true if the item can be renamed or false otherwise.
        /// </returns>
        internal override bool CanRenameItem(object item)
        {
            bool result = false;

            FunctionInfo functionInfo = item as FunctionInfo;
            if (functionInfo != null)
            {
                if ((functionInfo.Options & ScopedItemOptions.Constant) != 0 ||
                    ((functionInfo.Options & ScopedItemOptions.ReadOnly) != 0 && !Force))
                {
                    SessionStateUnauthorizedAccessException e =
                        new SessionStateUnauthorizedAccessException(
                            functionInfo.Name,
                            SessionStateCategory.Function,
                            "CannotRenameFunction",
                            SessionStateStrings.CannotRenameFunction);

                    throw e;
                }

                result = true;
            }

            return result;
        }
Example #5
0
 private void CheckVariableCanBeRemoved(PSVariable variable)
 {
     if ((variable.ItemOptions.HasFlag(ScopedItemOptions.ReadOnly) && !Force) ||
         variable.ItemOptions.HasFlag(ScopedItemOptions.Constant))
     {
         throw SessionStateUnauthorizedAccessException.CreateVariableNotRemovableError(variable);
     }
 }
Example #6
0
 protected internal void CheckVariableCanBeChanged(PSVariable variable, bool force)
 {
     if ((variable.ItemOptions.HasFlag(ScopedItemOptions.ReadOnly) && !force) ||
         variable.ItemOptions.HasFlag(ScopedItemOptions.Constant))
     {
         throw SessionStateUnauthorizedAccessException.CreateVariableNotWritableError(variable);
     }
 }
Example #7
0
        private void CheckVariableOptionCanBeChanged(PSVariable variable)
        {
            CheckVariableCanBeChanged(variable, Force);

            if (_option == ScopedItemOptions.Constant)
            {
                throw SessionStateUnauthorizedAccessException.CreateVariableCannotBeMadeConstantError(variable);
            }
        }
Example #8
0
        internal override bool CanRenameItem(object item)
        {
            bool      flag = false;
            AliasInfo info = item as AliasInfo;

            if (info == null)
            {
                return(flag);
            }
            if (((info.Options & ScopedItemOptions.Constant) != ScopedItemOptions.None) || (((info.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None) && (base.Force == 0)))
            {
                SessionStateUnauthorizedAccessException exception = new SessionStateUnauthorizedAccessException(info.Name, SessionStateCategory.Alias, "CannotRenameAlias", SessionStateStrings.CannotRenameAlias);
                throw exception;
            }
            return(true);
        }
Example #9
0
        internal override bool CanRenameItem(object item)
        {
            bool       flag     = false;
            PSVariable variable = item as PSVariable;

            if (variable == null)
            {
                return(flag);
            }
            if (((variable.Options & ScopedItemOptions.Constant) != ScopedItemOptions.None) || (((variable.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None) && (base.Force == 0)))
            {
                SessionStateUnauthorizedAccessException exception = new SessionStateUnauthorizedAccessException(variable.Name, SessionStateCategory.Variable, "CannotRenameVariable", SessionStateStrings.CannotRenameVariable);
                throw exception;
            }
            return(true);
        }
Example #10
0
 internal override bool CanRenameItem(object item)
 {
     using (AliasProvider.tracer.TraceMethod())
     {
         bool flag = false;
         if (item is AliasInfo aliasInfo)
         {
             if ((aliasInfo.Options & ScopedItemOptions.Constant) != ScopedItemOptions.None || (aliasInfo.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None && !(bool)this.Force)
             {
                 SessionStateUnauthorizedAccessException unauthorizedAccessException = new SessionStateUnauthorizedAccessException(aliasInfo.Name, SessionStateCategory.Alias, "CannotRenameAlias");
                 AliasProvider.tracer.TraceException((Exception)unauthorizedAccessException);
                 throw unauthorizedAccessException;
             }
             flag = true;
         }
         AliasProvider.tracer.WriteLine("result = {0}", (object)flag);
         return(flag);
     }
 }
Example #11
0
        /// <summary>
        /// Sets the new definition for the alias.
        /// </summary>
        /// <param name="definition">
        /// The new definition for the alias.
        /// </param>
        /// <param name="force">
        /// If true, the value will be set even if the alias is ReadOnly.
        /// </param>
        /// <exception cref="SessionStateUnauthorizedAccessException">
        /// If the alias is readonly or constant.
        /// </exception>
        internal void SetDefinition(string definition, bool force)
        {
            // Check to see if the variable is writable

            if ((_options & ScopedItemOptions.Constant) != 0 ||
                (!force && (_options & ScopedItemOptions.ReadOnly) != 0))
            {
                SessionStateUnauthorizedAccessException e =
                    new SessionStateUnauthorizedAccessException(
                        Name,
                        SessionStateCategory.Alias,
                        "AliasNotWritable",
                        SessionStateStrings.AliasNotWritable);

                throw e;
            }

            _definition = definition;
        }
Example #12
0
 internal override bool CanRenameItem(object item)
 {
     using (VariableProvider.tracer.TraceMethod())
     {
         bool flag = false;
         if (item is PSVariable psVariable)
         {
             if ((psVariable.Options & ScopedItemOptions.Constant) != ScopedItemOptions.None || (psVariable.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None && !(bool)this.Force)
             {
                 SessionStateUnauthorizedAccessException unauthorizedAccessException = new SessionStateUnauthorizedAccessException(psVariable.Name, SessionStateCategory.Variable, "CannotRenameVariable");
                 VariableProvider.tracer.TraceException((Exception)unauthorizedAccessException);
                 throw unauthorizedAccessException;
             }
             flag = true;
         }
         VariableProvider.tracer.WriteLine("result = {0}", (object)flag);
         return(flag);
     }
 }
Example #13
0
        /// <summary>
        /// Removes the current scope from the scope tree and
        /// changes the current scope to the parent scope.
        /// </summary>
        /// <param name="scope">
        /// The scope to cleanup and remove.
        /// </param>
        /// <exception cref="SessionStateUnauthorizedAccessException">
        /// The global scope cannot be removed.
        /// </exception>
        internal void RemoveScope(SessionStateScope scope)
        {
            Diagnostics.Assert(
                _currentScope != null,
                "The currentScope should always be set.");

            if (scope == GlobalScope)
            {
                SessionStateUnauthorizedAccessException e =
                    new SessionStateUnauthorizedAccessException(
                        StringLiterals.Global,
                        SessionStateCategory.Scope,
                        "GlobalScopeCannotRemove",
                        SessionStateStrings.GlobalScopeCannotRemove);

                throw e;
            }

            // Give the provider a chance to cleanup the drive data associated
            // with drives in this scope

            foreach (PSDriveInfo drive in scope.Drives)
            {
                if (drive == null)
                {
                    continue;
                }

                CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext);

                // Call CanRemoveDrive to give the provider a chance to cleanup
                // but ignore the return value and exceptions

                try
                {
                    CanRemoveDrive(drive, context);
                }
                catch (LoopFlowException)
                {
                    throw;
                }
                catch (PipelineStoppedException)
                {
                    throw;
                }
                catch (ActionPreferenceStopException)
                {
                    throw;
                }
                catch (Exception) // Catch-all OK, 3rd party callout.
                {
                    // Ignore all exceptions from the provider as we are
                    // going to force the removal anyway
                }
            }

            scope.RemoveAllDrives();

            // If the scope being removed is the current scope,
            // then it must be removed from the tree.

            if (scope == _currentScope && _currentScope.Parent != null)
            {
                _currentScope = _currentScope.Parent;
            }

            scope.Parent = null;
        }
Example #14
0
        /// <summary>
        /// Sets the options for the alias and allows changes ReadOnly options only if force is specified.
        /// </summary>
        /// <param name="newOptions">
        /// The new options value.
        /// </param>
        /// <param name="force">
        /// If true the change to the options will happen even if the existing options are read-only.
        /// </param>
        internal void SetOptions(ScopedItemOptions newOptions, bool force)
        {
            // Check to see if the variable is constant, if so
            // throw an exception because the options cannot be changed.

            if ((_options & ScopedItemOptions.Constant) != 0)
            {
                SessionStateUnauthorizedAccessException e =
                    new SessionStateUnauthorizedAccessException(
                        Name,
                        SessionStateCategory.Alias,
                        "AliasIsConstant",
                        SessionStateStrings.AliasIsConstant);

                throw e;
            }

            // Check to see if the variable is readonly, if so
            // throw an exception because the options cannot be changed.

            if (!force && (_options & ScopedItemOptions.ReadOnly) != 0)
            {
                SessionStateUnauthorizedAccessException e =
                    new SessionStateUnauthorizedAccessException(
                        Name,
                        SessionStateCategory.Alias,
                        "AliasIsReadOnly",
                        SessionStateStrings.AliasIsReadOnly);

                throw e;
            }

            // Now check to see if the caller is trying to set
            // the options to constant. This is only allowed at
            // variable creation

            if ((newOptions & ScopedItemOptions.Constant) != 0)
            {
                // user is trying to set the variable to constant after
                // creating the variable. Do not allow this (as per spec).

                SessionStateUnauthorizedAccessException e =
                    new SessionStateUnauthorizedAccessException(
                        Name,
                        SessionStateCategory.Alias,
                        "AliasCannotBeMadeConstant",
                        SessionStateStrings.AliasCannotBeMadeConstant);

                throw e;
            }

            if ((newOptions & ScopedItemOptions.AllScope) == 0 &&
                (_options & ScopedItemOptions.AllScope) != 0)
            {
                // user is trying to remove the AllScope option from the alias.
                // Do not allow this (as per spec).

                SessionStateUnauthorizedAccessException e =
                    new SessionStateUnauthorizedAccessException(
                        this.Name,
                        SessionStateCategory.Alias,
                        "AliasAllScopeOptionCannotBeRemoved",
                        SessionStateStrings.AliasAllScopeOptionCannotBeRemoved);

                throw e;
            }

            _options = newOptions;
        }