/********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ using System.Collections.Generic; using System.Management.Automation.Runspaces; using Dbg = System.Management.Automation; namespace System.Management.Automation { /// /// Exposes the APIs to manipulate the providers, Runspace data, and location to the Cmdlet base class. /// public sealed class SessionState { #region Constructors /// /// The internal constructor for this object. It should be the only one that gets called. /// /// /// /// An instance of SessionState that the APIs should work against. /// /// /// /// If is null. /// /// internal SessionState(SessionStateInternal sessionState) { if (sessionState == null) { throw PSTraceSource.NewArgumentNullException("sessionState"); } _sessionState = sessionState; #if RELATIONSHIP_SUPPORTED // 2004/11/24-JeffJon - Relationships have been removed from the Exchange release this.relationship = new RelationshipProviderManagementIntrinsics (sessionState); #endif } // SessionState /// /// The internal constructor for this object. It should be the only one that gets called. /// /// /// /// An instance of ExecutionContext whose EngineSessionState represents the parent session state. /// /// /// True if the session state should be created as a child session state. /// /// /// True if the session state should be linked to the global scope. /// /// /// /// If is null. /// internal SessionState(ExecutionContext context, bool createAsChild, bool linkToGlobal) { if (context == null) throw new InvalidOperationException("ExecutionContext"); if (createAsChild) { _sessionState = new SessionStateInternal(context.EngineSessionState, linkToGlobal, context); } else { _sessionState = new SessionStateInternal(context); } _sessionState.PublicSessionState = this; } // SessionState /// /// Construct a new session state object... /// public SessionState() { ExecutionContext ecFromTLS = LocalPipeline.GetExecutionContextFromTLS(); if (ecFromTLS == null) throw new InvalidOperationException("ExecutionContext"); _sessionState = new SessionStateInternal(ecFromTLS); _sessionState.PublicSessionState = this; } #endregion Constructors #region Public methods /// /// Gets the APIs to access drives /// public DriveManagementIntrinsics Drive { get { return _drive ?? (_drive = new DriveManagementIntrinsics(_sessionState)); } } /// /// Gets the APIs to access providers /// public CmdletProviderManagementIntrinsics Provider { get { return _provider ?? (_provider = new CmdletProviderManagementIntrinsics(_sessionState)); } // get } /// /// Gets the APIs to access paths and location /// public PathIntrinsics Path { get { return _path ?? (_path = new PathIntrinsics(_sessionState)); } } /// /// Gets the APIs to access variables in session state. /// public PSVariableIntrinsics PSVariable { get { return _variable ?? (_variable = new PSVariableIntrinsics(_sessionState)); } // get } /// /// Get/set constraints for this execution environment /// public PSLanguageMode LanguageMode { get { return _sessionState.LanguageMode; } set { _sessionState.LanguageMode = value; } } /// /// If true the PowerShell debugger will use FullLanguage mode, otherwise it will use the current language mode /// public bool UseFullLanguageModeInDebugger { get { return _sessionState.UseFullLanguageModeInDebugger; } } /// /// Public proxy for the list of scripts that are allowed to be run. If the name "*" /// is in the list, then all scripts can be run. (This is the default.) /// public List Scripts { get { return _sessionState.Scripts; } } /// /// Public proxy for the list of applications that are allowed to be run. If the name "*" /// is in the list, then all applications can be run. (This is the default.) /// public List Applications { get { return _sessionState.Applications; } } /// /// The module associated with this session state instance... /// public PSModuleInfo Module { get { return _sessionState.Module; } } /// /// The provider intrinsics for this session state instance. /// public ProviderIntrinsics InvokeProvider { get { return _sessionState.InvokeProvider; } } /// /// The command invocation intrinsics for this session state instance. /// public CommandInvocationIntrinsics InvokeCommand { get { return _sessionState.ExecutionContext.EngineIntrinsics.InvokeCommand; } } /// /// Utility to check the visibility of an object based on the current /// command origin. If the object implements IHasSessionStateEntryVisibility /// then the check will be made. If the check fails, then an exception will be thrown... /// /// The command origin value to check against... /// The object to check public static void ThrowIfNotVisible(CommandOrigin origin, object valueToCheck) { SessionStateException exception; if (!IsVisible(origin, valueToCheck)) { PSVariable sv = valueToCheck as PSVariable; if (sv != null) { exception = new SessionStateException( sv.Name, SessionStateCategory.Variable, "VariableIsPrivate", SessionStateStrings.VariableIsPrivate, ErrorCategory.PermissionDenied); throw exception; } CommandInfo cinfo = valueToCheck as CommandInfo; if (cinfo != null) { string commandName = null; if (cinfo != null) commandName = cinfo.Name; if (commandName != null) { // If we have a name, use it in the error message exception = new SessionStateException( commandName, SessionStateCategory.Command, "NamedCommandIsPrivate", SessionStateStrings.NamedCommandIsPrivate, ErrorCategory.PermissionDenied); } else { exception = new SessionStateException( "", SessionStateCategory.Command, "CommandIsPrivate", SessionStateStrings.CommandIsPrivate, ErrorCategory.PermissionDenied); } throw exception; } // Catch all error for other types of resources... exception = new SessionStateException( null, SessionStateCategory.Resource, "ResourceIsPrivate", SessionStateStrings.ResourceIsPrivate, ErrorCategory.PermissionDenied); throw exception; } } /// /// Checks the visibility of an object based on the command origin argument. /// /// The origin to check against /// The object to check /// Returns true if the object is visible, false otherwise public static bool IsVisible(CommandOrigin origin, object valueToCheck) { if (origin == CommandOrigin.Internal) return true; IHasSessionStateEntryVisibility obj = valueToCheck as IHasSessionStateEntryVisibility; if (obj != null) { return (obj.Visibility == SessionStateEntryVisibility.Public); } return true; } /// /// Checks the visibility of an object based on the command origin argument. /// /// The origin to check against /// The variable to check /// Returns true if the object is visible, false otherwise public static bool IsVisible(CommandOrigin origin, PSVariable variable) { if (origin == CommandOrigin.Internal) return true; if (variable == null) { throw PSTraceSource.NewArgumentNullException("variable"); } return (variable.Visibility == SessionStateEntryVisibility.Public); } /// /// Checks the visibility of an object based on the command origin argument. /// /// The origin to check against /// The command to check /// Returns true if the object is visible, false otherwise public static bool IsVisible(CommandOrigin origin, CommandInfo commandInfo) { if (origin == CommandOrigin.Internal) return true; if (commandInfo == null) { throw PSTraceSource.NewArgumentNullException("commandInfo"); } return (commandInfo.Visibility == SessionStateEntryVisibility.Public); } #if RELATIONSHIP_SUPPORTED // 2004/11/24-JeffJon - Relationships have been removed from the Exchange release /// /// The state APIs to access relationship providers in session state. /// /// public RelationshipProviderManagementIntrinsics Relationship { get { using (tracer.TraceProperty()) { Dbg.Diagnostics.Assert ( relationship != null, "The only constructor for this class should always set the relationship field"); return relationship; } // TraceProperty } // get } // RelationshipProvider #endif #endregion Public methods #region Internal methods /// /// Gets a reference to the "real" session state object instead of the facade /// /// internal SessionStateInternal Internal { get { return _sessionState; } } // Internal #endregion Internal methods #region private data private SessionStateInternal _sessionState; private DriveManagementIntrinsics _drive; private CmdletProviderManagementIntrinsics _provider; private PathIntrinsics _path; private PSVariableIntrinsics _variable; #if RELATIONSHIP_SUPPORTED // 2004/11/24-JeffJon - Relationships have been removed from the Exchange release private RelationshipProviderManagementIntrinsics relationship = null; #endif #endregion private data } // SessionStatePublic /// /// This enum defines the visibility of execution environment elements... /// public enum SessionStateEntryVisibility { /// /// Entries are visible to requests from outside the runspace /// Public = 0, /// /// Entries are not visible to requests from outside the runspace /// Private = 1 } internal interface IHasSessionStateEntryVisibility { SessionStateEntryVisibility Visibility { get; set; } } /// /// This enum defines what subset of the PowerShell language is permitted when /// calling into this execution environment. /// public enum PSLanguageMode { /// /// All PowerShell language elements are available /// FullLanguage = 0, /// /// A subset of language elements are available to external requests /// RestrictedLanguage = 1, /// /// Commands containing script text to evaluate are not allowed. You can only /// call commands using the Runspace APIs when in this mode. /// NoLanguage = 2, /// /// Exposes a subset of the PowerShell language that limits itself to core PowerShell /// types, does not support method invocation (except on those types), and does not /// support property setters (except on those types). /// ConstrainedLanguage = 3 } }