/// <summary>
        /// Populate the drop down lists for editing the user
        /// </summary>
        /// <typeparam name="TViewModel"></typeparam>
        /// <param name="viewModel"></param>
        protected override void PopulateAdditionalEditViewModelAttributes <TViewModel>(TViewModel viewModel)
        {
            var model = viewModel as SecurityUserMaintenanceEditViewModel;

            //Get the list of available roles and the ones assigned by default to a new user.
            var userRolesRequest = new SecurityUserMaintenanceGetRolesForUserRequest()
            {
                SecurityUserId = model.SecurityUserId
            };
            var userRolesResponse = _service.GetRolesForUser(userRolesRequest);

            model.AvailableRoles = userRolesResponse.AvailableRoles;
            model.AssignedRoles  = userRolesResponse.AssignedRoles;

            //
            //And in the case where we submitted the form, but a validation error occurred causing
            //the assigned roles to not be saved, we add those back from the available to assigned list.
            //
            if (model.AssignedRoleIds != null && model.AssignedRoleIds.Count > 0)
            {
                foreach (var availableRole in model.AvailableRoles.Where(p => model.AssignedRoleIds.Contains(p.SecurityRoleId)))
                {
                    model.AssignedRoles.Add(availableRole);
                }
                model.AvailableRoles = model.AvailableRoles.Except(model.AssignedRoles).ToList();
                model.AssignedRoles  = model.AssignedRoles.OrderBy(p => p.Name).ToList();
            }
        }
        /// <summary>
        /// Post get, after the record is retrieved from a get request
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <typeparam name="TResponse"></typeparam>
        /// <typeparam name="TId"></typeparam>
        /// <param name="request"></param>
        /// <param name="response"></param>
        public override void PostGet <TEntity, TResponse, TId>(BaseServiceGetRequest <TId> request, TResponse response)
        {
            var getRequest  = request as SecurityUserMaintenanceGetRequest;
            var getResponse = response as SecurityUserMaintenanceGetResponse;

            //
            //Get the roles available for and assigned to the user.
            //
            var rolesForUserRequest = new SecurityUserMaintenanceGetRolesForUserRequest()
            {
                SecurityUserId = getRequest.Id
            };
            var rolesForUserResponse = GetRolesForUser(rolesForUserRequest);

            getResponse.AvailableRoles = rolesForUserResponse.AvailableRoles;
            getResponse.AssignedRoles  = rolesForUserResponse.AssignedRoles;
        }
        /// <summary>
        /// Helper method used to get the list of available roles and list of assigned roles for a user.
        /// </summary>
        /// <param name="securityUserId"></param>
        /// <param name="availableRoles"></param>
        /// <param name="assignedRoles"></param>
        public SecurityUserMaintenanceGetRolesForUserResponse GetRolesForUser(SecurityUserMaintenanceGetRolesForUserRequest request)
        {
            var allRoles = _repository.GetAll <SecurityRole>()
                           .Where(p => p.Active == true)
                           .OrderBy(p => p.Name)
                           .ToList();


            var assignedRoles = new List <SecurityRole>();

            if (request.SecurityUserId != null)
            {
                assignedRoles = _repository.GetAll <SecurityUserRoleMembership>(p => p.User, p => p.Role)
                                .Where(
                    p => p.SecurityUserId == request.SecurityUserId && p.Role.Active == true)
                                .OrderBy(p => p.Role.Name)
                                .Select(p => p.Role)
                                .ToList();
            }
            //
            //Add any default roles to the user if it is a new user being created.
            //
            else if (request.SecurityUserId == null)
            {
                assignedRoles = _repository.GetAll <SecurityRole>()
                                .Where(p => p.Active == true && p.Default == true)
                                .OrderBy(p => p.Name)
                                .ToList();
            }

            //
            //Get all the roles not yet assigned to the user
            //
            var availableRoles = allRoles.Except(assignedRoles).ToList();

            return(new SecurityUserMaintenanceGetRolesForUserResponse()
            {
                SecurityUserId = request.SecurityUserId,
                AvailableRoles = availableRoles,
                AssignedRoles = assignedRoles,
                IsSuccessful = true
            });
        }
        /// <summary>
        /// After the Record has been saved
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="R"></typeparam>
        /// <typeparam name="S"></typeparam>
        /// <param name="request"></param>
        /// <param name="originalRecord"></param>
        /// <param name="record"></param>
        /// <returns></returns>
        public override bool PostSave <T, R, S>(S request, T originalRecord, T record, R response)
        {
            var saveRequest  = request as SecurityUserMaintenanceSaveRequest;
            var saveResponse = response as SecurityUserMaintenanceSaveResponse;
            var securityUser = record as SecurityUser;

            //
            //Change the users password if a new password was submitted
            //
            if (!String.IsNullOrWhiteSpace(saveRequest.NewPassword) ||
                !String.IsNullOrWhiteSpace(saveRequest.NewPasswordConfirm))
            {
                var changePasswordRequest = new ChangePasswordRequest()
                {
                    UserName                             = securityUser.UserName,
                    AuthenticationMethod                 = securityUser.AuthenticationMethod,
                    SecurityUserId                       = securityUser.SecurityUserId,
                    NewPassword                          = saveRequest.NewPassword,
                    NewPasswordConfirm                   = saveRequest.NewPasswordConfirm,
                    CheckPasswordComplexity              = false,
                    CheckIfUserPasswordCanBeChanged      = false,
                    EnforcePasswordHistory               = false,
                    SendPasswordSuccessfullyChangedEmail = false
                };

                var changePasswordResponse = _passwordService.ChangePassword(changePasswordRequest);

                if (!changePasswordResponse.IsSuccessful)
                {
                    response.IsSuccessful = false;
                    response.Message      = "An error occurred setting the users password. " + changePasswordResponse.Message;
                }
            }

            //
            //Assign the roles to the user submitted in the save request
            //
            if (saveRequest.AssignedRoleIds != null)
            {
                var assignedRoles = _repository.GetAll <SecurityUserRoleMembership>(p => p.User, p => p.Role)
                                    .Where(p => p.SecurityUserId == securityUser.SecurityUserId && p.Role.Active == true)
                                    .ToList();
                //
                //Remove any roles currently assigned to the user that were not submitted in the list
                //of AssignedRoleIds (the list of roles the user should have)
                //
                foreach (var assignedRole in assignedRoles)
                {
                    if (!saveRequest.AssignedRoleIds.Contains(assignedRole.SecurityRoleId))
                    {
                        _repository.Delete(assignedRole);
                    }
                }

                //
                //Add any new roles to the user not yet assigned. The roles submitted in the list
                //of AssignedRoleIds are the current roles the user should have.
                //
                foreach (var assignedRoleId in saveRequest.AssignedRoleIds)
                {
                    if (assignedRoles.FirstOrDefault(p => p.SecurityRoleId == assignedRoleId) == null)
                    {
                        var securityUserRoleMembership = new SecurityUserRoleMembership()
                        {
                            SecurityRoleId = assignedRoleId,
                            SecurityUserId = securityUser.SecurityUserId,
                            Active         = true
                        };
                        _repository.Add(securityUserRoleMembership);
                    }
                }
            }

            _repository.Commit();

            //
            //Get the roles available for and assigned to the user.
            //
            var rolesForUserRequest = new SecurityUserMaintenanceGetRolesForUserRequest()
            {
                SecurityUserId = securityUser.SecurityUserId
            };
            var rolesForUserResponse = GetRolesForUser(rolesForUserRequest);

            saveResponse.AvailableRoles = rolesForUserResponse.AvailableRoles;
            saveResponse.AssignedRoles  = rolesForUserResponse.AssignedRoles;

            //Dont need the caller to commit after this processing is done
            return(false);
        }