internal AliasInfo SetAliasValue(string name, string value, System.Management.Automation.ExecutionContext context, bool force, CommandOrigin origin) { if (!this.GetAliases().ContainsKey(name)) { if (this.GetAliases().Count > (this.AliasCapacity.FastValue - 1)) { SessionStateOverflowException exception = new SessionStateOverflowException(name, SessionStateCategory.Alias, "AliasOverflow", SessionStateStrings.AliasOverflow, new object[] { this.AliasCapacity.FastValue }); throw exception; } this.GetAliases()[name] = new AliasInfo(name, value, context); } else { AliasInfo valueToCheck = this.GetAliases()[name]; if (((valueToCheck.Options & ScopedItemOptions.Constant) != ScopedItemOptions.None) || (!force && ((valueToCheck.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None))) { SessionStateUnauthorizedAccessException exception2 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Alias, "AliasNotWritable", SessionStateStrings.AliasNotWritable); throw exception2; } SessionState.ThrowIfNotVisible(origin, valueToCheck); this.RemoveAliasFromCache(valueToCheck.Name, valueToCheck.Definition); if (force) { this.GetAliases().Remove(name); valueToCheck = new AliasInfo(name, value, context); this.GetAliases()[name] = valueToCheck; } else { valueToCheck.SetDefinition(value, false); } } this.AddAliasToCache(name, value); return(this.GetAliases()[name]); }
internal AliasInfo SetAliasItem(AliasInfo aliasToSet, bool force, CommandOrigin origin = CommandOrigin.Internal) { if (!this.GetAliases().ContainsKey(aliasToSet.Name)) { if (this.GetAliases().Count > (this.AliasCapacity.FastValue - 1)) { SessionStateOverflowException exception = new SessionStateOverflowException(aliasToSet.Name, SessionStateCategory.Alias, "AliasOverflow", SessionStateStrings.AliasOverflow, new object[] { this.AliasCapacity.FastValue }); throw exception; } this.GetAliases()[aliasToSet.Name] = aliasToSet; } else { AliasInfo valueToCheck = this.GetAliases()[aliasToSet.Name]; SessionState.ThrowIfNotVisible(origin, valueToCheck); if (((valueToCheck.Options & ScopedItemOptions.Constant) != ScopedItemOptions.None) || (((valueToCheck.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None) && !force)) { SessionStateUnauthorizedAccessException exception2 = new SessionStateUnauthorizedAccessException(aliasToSet.Name, SessionStateCategory.Alias, "AliasNotWritable", SessionStateStrings.AliasNotWritable); throw exception2; } if (((aliasToSet.Options & ScopedItemOptions.AllScope) == ScopedItemOptions.None) && ((valueToCheck.Options & ScopedItemOptions.AllScope) != ScopedItemOptions.None)) { SessionStateUnauthorizedAccessException exception3 = new SessionStateUnauthorizedAccessException(aliasToSet.Name, SessionStateCategory.Alias, "AliasAllScopeOptionCannotBeRemoved", SessionStateStrings.AliasAllScopeOptionCannotBeRemoved); throw exception3; } this.RemoveAliasFromCache(valueToCheck.Name, valueToCheck.Definition); this.GetAliases()[aliasToSet.Name] = aliasToSet; } this.AddAliasToCache(aliasToSet.Name, aliasToSet.Definition); return(this.GetAliases()[aliasToSet.Name]); }
protected override bool GetScopeItem(SessionStateScope scope, VariablePath path, out FunctionInfo script) { bool flag = true; this.name = path.IsFunction ? path.UnqualifiedPath : path.QualifiedName; script = scope.GetFunction(this.name); if (script != null) { bool flag2; FilterInfo info = script as FilterInfo; if (info != null) { flag2 = (info.Options & ScopedItemOptions.Private) != ScopedItemOptions.None; } else { flag2 = (script.Options & ScopedItemOptions.Private) != ScopedItemOptions.None; } if (flag2 && (scope != base.sessionState.CurrentScope)) { return(false); } SessionState.ThrowIfNotVisible(this._origin, script); return(flag); } return(false); }
internal FunctionInfo SetFunction(string name, ScriptBlock function, FunctionInfo originalFunction, ScopedItemOptions options, bool force, CommandOrigin origin, System.Management.Automation.ExecutionContext context, string helpFile, Func <string, ScriptBlock, FunctionInfo, ScopedItemOptions, System.Management.Automation.ExecutionContext, string, FunctionInfo> functionFactory) { if (!this.GetFunctions().ContainsKey(name)) { if (this.GetFunctions().Count > (this.FunctionCapacity.FastValue - 1)) { SessionStateOverflowException exception = new SessionStateOverflowException(name, SessionStateCategory.Function, "FunctionOverflow", SessionStateStrings.FunctionOverflow, new object[] { this.FunctionCapacity.FastValue }); throw exception; } FunctionInfo info = functionFactory(name, function, originalFunction, options, context, helpFile); this.GetFunctions()[name] = info; if (IsFunctionOptionSet(info, ScopedItemOptions.AllScope)) { this.GetAllScopeFunctions()[name] = info; } } else { FunctionInfo valueToCheck = this.GetFunctions()[name]; SessionState.ThrowIfNotVisible(origin, valueToCheck); if (IsFunctionOptionSet(valueToCheck, ScopedItemOptions.Constant) || (!force && IsFunctionOptionSet(valueToCheck, ScopedItemOptions.ReadOnly))) { SessionStateUnauthorizedAccessException exception2 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Function, "FunctionNotWritable", SessionStateStrings.FunctionNotWritable); throw exception2; } if ((options & ScopedItemOptions.Constant) != ScopedItemOptions.None) { SessionStateUnauthorizedAccessException exception3 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Function, "FunctionCannotBeMadeConstant", SessionStateStrings.FunctionCannotBeMadeConstant); throw exception3; } if (((options & ScopedItemOptions.AllScope) == ScopedItemOptions.None) && IsFunctionOptionSet(valueToCheck, ScopedItemOptions.AllScope)) { SessionStateUnauthorizedAccessException exception4 = new SessionStateUnauthorizedAccessException(name, SessionStateCategory.Function, "FunctionAllScopeOptionCannotBeRemoved", SessionStateStrings.FunctionAllScopeOptionCannotBeRemoved); throw exception4; } FunctionInfo info3 = valueToCheck; FunctionInfo newFunction = null; if (info3 != null) { newFunction = functionFactory(name, function, originalFunction, options, context, helpFile); if (!info3.GetType().Equals(newFunction.GetType()) || (((info3.Options & ScopedItemOptions.ReadOnly) != ScopedItemOptions.None) && force)) { this.GetFunctions()[name] = newFunction; } else { bool flag2 = force || ((options & ScopedItemOptions.ReadOnly) == ScopedItemOptions.None); info3.Update(newFunction, flag2, options, helpFile); } } } return(this.GetFunctions()[name]); }
internal bool TryGetVariable(string name, CommandOrigin origin, bool fromNewOrSet, out PSVariable variable) { if (this.TryGetLocalVariableFromTuple(name, fromNewOrSet, out variable)) { SessionState.ThrowIfNotVisible(origin, variable); return(true); } if (this.GetPrivateVariables().TryGetValue(name, out variable)) { SessionState.ThrowIfNotVisible(origin, variable); return(true); } return(false); }
/// <summary> /// Derived classes override this method to return their /// particular type of scoped item. /// </summary> /// <param name="scope"> /// The scope to look the item up in. /// </param> /// <param name="path"> /// The name of the item to retrieve. /// </param> /// <param name="script"> /// The scope item that the derived class should return. /// </param> /// <returns> /// True if the scope item was found or false otherwise. /// </returns> protected override bool GetScopeItem( SessionStateScope scope, VariablePath path, out FunctionInfo script) { Diagnostics.Assert(path is FunctionLookupPath, "name was scanned incorrect if we get here and it is not a FunctionLookupPath"); bool result = true; _name = path.IsFunction ? path.UnqualifiedPath : path.QualifiedName; script = scope.GetFunction(_name); if (script != null) { bool isPrivate; FilterInfo filterInfo = script as FilterInfo; if (filterInfo != null) { isPrivate = (filterInfo.Options & ScopedItemOptions.Private) != 0; } else { isPrivate = (script.Options & ScopedItemOptions.Private) != 0; } // If the function is private and the lookup scope // isn't the current scope, claim that the function // doesn't exist so that the lookup continues. if (isPrivate && scope != sessionState.CurrentScope) { result = false; } else { // Now check the visibility of the variable... SessionState.ThrowIfNotVisible(_origin, script); } } else { result = false; } return(result); }
/// <summary> /// Gets the value of the specified alias from the alias table. /// </summary> /// /// <param name="aliasName"> /// The name of the alias value to retrieve. /// </param> /// /// <param name="origin"> /// The origin of the command calling this API. /// </param> /// <returns> /// The AliasInfo representing the alias. /// </returns> /// internal AliasInfo GetAlias(string aliasName, CommandOrigin origin) { AliasInfo result = null; if (String.IsNullOrEmpty(aliasName)) { return(null); } // Use the scope enumerator to find the alias using the // appropriate scoping rules SessionStateScopeEnumerator scopeEnumerator = new SessionStateScopeEnumerator(_currentScope); foreach (SessionStateScope scope in scopeEnumerator) { result = scope.GetAlias(aliasName); if (result != null) { // Now check the visibility of the variable... SessionState.ThrowIfNotVisible(origin, result); // Make sure the alias isn't private or if it is that the current // scope is the same scope the alias was retrieved from. if ((result.Options & ScopedItemOptions.Private) != 0 && scope != _currentScope) { result = null; } else { break; } } } return(result); } // GetAlias