예제 #1
0
        /// <summary>
        /// Resets a user's password to a new, automatically generated password.
        /// </summary>
        /// <param name="username">The user to reset the password for.</param>
        /// <param name="answer">The password answer for the specified user.</param>
        /// <returns>
        /// The new password for the specified user.
        /// </returns>
        public override string ResetPassword(string username, string answer)
        {
            if (!EnablePasswordReset)
            {
                throw new NotSupportedException("Password Reset is not enabled");
            }

            UserService UserService = new CMS.UserService();

            Rock.CMS.User user = UserService.GetByApplicationNameAndUsername(applicationName, username);

            if (user != null)
            {
                if (string.IsNullOrEmpty(answer) && RequiresQuestionAndAnswer)
                {
                    UpdateFailureCount(user, FailureType.PasswordAnswer);
                    throw new ProviderException("Password answer required for password reset");
                }

                string newPassword = System.Web.Security.Membership.GeneratePassword(newPasswordLength, MinRequiredNonAlphanumericCharacters);

                ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username, newPassword, true);

                OnValidatingPassword(args);

                if (args.Cancel)
                {
                    if (args.FailureInformation != null)
                    {
                        throw args.FailureInformation;
                    }
                    else
                    {
                        throw new MembershipPasswordException("Reset password canceled due to password validation failure");
                    }
                }

                if (user.IsLockedOut ?? false)
                {
                    throw new MembershipPasswordException("The supplied user is locked out");
                }

                if (RequiresQuestionAndAnswer && EncodePassword(answer) != user.PasswordAnswer)
                {
                    UpdateFailureCount(user, FailureType.PasswordAnswer);
                    throw new MembershipPasswordException("Incorrect password answer");
                }

                user.Password = EncodePassword(newPassword);
                user.LastPasswordChangedDate = DateTime.Now;
                UserService.Save(user, CurrentPersonId());

                return(newPassword);
            }
            else
            {
                throw new MembershipPasswordException("User does not exist");
            }
        }
예제 #2
0
        /// <summary>
        /// Updates information about a user in the data source.
        /// </summary>
        /// <param name="user">A <see cref="T:System.Web.Security.MembershipUser"/> object that represents the user to update and the updated information for the user.</param>
        public override void UpdateUser(MembershipUser user)
        {
            UserService UserService = new CMS.UserService();

            Rock.CMS.User userModel = UserService.GetByApplicationNameAndUsername(applicationName, user.UserName);

            if (userModel != null)
            {
                userModel.Email      = user.Email;
                userModel.Comment    = user.Comment;
                userModel.IsApproved = user.IsApproved;
                UserService.Save(userModel, CurrentPersonId());
            }
        }
예제 #3
0
        /// <summary>
        /// Clears a lock so that the membership user can be validated.
        /// </summary>
        /// <param name="userName">The membership user whose lock status you want to clear.</param>
        /// <returns>
        /// true if the membership user was successfully unlocked; otherwise, false.
        /// </returns>
        public override bool UnlockUser(string userName)
        {
            UserService UserService = new CMS.UserService();

            Rock.CMS.User user = UserService.GetByApplicationNameAndUsername(applicationName, userName);

            if (user != null)
            {
                user.IsLockedOut       = false;
                user.LastLockedOutDate = DateTime.Now;
                UserService.Save(user, CurrentPersonId());
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #4
0
        /// <summary>
        /// Processes a request to update the password question and answer for a membership user.
        /// </summary>
        /// <param name="username">The user to change the password question and answer for.</param>
        /// <param name="password">The password for the specified user.</param>
        /// <param name="newPasswordQuestion">The new password question for the specified user.</param>
        /// <param name="newPasswordAnswer">The new password answer for the specified user.</param>
        /// <returns>
        /// true if the password question and answer are updated successfully; otherwise, false.
        /// </returns>
        public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
        {
            UserService UserService = new CMS.UserService();

            Rock.CMS.User user = UserService.GetByApplicationNameAndUsername(applicationName, username);

            if (user != null)
            {
                if (!ValidateUser(UserService, user, password))
                {
                    return(false);
                }

                user.PasswordQuestion = newPasswordQuestion;
                user.PasswordAnswer   = newPasswordAnswer;
                UserService.Save(user, CurrentPersonId());
                return(true);
            }
            else
            {
                return(false);
            }
        }
예제 #5
0
        /// <summary>
        /// Removes a user from the membership data source.
        /// </summary>
        /// <param name="username">The name of the user to delete.</param>
        /// <param name="deleteAllRelatedData">true to delete data related to the user from the database; false to leave data related to the user in the database.</param>
        /// <returns>
        /// true if the user was successfully deleted; otherwise, false.
        /// </returns>
        public override bool DeleteUser(string username, bool deleteAllRelatedData)
        {
            UserService UserService = new CMS.UserService();

            Rock.CMS.User user = UserService.GetByApplicationNameAndUsername(applicationName, username);

            if (user != null)
            {
                try
                {
                    UserService.Delete(user, CurrentPersonId());
                    UserService.Save(user, CurrentPersonId());
                    return(true);
                }
                catch (SystemException ex)
                {
                    throw new ProviderException("Error occurred attempting to delete User", ex);
                }
            }
            else
            {
                return(false);
            }
        }
예제 #6
0
        /// <summary>
        /// Adds a new membership user to the data source.
        /// </summary>
        /// <param name="username">The user name for the new user.</param>
        /// <param name="password">The password for the new user.</param>
        /// <param name="email">The e-mail address for the new user.</param>
        /// <param name="passwordQuestion">The password question for the new user.</param>
        /// <param name="passwordAnswer">The password answer for the new user</param>
        /// <param name="isApproved">Whether or not the new user is approved to be validated.</param>
        /// <param name="providerUserKey">The unique identifier from the membership data source for the user.</param>
        /// <param name="status">A <see cref="T:System.Web.Security.MembershipCreateStatus"/> enumeration value indicating whether the user was created successfully.</param>
        /// <returns>
        /// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the information for the newly created user.
        /// </returns>
        public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
        {
            ValidatePasswordEventArgs args = new ValidatePasswordEventArgs(username, password, true);

            OnValidatingPassword(args);

            if (args.Cancel)
            {
                status = MembershipCreateStatus.InvalidPassword;
                return(null);
            }

            UserService UserService = new CMS.UserService();

            if ((RequiresUniqueEmail && (GetUserNameByEmail(UserService, email) != String.Empty)))
            {
                status = MembershipCreateStatus.DuplicateEmail;
                return(null);
            }

            MembershipUser membershipUser = GetUser(UserService, username, false);

            if (membershipUser == null)
            {
                DateTime createDate = DateTime.Now;

                Rock.CMS.User user = new Rock.CMS.User();

                if (providerUserKey != null && providerUserKey is int)
                {
                    user.PersonId = ( int )providerUserKey;
                }
                else
                {
                    status = MembershipCreateStatus.InvalidProviderUserKey;
                    return(null);
                }

                user.ApplicationName                        = applicationName;
                user.Username                               = username;
                user.Password                               = EncodePassword(password);
                user.PasswordQuestion                       = passwordQuestion;
                user.PasswordAnswer                         = passwordAnswer;
                user.IsApproved                             = isApproved;
                user.Comment                                = string.Empty;
                user.CreationDate                           = createDate;
                user.LastPasswordChangedDate                = createDate;
                user.LastActivityDate                       = createDate;
                user.IsLockedOut                            = false;
                user.LastLockedOutDate                      = createDate;
                user.FailedPasswordAttemptCount             = 0;
                user.FailedPasswordAttemptWindowStart       = createDate;
                user.FailedPasswordAnswerAttemptCount       = 0;
                user.FailedPasswordAnswerAttemptWindowStart = createDate;
                user.AuthenticationType                     = (int)AuthenticationType.Database;

                try
                {
                    UserService.Add(user, CurrentPersonId());
                    UserService.Save(user, CurrentPersonId());
                    status = MembershipCreateStatus.Success;
                }
                catch
                {
                    status = MembershipCreateStatus.ProviderError;
                }

                return(GetUser(UserService, user, false));
            }
            else
            {
                status = MembershipCreateStatus.DuplicateUserName;
                return(null);
            }
        }