public async Task <Response <string> > ChangeUserRole(string userId, ChangeRoleDto model)
        {
            var response = new Response <string>();
            var user     = await _userManager.FindByIdAsync(userId);

            var currentRoles = await _userManager.GetRolesAsync(user);

            if (user == null)
            {
                response.Message = ($"user with {user.Id} could not be found");
                return(response);
            }

            if (!(await _userManager.IsInRoleAsync(user, model.Role)))
            {
                await _userManager.RemoveFromRoleAsync(user, currentRoles.Where(x => x == model.CurrentRole).ToString());

                await _userManager.AddToRoleAsync(user, model.Role);

                response.Message = "user role has been changed";
                response.Success = true;
            }
            else
            {
                response.Message = "user already has this role";
            }

            return(response);
        }
Ejemplo n.º 2
0
        public async Task <ActionResult> ChangeRoleToUser([FromBody] ChangeRoleDto model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var user = await _userManager.FindByNameAsync(model.Username);

            var role = await _context.Roles.FindAsync(model.RoleId);

            if (user == null || role == null)
            {
                return(NotFound("Either username or role is wrong"));
            }

            var alreadyAssigned = _context.UserRoles.Where(x => x.RoleId == role.Id && x.UserId == user.Id).FirstOrDefault();

            if (alreadyAssigned == null)
            {
                var result = await _userManager.AddToRoleAsync(user, role.Name);

                if (result.Succeeded)
                {
                    return(Ok($"Now {user.UserName} is a {role.Name}"));
                }

                else
                {
                    return(BadRequest(result.Errors));
                }
            }
            return(BadRequest($"{user.UserName} is {role.Name} already"));
        }
Ejemplo n.º 3
0
        public async Task <ActionResult> ChangeRoleAsync([FromBody] ChangeRoleDto changeRoleDto)
        {
            _logger.LogMethodCallingWithObject(changeRoleDto);
            await _userManager.ChangeRoleAsync(changeRoleDto);

            return(Ok());
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> ChangeUserRole(
            [FromServices] IChangeRoleUseCase useCase,
            [FromServices] ChangeRolePresenter presenter,
            [FromBody] ChangeRoleDto input)
        {
            await useCase.Execute(new ChangeRoleInput(input.UserId, input.DesiredRoles));

            return(presenter.ViewModel);
        }
        public async Task <IActionResult> ChangeRole(string userId, [FromBody] ChangeRoleDto model)
        {
            var result = await _userService.ChangeUserRole(userId, model);

            if (result.Success)
            {
                return(Ok(result));
            }
            return(BadRequest(result));
        }
Ejemplo n.º 6
0
        public async Task <IActionResult> ChangeRole(ChangeRoleDto changeRoleDto)
        {
            changeRoleDto.Username = changeRoleDto.Username.ToLower();

            var user = await _userRepo.ChangeUserRole(changeRoleDto.Username, changeRoleDto.Role);

            if (user == null)
            {
                return(BadRequest());
            }

            var userToReturn = _mapper.Map <UserDetailedDto>(user);

            return(Ok(userToReturn));
        }
Ejemplo n.º 7
0
        public IActionResult Put(int id, [FromBody] ChangeRoleDto dto
                                 , [FromServices] IChangeRoleCommand command
                                 , [FromServices] ChangeRoleValidator validator)
        {
            dto.Id = id;
            var result = validator.Validate(dto);

            if (result.IsValid)
            {
                Role role = _mapper.Map <Role>(dto);
                _useCaseExecutor.ExecuteCommand(command, role);
                return(Ok("Role updated successfully"));
            }

            return(UnprocessableEntity(UnprocessableEntityResponse.Message(result.Errors)));
        }
Ejemplo n.º 8
0
        public IActionResult ChangeRole(int userId, string title)
        {
            Log.Information("Attempting to change role of user" + userId + " to " + title);
            ChangeRoleDto u = _AdminService.ChangeRole(userId, title);

            if (u == null)
            {
                Log.Error("Failed to change role.");
                return(BadRequest());
            }
            else
            {
                Log.Information("Role changed successfully. " + u);
                return(Ok(u));
            }
        }
Ejemplo n.º 9
0
        public async Task ShouldReturnOkResultWhenValidModelPassed()
        {
            //Arrange
            var id    = Guid.NewGuid().ToString();
            var model = new ChangeRoleDto();

            MockUserService.Setup(x => x.ChangeUserRole(It.IsAny <string>(), It.IsAny <ChangeRoleDto>()))
            .Returns(Task.FromResult(new Response <string>()
            {
                Success = true
            }));
            var userController = new UserController(_serviceProvider);

            var expected = 200;

            //ACT
            var actual = await userController.ChangeRole(id, model) as OkObjectResult;

            //Assert
            //Assert.IsNotNull(actual);
            Assert.AreEqual(expected, actual.StatusCode);
        }
Ejemplo n.º 10
0
        public ChangeRoleDto ChangeRole(int userId, string title)
        {
            User u = _context.Users.Where(x => x.UserId == userId).FirstOrDefault();

            if (u == null)
            {
                return(null);
            }

            var userRole = _context.Roles.Where(x => x.Title == title).FirstOrDefault();

            u.Role   = userRole;
            u.RoleId = userRole.Id;
            ChangeRoleDto dto = new ChangeRoleDto();

            dto.UserId    = u.UserId;
            dto.FirstName = u.FirstName;
            dto.LastName  = u.LastName;
            dto.Email     = u.Email;
            dto.Title     = u.Role.Title;

            return(dto);
        }
Ejemplo n.º 11
0
 public async Task <ActionResult <UserToChangeRoleDto> > ChangeRole([FromRoute] int userId, [FromBody] ChangeRoleDto changeRoleDto)
 {
     return(await HandleExceptions(async() =>
     {
         if (ModelState.IsValid)
         {
             await _userManager.ChangeUserRoleAsync(userId, changeRoleDto.RoleName);
             return Ok();
         }
         return BadRequest("Model state is not valid");
     }));
 }