mirror of
https://github.com/opelly27/WinStudentGoalTracker.git
synced 2026-05-20 05:17:41 +00:00
latest
This commit is contained in:
@@ -11,6 +11,6 @@ public class dbUser
|
||||
public int FailedLoginAttempts { get; set; }
|
||||
public DateTime? LockedUntil { get; set; }
|
||||
public DateTime? CreatedAt { get; set; }
|
||||
public string? RoleInternalName { get; set; }
|
||||
public string? RoleDisplayName { get; set; }
|
||||
public required string RoleInternalName { get; set; }
|
||||
public required string RoleDisplayName { get; set; }
|
||||
}
|
||||
|
||||
@@ -2,20 +2,15 @@ namespace WinStudentGoalTracker.Models;
|
||||
|
||||
public static class UserRoles
|
||||
{
|
||||
// Role names from role-based-access-control.md
|
||||
public const string Teacher = "Teacher";
|
||||
public const string Paraeducator = "Paraeducator";
|
||||
public const string ProgramAdmin = "ProgramAdmin";
|
||||
public const string DistrictAdmin = "DistrictAdmin";
|
||||
public const string SuperAdmin = "SuperAdmin";
|
||||
public const string Teacher = "teacher";
|
||||
public const string Paraeducator = "paraeducator";
|
||||
public const string ProgramAdmin = "program_admin";
|
||||
public const string DistrictAdmin = "district_admin";
|
||||
public const string SuperAdmin = "super_admin";
|
||||
|
||||
public static readonly IReadOnlyList<string> All = new[]
|
||||
{
|
||||
Teacher,
|
||||
Paraeducator,
|
||||
ProgramAdmin,
|
||||
DistrictAdmin,
|
||||
SuperAdmin
|
||||
public static string? TryParse(string value) =>
|
||||
All.Contains(value) ? value : null;
|
||||
|
||||
};
|
||||
public static readonly IReadOnlyList<string> All =
|
||||
[Teacher, Paraeducator, ProgramAdmin, DistrictAdmin, SuperAdmin];
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ using System.IdentityModel.Tokens.Jwt;
|
||||
using System.Security.Claims;
|
||||
using System.Text;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using WinStudentGoalTracker.Models;
|
||||
|
||||
namespace WinStudentGoalTracker.Services;
|
||||
|
||||
@@ -15,8 +16,14 @@ public class TokenService
|
||||
_config = config;
|
||||
}
|
||||
|
||||
public string GenerateToken(Guid userId, string email, string? roleName)
|
||||
public string GenerateToken(Guid userId, string email, string role)
|
||||
{
|
||||
|
||||
if (UserRoles.TryParse(role) is null)
|
||||
{
|
||||
throw new ArgumentException("Invalid role name");
|
||||
}
|
||||
|
||||
var claims = new List<Claim>
|
||||
{
|
||||
new Claim(JwtRegisteredClaimNames.Sub, userId.ToString()),
|
||||
@@ -25,9 +32,9 @@ public class TokenService
|
||||
new Claim("user_id", userId.ToString())
|
||||
};
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(roleName))
|
||||
if (role is not null)
|
||||
{
|
||||
claims.Add(new Claim(ClaimTypes.Role, roleName));
|
||||
claims.Add(new Claim(ClaimTypes.Role, role));
|
||||
}
|
||||
|
||||
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config["Jwt:Key"]!));
|
||||
|
||||
Reference in New Issue
Block a user