2024-05-27 08:06:39 +00:00
|
|
|
|
using Elsa.Workflows.Management.Entities;
|
|
|
|
|
|
using Elsa.Workflows.Management.Options;
|
|
|
|
|
|
using JetBrains.Annotations;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Workflows.Api.Requirements;
|
|
|
|
|
|
|
|
|
|
|
|
public record NotReadOnlyResource(WorkflowDefinition? WorkflowDefinition = default);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public record NotReadOnlyRequirement() : IAuthorizationRequirement;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
[PublicAPI]
|
|
|
|
|
|
public class NotReadOnlyRequirementHandler : AuthorizationHandler<NotReadOnlyRequirement, NotReadOnlyResource>
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IOptions<ManagementOptions> _managementOptions;
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public NotReadOnlyRequirementHandler(
|
|
|
|
|
|
IOptions<ManagementOptions> managementOptions)
|
|
|
|
|
|
{
|
|
|
|
|
|
_managementOptions = managementOptions;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
2025-11-12 15:56:31 +00:00
|
|
|
|
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, NotReadOnlyRequirement requirement, NotReadOnlyResource resource)
|
2024-05-27 08:06:39 +00:00
|
|
|
|
{
|
|
|
|
|
|
if (_managementOptions.Value.IsReadOnlyMode)
|
|
|
|
|
|
{
|
2025-11-12 15:56:31 +00:00
|
|
|
|
context.Fail(new(this, "Workflow edit is not allowed when the read-only mode is enabled."));
|
2024-05-27 08:06:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (resource.WorkflowDefinition != null && (resource.WorkflowDefinition.IsReadonly || resource.WorkflowDefinition.IsSystem))
|
|
|
|
|
|
{
|
2025-11-12 15:56:31 +00:00
|
|
|
|
context.Fail(new(this, "Workflow edit is not allowed for a readonly or system workflow."));
|
2024-05-27 08:06:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
context.Succeed(requirement);
|
2025-11-12 15:56:31 +00:00
|
|
|
|
return Task.CompletedTask;
|
2024-05-27 08:06:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|