120 lines
3.8 KiB
C#
120 lines
3.8 KiB
C#
using AutoMapper;
|
|
using UserManagement.Application.Dtos.Incomming;
|
|
using UserManagement.Application.Dtos.Outgoing;
|
|
using UserManagement.Application.Interfaces;
|
|
using UserManagement.Domain.Entities;
|
|
using UserManagement.Infrastructure.Interfaces;
|
|
|
|
namespace UserManagement.Application.Services
|
|
{
|
|
public class UserRolesService : IUserRolesService
|
|
{
|
|
// CTOR
|
|
private readonly IUserRolesRepository _userRolesRepository;
|
|
private readonly IUserRepository _userRepository;
|
|
private readonly IRoleRepository _roleRepository;
|
|
private readonly IMapper _mapper;
|
|
|
|
public UserRolesService(IUserRolesRepository userRolesRepository, IUserRepository userRepository, IRoleRepository roleRepository, IMapper mapper)
|
|
{
|
|
_userRolesRepository = userRolesRepository;
|
|
_userRepository = userRepository;
|
|
_roleRepository = roleRepository;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
// CREATE ASSIGNMENT
|
|
public async Task<bool> CreateAssignmentAsync(CreatingUserRolesDto creatingUserRolesDto)
|
|
{
|
|
if (creatingUserRolesDto.UserId <= 0)
|
|
{
|
|
throw new ArgumentException("Ungültige Id!");
|
|
}
|
|
|
|
var user = await _userRepository.GetByIdAsync(creatingUserRolesDto.UserId);
|
|
|
|
if (user == null)
|
|
{
|
|
throw new KeyNotFoundException("Benutzer nicht gefunden!");
|
|
}
|
|
|
|
if (creatingUserRolesDto.RoleId <= 0)
|
|
{
|
|
throw new ArgumentException("Ungültige Id!");
|
|
}
|
|
|
|
var role = await _roleRepository.GetByIdAsync(creatingUserRolesDto.RoleId);
|
|
|
|
if (role == null)
|
|
{
|
|
throw new KeyNotFoundException("Rolle nicht gefunden!");
|
|
}
|
|
|
|
var userRoles = _mapper.Map<UserRoles>(creatingUserRolesDto);
|
|
|
|
var assigned = await _userRolesRepository.CreateAssignmentAsync(userRoles);
|
|
|
|
return assigned;
|
|
}
|
|
|
|
//// ASSIGNING ROLES BY NAMES
|
|
//public async Task<bool> AssignAsync(string username, string roleName)
|
|
//{
|
|
// var user = await _userRepository.GetByUsernameAsync(username);
|
|
// var role = await _roleRepository.GetByNameAsync(roleName);
|
|
|
|
// if (user is null || role is null)
|
|
// {
|
|
// return false;
|
|
// }
|
|
|
|
// var dto = new CreatingUserRolesDto()
|
|
// {
|
|
// RoleId = role.Id,
|
|
// UserId = user.Id
|
|
// };
|
|
|
|
// return await CreateAssignmentAsync(dto);
|
|
//}
|
|
|
|
// REMOVE ROLE FROM USER
|
|
public async Task<bool> RemoveRoleFromUserAsync(CreatingUserRolesDto creatingUserRolesDto)
|
|
{
|
|
if (creatingUserRolesDto.UserId <= 0)
|
|
{
|
|
throw new ArgumentException("Ungültige Id!");
|
|
}
|
|
|
|
var user = await _userRepository.GetByIdAsync(creatingUserRolesDto.UserId);
|
|
|
|
if (user == null)
|
|
{
|
|
throw new KeyNotFoundException("Benutzer nicht gefunden!");
|
|
}
|
|
|
|
if (creatingUserRolesDto.RoleId <= 0)
|
|
{
|
|
throw new ArgumentException("Ungültige Id!");
|
|
}
|
|
|
|
var role = await _roleRepository.GetByIdAsync(creatingUserRolesDto.RoleId);
|
|
|
|
if (role == null)
|
|
{
|
|
throw new KeyNotFoundException("Rolle nicht gefunden!");
|
|
}
|
|
|
|
var userRole = _mapper.Map<UserRoles>(creatingUserRolesDto);
|
|
|
|
if (userRole == null)
|
|
{
|
|
throw new KeyNotFoundException("Die Rolle ist dem Benutzer nicht zugewiesen!");
|
|
}
|
|
|
|
var result = await _userRolesRepository.DeleteAssignmentAsync(userRole);
|
|
|
|
return result;
|
|
}
|
|
}
|
|
}
|