public ActionResult Create(UserInputModel userInputModel)
        {
            if (_session.Query<User>().Any(x => x.Username == userInputModel.Username))
            {
                ModelState.AddModelError("Username", "Username is already in use");
            }

            if (ModelState.IsValid)
            {
                var company = new Company(userInputModel.Username + " Co.");
                _session.Store(company);

                var user = new User(
                    userInputModel.Username,
                    Encryption.HashPassword(userInputModel.Password),
                    new string[0],
                    company);

                _session.Store(user);

                _authenticator.SetCookie(user.Id, false, user.Roles.ToArray());

                return RedirectToAction("Index", "Home");
            }

            return View("New", userInputModel);
        }
        public void New_User_Cannot_Be_Saved_With_Same_Username_As_Another_User()
        {
            string userName = "******";

            // Arrange
            Arrange(() =>
                        {
                            var originalUser = new User(userName,
                                                        new RandomString().Build(),
                                                        new[] {new RandomString().Build(), new RandomString().Build()},
                                                        new EntityReference("company/123", "CompanyDescription"));

                            _session.Store(originalUser);
                        });

            // Act
            ActThrows<Exception>(() =>
                                 	{
                                 		var userWithSameName = new User(userName,
                                 		                                new RandomString().Build(),
                                 		                                new[] {new RandomString().Build(), new RandomString().Build()},
                                 		                                new EntityReference("company/123", "CompanyDescription"));

                                 		_session.Store(userWithSameName);
                                 	});

            // Assert
        }
        public void User_Is_Correctly_Persisted_And_Retrieved()
        {
            User user = new User(
                new RandomString().Build(),
                new RandomString().Build(),
                new[] {new RandomString().Build(), new RandomString().Build()},
                new EntityReference("company/123", "CompanyDescription"));

            Act(() => { _session.Store(user); });

            AssertThat(() =>
                       	{
                       		var retrievedUser = _session.Load<User>(user.Id);
                       		retrievedUser.ShouldBeEquivalentTo(user);
                       	});
        }
 public static void Run(IDocumentStore documentStore, string siteAdministratorPassword)
 {
     using (var session = documentStore.OpenSession())
      {
          if (!session.Query<User>().Any())
          {
              var masterAdministrator = new User(siteadministratorUsername,
                                                 Encryption.HashPassword(siteAdministratorPassword),
                                                 new[]
                                                	{
                                                		Roles.SiteAdministrator,
                                                     Roles.Administrator
                                                	},
                                                 null);
              session.Store(masterAdministrator);
              session.SaveChanges();
          }
      }
 }