< 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: 62
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.Abstractions;
 2using Elsa.Identity.Contracts;
 3using JetBrains.Annotations;
 4
 5namespace Elsa.Identity.Endpoints.Users.Update;
 6
 7/// <summary>
 8/// An endpoint that updates an existing user's password and roles.
 9/// </summary>
 10[PublicAPI]
 311internal class Update(IUserStore userStore, ISecretHasher secretHasher, IRoleAuthorizationService roleAuthorizationServi
 12{
 13    /// <inheritdoc />
 14    public override void Configure()
 15    {
 316        Put("/identity/users/{id}");
 317        ConfigurePermissions("update:user");
 318    }
 19
 20    /// <inheritdoc />
 21    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 22    {
 023        var id = Route<string>("id")!;
 24
 025        var user = await userStore.FindAsync(new()
 026            { Id = id }, cancellationToken);
 27
 028        if (user == null)
 29        {
 030            await Send.NotFoundAsync(cancellationToken);
 031            return;
 32        }
 33
 034        if (request.Roles != null)
 35        {
 036            if (!await roleAuthorizationService.CanAssignRolesAsync(User, request.Roles, cancellationToken))
 37            {
 038                await Send.ForbiddenAsync(cancellationToken);
 039                return;
 40            }
 41
 042            user.Roles = request.Roles;
 43        }
 44
 045        if (!string.IsNullOrWhiteSpace(request.Password))
 46        {
 047            var hashedPassword = secretHasher.HashSecret(request.Password.Trim());
 048            user.HashedPassword = hashedPassword.EncodeSecret();
 049            user.HashedPasswordSalt = hashedPassword.EncodeSalt();
 50        }
 51
 052        await userStore.SaveAsync(user, cancellationToken);
 53
 054        var response = new Response(
 055            user.Id,
 056            user.Name,
 057            user.Roles,
 058            user.TenantId);
 59
 060        await Send.OkAsync(response, cancellationToken);
 061    }
 62}