using System; using System.Collections.Generic; using System.Linq; namespace LaDOSE.Entity { /// /// The role names the API knows about. They must exist as rows in /// applicationrole — see Sql/2026-08-05_roles.sql. /// public static class Roles { /// May manage user accounts. public const string Admin = "Admin"; /// May use everything else. The absence of a role means the same thing. public const string User = "User"; public static readonly string[] All = { Admin, User }; /// Case-insensitive, so 'admin' typed into the SQL seed still counts. public static bool IsAdmin(this ApplicationUser user) { return user.Names().Any(name => Admin.Equals(name, StringComparison.OrdinalIgnoreCase)); } /// /// 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. /// public static List Names(this ApplicationUser user) { return user?.UserRoles? .Select(userRole => userRole.Role?.Name) .Where(name => !string.IsNullOrWhiteSpace(name)) .OrderBy(name => name) .ToList() ?? new List(); } } }