/// <summary> /// Sets up a user for each business that doesn't have a user extension entry /// Then adds the user extension entry for the user id / business id /// Also adds each of the created users to the business role /// </summary> private static void SetUpUserExtensionsForBusinessesInDatabase() { //Get all of the businesses BusinessDao businessDao = new BusinessDao(); UserExtensionDao userExtensionDao = new UserExtensionDao(); List<Business> businesses = businessDao.GetAll(); MembershipCreateStatus didItCreate; MembershipUser createdUser; List<UserExtension> userExtensionsAlreadyCreated = userExtensionDao.GetAll(); //role name for business string businessRole = "BusinessUser"; List<string> usersNeedingBusinessRole = new List<string>(); foreach (Business business in businesses) { string currentUserName = business.BusinessShortName + "User"; // Check if the user is in the business role if (Roles.IsUserInRole(currentUserName, businessRole) == false) { usersNeedingBusinessRole.Add(currentUserName); } // check if the user is already created if (userExtensionsAlreadyCreated.Exists(ue => ue.PrimaryBusinessId == business.Id)) { continue; } createdUser = Membership.CreateUser(currentUserName, "p@ssw0rd", "testerWithBusinessId_" + business.Id.ToString() + "@eviivo.com", "the answer is A", "A", true, out didItCreate); // If it was a dupe, get the user and use it to add the extension if (didItCreate == MembershipCreateStatus.DuplicateUserName) { //Already exists so get the id from the db createdUser = Membership.GetUser(currentUserName); } // This isn't all that helpful, just prevents user extension adds that aren't correct else if (didItCreate != MembershipCreateStatus.Success) { break; } userExtensionDao.Create((Guid)createdUser.ProviderUserKey, business.Id, (Guid)createdUser.ProviderUserKey); } if (usersNeedingBusinessRole.Count > 0) { Roles.AddUsersToRole(usersNeedingBusinessRole.ToArray(), businessRole); } }
/// <summary> /// Sets up a user with the given information /// </summary> /// <param name="validUserName">user name to use</param> /// <param name="validUserPassword">password to use</param> /// <param name="validUserEmail">email to use</param> /// <param name="userId">OPTIONAL: ID for the user</param> /// <returns>MembershipUser</returns> public static MembershipUser SetupUser(string validUserName, string validUserPassword, string validUserEmail, Guid userId = default(Guid)) { MembershipCreateStatus createStatus; MembershipUser validUser; if (userId != default(Guid)) { validUser = Membership.CreateUser(validUserName, validUserPassword, validUserEmail, "Answer is A", "A", true, userId, out createStatus); } else { validUser = Membership.CreateUser(validUserName, validUserPassword, validUserEmail, "Answer is A", "A", true, out createStatus); } if (createStatus == MembershipCreateStatus.Success) { SecurityHelper.StubSecurity(validUserName, validUser); // Create the user extension record for the membership user var userExtensionDao = new UserExtensionDao(); userExtensionDao.Create(new UserExtension { UserId = (Guid)validUser.ProviderUserKey, FirstName = "Test", LastName = "User", CultureCode = "en-GB" }); } if (createStatus == MembershipCreateStatus.DuplicateEmail || createStatus == MembershipCreateStatus.DuplicateUserName) { //Just get the user if it was duplicate validUser = Membership.GetUser(validUserName); } return validUser; }
/// <summary> /// Deletes a user with the given userName /// </summary> /// <param name="userName">User to delete</param> /// <returns>True is user deleted otherwise False</returns> public static bool DeleteUser(string userName) { var validUser = Membership.GetUser(userName); if (validUser != null) { var userExtensionDao = new UserExtensionDao(); userExtensionDao.Delete((Guid)validUser.ProviderUserKey); } return Membership.DeleteUser(userName); }