/// <summary> /// Gets a collection of all the users in the data source in pages of data. /// </summary> /// <param name="pageIndex">The index of the page of results to return. <paramref name="pageIndex"/> is zero-based.</param> /// <param name="pageSize">The size of the page of results to return.</param> /// <param name="totalRecords">The total number of matched users.</param> /// <returns> /// A <see cref="T:System.Web.Security.MembershipUserCollection"/> collection that contains a page of <paramref name="pageSize"/><see cref="T:System.Web.Security.MembershipUser"/> objects beginning at the page specified by <paramref name="pageIndex"/>. /// </returns> public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords) { MembershipUserCollection membershipUsers = new MembershipUserCollection(); UserService UserService = new CMS.UserService(); int counter = 0; int startIndex = pageSize * pageIndex; int endIndex = startIndex + pageSize - 1; foreach (Rock.CMS.User user in UserService.Queryable().Where(u => u.ApplicationName == applicationName)) { if (counter >= startIndex && counter <= endIndex) { membershipUsers.Add(GetUserFromModel(user)); } counter++; } totalRecords = counter; return(membershipUsers); }
/// <summary> /// Gets a collection of membership users where the user name contains the specified user name to match. /// </summary> /// <param name="usernameToMatch">The user name to search for.</param> /// <param name="pageIndex">The index of the page of results to return. <paramref name="pageIndex"/> is zero-based.</param> /// <param name="pageSize">The size of the page of results to return.</param> /// <param name="totalRecords">The total number of matched users.</param> /// <returns> /// A <see cref="T:System.Web.Security.MembershipUserCollection"/> collection that contains a page of <paramref name="pageSize"/><see cref="T:System.Web.Security.MembershipUser"/> objects beginning at the page specified by <paramref name="pageIndex"/>. /// </returns> public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords) { MembershipUserCollection membershipUsers = new MembershipUserCollection(); UserService UserService = new CMS.UserService(); int counter = 0; int startIndex = pageSize * pageIndex; int endIndex = startIndex + pageSize - 1; foreach (Rock.CMS.User user in UserService.Queryable().Where(u => u.ApplicationName == applicationName && u.Username.ToLower().StartsWith(usernameToMatch.ToLower()))) { if (counter >= startIndex && counter <= endIndex) { membershipUsers.Add(GetUserFromModel(user)); } counter++; } totalRecords = counter; return(membershipUsers); }
/// <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> /// Gets information from the data source for a user. Provides an option to update the last-activity date/time stamp for the user. /// </summary> /// <param name="username">The name of the user to get information for.</param> /// <param name="userIsOnline">true to update the last-activity date/time stamp for the user; false to return user information without updating the last-activity date/time stamp for the user.</param> /// <returns> /// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the specified user's information from the data source. /// </returns> public override MembershipUser GetUser(string username, bool userIsOnline) { if (username.Trim() == string.Empty) { return(null); } UserService UserService = new CMS.UserService(); return(GetUser(UserService, username, userIsOnline)); }
/// <summary> /// Gets the number of users currently accessing the application. /// </summary> /// <returns> /// The number of users currently accessing the application. /// </returns> public override int GetNumberOfUsersOnline() { UserService UserService = new CMS.UserService(); TimeSpan onlineSpan = new TimeSpan(0, System.Web.Security.Membership.UserIsOnlineTimeWindow, 0); DateTime compareTime = DateTime.Now.Subtract(onlineSpan); return(UserService.Queryable().Where(u => u.ApplicationName == applicationName && u.LastActivityDate > compareTime).Count()); }
/// <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> /// Gets user information from the data source based on the unique identifier for the membership user. Provides an option to update the last-activity date/time stamp for the user. /// </summary> /// <param name="providerUserKey">The unique identifier for the membership user to get information for.</param> /// <param name="userIsOnline">true to update the last-activity date/time stamp for the user; false to return user information without updating the last-activity date/time stamp for the user.</param> /// <returns> /// A <see cref="T:System.Web.Security.MembershipUser"/> object populated with the specified user's information from the data source. /// </returns> public override MembershipUser GetUser(object providerUserKey, bool userIsOnline) { if (providerUserKey != null && providerUserKey is int) { UserService UserService = new CMS.UserService(); Rock.CMS.User user = UserService.Get(( int )providerUserKey); if (user != null) { return(GetUser(UserService, user, userIsOnline)); } else { return(null); } } else { throw new ProviderException("Invalid providerUserKey"); } }
/// <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); } }
/// <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); } }