/// <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"); } }
/// <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()); } }
/// <summary> /// Verifies that the specified user name and password exist in the data source. /// </summary> /// <param name="username">The name of the user to validate.</param> /// <param name="password">The password for the specified user.</param> /// <returns> /// true if the specified username and password are valid; otherwise, false. /// </returns> public override bool ValidateUser(string username, string password) { UserService UserService = new CMS.UserService(); Rock.CMS.User user = UserService.GetByApplicationNameAndUsername(applicationName, username); if (user != null) { return(ValidateUser(UserService, user, password)); } else { return(false); } }
/// <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); } }
/// <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); } }
/// <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); } }