public void SaveUser(User userToSave)
 {
     using (var session = SessionFactory.OpenSession())
     {
         using (var trans = session.BeginTransaction())
         {
             session.SaveOrUpdate(userToSave);
             trans.Commit();
         }
     }
 }
 public static List<User> GetStubUsers(int numUsers)
 {
     var uList = new List<User>();
     for (int i = 0; i < numUsers; i++)
     {
         var u = new User();
         u.CreationDate = DateTime.Now;
         u.UserName = "******" + i;
         u.UserId = i;
         uList.Add(u);
     }
     return uList;
 }
        public static IMembershipRepository GetMockRepository()
        {
            User goodUser = new User() {Password = "******", IsApproved = true, CreationDate = DateTime.Now, 
                PasswordAnswer = "GoodAnswer", LastLockedOutDate = DateTime.Now, 
                LastPasswordChangedDate = DateTime.Now, LastLoginDate = DateTime.Now,
                UserName = "******"};
            var mockRepo = MockRepository.GenerateMock<IMembershipRepository>();
            mockRepo.Stub(v => v.GetUserByName("GoodUser", null)).Return(goodUser);
            mockRepo.Stub(v => v.GetUserByName("EmptyUser", null)).Return(goodUser);
            mockRepo.Stub(v => v.GetUserByName("LockedUser", null)).Return(new User() { IsLockedOut = true });
            mockRepo.Stub(v => v.GetUserByName("BadAnswerUser", null)).Return(new User() {PasswordAnswer = "GoodAnswer", Password = "******"});
            mockRepo.Stub(v => v.GetUserByName("EncryptUser", null)).Return(new User() { PasswordAnswer = "SerLEVf28XZ/mBLKLgqulBDfUK05rOsefCL0gd+WRDE=", Password = "******", IsApproved = true });
            mockRepo.Stub(v => v.GetUserByName("HashUser", null)).Return(new User() { PasswordAnswer = "IbltIxTKvuFlsy0hjV/7dQLS9wA=", Password = "******", IsApproved = true });
            mockRepo.Stub(v => v.GetUserByName("NewUser", null)).Return(null);
            mockRepo.Stub(v => v.GetUserByName("ExceptionUser", null)).Throw(new Exception());
            mockRepo.Stub(v => v.GetUserByID(1)).Return(goodUser);
            mockRepo.Stub(v => v.GetUserByID(999)).Throw(new Exception());
            mockRepo.Stub(v => v.FindUsersByEMail("GoodEmail", 0, 99, null)).Return(GetStubUsers(1));
            mockRepo.Stub(v => v.FindUsersByEMail("BadEmail", 0, 99, null)).Return(new List<User>());
            mockRepo.Stub(v => v.FindUsersByEMail("DupEmail", 0, 99, null)).Return(GetStubUsers(2));
            mockRepo.Stub(v => v.FindUsersByEMail("ExceptionMail",0,99, null)).Throw(new Exception());
            mockRepo.Stub(v => v.FindUsersByName("GoodName", 0, 99, null)).Return(GetStubUsers(1));
            mockRepo.Stub(v => v.FindUsersByName("BadName", 0, 99, null)).Return(new List<User>());
            mockRepo.Stub(v => v.FindUsersByName("DupName", 0, 99, null)).Return(GetStubUsers(2));
            mockRepo.Stub(v => v.FindUsersByName("ExceptionMail", 0, 99, null)).Throw(new Exception());
            mockRepo.Stub(v => v.GetUserNameByEMail("NewEmail", null)).Return("");
            mockRepo.Stub(v => v.GetUserNameByEMail("DupEmail", null)).Return("DupUser");
            mockRepo.Stub(v => v.GetUserNameByEMail("ExceptionEmail", null)).Throw(new Exception());
            mockRepo.Stub(v => v.GetAllUsers(0, 99, null)).Return(GetStubUsers(2));
            mockRepo.Stub(v => v.GetAllUsers(1, 99, null)).Return(new List<User>());
            mockRepo.Stub(v => v.GetAllUsers(2, 99, null)).Throw(new Exception());
            
            mockRepo.Stub(v => v.GetRoleByName("InvalidRole", null)).Return(null);
            mockRepo.Stub(v => v.GetRoleByName("ValidRole", null)).Return(new Role());
            mockRepo.Stub(v => v.GetRoleByName("PopulatedRole", null)).Return(new Role{RoleName = "PopulatedRole"});
            mockRepo.Stub(v => v.GetRoleByName("UnpopulatedRole", null)).Return(new Role{RoleName = "UnpopulatedRole"});
            mockRepo.Stub(v => v.FindUsersInRole("PopulatedRole", "SampleUser", null)).
                Return(new List<User>{new User {UserName = "******"}});
            mockRepo.Stub(v => v.FindUsersInRole("UnpopulatedRole", "", null)).Return(new List<User>());
            mockRepo.Stub(v => v.FindUsersInRole("UnpopulatedRole", "GoodUser", null)).Return(new List<User>());
            mockRepo.Stub(v => v.GetAllUsersInRole("PopulatedRole", null)).
                Return(new List<User> { new User { UserName = "******" } });
            mockRepo.Stub(v => v.FindUsersInRole("ValidRole","GoodUser", null)).
                Return(new List<User> { goodUser } ); 
            mockRepo.Stub(v => v.GetAllUsersInRole("UnpopulatedRole", null)).Return(new List<User>());
            mockRepo.Stub(v => v.GetAllRoles(null)).Return(new List<Role> {new Role(), new Role()});

            mockRepo.Stub(v => v.GetAllRolesForUser("GoodUser",null)).Return(new List<Role> { new Role(), new Role() });
            mockRepo.Stub(v => v.GetAllRolesForUser("EmptyUser",null)).Return(new List<Role>());
            return mockRepo;
        }
        /// <summary>
        /// Create a MembershipUser object from a user object.
        /// </summary>
        /// <param name="u">User Object.</param>
        /// <returns>MembershipUser object.</returns>
        private MembershipUser GetUserFromObject(User u)
        {
            var creationDate = (DateTime) u.CreationDate;
            var lastLoginDate = new DateTime();
            if (u.LastLoginDate != null)
            {
                lastLoginDate = (DateTime) u.LastLoginDate;
            }
            var lastActivityDate = new DateTime();
            if (u.LastActivityDate != null)
            {
                lastActivityDate = (DateTime) u.LastActivityDate;
            }
            var lastPasswordChangedDate = new DateTime();
            if (u.LastPasswordChangedDate != null)
            {
                lastPasswordChangedDate = (DateTime) u.LastPasswordChangedDate;
            }
            var lastLockedOutDate = new DateTime();
            if (u.LastLockedOutDate != null)
            {
                lastLockedOutDate = (DateTime) u.LastLockedOutDate;
            }

            var membershipUser = new MembershipUser(
                this.Name,
                u.UserName,
                u.UserId,
                u.EMail,
                u.PasswordQuestion,
                u.Comment,
                u.IsApproved,
                u.IsLockedOut,
                creationDate,
                lastLoginDate,
                lastActivityDate,
                lastPasswordChangedDate,
                lastLockedOutDate
                );
            return membershipUser;
        }
 /// <summary>
 /// Create a new user.
 /// </summary>
 /// <param name="username">User name.</param>
 /// <param name="password">Password.</param>
 /// <param name="email">Email address.</param>
 /// <param name="passwordQuestion">Security quesiton for password.</param>
 /// <param name="passwordAnswer">Security quesiton answer for password.</param>
 /// <param name="isApproved"></param>
 /// <param name="userID">User ID</param>
 /// <param name="status"></param>
 /// <returns>MembershipUser</returns>
 public override MembershipUser CreateUser(string username, string password, string email,
                                           string passwordQuestion, string passwordAnswer, bool isApproved,
                                           object userID, out MembershipCreateStatus status)
 {
     try
     {
         ValidateNewPasswordFormat(username, password);
     }
     catch (Exception)
     {
         status = MembershipCreateStatus.InvalidPassword;
         return null;
     }
     if (DuplicatesEmailAddress(email))
     {
         status = MembershipCreateStatus.DuplicateEmail;
         return null;
     }
     MembershipUser membershipUser = GetUser(username, false);
     if (membershipUser == null)
     {
         User u = new User();
         u.UserName = username;
         u.ApplicationName = _applicationName;
         u.Password = EncodePassword(password);
         u.EMail = email;
         u.PasswordQuestion = passwordQuestion;
         u.PasswordAnswer = EncodePassword(passwordAnswer);
         u.IsApproved = isApproved;
         u.Comment = String.Empty;
         u.CreationDate = DateTime.Now;
         try
         {
             _memberRepo.SaveUser(u);
             status = MembershipCreateStatus.Success;
         }
         catch (Exception ex)
         {
             throw new MemberAccessException("Error processing membership data - " + ex.Message);
         }
         return GetUser(username, false);
     }
     else
     {
         status = MembershipCreateStatus.DuplicateUserName;
     }
     return null;
 }
 public void DeleteUser(User userToDelete)
 {
     using (var session = SessionFactory.OpenSession())
     {
         using (var trans = session.BeginTransaction())
         {
             session.Delete(userToDelete);
             trans.Commit();
         }
     }
 }