< Summary

Information
Class: Elsa.Identity.Providers.AdminUserProvider
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Providers/AdminUserProvider.cs
Line coverage
86%
Covered lines: 20
Uncovered lines: 3
Coverable lines: 23
Total lines: 59
Line coverage: 86.9%
Branch coverage
85%
Covered branches: 12
Total branches: 14
Branch coverage: 85.7%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
.ctor(...)100%44100%
FindAsync(...)80%101085.71%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Providers/AdminUserProvider.cs

#LineLine coverage
 1using Elsa.Identity.Contracts;
 2using Elsa.Identity.Entities;
 3using Elsa.Identity.Models;
 4using Elsa.Identity.Options;
 5using Microsoft.Extensions.Options;
 6
 7namespace Elsa.Identity.Providers;
 8
 9/// <summary>
 10/// Represents a user provider that returns a single explicitly configured admin user. This is useful for development pu
 11/// </summary>
 12public class AdminUserProvider : IUserProvider
 13{
 14    private readonly User? _adminUser;
 15
 16    /// <summary>
 17    /// Initializes a new instance of the <see cref="AdminUserProvider"/> class.
 18    /// </summary>
 19    [Obsolete("Use the options-based constructor. The built-in admin user is disabled unless explicitly configured.")]
 020    public AdminUserProvider(ISecretHasher secretHasher) : this(secretHasher, Microsoft.Extensions.Options.Options.Creat
 21    {
 022    }
 23
 24    /// <summary>
 25    /// Initializes a new instance of the <see cref="AdminUserProvider"/> class.
 26    /// </summary>
 427    public AdminUserProvider(ISecretHasher secretHasher, IOptions<AdminUserProviderOptions> options)
 28    {
 429        var providerOptions = options.Value;
 430        if (string.IsNullOrWhiteSpace(providerOptions.UserName) || string.IsNullOrWhiteSpace(providerOptions.Password))
 231            return;
 32
 233        var hashedSecret = secretHasher.HashSecret(providerOptions.Password);
 34
 235        _adminUser = new User
 236        {
 237            Id = providerOptions.UserId,
 238            Name = providerOptions.UserName,
 239            HashedPassword = hashedSecret.EncodeSecret(),
 240            HashedPasswordSalt = hashedSecret.EncodeSalt(),
 241            Roles = providerOptions.Roles.ToList()
 242        };
 243    }
 44
 45    /// <inheritdoc />
 46    public Task<User?> FindAsync(UserFilter filter, CancellationToken cancellationToken = default)
 47    {
 448        if (_adminUser == null)
 249            return Task.FromResult<User?>(null);
 50
 251        if (filter.Id != null && filter.Id != _adminUser.Id)
 052            return Task.FromResult<User?>(null);
 53
 254        if (filter.Name != null && filter.Name != _adminUser.Name)
 155            return Task.FromResult<User?>(null);
 56
 157        return Task.FromResult<User?>(_adminUser);
 58    }
 59}