< Summary

Information
Class: Elsa.Identity.Services.RoleManager
Assembly: Elsa.Identity
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Services/RoleManager.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 49
Line coverage: 100%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
CreateRoleAsync()83.33%66100%
RoleExistsAsync()100%22100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Identity/Services/RoleManager.cs

#LineLine coverage
 1using Elsa.Common.Multitenancy;
 2using Elsa.Identity.Contracts;
 3using Elsa.Identity.Entities;
 4using Elsa.Identity.Models;
 5using Humanizer;
 6
 7namespace Elsa.Identity.Services;
 8
 9/// <summary>
 10/// Default implementation of <see cref="IRoleManager"/>.
 11/// </summary>
 812public class RoleManager(IRoleStore roleStore, IRoleProvider roleProvider, ITenantAccessor tenantAccessor) : IRoleManage
 13{
 14    /// <inheritdoc />
 15    public async Task<CreateRoleResult> CreateRoleAsync(
 16        string name,
 17        ICollection<string>? permissions = null,
 18        string? id = null,
 19        CancellationToken cancellationToken = default)
 20    {
 421        var roleId = id ?? name.Kebaberize();
 22
 423        if (await RoleExistsAsync(roleId, cancellationToken))
 224            throw new InvalidOperationException($"A role with ID '{roleId}' already exists.");
 25
 226        var role = new Role
 227        {
 228            Id = roleId,
 229            Name = name,
 230            // The in-memory path does not run EF's ApplyTenantId saving handler.
 231            TenantId = tenantAccessor.TenantId,
 232            Permissions = permissions ?? new List<string>()
 233        };
 34
 235        await roleStore.SaveAsync(role, cancellationToken);
 36
 237        return new CreateRoleResult(role);
 238    }
 39
 40    private async Task<bool> RoleExistsAsync(string roleId, CancellationToken cancellationToken)
 41    {
 442        var storedRole = await roleStore.FindAsync(new() { Id = roleId }, cancellationToken);
 443        if (storedRole != null)
 144            return true;
 45
 346        var providedRoles = await roleProvider.FindManyAsync(new() { Id = roleId }, cancellationToken);
 447        return providedRoles.Any(x => x.Id == roleId);
 448    }
 49}