public override async Task <AddUserClaimResponse> AddUserClaim(AddUserClaimRequest request, ServerCallContext context) { var user = await _userService.FindByIdAsync(request.UserId); if (user == null) { throw context.NotFoundRpcException($"The user '{request.UserId}' wasn't found."); } var claims = await _userService.GetClaimsAsync(user); var claim = new Claim(request.Claim.Type, request.Claim.Value); if (claims.Any(x => x.Type == claim.Type && x.Value == claim.Value)) { var detail = $"The claim type '{claim.Type}' (value=\"{claim.Value}\") already exists in user '{user.Email}'. (userId=${user.Id})"; var response = new AddUserClaimResponse { Claim = new UserClaimDto { Type = claim.Type, Value = claim.Value } }; return(context.AlreadyExists(response, detail)); } var result = await _userService.AddClaimAsync(user, claim); if (result.Succeeded) { var detail = $"The claim type '{claim.Type}' (value=\"{claim.Value}\") as been added to the user '{user.Email}'. (userId=${user.Id})"; var response = new AddUserClaimResponse { Claim = new UserClaimDto { Type = claim.Type, Value = claim.Value } }; return(context.Ok(response, detail)); } throw context.FailedPreconditionRpcException( result, $"Failed to add the claim type '{claim.Type}' (value=\"{claim.Value}\") to the user '{user.Email}'. (userId=${user.Id})"); }
public override async Task <AddRoleClaimResponse> AddRoleClaim(AddRoleClaimRequest request, ServerCallContext context) { if (!await _roleService.RoleExistsAsync(request.RoleName)) { throw context.NotFoundRpcException($"The role '{request.RoleName}' wasn't found."); } var role = await _roleService.FindByNameAsync(request.RoleName); var claims = await _roleService.GetClaimsAsync(role); var claim = new Claim(request.Claim.Type, request.Claim.Value); if (claims.Any(x => x.Type == claim.Type && x.Value == claim.Value)) { var detail = $"The claim type '{claim.Type}' (value=\"{claim.Value}\") already exists in role '{role.Name}'. (roleId=${role.Id})"; var response = new AddRoleClaimResponse { Claim = new RoleClaimDto { Type = claim.Type, Value = claim.Value } }; return(context.AlreadyExists(response, detail)); } var result = await _roleService.AddClaimAsync(role, claim); if (result.Succeeded) { var detail = $"The claim type '{claim.Type}' (value=\"{claim.Value}\") as been added to the role '{role.Name}'. (roleId=${role.Id})"; var response = new AddRoleClaimResponse { Claim = new RoleClaimDto { Type = claim.Type, Value = claim.Value } }; return(context.Ok(response, detail)); } throw context.FailedPreconditionRpcException( result, $"Failed to add the claim type '{claim.Type}' (value=\"{claim.Value}\") to the role '{role.Name}'. (roleId=${role.Id})"); }
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 override async Task <CreateRoleResponse> CreateRole(CreateRoleRequest request, ServerCallContext context) { if (await _roleService.RoleExistsAsync(request.RoleName)) { var existingRole = await _roleService.FindByNameAsync(request.RoleName); var detail = $"The role '{existingRole.Name}' already exists. (roleId=${existingRole.Id})"; var response = new CreateRoleResponse { Role = new RoleDto { Id = existingRole.Id.ToString(), Name = existingRole.Name } }; return(context.AlreadyExists(response, detail)); } var role = new Role { Id = Guid.NewGuid(), Name = request.RoleName }; var result = await _roleService.CreateAsync(role); if (result.Succeeded) { var detail = $"The role '{role.Name}' as been created. (roleId=${role.Id})"; var response = new CreateRoleResponse { Role = new RoleDto { Id = role.Id.ToString(), Name = role.Name } }; return(context.Ok(response, detail)); } throw context.FailedPreconditionRpcException(result, $"Failed to create the role '{role.Name}'. (roleId=${role.Id})"); }