< Summary

Information
Class: Elsa.Identity.Endpoints.Users.Create.Create
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Endpoints/Users/Create/Endpoint.cs
Line coverage
42%
Covered lines: 16
Uncovered lines: 22
Coverable lines: 38
Total lines: 72
Line coverage: 42.1%
Branch coverage
0%
Covered branches: 0
Total branches: 4
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%2040%

File(s)

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

#LineLine coverage
 1using Elsa.Abstractions;
 2using Elsa.Identity.Contracts;
 3using Elsa.Identity.Entities;
 4using Elsa.Workflows;
 5using JetBrains.Annotations;
 6
 7namespace Elsa.Identity.Endpoints.Users.Create;
 8
 9/// <summary>
 10/// An endpoint that creates a new user. Requires the <code>SecurityRoot</code> policy.
 11/// </summary>
 12[PublicAPI]
 13internal class Create : ElsaEndpoint<Request, Response>
 14{
 15    private readonly IIdentityGenerator _identityGenerator;
 16    private readonly ISecretGenerator _secretGenerator;
 17    private readonly ISecretHasher _secretHasher;
 18    private readonly IUserStore _userStore;
 19    private readonly IRoleStore _roleStore;
 20
 121    public Create(
 122        IIdentityGenerator identityGenerator,
 123        ISecretGenerator secretGenerator,
 124        ISecretHasher secretHasher,
 125        IUserStore userStore,
 126        IRoleStore roleStore)
 27    {
 128        _identityGenerator = identityGenerator;
 129        _secretGenerator = secretGenerator;
 130        _secretHasher = secretHasher;
 131        _userStore = userStore;
 132        _roleStore = roleStore;
 133    }
 34
 35    /// <inheritdoc />
 36    public override void Configure()
 37    {
 138        Post("/identity/users");
 139        ConfigurePermissions("create:user");
 140        Policies(IdentityPolicyNames.SecurityRoot);
 141    }
 42
 43    /// <inheritdoc />
 44    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 45    {
 046        var id = _identityGenerator.GenerateId();
 047        var password = string.IsNullOrWhiteSpace(request.Password) ? _secretGenerator.Generate() : request.Password.Trim
 048        var hashedPassword = _secretHasher.HashSecret(password);
 49
 050        var user = new User
 051        {
 052            Id = id,
 053            Name = request.Name,
 054            Roles = request.Roles ?? new List<string>(),
 055            HashedPassword = hashedPassword.EncodeSecret(),
 056            HashedPasswordSalt = hashedPassword.EncodeSalt()
 057        };
 58
 059        await _userStore.SaveAsync(user, cancellationToken);
 60
 061        var response = new Response(
 062            id,
 063            user.Name,
 064            password,
 065            user.Roles,
 066            user.TenantId,
 067            hashedPassword.EncodeSecret(),
 068            hashedPassword.EncodeSalt());
 69
 070        await Send.OkAsync(response, cancellationToken);
 071    }
 72}