Example #1
0
        private async Task ApproveContent(string values, string approvalComment)
        {
            if (!string.IsNullOrEmpty(values))
            {
                var ids = values.Split(',');
                foreach (var id in ids)
                {
                    int.TryParse(id, out var approvalId);
                    if (approvalId != 0)
                    {
                        var approval = await _approvalRepository.GetAsync(approvalId);

                        await _approvalEngine.ForceApproveAsync(approvalId, PrincipalInfo.CurrentPrincipal.Identity.Name, approvalComment);

                        if (approval is ContentApproval contentApproval)
                        {
                            _contentRepository.TryGet(contentApproval.ContentLink, out IContent content);

                            var canUserPublish = await _helper.CanUserPublish(content);

                            if (content != null && canUserPublish)
                            {
                                switch (content)
                                {
                                case PageData page:
                                {
                                    var clone = page.CreateWritableClone();
                                    _contentRepository.Save(clone, SaveAction.Publish, AccessLevel.Publish);
                                    break;
                                }

                                case BlockData block:
                                {
                                    var clone = block.CreateWritableClone() as IContent;
                                    _contentRepository.Save(clone, SaveAction.Publish, AccessLevel.Publish);
                                    break;
                                }

                                case ImageData image:
                                {
                                    var clone = image.CreateWritableClone() as IContent;
                                    _contentRepository.Save(clone, SaveAction.Publish, AccessLevel.Publish);
                                    break;
                                }

                                case MediaData media:
                                {
                                    var clone = media.CreateWritableClone() as IContent;
                                    _contentRepository.Save(clone, SaveAction.Publish, AccessLevel.Publish);
                                    break;
                                }
                                }
                            }
                        }
                    }
                }
            }
        }
        private async void OnStepStarted(ApprovalStepEventArgs e)
        {
            // This will be slightly easier soon :)

            var approval = await _approvalRepository
                           .GetAsync(e.ApprovalID).ConfigureAwait(false);

            var approvalDefinition = await _approvalDefinitionVersionRepository
                                     .GetAsync(approval.DefinitionVersionID).ConfigureAwait(false);

            var approversInStep = approvalDefinition
                                  .Steps[e.StepIndex]
                                  .Approvers
                                  .Select(x => x.Username);

            var botsInStep = _bots
                             .Where(x => approversInStep.Contains(x.Username));

            var page = _contentRepository
                       .Get <PageData>(approval.ContentLink);

            foreach (var bot in botsInStep)
            {
                // Approve or reject. The first approver "wins" but the subsequent approves won't fail and in the future that information could be useful.
                var decision = bot.DoDecide(page);

                if (decision.Item1 == ApprovalStatus.Rejected)
                {
                    _approvalEngine.RejectAsync(
                        approval.ID,
                        bot.Username,
                        e.StepIndex,
                        ApprovalDecisionScope.Step).Wait();

                    SendNotification(false, bot.Username, decision.Item2, page.Name, approval.StartedBy);
                }
                else if (decision.Item1 == ApprovalStatus.Approved)
                {
                    _approvalEngine.ApproveAsync(
                        approval.ID,
                        bot.Username,
                        e.StepIndex,
                        ApprovalDecisionScope.Step).Wait();

                    SendNotification(true, bot.Username, decision.Item2, page.Name, approval.StartedBy);

                    // Note: Rejecting will throw an exception if the step has already been approved.
                    break;
                }
            }
        }
Example #3
0
        public async Task <ActionResult> Index(
            ContentApprovalsManagerPage currentPage,
            string task, int?stepIndex, string user, string decision)
        {
            var viewmodel = new ContentApprovalsManagerViewModel(currentPage);

            if (!string.IsNullOrWhiteSpace(task))
            {
                switch (task)
                {
                case "createDefinition":

                    var all     = new[] { CultureInfo.InvariantCulture };
                    var english = new[] { CultureInfo.GetCultureInfo("en") };
                    var swedish = new[] { CultureInfo.GetCultureInfo("sv") };

                    var def = new ContentApprovalDefinition
                    {
                        ContentLink = ContentReference.StartPage,
                        Steps       = new List <ApprovalDefinitionStep>
                        {
                            new ApprovalDefinitionStep(
                                "Alice reviews English, Bob reviews Swedish", new[]
                            {
                                new ApprovalDefinitionReviewer(userName1, english),
                                new ApprovalDefinitionReviewer(userName2, swedish),
                            }),
                            new ApprovalDefinitionStep(
                                "Editors (Eve or Bob) reviews all languages", new[]
                            {
                                new ApprovalDefinitionReviewer(editors, all, ApprovalDefinitionReviewerType.Role)
                            })
                        },
                        RequireCommentOnReject = true
                    };

                    await repoDefinitions.SaveAsync(def);

                    break;

                case "modifyStart":

                    var approvalToDelete = await repoApprovals.GetAsync(ContentReference.StartPage);

                    if (approvalToDelete != null)
                    {
                        await repoApprovals.DeleteAsync(approvalToDelete.ID);
                    }

                    var start = repoContent.Get <StartPage>(ContentReference.StartPage)
                                .CreateWritableClone() as StartPage;

                    start.Name += "X";

                    repoContent.Save(content: start,
                                     action: SaveAction.RequestApproval,
                                     access: AccessLevel.NoAccess);

                    break;

                case "processStep":

                    var approval = await repoApprovals.GetAsync(ContentReference.StartPage);

                    if (approval.ActiveStepIndex <= stepIndex)
                    {
                        if (decision == "Approve")
                        {
                            await engine.ApproveStepAsync(
                                id : approval.ID,
                                username : user,
                                stepIndex : stepIndex.Value,
                                comment : "I approve: the page looks great!");
                        }
                        else
                        {
                            await engine.RejectStepAsync(
                                id : approval.ID,
                                username : user,
                                stepIndex : stepIndex.Value,
                                comment : "I decline: the page looks horrible!");
                        }
                    }
                    break;

                case "deleteApprovals":

                    var list = await repoApprovals.ListAsync(new ApprovalQuery());

                    foreach (var item in list)
                    {
                        await repoApprovals.DeleteAsync(item.ID);
                    }

                    break;
                }
            }

            // GetAsync(ContentReference) extension methods need
            // using EPiServer.Approvals.ContentApprovals
            viewmodel.ApprovalDefinition = await repoDefinitions.GetAsync(ContentReference.StartPage);

            viewmodel.Approval = await repoApprovals.GetAsync(ContentReference.StartPage);

            return(View("~/Features/ContentApprovals/ContentApprovalsManager.cshtml", viewmodel));
        }
Example #4
0
        public async Task <ActionResult> Index(
            ContentApprovalsManagerPage currentPage,
            string task, int?stepIndex, string user, string decision)
        {
            var viewmodel = new ContentApprovalsManagerPageViewModel(currentPage);

            if (!string.IsNullOrWhiteSpace(task))
            {
                switch (task)
                {
                case "createDefinition":

                    var langEN = new[] { CultureInfo.GetCultureInfo("en") };
                    var langSV = new[] { CultureInfo.GetCultureInfo("sv") };

                    var def = new ContentApprovalDefinition
                    {
                        ContentLink = ContentReference.StartPage,
                        Steps       = new List <ApprovalDefinitionStep>
                        {
                            new ApprovalDefinitionStep(
                                "Alice reviews English, Bob reviews Swedish", new[]
                            {
                                new ApprovalDefinitionReviewer(userName1, langEN),
                                new ApprovalDefinitionReviewer(userName2, langSV),
                            }),
                            new ApprovalDefinitionStep(
                                "Eve reviews both languages", new[]
                            {
                                new ApprovalDefinitionReviewer(userName3, langEN.Union(langSV))
                            })
                        },
                        RequireCommentOnReject = true
                    };

                    await repoDefinitions.SaveAsync(def);

                    break;

                case "modifyStart":

                    var start = repoContent.Get <StartPage>(ContentReference.StartPage)
                                .CreateWritableClone() as StartPage;

                    start.Name += "X";

                    repoContent.Save(content: start,
                                     action: SaveAction.RequestApproval,
                                     access: AccessLevel.NoAccess);

                    break;

                case "processStep":

                    var approval = await repoApprovals.GetAsync(ContentReference.StartPage);

                    if (decision == "Approve")
                    {
                        await engine.ApproveStepAsync(
                            id : approval.ID,
                            username : user,
                            stepIndex : stepIndex.Value,
                            comment : "I approve: the page looks great!");
                    }
                    else
                    {
                        await engine.RejectStepAsync(
                            id : approval.ID,
                            username : user,
                            stepIndex : stepIndex.Value,
                            comment : "I decline: the page looks horrible!");
                    }
                    break;
                }
            }

            // GetAsync(ContentReference) extension methods need
            // using EPiServer.Approvals.ContentApprovals
            viewmodel.ApprovalDefinition = await repoDefinitions.GetAsync(ContentReference.StartPage);

            viewmodel.Approval = await repoApprovals.GetAsync(ContentReference.StartPage);

            return(View(viewmodel));
        }