Created
October 8, 2017 20:04
-
-
Save davidknipe/96e2b4fe2e63e476f14d3ae55457d60d to your computer and use it in GitHub Desktop.
Approval Engine that throws an exception if a user tries to approve their own content
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using EPiServer.Approvals; | |
using System; | |
using System.Collections.Generic; | |
using System.Threading.Tasks; | |
using EPiServer.Framework; | |
using EPiServer.ServiceLocation; | |
using EPiServer.Framework.Initialization; | |
using EPiServer.Security; | |
namespace ApprovalSequences | |
{ | |
[ModuleDependency(typeof(EPiServer.Web.InitializationModule))] | |
public class ApprovalEngineInterceptor : IConfigurableModule | |
{ | |
public void ConfigureContainer(ServiceConfigurationContext context) | |
{ | |
context.Services.Intercept<IApprovalEngine>( | |
(locator, defaultEngine) => new ValidatingApprovalEngine(defaultEngine)); | |
} | |
public void Initialize(InitializationEngine context) { } | |
public void Uninitialize(InitializationEngine context) { } | |
} | |
public class ValidatingApprovalEngine : IApprovalEngine | |
{ | |
private readonly IApprovalEngine _defaultApprovalEngine; | |
public ValidatingApprovalEngine(IApprovalEngine defaultApprovalEngine) | |
{ | |
_defaultApprovalEngine = defaultApprovalEngine; | |
} | |
public Task ApproveAsync(int id, string username, int stepIndex, ApprovalDecisionScope scope, string comment) | |
{ | |
if (username == PrincipalInfo.CurrentPrincipal.Identity.Name) | |
{ | |
return Task.FromException(new Exception("You cannot approve your own content")); | |
} | |
else | |
{ | |
return _defaultApprovalEngine.ApproveAsync(id, username, stepIndex, scope, comment); | |
} | |
} | |
public Task AbortAsync(IEnumerable<int> ids, string username) => _defaultApprovalEngine.AbortAsync(ids, username); | |
public Task RejectAsync(int id, string username, int stepIndex, ApprovalDecisionScope scope, string comment) => _defaultApprovalEngine.RejectAsync(id, username, stepIndex, scope, comment); | |
public Task<IEnumerable<Approval>> StartAsync(IEnumerable<Uri> references, string username, bool throwOnMissingDefinition) => _defaultApprovalEngine.StartAsync(references, username, throwOnMissingDefinition); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment