< Summary

Information
Class: Elsa.Identity.Endpoints.Roles.Create.Create
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Endpoints/Roles/Create/Endpoint.cs
Line coverage
13%
Covered lines: 4
Uncovered lines: 25
Coverable lines: 29
Total lines: 71
Line coverage: 13.7%
Branch coverage
0%
Covered branches: 0
Total branches: 6
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%4260%

File(s)

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

#LineLine coverage
 1using Elsa.Authorization;
 2using Elsa.Abstractions;
 3using Elsa.Identity.Contracts;
 4using Elsa.Identity.Models;
 5using Elsa.Permissions;
 6using JetBrains.Annotations;
 7using Microsoft.AspNetCore.Http;
 8
 9namespace Elsa.Identity.Endpoints.Roles.Create;
 10
 11/// <summary>
 12/// An endpoint that creates a new role.
 13/// </summary>
 14[PublicAPI]
 315internal class Create(IRoleManager roleManager, IRoleAuthorizationService roleAuthorizationService, IPermissionGrantVali
 16{
 17    /// <inheritdoc />
 18    public override void Configure()
 19    {
 520        Post("/identity/roles");
 521        RequirePermission(Elsa.Identity.Permissions.IdentityPermissions.Roles, CoreVerbs.Create);
 522    }
 23
 24    /// <inheritdoc />
 25    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 26    {
 27        // Reject grants the catalog cannot account for before the anti-escalation check, so an author gets
 28        // a specific message rather than a blanket 403 for what is really a typo.
 029        var validation = grantValidator.Validate(request.Permissions);
 30
 031        if (!validation.IsValid)
 32        {
 33            // Both parts matter: the permission identifies which entry to fix, the reason says how.
 034            foreach (var error in validation.Errors)
 035                AddError($"{error.Permission} — {error.Reason}");
 36
 037            await Send.ErrorsAsync(cancellation: cancellationToken);
 038            return;
 39        }
 40
 041        if (!roleAuthorizationService.CanCreateRoleWithPermissions(User, request.Permissions))
 42        {
 043            await Send.ForbiddenAsync(cancellationToken);
 044            return;
 45        }
 46
 47        CreateRoleResult result;
 48        try
 49        {
 050            result = await roleManager.CreateRoleAsync(
 051                request.Name,
 052                request.Permissions,
 053                request.Id,
 054                cancellationToken);
 055        }
 056        catch (InvalidOperationException ex) when (ex.Message.Contains("already exists", StringComparison.OrdinalIgnoreC
 57        {
 058            await Send.ErrorsAsync(StatusCodes.Status409Conflict, cancellationToken);
 059            return;
 60        }
 61
 062        await securityNotifier.RoleChangedAsync(User, "created", result.Role.Id, result.Role.Name, result.Role.Permissio
 63
 064        var response = new Response(
 065            result.Role.Id,
 066            result.Role.Name,
 067            result.Role.Permissions);
 68
 069        await Send.OkAsync(response, cancellationToken);
 070    }
 71}