public async Task <RightNavigation> Handle(GetRightByIdQuery request, CancellationToken cancellationToken) { var right = await _readRightRepository.GetOneRightNavigationById(request.RightId); if (right is null) { throw new NotFoundException(request.RightId.ToString(), "Right"); } return(right); }
public async Task <string> Handle(ChangeUserRightsCommand request, CancellationToken cancellationToken) { var user = await _userRepository.FindByIdAsync(new UserId(request.UserId)); var rights = new List <RightNavigation>(); foreach (var rightId in request.RightsIds) { var right = await _rightsRepository.GetOneRightNavigationById(rightId); if (right is null) { throw new NotFoundException(rightId.ToString(), "Right"); } rights.Add(right); } if (user is null) { throw new NotFoundException(_currentUserService.UserId.Value.ToString(), "user"); } if (user.Rights.Any(r => r.Name == RightEnum.Admin) && rights.All(r => r.Name != RightEnum.Admin)) { var adminRight = await _rightsRepository.GetOneRightNavigationByName(RightEnum.Admin); if (adminRight is null) { throw new NotFoundException(RightEnum.Admin.ToString(), "Right"); } var adminUsers = await _readUserRepository.FindAllPublicUsers(new GetPublicUsersQuery() { RightIdFilter = adminRight.Id }); if (adminUsers.Count <= 1) { throw new ApplicationException("Cannot remove user admin right if user is the last admin"); } } user.SetRights(rights.Select(r => new Right(r.Name)).ToList()); await _userRepository.SetAsync(user); return(user.Id.ToString()); }