Commit 66384a51 authored by Almouhannad's avatar Almouhannad

(B) Add role to JWT, make controllers authorized

parent 2ea3eb22
......@@ -4,9 +4,13 @@ public static class Roles
{
#region Constant values
public static int Count => 3;
public static Role Admin => Role.Create(1, "admin");
public static Role Doctor => Role.Create(2, "doctor");
public static Role Receptionist => Role.Create(3, "receptionist");
public const string AdminName = "admin";
public const string DoctorName = "doctor";
public const string ReceptionistName = "receptionist";
public static Role Admin => Role.Create(1, AdminName);
public static Role Doctor => Role.Create(2, DoctorName);
public static Role Receptionist => Role.Create(3, ReceptionistName);
public static List<Role> GetAll()
{
......
......@@ -21,8 +21,8 @@ public sealed class JWTProvider : IJWTProvider
{
var claims = new Claim[]
{
new(JwtRegisteredClaimNames.Sub, user.Id.ToString()),
new(JwtRegisteredClaimNames.UniqueName, user.UserName),
new(ClaimTypes.Name, user.UserName),
new(ClaimTypes.Role, user.Role.Name)
};
......
using Application.Employees.Commands.AttachFamilyMemberToEmployee;
using Application.Employees.Commands.CreateEmployee;
using Domain.Entities.Identity.UserRoles;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
......@@ -17,7 +18,7 @@ public class EmployeesController : ApiController
}
#endregion
[Authorize]
[Authorize(Roles = Roles.AdminName)]
[HttpPost]
public async Task<IActionResult> Create([FromBody] CreateEmployeeCommand command)
{
......@@ -26,7 +27,7 @@ public class EmployeesController : ApiController
return HandleFailure(result);
return Created();
}
[Authorize(Roles = Roles.DoctorName)]
[HttpPut("FamilyMembers")]
public async Task<IActionResult> AttachFamilyMember([FromBody] AttachFamilyMemberToEmployeeCommand command)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment