private void InformGeoOfOrphanedOrganisations(List <Organisation> orphanedOrganisations)
 {
     // Email GEO for each newly orphaned organisation
     orphanedOrganisations
     .ForEach(
         org => emailSendingService.SendGeoOrphanOrganisationEmail(org.OrganisationName));
 }
示例#2
0
        public IActionResult RemoveOrganisation(RemoveOrganisationModel model)
        {
            // Ensure user has completed the registration process
            IActionResult checkResult = CheckUserRegisteredOk(out User currentUser);

            if (checkResult != null)
            {
                return(checkResult);
            }

            // Decrypt org id
            if (!model.EncOrganisationId.DecryptToId(out long organisationId))
            {
                return(new HttpBadRequestResult($"Cannot decrypt employer id {model.EncOrganisationId}"));
            }

            // Check the current user has permission for this organisation
            UserOrganisation userOrgToUnregister = currentUser.UserOrganisations.FirstOrDefault(uo => uo.OrganisationId == organisationId);

            if (userOrgToUnregister == null)
            {
                return(new HttpForbiddenResult($"User {currentUser?.EmailAddress} is not registered for employer id {organisationId}"));
            }

            // Decrypt user id
            if (!model.EncUserId.DecryptToId(out long userIdToRemove))
            {
                return(new HttpBadRequestResult($"Cannot decrypt user id '{model.EncUserId}'"));
            }

            Organisation sourceOrg        = userOrgToUnregister.Organisation;
            User         userToUnregister = currentUser;

            if (currentUser.UserId != userIdToRemove)
            {
                // Ensure the other user has registered this organisation
                UserOrganisation otherUserOrg =
                    sourceOrg.UserOrganisations.FirstOrDefault(uo => uo.OrganisationId == organisationId && uo.UserId == userIdToRemove);
                if (otherUserOrg == null)
                {
                    return(new HttpForbiddenResult($"User {userIdToRemove} is not registered for employer id {organisationId}"));
                }

                userToUnregister    = otherUserOrg.User;
                userOrgToUnregister = otherUserOrg;
            }

            // Remove the registration
            User         actionByUser = IsImpersonatingUser == false ? currentUser : OriginalUser;
            Organisation orgToRemove  = userOrgToUnregister.Organisation;

            RegistrationRepository.RemoveRegistration(userOrgToUnregister, actionByUser);

            // Email user that has been unregistered
            emailSendingService.SendRemovedUserFromOrganisationEmail(
                userToUnregister.EmailAddress,
                orgToRemove.OrganisationName,
                userToUnregister.Fullname);

            // Email the other users of the organisation
            IEnumerable <string> emailAddressesForOrganisation = orgToRemove.UserOrganisations.Select(uo => uo.User.EmailAddress);

            foreach (string emailAddress in emailAddressesForOrganisation)
            {
                emailSendingService.SendRemovedUserFromOrganisationEmail(
                    emailAddress,
                    orgToRemove.OrganisationName,
                    userToUnregister.Fullname);
            }

            // Send the notification to GEO for each newly orphaned organisation
            if (orgToRemove.GetIsOrphan())
            {
                emailSendingService.SendGeoOrphanOrganisationEmail(orgToRemove.OrganisationName);
            }

            //Make sure this organisation is no longer selected
            if (ReportingOrganisationId == organisationId)
            {
                ReportingOrganisationId = 0;
            }

            this.StashModel(model);

            return(RedirectToAction("RemoveOrganisationCompleted"));
        }
        private IActionResult UnlinkUserAndOrganisationPost(
            long organisationId,
            long userId,
            AdminRemoveUserViewModel viewModel,
            bool fromViewUserPage)
        {
            UpdateAdminRemoveUserViewModelFromUserOrganisation(viewModel, organisationId, userId);
            viewModel.FromViewUserPage = fromViewUserPage;

            viewModel.ParseAndValidateParameters(Request, m => m.Reason);

            if (viewModel.HasAnyErrors())
            {
                // If there are any errors, return the user back to the same page to correct the mistakes
                return(View("ConfirmRemoving", viewModel));
            }

            var user         = dataRepository.Get <User>(viewModel.UserId);
            var organisation = dataRepository.Get <Organisation>(viewModel.OrganisationId);

            // Remove user organisation
            UserOrganisation userOrg = dataRepository.GetAll <UserOrganisation>()
                                       .Where(u => u.UserId == viewModel.UserId)
                                       .Where(u => u.OrganisationId == viewModel.OrganisationId)
                                       .FirstOrDefault();

            dataRepository.Delete(userOrg);

            dataRepository.SaveChanges();

            // Email user that has been unregistered
            emailSendingService.SendRemovedUserFromOrganisationEmail(
                user.EmailAddress,
                organisation.OrganisationName,
                user.Fullname);

            // Email the other users of the organisation
            IEnumerable <string> emailAddressesForOrganisation = organisation.UserOrganisations.Select(uo => uo.User.EmailAddress);

            foreach (string emailAddress in emailAddressesForOrganisation)
            {
                emailSendingService.SendRemovedUserFromOrganisationEmail(
                    emailAddress,
                    organisation.OrganisationName,
                    user.Fullname);
            }

            // Send the notification to GEO for each newly orphaned organisation
            if (organisation.GetIsOrphan())
            {
                emailSendingService.SendGeoOrphanOrganisationEmail(organisation.OrganisationName);
            }

            // Audit log
            auditLogger.AuditChangeToOrganisation(
                AuditedAction.AdminRemoveUserFromOrganisation,
                organisation,
                new { RemovedUserId = user.UserId, viewModel.Reason },
                User);

            return(View("SuccessfullyRemoved", viewModel));
        }