private UserDto GetAndUpdateUserFromEmailOrReturnNewUser(KeystoneService.KeystoneApiResponse <KeystoneService.KeystoneNewUserModel> response, string email, int roleID) { var keystoneUser = response.Payload.Claims; var existingUser = EFModels.Entities.User.GetByEmail(_dbContext, email); if (existingUser != null) { existingUser = EFModels.Entities.User.UpdateUserGuid(_dbContext, existingUser.UserID, keystoneUser.UserGuid); return(existingUser); } var newUser = new UserUpsertDto { FirstName = keystoneUser.FirstName, LastName = keystoneUser.LastName, OrganizationName = keystoneUser.OrganizationName, Email = keystoneUser.Email, PhoneNumber = keystoneUser.PrimaryPhone, RoleID = roleID }; var user = EFModels.Entities.User.CreateNewUser(_dbContext, newUser, keystoneUser.LoginName, keystoneUser.UserGuid); return(user); }
public ActionResult Invite(InviteViewModel viewModel) { var toolDisplayName = MultiTenantHelpers.GetToolDisplayName(); var homeUrl = SitkaRoute <HomeController> .BuildAbsoluteUrlHttpsFromExpression(x => x.Index()); var supportUrl = SitkaRoute <HelpController> .BuildAbsoluteUrlHttpsFromExpression(x => x.RequestSupport()); var tenantAttribute = MultiTenantHelpers.GetTenantAttributeFromCache(); var primaryContactFullName = tenantAttribute.PrimaryContactPerson .GetFullNameFirstLast(); var primaryContactOrganizationName = tenantAttribute.PrimaryContactPerson .Organization.OrganizationName; var primaryContactEmail = tenantAttribute.PrimaryContactPerson.Email; KeystoneService.KeystoneApiResponse <KeystoneService.KeystoneNewUserModel> keystoneNewUserResponse = null; var theSelectedOrganization = HttpRequestStorage.DatabaseEntities.Organizations.GetOrganization(viewModel.OrganizationID); Check.EnsureNotNull(theSelectedOrganization); bool organizationSelectedIsNotUnknownOrg = !theSelectedOrganization.IsUnknown(); if (organizationSelectedIsNotUnknownOrg && theSelectedOrganization.KeystoneOrganizationGuid == null) { // If we pick an Org, it must already be in Keystone, and so the local dbo.Organization must have a valid OrganizationGuid ModelState.AddModelError("OrganizationID", $"Organization is not in Keystone"); } else { var inviteModel = new KeystoneService.KeystoneInviteModel { FirstName = viewModel.FirstName, LastName = viewModel.LastName, Email = viewModel.Email, SiteName = toolDisplayName, Subject = $"Invitation to {toolDisplayName}", WelcomeText = $"You have been invited by {CurrentPerson.GetFullNameFirstLast()} at {CurrentPerson.Organization.OrganizationName} ({CurrentPerson.Email}), to create an account in <a href=\"{homeUrl}\">{toolDisplayName}</a>.", RedirectURL = homeUrl, SupportBlock = $"If you have any questions, please visit our <a href=\"{supportUrl}\">support page</a> or contact {primaryContactFullName} at {primaryContactOrganizationName} ({primaryContactEmail})", OrganizationGuid = theSelectedOrganization.KeystoneOrganizationGuid, SignatureBlock = $"The {toolDisplayName} team" }; var keystoneService = new KeystoneService(HttpRequestStorage.GetHttpContextUserThroughOwin()); keystoneNewUserResponse = keystoneService.Invite(inviteModel); if (keystoneNewUserResponse.StatusCode != HttpStatusCode.OK || keystoneNewUserResponse.Error != null) { ModelState.AddModelError("Email", $"There was a problem inviting the user to Keystone: {keystoneNewUserResponse.Error.Message}."); if (keystoneNewUserResponse.Error.ModelState != null) { foreach (var modelStateKey in keystoneNewUserResponse.Error.ModelState.Keys) { foreach (var err in keystoneNewUserResponse.Error.ModelState[modelStateKey]) { ModelState.AddModelError(modelStateKey, err); } } } } else { // Sanity check - did we get back the same Organization GUID we asked for? // (The GUID could also be null here, for the unknown org, but in that case we'll also get back null so this check is still valid.) var keystoneUserTmp = keystoneNewUserResponse.Payload.Claims; if (keystoneUserTmp.OrganizationGuid != inviteModel.OrganizationGuid) { string errorMessage = $"There was a problem with the Keystone Organization GUID Invited:{inviteModel.OrganizationGuid} Received back: {keystoneUserTmp.OrganizationGuid}. Please contact Sitka for assistance."; _logger.Error(errorMessage); ModelState.AddModelError("OrganizationID", errorMessage); } } } if (!ModelState.IsValid) { return(ViewInvite(viewModel)); } var keystoneUser = keystoneNewUserResponse.Payload.Claims; var existingUser = HttpRequestStorage.DatabaseEntities.People.GetPersonByPersonGuid(keystoneUser.UserGuid); if (existingUser != null) { SetMessageForDisplay($"{existingUser.GetFullNameFirstLastAndOrgAsUrl(CurrentFirmaSession)} already has an account.</a>."); return(RedirectToAction(new SitkaRoute <UserController>(x => x.Detail(existingUser)))); } var newUser = CreateNewFirmaPerson(keystoneUser, keystoneUser.OrganizationGuid); HttpRequestStorage.DatabaseEntities.SaveChanges(); newUser.RoleID = Role.Normal.RoleID; HttpRequestStorage.DatabaseEntities.SaveChanges(); if (!viewModel.DoNotSendInviteEmailIfExisting && !keystoneNewUserResponse.Payload.Created) { SendExistingKeystoneUserCreatedMessage(newUser, CurrentPerson); } SetMessageForDisplay( $"{newUser.GetFullNameFirstLastAndOrgAsUrl(CurrentFirmaSession)} successfully added. You may want to assign them a role</a>."); return(RedirectToAction(new SitkaRoute <UserController>(x => x.Detail(newUser)))); }