public async void BtnConfirmClaimDelete_ClickAsync(bool isDeleteConfirmed) { if (!isDeleteConfirmed) { SetButtonStates(ButtonState.Enabled); _claimWaitingForDeleteConfirmation = null; return; } var editClaimResponse = await AdminService.DeleteClaimAsync(_claimWaitingForDeleteConfirmation); var claimsToEditbyAdminResponse = await AdminService.GetClaimsAsync(); ClaimsToEditByAdmin = Mapper.ProjectTo <AdminEditClaimVM>(claimsToEditbyAdminResponse.Result.Where(c => !c.Name.EqualsIgnoreCase("Email")).AsQueryable()).ToList(); if (editClaimResponse.IsError || claimsToEditbyAdminResponse.IsError) { SetButtonStates(ButtonState.Enabled); await Main.PromptMessageAsync(PromptType.Error, editClaimResponse.Message ?? claimsToEditbyAdminResponse.Message); _claimWaitingForDeleteConfirmation = null; return; } SetButtonStates(ButtonState.Enabled); await Main.PromptMessageAsync(PromptType.Success, editClaimResponse.Message); _claimWaitingForDeleteConfirmation = null; }
public void BtnDeleteClaim_ClickAsync(AdminEditClaimVM claimToDelete) { SetButtonStates(ButtonState.Disabled); _btnDeleteClaimStates[claimToDelete.Name] = ButtonState.Loading; ConfirmationDialog_DeleteClaim.Show($"Are you sure you want to delete Claim \"{claimToDelete.Name}\"?");; _claimWaitingForDeleteConfirmation = claimToDelete; }
public async Task <ApiResponse <AdminEditClaimVM> > EditClaimAsync(AdminEditClaimVM claimToedit) { try { var authUser = (await _accountService.GetAuthenticatedUserAsync())?.Result; return(await _httpClient.PostJTokenAsync <ApiResponse <AdminEditClaimVM> >("api/admin/editclaim", new { AuthenticatedUser = authUser, ClaimToEdit = claimToedit })); } catch (Exception ex) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status500InternalServerError, "API threw an exception while editing Claim", null, null, ex)); } }
public async Task <ApiResponse <AdminEditClaimVM> > EditClaimAsync(AuthenticateUserVM authUser, AdminEditClaimVM claimToEdit) { try { if (authUser == null || !authUser.IsAuthenticated || !authUser.HasRole("Admin")) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status401Unauthorized, "You are not Authorized to Edit Claims", null)); } if (claimToEdit.Name.IsNullOrWhiteSpace()) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status400BadRequest, "Claim Name cannot be Empty", new[] { new KeyValuePair <string, string>("Name", "You need to provide Claim Name") }.ToLookup())); } var claimsResp = await GetClaimsAsync(authUser); if (claimsResp.IsError) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status400BadRequest, "Unable to retrieve other Claims", null)); } var otherClaims = claimsResp.Result.Where(c => !c.Name.EqualsIgnoreCase(claimToEdit.OriginalName)); if (claimToEdit.Name.EqAnyIgnoreCase(otherClaims.Select(c => c.Name))) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status400BadRequest, "Claim Name cannot be a Duplicate", new[] { new KeyValuePair <string, string>("Name", "Claim Name is a Duplicate") }.ToLookup())); } if (!claimToEdit.GetUserNames().Any()) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status400BadRequest, "You need to choose at least one User because Claims exist soloely in the context of users", null)); } _db.UserClaims.RemoveBy(c => c.ClaimType.ToLower() == claimToEdit.OriginalName.ToLower()); // as much as I'd love to add 'EqualsInvariantIgnoreCase' in all these 'Queryable' backed places, I can't :/. await _db.SaveChangesAsync(); foreach (var claimVal in claimToEdit.Values) // claims have no table, they exist in thew context of users only so if sb removes a claim from all users, the claim is no longer stored anywhere { foreach (var userName in claimVal.UserNames) { var user = await _userManager.FindByNameAsync(userName); if (user == null) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status400BadRequest, $"Editing Claim \"{claimToEdit.Name}\" for User \"{userName}\" Failed, there is no such User", null)); } var hasClaimResp = await HasClaimAsync(user, claimToEdit.Name); if (hasClaimResp.IsError) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status400BadRequest, $"Checking Claim \"{claimToEdit.Name}\" existence for User \"{userName}\" Failed", null)); } var hasClaim = hasClaimResp.Result; if (!hasClaim) { var addClaimToUserResp = await _userManager.AddClaimAsync(user, new Claim(claimToEdit.Name, claimVal.Value)); if (!addClaimToUserResp.Succeeded) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status400BadRequest, $"Editing Claim \"{claimToEdit.Name}\" for User \"{userName}\" Failed. ({addClaimToUserResp.FirstError()})", null)); } } } } return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status201Created, $"Successfully Edited Claim: \"{claimToEdit.Name}\"", null, claimToEdit)); } catch (Exception ex) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status500InternalServerError, "Editing Claim Failed", null, null, ex)); } }
public async Task <ApiResponse <AdminEditClaimVM> > DeleteClaimAsync(AuthenticateUserVM authUser, AdminEditClaimVM claimToDelete) { try { if (authUser == null || !authUser.IsAuthenticated || !authUser.HasRole("Admin")) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status401Unauthorized, "You are not Authorized to Delete Claims", null)); } if (claimToDelete.Name.IsNullOrWhiteSpace()) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status400BadRequest, "Name for the Claim was not supplied, as it is done automatically it should never happen", null)); } var claimResp = await FindClaimByNameAsync(claimToDelete.Name); if (claimResp.IsError) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status400BadRequest, $"Claim \"{claimToDelete.Name}\" was not found, it should never happen", null)); } _db.UserClaims.RemoveBy(c => c.ClaimType.ToLower() == claimToDelete.Name.ToLower()); await _db.SaveChangesAsync(); return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status201Created, $"Successfully Deleted Claim \"{claimToDelete.Name}\"", null, claimToDelete)); } catch (Exception ex) { return(new ApiResponse <AdminEditClaimVM>(StatusCodeType.Status500InternalServerError, "Deleting Claim Failed", null, null, ex)); } }
public void BtnEditClaim_ClickAsync(AdminEditClaimVM claimToEdit) { SetButtonStates(ButtonState.Disabled); _btnEditClaimStates[claimToEdit.Name] = ButtonState.Loading; NavigationManager.NavigateTo($"admin/editclaim/{claimToEdit.Name}"); }