[CHANGE] Adding functionality to manager
This commit is contained in:
@@ -4,8 +4,8 @@ namespace DotBased.AspNet.Authority.Managers;
|
||||
|
||||
public partial class AuthorityManager
|
||||
{
|
||||
public async Task<ListResult<AuthorityGroupItem>> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default)
|
||||
public async Task<ListResult<AuthorityGroup>> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return ListResult<AuthorityGroupItem>.Failed("Not implemented!");
|
||||
return await GroupRepository.GetUserGroupsAsync(user, cancellationToken);
|
||||
}
|
||||
}
|
@@ -14,6 +14,7 @@ public partial class AuthorityManager(
|
||||
IServiceProvider services,
|
||||
ICryptographer cryptographer,
|
||||
IUserRepository userRepository,
|
||||
IGroupRepository groupRepository,
|
||||
IRoleRepository roleRepository,
|
||||
IPasswordHasher passwordHasher)
|
||||
{
|
||||
@@ -24,6 +25,7 @@ public partial class AuthorityManager(
|
||||
public ICryptographer Cryptographer { get; } = cryptographer;
|
||||
|
||||
public IUserRepository UserRepository { get; } = userRepository;
|
||||
public IGroupRepository GroupRepository { get; } = groupRepository;
|
||||
public IRoleRepository RoleRepository { get; } = roleRepository;
|
||||
|
||||
public IPasswordHasher PasswordHasher { get; } = passwordHasher;
|
||||
|
@@ -8,43 +8,103 @@ public partial class AuthorityManager
|
||||
public async Task<AuthorityResult<AuthorityRole>> CreateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default)
|
||||
{
|
||||
role.Version = GenerateVersion();
|
||||
var createResult = await roleRepository.CreateRoleAsync(role, cancellationToken);
|
||||
var createResult = await RoleRepository.CreateRoleAsync(role, cancellationToken);
|
||||
return AuthorityResult<AuthorityRole>.FromResult(createResult);
|
||||
}
|
||||
|
||||
public async Task<Result> DeleteRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default)
|
||||
public async Task<Result> DeleteRolesAsync(List<AuthorityRole> roles, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var result = await roleRepository.DeleteRoleAsync(role, cancellationToken);
|
||||
var result = await RoleRepository.DeleteRolesAsync(roles, cancellationToken);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<Result<AuthorityRole>> UpdateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var result = await roleRepository.UpdateRoleAsync(role, cancellationToken);
|
||||
var result = await RoleRepository.UpdateRoleAsync(role, cancellationToken);
|
||||
return result;
|
||||
}
|
||||
|
||||
public async Task<ListResult<AuthorityRoleItem>> GetRolesAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default)
|
||||
{
|
||||
var searchResult = await roleRepository.GetRolesAsync(limit, offset, search, cancellationToken);
|
||||
var searchResult = await RoleRepository.GetRolesAsync(limit, offset, search, cancellationToken);
|
||||
return searchResult;
|
||||
}
|
||||
|
||||
public async Task AddRoleToUserAsync(AuthorityUser user, AuthorityRole role, CancellationToken cancellationToken = default)
|
||||
public async Task AddRolesToUserAsync(List<AuthorityRole> roles, AuthorityUser user, CancellationToken cancellationToken = default)
|
||||
{
|
||||
/*
|
||||
- Validate User & Role
|
||||
- Check if role is already in linked to user (if user already has the role, return)
|
||||
- Add to UsersRoles table
|
||||
*/
|
||||
var usrValidation = await IsValidUserAsync(user, cancellationToken);
|
||||
if (!usrValidation.Success)
|
||||
{
|
||||
_logger.Error(usrValidation.Exception ?? new Exception("Validation for user failed!"), "Invalid user!");
|
||||
return;
|
||||
}
|
||||
|
||||
var checkResult = await RoleRepository.HasRolesAsync(user.Id, roles, cancellationToken);
|
||||
if (!checkResult.Success)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var rolesToAdd = roles;
|
||||
if (checkResult.Count != 0)
|
||||
{
|
||||
rolesToAdd = roles.Where(r => !checkResult.Items.Contains(r.Id)).ToList();
|
||||
}
|
||||
|
||||
var addResult = await RoleRepository.AddRolesLinkAsync(rolesToAdd, user.Id, cancellationToken);
|
||||
if (!addResult.Success)
|
||||
{
|
||||
_logger.Error(addResult.Exception ?? new Exception("Adding role to user failed!, No further information available!"),"Failed to add role to user!");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RemoveRoleFromUserAsync(AuthorityRole role, AuthorityUser user, CancellationToken cancellationToken = default)
|
||||
public async Task RemoveRolesFromUserAsync(List<AuthorityRole> roles, AuthorityUser user, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var usrValidation = await IsValidUserAsync(user, cancellationToken);
|
||||
if (!usrValidation.Success)
|
||||
{
|
||||
_logger.Error(usrValidation.Exception ?? new Exception("Validation for user failed!"), "Invalid user!");
|
||||
return;
|
||||
}
|
||||
|
||||
var checkResult = await RoleRepository.HasRolesAsync(user.Id, roles, cancellationToken);
|
||||
if (!checkResult.Success)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var rolesToRemove = roles;
|
||||
if (checkResult.Count != 0)
|
||||
{
|
||||
rolesToRemove = roles.Where(r => !checkResult.Items.Contains(r.Id)).ToList();
|
||||
}
|
||||
|
||||
var removeResult = await RoleRepository.DeleteRolesLinkAsync(rolesToRemove, user.Id, cancellationToken);
|
||||
if (!removeResult.Success)
|
||||
{
|
||||
_logger.Error(removeResult.Exception ?? new Exception("Removing roles from user failed!"), "Failed to remove roles from user!");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task AddRoleToGroupAsync(AuthorityRole role, AuthorityGroup group, CancellationToken cancellationToken = default)
|
||||
public async Task AddRolesToGroupAsync(List<AuthorityRole> roles, AuthorityGroup group, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var checkResult = await RoleRepository.HasRolesAsync(group.Id, roles, cancellationToken);
|
||||
if (!checkResult.Success)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var rolesToAdd = roles;
|
||||
if (checkResult.Count != 0)
|
||||
{
|
||||
rolesToAdd = roles.Where(r => !checkResult.Items.Contains(r.Id)).ToList();
|
||||
}
|
||||
|
||||
var addResult = await RoleRepository.AddRolesLinkAsync(rolesToAdd, group.Id, cancellationToken);
|
||||
if (!addResult.Success)
|
||||
{
|
||||
_logger.Error(addResult.Exception ?? new Exception("Adding roles to group failed!"), "Failed to add roles to group!");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -60,26 +120,19 @@ public partial class AuthorityManager
|
||||
return ListResult<AuthorityRole>.Failed("Invalid user");
|
||||
}
|
||||
|
||||
List<AuthorityRole> roles = [];
|
||||
var usrRoles = await GetUserRolesAsync(user, cancellationToken);
|
||||
if (usrRoles.Success)
|
||||
{
|
||||
roles.AddRange(usrRoles.Items);
|
||||
}
|
||||
var searchIds = new List<Guid> { user.Id };
|
||||
|
||||
var usrGroups = await GetUserGroupsAsync(user, cancellationToken);
|
||||
if (usrGroups.Success)
|
||||
{
|
||||
var groupRolesResult = await GetGroupRolesAsync(usrGroups.Items.Select(g => g.Id).ToList(), cancellationToken);
|
||||
if (groupRolesResult.Success)
|
||||
{
|
||||
roles.AddRange(groupRolesResult.Items);
|
||||
}
|
||||
searchIds.AddRange(usrGroups.Items.Select(g => g.Id).ToList());
|
||||
}
|
||||
return ListResult<AuthorityRole>.Ok(roles, roles.Count);
|
||||
|
||||
return await RoleRepository.GetLinkedRolesAsync(searchIds, cancellationToken);
|
||||
}
|
||||
|
||||
public async Task<ListResult<AuthorityRole>> GetGroupRolesAsync(List<Guid> groupIds, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return ListResult<AuthorityRole>.Failed("Not implemented!");
|
||||
return await RoleRepository.GetLinkedRolesAsync(groupIds, cancellationToken);
|
||||
}
|
||||
}
|
@@ -91,7 +91,7 @@ public partial class AuthorityManager
|
||||
|
||||
public async Task<Result> IsValidUserAsync(AuthorityUser user, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var usrResult = await userRepository.GetVersionAsync(user, cancellationToken);
|
||||
var usrResult = await UserRepository.GetVersionAsync(user, cancellationToken);
|
||||
return usrResult;
|
||||
}
|
||||
}
|
@@ -1,12 +1,8 @@
|
||||
namespace DotBased.AspNet.Authority.Models.Authority;
|
||||
|
||||
public class AuthorityAttribute(string attributeKey, Guid bound)
|
||||
public class AuthorityAttribute(string attributeKey, Guid foreignKey)
|
||||
{
|
||||
public AuthorityAttribute() : this(string.Empty, Guid.NewGuid())
|
||||
{
|
||||
}
|
||||
|
||||
public Guid BoundId { get; set; } = bound;
|
||||
public Guid ForeignKey { get; set; } = foreignKey;
|
||||
|
||||
public string AttributeKey { get; set; } = attributeKey;
|
||||
|
||||
|
@@ -9,7 +9,7 @@ public class AuthorityRole()
|
||||
|
||||
public Guid Id { get; set; } = Guid.NewGuid();
|
||||
|
||||
public string? Name { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
|
||||
public long Version { get; set; }
|
||||
|
||||
@@ -17,5 +17,5 @@ public class AuthorityRole()
|
||||
|
||||
public IEnumerable<AuthorityAttribute> Attributes { get; set; } = [];
|
||||
|
||||
public override string ToString() => Name ?? string.Empty;
|
||||
public override string ToString() => Name;
|
||||
}
|
@@ -23,7 +23,7 @@ public class AuthorityResult<TResultValue> : Result<TResultValue>
|
||||
public IReadOnlyList<ValidationError>? ValidationErrors { get; }
|
||||
|
||||
|
||||
public static AuthorityResult<TResultValue> Ok(TResultValue? value) => new AuthorityResult<TResultValue>(true, value:value);
|
||||
public new static AuthorityResult<TResultValue> Ok(TResultValue? value) => new AuthorityResult<TResultValue>(true, value:value);
|
||||
|
||||
public static AuthorityResult<TResultValue> Error(string errorMessage, ResultFailReason reason = ResultFailReason.Error) =>
|
||||
new AuthorityResult<TResultValue>(false, errorMessage, reason:reason);
|
||||
|
@@ -6,6 +6,7 @@ public interface IGroupRepository
|
||||
{
|
||||
public Task<ListResult<AuthorityGroupItem>> GetGroupsAsync(int limit = 20, int offset = 0, string search = "", CancellationToken cancellationToken = default);
|
||||
public Task<Result<AuthorityGroup>> GetGroupByIdAsync(string id, CancellationToken cancellationToken = default);
|
||||
public Task<ListResult<AuthorityGroup>> GetUserGroupsAsync(AuthorityUser user, CancellationToken cancellationToken = default);
|
||||
public Task<Result<AuthorityGroup>> CreateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default);
|
||||
public Task<Result<AuthorityGroup>> UpdateGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default);
|
||||
public Task<Result> DeleteGroupAsync(AuthorityGroup group, CancellationToken cancellationToken = default);
|
||||
|
@@ -8,5 +8,17 @@ public interface IRoleRepository
|
||||
public Task<Result<AuthorityRole>> GetRoleByIdAsync(string id, CancellationToken cancellationToken = default);
|
||||
public Task<Result<AuthorityRole>> CreateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default);
|
||||
public Task<Result<AuthorityRole>> UpdateRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default);
|
||||
public Task<Result> DeleteRoleAsync(AuthorityRole role, CancellationToken cancellationToken = default);
|
||||
public Task<Result> DeleteRolesAsync(List<AuthorityRole> roles, CancellationToken cancellationToken = default);
|
||||
public Task<Result> AddRolesLinkAsync(List<AuthorityRole> roles, Guid linkId, CancellationToken cancellationToken = default);
|
||||
public Task<ListResult<AuthorityRole>> GetLinkedRolesAsync(List<Guid> linkIds, CancellationToken cancellationToken = default);
|
||||
public Task<Result> DeleteRolesLinkAsync(List<AuthorityRole> roles, Guid linkId, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Return the role ids the linkId has.
|
||||
/// </summary>
|
||||
/// <param name="linkId"></param>
|
||||
/// <param name="roles"></param>
|
||||
/// <param name="cancellationToken"></param>
|
||||
/// <returns></returns>
|
||||
public Task<ListResult<Guid>> HasRolesAsync(Guid linkId, List<AuthorityRole> roles, CancellationToken cancellationToken = default);
|
||||
}
|
Reference in New Issue
Block a user