Пример #1
0
        public void Process(GetContentEditorWarningsArgs args)
        {
            Assert.ArgumentNotNull(args, "args");

            if (!args.Item.IsPowerShellScript() && !args.Item.IsPowerShellLibrary() && !args.Item.InheritsFrom(Templates.ScriptModule.Id))
            {
                return;
            }

            var action = SessionElevationManager.GetToken(ApplicationNames.ItemSave).Action;

            var warning = new GetContentEditorWarningsArgs.ContentEditorWarning();

            switch (action)
            {
            case SessionElevationManager.TokenDefinition.ElevationAction.Password:
            case SessionElevationManager.TokenDefinition.ElevationAction.Confirm:
                if (SessionElevationManager.IsSessionTokenElevated(ApplicationNames.ItemSave))
                {
                    warning.Title = "You have temporarily enabled script viewing and editing.";
                    warning.Text  =
                        "Drop access if you no longer require it. For more information, refer to our <a href=\"https://sitecorepowershell.com/session-state-elevation/\" class=\"scEditorWarningOption\" target=\"_blank\">Documentation.</a>";
                    warning.AddOption("Drop access", "item:dropelevatescriptedit");
                    args.Warnings.Add(warning);
                }
                else
                {
                    warning.HideFields = true;
                    warning.Title      = "Elevated session state is required to view and edit scripts.";
                    warning.Text       =
                        "A security dialog will prompt you for your credentials before allowing access to view and edit scripts. For more information, refer to our <a href=\"https://sitecorepowershell.com/session-state-elevation/\" class=\"scEditorWarningOption\" target=\"_blank\">Documentation.</a>";
                    warning.AddOption("Elevate session", "item:elevatescriptedit");
                    args.Warnings.Add(warning);
                }
                break;

            case SessionElevationManager.TokenDefinition.ElevationAction.Block:
                warning.HideFields = true;
                warning.Title      = "Elevated session state is blocked. Access to view and edit scripts is disabled.";
                warning.Text       =
                    "For more information, refer to our <a href=\"https://sitecorepowershell.com/session-state-elevation/\" class=\"scEditorWarningOption\" target=\"_blank\">Documentation.</a>";
                args.Warnings.Add(warning);
                break;
            }
        }
        public void OnEvent(object sender, EventArgs args)
        {
            Item item = null;

            if (!(args is SitecoreEventArgs scArgs) || HttpContext.Current?.Session == null || scArgs.Parameters.Length < 1 ||
                SecurityDisabler.CurrentValue == SecurityState.Disabled)
            {
                // allow jobs to modify scripts as otherwise all kind of things break
                // allow modifying scripts when SecurityDisabler is active - needed for Update Packages to function
                return;
            }

            item = scArgs.Parameters[0] as Item;
            if (item != null && !item.IsPowerShellScript() && !item.IsPowerShellLibrary())
            {
                // not a PowerShell related item
                return;
            }

            var itemCreatingEventArgs = scArgs.Parameters[0] as ItemCreatingEventArgs;

            if (itemCreatingEventArgs?.Parent?.Database != null && itemCreatingEventArgs.TemplateId != (ID)null)
            {
                var template = TemplateManager.GetTemplate(itemCreatingEventArgs.TemplateId,
                                                           itemCreatingEventArgs.Parent.Database);
                if (template == null || (!template.InheritsFrom(Templates.Script.Id) &&
                                         !template.InheritsFrom(Templates.ScriptLibrary.Id)))
                {
                    // not creating Script or Library
                    return;
                }
            }


            if (!SessionElevationManager.IsSessionTokenElevated(ApplicationNames.ItemSave))
            {
                SessionElevationErrors.OperationRequiresElevation();

                if (itemCreatingEventArgs != null)
                {
                    itemCreatingEventArgs.Cancel = true;
                    PowerShellLog.Warn(
                        $"Prevented Script/Library '{itemCreatingEventArgs.Parent?.Paths?.Path}/{itemCreatingEventArgs.ItemName}' creation by '{Context.User?.Name}'.");
                }
                else
                {
                    PowerShellLog.Warn(
                        $"Prevented Script/Library save '{item?.Parent.Paths.Path}' by user '{Context.User?.Name}'.");
                }

                scArgs.Result.Cancel = true;
                scArgs.Result.Messages.Add("Item save prevented");
                return;
            }

            if (itemCreatingEventArgs != null)
            {
                PowerShellLog.Info(
                    $" Script/Library '{itemCreatingEventArgs.Parent?.Paths?.Path}/{itemCreatingEventArgs.ItemName}' created by user '{Context.User?.Name}'");
            }
            else
            {
                PowerShellLog.Info(
                    $" Script/Library saved '{item?.Parent.Paths.Path}' by user '{Context.User?.Name}'");
                if (item.IsPowerShellScript())
                {
                    PowerShellLog.Debug(item[Templates.Script.Fields.ScriptBody]);
                }
            }
        }
 public override void Execute(CommandContext context)
 {
     SessionElevationManager.DropSessionTokenElevation(ApplicationNames.ItemSave);
     Context.ClientPage.SendMessage(this, $"item:refresh(id={context.Items[0]})");
 }