42 lines
1.5 KiB
C#
42 lines
1.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace LaDOSE.Entity
|
|
{
|
|
/// <summary>
|
|
/// The role names the API knows about. They must exist as rows in
|
|
/// <c>applicationrole</c> — see Sql/2026-08-05_roles.sql.
|
|
/// </summary>
|
|
public static class Roles
|
|
{
|
|
/// <summary>May manage user accounts.</summary>
|
|
public const string Admin = "Admin";
|
|
|
|
/// <summary>May use everything else. The absence of a role means the same thing.</summary>
|
|
public const string User = "User";
|
|
|
|
public static readonly string[] All = { Admin, User };
|
|
|
|
/// <summary>Case-insensitive, so 'admin' typed into the SQL seed still counts.</summary>
|
|
public static bool IsAdmin(this ApplicationUser user)
|
|
{
|
|
return user.Names().Any(name => Admin.Equals(name, StringComparison.OrdinalIgnoreCase));
|
|
}
|
|
|
|
/// <summary>
|
|
/// The user's role names. Empty when the user holds no role, and also when the
|
|
/// query did not include them — callers that authorize must load them.
|
|
/// </summary>
|
|
public static List<string> Names(this ApplicationUser user)
|
|
{
|
|
return user?.UserRoles?
|
|
.Select(userRole => userRole.Role?.Name)
|
|
.Where(name => !string.IsNullOrWhiteSpace(name))
|
|
.OrderBy(name => name)
|
|
.ToList()
|
|
?? new List<string>();
|
|
}
|
|
}
|
|
}
|