< Summary

Information
Class: Elsa.Identity.Endpoints.Users.Update.Update
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Endpoints/Users/Update/Endpoint.cs
Line coverage
14%
Covered lines: 4
Uncovered lines: 23
Coverable lines: 27
Total lines: 63
Line coverage: 14.8%
Branch coverage
0%
Covered branches: 0
Total branches: 8
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%7280%

File(s)

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

#LineLine coverage
 1using Elsa.Authorization;
 2using Elsa.Abstractions;
 3using Elsa.Identity.Contracts;
 4using JetBrains.Annotations;
 5
 6namespace Elsa.Identity.Endpoints.Users.Update;
 7
 8/// <summary>
 9/// An endpoint that updates an existing user's password and roles.
 10/// </summary>
 11[PublicAPI]
 312internal class Update(IUserStore userStore, ISecretHasher secretHasher, IRoleAuthorizationService roleAuthorizationServi
 13{
 14    /// <inheritdoc />
 15    public override void Configure()
 16    {
 317        Put("/identity/users/{id}");
 318        RequirePermission(Elsa.Identity.Permissions.IdentityPermissions.Users, CoreVerbs.Update);
 319    }
 20
 21    /// <inheritdoc />
 22    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 23    {
 024        var id = Route<string>("id")!;
 25
 026        var user = await userStore.FindAsync(new()
 027            { Id = id }, cancellationToken);
 28
 029        if (user == null)
 30        {
 031            await Send.NotFoundAsync(cancellationToken);
 032            return;
 33        }
 34
 035        if (request.Roles != null)
 36        {
 037            if (!await roleAuthorizationService.CanAssignRolesAsync(User, request.Roles, cancellationToken))
 38            {
 039                await Send.ForbiddenAsync(cancellationToken);
 040                return;
 41            }
 42
 043            user.Roles = request.Roles;
 44        }
 45
 046        if (!string.IsNullOrWhiteSpace(request.Password))
 47        {
 048            var hashedPassword = secretHasher.HashSecret(request.Password.Trim());
 049            user.HashedPassword = hashedPassword.EncodeSecret();
 050            user.HashedPasswordSalt = hashedPassword.EncodeSalt();
 51        }
 52
 053        await userStore.SaveAsync(user, cancellationToken);
 54
 055        var response = new Response(
 056            user.Id,
 057            user.Name,
 058            user.Roles,
 059            user.TenantId);
 60
 061        await Send.OkAsync(response, cancellationToken);
 062    }
 63}