// POST api/gateway/addusertorole
        public async Task <IActionResult> AddUserToRole(AddUserToRoleViewModel model, [FromHeader] string authorization)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            HttpParameters httParameters =
                HttpParametersService
                .GetHttpParameters(
                    model,
                    ConfigHelper.AppSetting(Constants.ServerUrls, Constants.AddUserToRole),
                    HttpMethod.Put,
                    string.Empty,
                    authorization
                    );

            AddUserToRoleResponse addUserToRoleResult = await _gWService.PostTo <AddUserToRoleResponse>(httParameters);

            if (addUserToRoleResult.StatusCode == 401)
            {
                return(await ResponseService.GetResponse <UnauthorizedObjectResult, AddUserToRoleResponse>(addUserToRoleResult, ModelState));
            }
            else if (addUserToRoleResult.StatusCode != 200)
            {
                return(await ResponseService.GetResponse <BadRequestObjectResult, AddUserToRoleResponse>(addUserToRoleResult, ModelState));
            }

            return(new OkObjectResult(addUserToRoleResult));
        }
示例#2
0
        public override async Task <AddUserToRoleResponse> AddUserToRole(AddUserToRoleRequest request, ServerCallContext context)
        {
            var user = await _userService.FindByIdAsync(request.UserId);

            if (user == null)
            {
                throw context.NotFoundRpcException($"The user '{request.UserId}' wasn't found.");
            }

            if (await _userService.IsInRoleAsync(user, request.RoleName))
            {
                var existingRole = await _roleService.FindByNameAsync(request.RoleName);

                var detail = $"The role '{existingRole.Name}' already exists. (roleId=${existingRole.Id})";

                var response = new AddUserToRoleResponse
                {
                    Role = new RoleDto
                    {
                        Id   = existingRole.Id.ToString(),
                        Name = existingRole.Name
                    }
                };

                return(context.AlreadyExists(response, detail));
            }

            var result = await _userService.AddToRoleAsync(user, request.RoleName);

            if (result.Succeeded)
            {
                var role = await _roleService.FindByNameAsync(request.RoleName);

                var detail = $"The role '{role.Name}' as been created. (roleId=${role.Id})";

                var response = new AddUserToRoleResponse
                {
                    Role = new RoleDto
                    {
                        Id   = role.Id.ToString(),
                        Name = role.Name
                    }
                };

                return(context.Ok(response, detail));
            }

            throw context.FailedPreconditionRpcException(result, string.Empty);
        }
        public static AddUserToRoleResponse WrapAddRoleToUserResponse(string email, string role)
        {
            AddUserToRoleResponse responseMessage =
                new AddUserToRoleResponse()
            {
                Email       = email,
                Role        = role,
                Code        = "role_added_to_user",
                StatusCode  = 200,
                Description = "Role has successfully been added to the user.",
                Error       = "no_error"
            };

            return(responseMessage);
        }
        public static AddUserToRoleResponse WrapRemoveUserFromRole(
            string userId,
            string email,
            string role,
            int statusCode)
        {
            AddUserToRoleResponse responseMessage =
                new AddUserToRoleResponse()
            {
                Role        = role,
                Email       = email,
                UserId      = userId,
                Code        = "role_has_successfully_been_removed",
                Description = "Role hase successfully been removed from user",
                Error       = "Role has successfully been removed.",
                StatusCode  = statusCode
            };

            return(responseMessage);
        }