private async Task <string[]> ManipulateRolesAsync( int organizationId, string userId, string role, Func <ICollection <string>, string, bool> f) { await _organizationRetrievalService.GetOrganizationByIdAsync(organizationId); // just to check its existence var user = await _userRetrievalService.GetUserByIdAsync(userId, new UserRetrievalOptions { IncludeOrgMembership = true }); var m = user.OrganizationMembership .FirstOrDefault(m => m.OrganizationId == organizationId); if (m == null) { throw new NotFoundException($"User {userId} is not a member of organization {organizationId}"); } var roles = m.Roles .Select(r => r.Role) .ToHashSet(); if (f(roles, role)) { await _rolesManagementService .UpdateOrganizationMemberRolesAsync(m.Id, roles.ToArray()); } return(roles.ToArray()); }
public async Task <IActionResult> OnPostAsync() { if (!ModelState.IsValid) { return(await PageAsync()); } var user = new ApplicationUser { Name = Input.Name, UserName = Input.Email, Email = Input.Email, PhoneNumber = (Input.PhoneCountryCode + Input.PhoneNumber) }; await _userManager.CreateAsync(user); try { if (User.IsInRole(Roles.SuperAdmin)) { // Only SuperAdmin can choose whether to associate user with org, // Admins can only create org members. if (Input.IsOrgMember) { var member = await _organizationMemberManagementService.AddToOrganizationAsync(user); await _memberRolesManagementService.UpdateOrganizationMemberRolesAsync(member.Id, MemberRoles.Where(r => r.Value) .Select(r => r.Key) .ToArray()); } } else { await _organizationMemberManagementService.AddToOrganizationAsync(user); } } catch (AccessViolationException e) { _logger.LogError(e, e.Message); return(Forbid()); } return(RedirectToPage("./Index")); }
public async Task <IActionResult> OnPostAsync(string id) { if (!ModelState.IsValid) { return(await PageAsync(id)); } var user = await _userManager.FindByIdAsync(id); if (user == null) { throw new ApplicationException($"Unable to load user with ID '{_userManager.GetUserId(User)}'."); } if (Input.Name != user.Name) { user.Name = Input.Name; var setNameResult = await _userManager.UpdateAsync(user); if (!setNameResult.Succeeded) { throw new ApplicationException($"Unexpected error occurred setting email for user with ID '{user.Id}'."); } } if (Input.Email != user.Email) { // Set new email address var setEmailResult = await _userManager.SetEmailAsync(user, Input.Email); // Change username to the new address user.UserName = Input.Email; var setNameResult = await _userManager.UpdateAsync(user); if (!setEmailResult.Succeeded) { throw new ApplicationException($"Unexpected error occurred setting email for user with ID '{user.Id}'."); } } if (Input.PhoneNumber != user.PhoneNumber) { var setPhoneResult = await _userManager.SetPhoneNumberAsync(user, Input.PhoneNumber); if (!setPhoneResult.Succeeded) { throw new ApplicationException($"Unexpected error occurred setting phone number for user with ID '{user.Id}'."); } } if (Input.SignatureImageBase64 != user.SignatureImageBase64) { user.SignatureImageBase64 = Input.SignatureImageBase64; var setSignatureResult = await _userManager.UpdateAsync(user); if (!setSignatureResult.Succeeded) { throw new ApplicationException($"Unexpected error occurred setting email for user with ID '{user.Id}'."); } } try { if (User.IsInRole(Roles.SuperAdmin)) { if (Input.IsOrgMember) { var member = await _organizationMemberManagementService.AddToOrganizationAsync(user); await _memberRolesManagementService.UpdateOrganizationMemberRolesAsync(member.Id, MemberRoles.Where(r => r.Value) .Select(r => r.Key) .ToArray()); } else { await _organizationMemberManagementService.RemoveFromOrganizationAsync(user); } } } catch (AccessViolationException e) { _logger.LogError(e, e.Message); return(Forbid()); } return(RedirectToPage("Index")); }