Пример #1
0
        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))));
        }
Пример #2
0
        public ActionResult Invite(InviteViewModel viewModel)
        {
            var toolDisplayName = "Orange County Stormwater Tools";
            var homeUrl         = SitkaRoute <HomeController> .BuildAbsoluteUrlHttpsFromExpression(x => x.Index(), NeptuneWebConfiguration.CanonicalHostNameRoot);

            var loginUrl =
                SitkaRoute <AccountController> .BuildAbsoluteUrlHttpsFromExpression(x => x.LogOn(),
                                                                                    NeptuneWebConfiguration.CanonicalHostName);

            var supportUrl = SitkaRoute <HelpController> .BuildAbsoluteUrlHttpsFromExpression(x => x.Support(), NeptuneWebConfiguration.CanonicalHostNameRoot);

            var inviteModel = new KeystoneService.KeystoneInviteModel
            {
                FirstName        = viewModel.FirstName,
                LastName         = viewModel.LastName,
                Email            = viewModel.Email,
                SiteName         = toolDisplayName,
                Subject          = $"Invitation to the {toolDisplayName}",
                WelcomeText      = $"You have been invited by a colleague to create an account in the <a href=\"{homeUrl}\">{toolDisplayName}</a>. The {toolDisplayName} application is a collaborative effort of Orange County Public Works, MS4 Permittees, and other organizations.",
                RedirectURL      = loginUrl,
                SupportBlock     = $"If you have any questions, please visit our <a href=\"{supportUrl}\">support page</a>",
                OrganizationGuid = viewModel.OrganizationGuid,
                SignatureBlock   = $"The {toolDisplayName} team"
            };

            var keystoneService = new KeystoneService(HttpRequestStorage.GetHttpContextUserThroughOwin());
            var response        = keystoneService.Invite(inviteModel);

            if (response.StatusCode != HttpStatusCode.OK || response.Error != null)
            {
                ModelState.AddModelError("Email", $"There was a problem inviting the user to Keystone: {response.Error.Message}.");
                if (response.Error.ModelState != null)
                {
                    foreach (var modelStateKey in response.Error.ModelState.Keys)
                    {
                        foreach (var err in response.Error.ModelState[modelStateKey])
                        {
                            ModelState.AddModelError(modelStateKey, err);
                        }
                    }
                }
            }

            if (!ModelState.IsValid)
            {
                return(ViewInvite(viewModel));
            }

            var keystoneUser = response.Payload.Claims;
            var existingUser = HttpRequestStorage.DatabaseEntities.People.GetPersonByPersonGuid(keystoneUser.UserGuid);

            if (existingUser != null)
            {
                SetMessageForDisplay($"{existingUser.GetFullNameFirstLastAndOrgAsUrl()} already has an account.</a>.");
                return(RedirectToAction(new SitkaRoute <UserController>(x => x.Detail(existingUser))));
            }

            var setJurisdictions = !CurrentPerson.IsAdministrator();
            var newUser          = CreateNewFirmaPerson(keystoneUser, keystoneUser.OrganizationGuid);

            HttpRequestStorage.DatabaseEntities.SaveChanges();

            if (setJurisdictions)
            {
                foreach (var stormwaterJurisdictionPerson in CurrentPerson.StormwaterJurisdictionPeople)
                {
                    newUser.StormwaterJurisdictionPeople.Add(new StormwaterJurisdictionPerson(stormwaterJurisdictionPerson.StormwaterJurisdictionID, newUser.PersonID));
                }
            }

            newUser.RoleID = Role.JurisdictionEditor.RoleID;

            HttpRequestStorage.DatabaseEntities.SaveChanges();

            SetMessageForDisplay(
                $"{newUser.GetFullNameFirstLastAndOrgAsUrl()} successfully added. You may want to assign them a role</a>.");
            return(RedirectToAction(new SitkaRoute <UserController>(x => x.Detail(newUser))));
        }