< Summary

Information
Class: Elsa.Identity.Endpoints.Roles.Update.Update
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Endpoints/Roles/Update/Endpoint.cs
Line coverage
11%
Covered lines: 4
Uncovered lines: 30
Coverable lines: 34
Total lines: 73
Line coverage: 11.7%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Configure()100%11100%
HandleAsync()0%156120%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Endpoints/Roles/Update/Endpoint.cs

#LineLine coverage
 1using Elsa.Authorization;
 2using Elsa.Abstractions;
 3using Elsa.Identity.Contracts;
 4using Elsa.Permissions;
 5using JetBrains.Annotations;
 6
 7namespace Elsa.Identity.Endpoints.Roles.Update;
 8
 9/// <summary>
 10/// An endpoint that updates an existing role.
 11/// </summary>
 12[PublicAPI]
 313internal class Update(IRoleStore roleStore, IRoleAuthorizationService roleAuthorizationService, IPermissionGrantValidato
 14{
 15    /// <inheritdoc />
 16    public override void Configure()
 17    {
 318        Put("/identity/roles/{id}");
 319        RequirePermission(Elsa.Identity.Permissions.IdentityPermissions.Roles, CoreVerbs.Update);
 320    }
 21
 22    /// <inheritdoc />
 23    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 24    {
 025        var id = Route<string>("id")!;
 26
 027        var role = await roleStore.FindAsync(new()
 028        {
 029            Id = id
 030        }, cancellationToken);
 31
 032        if (role == null)
 33        {
 034            await Send.NotFoundAsync(cancellationToken);
 035            return;
 36        }
 37
 038        var validation = grantValidator.Validate(request.Permissions);
 39
 040        if (!validation.IsValid)
 41        {
 42            // Both parts matter: the permission identifies which entry to fix, the reason says how.
 043            foreach (var error in validation.Errors)
 044                AddError($"{error.Permission} — {error.Reason}");
 45
 046            await Send.ErrorsAsync(cancellation: cancellationToken);
 047            return;
 48        }
 49
 050        if (!roleAuthorizationService.CanMutateRole(User, role, request.Permissions))
 51        {
 052            await Send.ForbiddenAsync(cancellationToken);
 053            return;
 54        }
 55
 056        if (!string.IsNullOrWhiteSpace(request.Name))
 057            role.Name = request.Name.Trim();
 58
 059        if (request.Permissions != null)
 060            role.Permissions = request.Permissions;
 61
 062        await roleStore.SaveAsync(role, cancellationToken);
 063        await securityNotifier.RoleChangedAsync(User, "updated", role.Id, role.Name, role.Permissions.ToArray(), cancell
 64
 065        var response = new Response(
 066            role.Id,
 067            role.Name,
 068            role.Permissions,
 069            role.TenantId);
 70
 071        await Send.OkAsync(response, cancellationToken);
 072    }
 73}