Пример #1
0
        private void UpdateWarning()
        {
            var isSessionElevated = SessionElevationManager.IsSessionTokenElevated(ApplicationNames.Console);

            var controlContent = string.Empty;
            var hidePanel      = false;
            var tokenAction    = SessionElevationManager.GetToken(ApplicationNames.Console).Action;

            switch (tokenAction)
            {
            case (SessionElevationManager.TokenDefinition.ElevationAction.Allow):
                // it is always elevated
                hidePanel = true;
                break;

            case (SessionElevationManager.TokenDefinition.ElevationAction.Password):
                // show that session elevation can be dropped
                controlContent = HtmlUtil.RenderControl(isSessionElevated ? ElevatedPanel : ElevationRequiredPanel);
                break;

            case (SessionElevationManager.TokenDefinition.ElevationAction.Block):
                controlContent = HtmlUtil.RenderControl(ElevationBlockedPanel);
                break;
            }

            InfoPanel.InnerHtml = controlContent;
            InfoPanel.Visible   = !hidePanel;
            SheerResponse.Eval($"cognifide.powershell.showInfoPanel({(!hidePanel).ToString().ToLower()});");
        }
Пример #2
0
        public object ExecuteCommand(string guid, string command, string stringFormat)
        {
            var serializer = new JavaScriptSerializer();
            var output     = new StringBuilder();

            if (!IsLoggedInUserAuthorized ||
                !SessionElevationManager.IsSessionTokenElevated(ApplicationNames.Console))
            {
                return(serializer.Serialize(
                           new
                {
                    status = StatusElevationRequired,
                    result =
                        "You need to be authenticated, elevated and have sufficient privileges to use the PowerShell console. Please (re)login to Sitecore.",
                    prompt = "PS >",
                    background = OutputLine.ProcessHtmlColor(ConsoleColor.DarkBlue)
                }));
            }

            PowerShellLog.Info($"Arbitrary script execution in Console session '{guid}' by user: '******'");

            var session = GetScriptSession(guid);

            session.Interactive = true;
            session.SetItemContextFromLocation();
            try
            {
                var handle     = ID.NewID.ToString();
                var jobOptions = new JobOptions(GetJobId(guid, handle), "PowerShell", "shell", this, nameof(RunJob),
                                                new object[] { session, command })
                {
                    AfterLife      = new TimeSpan(0, 0, 20),
                    ContextUser    = Sitecore.Context.User,
                    EnableSecurity = true,
                    ClientLanguage = Sitecore.Context.ContentLanguage
                };
                JobManager.Start(jobOptions);
                Thread.Sleep(WebServiceSettings.CommandWaitMillis);
                return(PollCommandOutput(guid, handle, stringFormat));
            }
            catch (Exception ex)
            {
                return
                    (serializer.Serialize(
                         new Result
                {
                    status = StatusError,
                    result =
                        output +
                        ScriptSession.GetExceptionString(ex, ScriptSession.ExceptionStringFormat.Console) +
                        "\r\n" +
                        "\r\n[[;#f00;#000]Uh oh, looks like the command you ran is invalid or something else went wrong. Is it something we should know about?]\r\n" +
                        "[[;#f00;#000]Please submit a support ticket here https://git.io/spe with error details, screenshots, and anything else that might help.]\r\n\r\n" +
                        "[[;#f00;#000]We also have a user guide here http://doc.sitecorepowershell.com/.]\r\n\r\n",
                    prompt = $"PS {session.CurrentLocation}>",
                    background = OutputLine.ProcessHtmlColor(session.PrivateData.BackgroundColor),
                    color = OutputLine.ProcessHtmlColor(session.PrivateData.ForegroundColor)
                }));
            }
        }
Пример #3
0
        public object GetAutoCompletionPrefix(string guid, string command)
        {
            if (!IsLoggedInUserAuthorized ||
                !SessionElevationManager.IsSessionTokenElevated(ApplicationNames.ISE))
            {
                return(string.Empty);
            }

            PowerShellLog.Info($"Auto completion requested in session '{guid}' by user: '******'");

            var serializer = new JavaScriptSerializer();
            var session    = GetScriptSession(guid);

            try
            {
                var result = serializer.Serialize(CommandCompletion.GetPrefix(session, command));
                return(result);
            }
            finally
            {
                if (string.IsNullOrEmpty(guid))
                {
                    ScriptSessionManager.RemoveSession(session);
                }
            }
        }
Пример #4
0
        public object GetHelpForCommand(string guid, string command)
        {
            if (!IsLoggedInUserAuthorized ||
                !SessionElevationManager.IsSessionTokenElevated(ApplicationNames.ISE))
            {
                return(string.Empty);
            }

            PowerShellLog.Info($"Help message requested in session '{guid}' by user: '******'");

            var serializer = new JavaScriptSerializer();
            var result     = serializer.Serialize(GetHelpOutputs(guid, command));

            return(result);
        }
Пример #5
0
        public object CompleteCommand(string guid, string command)
        {
            if (!IsLoggedInUserAuthorized ||
                !SessionElevationManager.IsSessionTokenElevated(ApplicationNames.Console))
            {
                return(string.Empty);
            }

            PowerShellLog.Info($"Auto completion requested for command in Console session '{guid}' by user: '******'");

            var serializer = new JavaScriptSerializer();
            var result     = serializer.Serialize(GetTabCompletionOutputs(guid, command, false));

            return(result);
        }
Пример #6
0
 public void SessionElevationPipeline(ClientPipelineArgs args)
 {
     if (!args.IsPostBack)
     {
         var url = new UrlString(UIUtil.GetUri("control:PowerShellSessionElevation"));
         url.Parameters["app"] = ApplicationNames.Console;
         TypeResolver.Resolve <ISessionElevationWindowLauncher>().ShowSessionElevationWindow(url);
         args.WaitForPostBack(true);
     }
     else
     {
         SheerResponse.Eval(SessionElevationManager.IsSessionTokenElevated(ApplicationNames.Console)
             ? @"$ise(function() { cognifide.powershell.bootstrap(); });"
             : @"$ise(function() { cognifide.powershell.showUnelevated(); });");
     }
 }
Пример #7
0
        private void UpdateWarning(string updateFromMessage = "")
        {
            var isSessionElevated = SessionElevationManager.IsSessionTokenElevated(ApplicationNames.ISE);

            var controlContent = string.Empty;
            var hidePanel      = false;
            var tokenAction    = SessionElevationManager.GetToken(ApplicationNames.ISE).Action;

            switch (tokenAction)
            {
            case SessionElevationManager.TokenDefinition.ElevationAction.Allow:
                // it is always elevated
                hidePanel = true;
                break;

            case SessionElevationManager.TokenDefinition.ElevationAction.Password:
            case SessionElevationManager.TokenDefinition.ElevationAction.Confirm:
                // show that session elevation can be dropped
                if (isSessionElevated)
                {
                    controlContent = HtmlUtil.RenderControl(ElevatedPanel);
                }
                else
                {
                    if (WasElevated)
                    {
                        // we're cool devs know that session will need to be elevated.
                        hidePanel = true;
                    }
                    else
                    {
                        controlContent = HtmlUtil.RenderControl(ElevationRequiredPanel);
                    }
                }
                break;

            case SessionElevationManager.TokenDefinition.ElevationAction.Block:
                controlContent = HtmlUtil.RenderControl(ElevationBlockedPanel);
                break;
            }

            InfoPanel.InnerHtml = controlContent;
            InfoPanel.Visible   = !hidePanel;
            SheerResponse.Eval($"spe.showInfoPanel({(!hidePanel).ToString().ToLower()}, '{updateFromMessage}');");
        }
Пример #8
0
 private bool RequestSessionElevationEx(ClientPipelineArgs args, string appName, string action)
 {
     if (!SessionElevationManager.IsSessionTokenElevated(appName))
     {
         if (args.Parameters.AllKeys.Contains("elevationResult"))
         {
             SessionElevationErrors.OperationRequiresElevation();
             return(false);
         }
         var pipelineArgs = new ClientPipelineArgs();
         pipelineArgs.Parameters["message"] = args.Parameters["message"];
         pipelineArgs.Parameters["app"]     = appName;
         pipelineArgs.Parameters["action"]  = action;
         Context.ClientPage.Start(this, nameof(SessionElevationPipeline), pipelineArgs);
         return(false);
     }
     return(true);
 }
Пример #9
0
        protected virtual void ElevateSession(ClientPipelineArgs args)
        {
            var isSessionElevated = SessionElevationManager.IsSessionTokenElevated(ApplicationNames.Console);
            var tokenAction       = SessionElevationManager.GetToken(ApplicationNames.Console).Action;

            if (!isSessionElevated)
            {
                if (tokenAction == SessionElevationManager.TokenDefinition.ElevationAction.Block)
                {
                    SheerResponse.Eval(@"$ise(function() { cognifide.powershell.bootstrap(true); });");
                }
                else
                {
                    Context.ClientPage.Start(this, nameof(SessionElevationPipeline));
                }
            }
            else
            {
                SheerResponse.Eval(@"$ise(function() { cognifide.powershell.bootstrap(false); });");
            }
        }