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