public async Task <IActionResult> Edit(EditUserRoleRequest request)
        {
            var userRole = await _userRoleService.Edit(request);

            if (userRole == null)
            {
                return(NotFound());
            }

            return(Ok());
        }
Beispiel #2
0
        /// <summary>
        /// update user role
        /// </summary>
        /// <param name="request">user role request</param>
        /// <returns></returns>
        public async Task <UserRole> Edit(EditUserRoleRequest request)
        {
            var userRole = await GetById(request.Id);

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

            request.ToCopy(userRole);

            var users = await _userService.GetUsersByIds(request.UserIds);

            userRole.Users.RemoveAll(x => !request.UserIds.Contains(x.Id));
            userRole.Users.AddRange(users.Where(x => !userRole.Users.Contains(x)));

            await _context.SaveChangesAsync();

            return(userRole);
        }
Beispiel #3
0
        public async Task <IActionResult> EditUsersInRole([FromQuery] string roleId, EditUserRoleRequest request, CancellationToken cancellationToken)
        {
            var apiResponse = new Response();

            var role = await _roleManager.FindByIdAsync(roleId);

            if (role == null)
            {
                apiResponse.AddError(ExceptionCreator.CreateNotFoundError(nameof(role), $"Role id: {roleId} is not found"));
                return(NotFound(apiResponse));
            }

            var user = await _userManager.FindByIdAsync(request.UserId);

            IdentityResult result = null;

            try
            {
                if (request.IsSelected && !(await _userManager.IsInRoleAsync(user, role.Name)))
                {
                    result = await _userManager.AddToRoleAsync(user, role.Name);
                }
                else if (!request.IsSelected && await _userManager.IsInRoleAsync(user, role.Name))
                {
                    result = await _userManager.RemoveFromRoleAsync(user, role.Name);
                }

                if (result.Succeeded)
                {
                    return(Ok());
                }

                return(BadRequest(apiResponse));
            }
            catch (Exception exception)
            {
                return(_actionResultMapper.Map(exception));
            }
        }