Example #1
0
        public async Task <IActionResult> ChangeUserRole([FromBody] ChangeUserRoleDto changeUserRoleDto)
        {
            var result = await _assignmentUserToWalletService.ChangeUserRole(changeUserRoleDto, UserId);

            if (!result.Succedeed)
            {
                return(BadRequest());
            }
            return(NoContent());
        }
        public async Task <Result> ChangeUserRole(ChangeUserRoleDto changeUserRoleDto, string modifierId)
        {
            var validationResult = await Validate(changeUserRoleDto, modifierId);

            if (!validationResult.Succedeed)
            {
                return(validationResult);
            }

            var userWallet = await _walletRepository.GetWithoutDependencies(changeUserRoleDto.WalletId.ToDeobfuscated(),
                                                                            changeUserRoleDto.UserId);

            if (userWallet == null)
            {
                return(Result.Failure());
            }
            return(ChangeUserAssignment(userWallet, changeUserRoleDto.Role.ToEnumValue <WalletRole>()));
        }
        public void Execute(ChangeUserRoleDto request)
        {
            if (_actor.RoleType != RoleType.Administrator && _actor.RoleType != RoleType.Moderator)
            {
                throw new NotAllowedException(UseCase.getUseCase(this.Id), _actor, "Only administrator or moderator can change role for users.");
            }

            var user = _context.Users.Include(u => u.Role).FirstOrDefault(u => !u.IsDeleted && u.Id == request.UserId);

            if (user == null)
            {
                throw new EntityNotFoundException(request.UserId, typeof(User));
            }

            // can only perform action on role that lower than own
            if (_actor.RoleType != RoleType.Administrator && user.Role.RoleType <= _actor.RoleType)
            {
                throw new DependencyException(UseCase.getUseCase(this.Id), _actor, "You can only perform this action on users with role types with lower priviledges than yours.");
            }

            // can only assign roles with priviledges lower than own
            if (_actor.RoleType != RoleType.Administrator && _context.Roles.Find(request.RoleId).RoleType <= _actor.RoleType)
            {
                throw new DependencyException(UseCase.getUseCase(this.Id), _actor, "You can only assign roles with role types with lower priviledges than yours.");
            }

            // can't change role of administrator if he is only active user with that role
            if (user.Role.RoleType == RoleType.Administrator && !_context.Users.Include(u => u.Role).Any(u => !u.IsDeleted && u.IsActive && u.Id != user.Id && u.Role.RoleType == RoleType.Administrator))
            {
                throw new DependencyException(UseCase.getUseCase(this.Id), _actor, $"Can't change role of only active user with role type Administraotr.");
            }

            _validator.ValidateAndThrow(request);

            _mapper.Map <ChangeUserRoleDto, User>(request, user);

            _context.SaveChanges(_actor.Id);
        }
Example #4
0
 public IActionResult ChangeRole(int id, [FromBody] ChangeUserRoleDto request, [FromServices] IChangeUserRoleCommand command)
 {
     request.UserId = id;
     _executor.ExecuteCommand(command, request);
     return(NoContent());
 }