#48 added basic role features and some cleanUp
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
using System.Security.Claims;
|
||||
using Hutopy.Application.Common.Interfaces;
|
||||
using Hutopy.Application.Common.Models;
|
||||
using Hutopy.Domain.Models;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
@@ -36,7 +35,7 @@ public class IdentityService(
|
||||
return (result.ToApplicationResult(), user.Id);
|
||||
}
|
||||
|
||||
public async Task CreateUserAsync(string email, string userName, string firstName, string lastName, string password)
|
||||
public async Task<Result> CreateUserAsync(string email, string userName, string firstName, string lastName, string password)
|
||||
{
|
||||
var applicationUser = new ApplicationUser
|
||||
{
|
||||
@@ -46,13 +45,9 @@ public class IdentityService(
|
||||
LastName = lastName
|
||||
};
|
||||
|
||||
//todo: Need to handle errors better for the user.
|
||||
var response = await userManager.CreateAsync(applicationUser, password);
|
||||
|
||||
if (response.Errors.Any())
|
||||
{
|
||||
throw new InvalidOperationException(response.Errors.First().Description);
|
||||
}
|
||||
|
||||
return response.ToApplicationResult();
|
||||
}
|
||||
|
||||
public async Task<UserModel?> FindUserByIdAsync(string id)
|
||||
@@ -108,6 +103,14 @@ public class IdentityService(
|
||||
|
||||
return user != null && await userManager.IsInRoleAsync(user, role);
|
||||
}
|
||||
|
||||
public async Task<bool> CurrentUserIsInRoleAsync(string role)
|
||||
{
|
||||
var currentUserModel = await GetCurrentUserAsync();
|
||||
var currentUser = await userManager.FindByIdAsync(currentUserModel?.Id ?? "");
|
||||
|
||||
return currentUser != null && await userManager.IsInRoleAsync(currentUser, role);
|
||||
}
|
||||
|
||||
public async Task<bool> AuthorizeAsync(string userId, string policyName)
|
||||
{
|
||||
@@ -138,4 +141,32 @@ public class IdentityService(
|
||||
|
||||
return result.ToApplicationResult();
|
||||
}
|
||||
|
||||
public async Task<Result> AddRoleAsync(string userId, string role)
|
||||
{
|
||||
var hasAdminAccess = await CurrentUserIsInRoleAsync("Administrator");
|
||||
|
||||
if (!hasAdminAccess) return Result.Failure(new []{"Only administrator can assign new roles to a user."});
|
||||
|
||||
var user = await userManager.FindByIdAsync(userId);
|
||||
|
||||
if (user is null) return Result.Failure(new []{"User not found."});
|
||||
|
||||
var result = await userManager.AddToRoleAsync(user, role);
|
||||
|
||||
return result.ToApplicationResult();
|
||||
}
|
||||
|
||||
public async Task<IList<string>> GetCurrentUserRolesAsync()
|
||||
{
|
||||
var currentUserModel = await GetCurrentUserAsync();
|
||||
|
||||
var currentUser = await userManager.FindByIdAsync(currentUserModel?.Id ?? "");
|
||||
|
||||
if (currentUser is null) return [];
|
||||
|
||||
var userRoles = await userManager.GetRolesAsync(currentUser);
|
||||
|
||||
return userRoles;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user