A user object for use with the data store, whatever that might be (e.g. an RDMS or MongoDB)
Inheritance: IDataStoreEntity
Exemple #1
0
		public void fromuser_shouldfillcorrectproperties()
		{
			// Arrange
			User user = new User();
			user.Id = Guid.NewGuid();
			user.ActivationKey = "key";
			user.Email = "email";
			user.Firstname = "firstname";
			user.Id = Guid.NewGuid();
			user.IsActivated = true;
			user.IsAdmin = true;
			user.IsEditor = true;
			user.Lastname = "lastname";
			user.SetPassword("pwd");
			user.PasswordResetKey = "resetkey";
			user.Salt = "salt";

			UserEntity entity = new UserEntity();

			// Act
			ToEntity.FromUser(user, entity);

			// Assert
			Assert.That(entity.Id, Is.Not.EqualTo(user.Id));  // the id isn't copied from the page
			Assert.That(entity.ActivationKey, Is.EqualTo(user.ActivationKey));
			Assert.That(entity.Email, Is.EqualTo(user.Email));
			Assert.That(entity.Firstname, Is.EqualTo(user.Firstname));
			Assert.That(entity.IsActivated, Is.EqualTo(user.IsActivated));
			Assert.That(entity.IsAdmin, Is.EqualTo(user.IsAdmin));
			Assert.That(entity.IsEditor, Is.EqualTo(user.IsEditor));
			Assert.That(entity.Lastname, Is.EqualTo(user.Lastname));
			Assert.That(entity.Password, Is.EqualTo(user.Password));
			Assert.That(entity.Salt, Is.EqualTo(user.Salt));
		}
		/// <summary>
		/// Adds a user to the system, and sets the <see cref="User.IsActivated"/> to true.
		/// </summary>
		/// <param name="email">The email or username.</param>
		/// <param name="password">The password.</param>
		/// <param name="isAdmin">if set to <c>true</c> the user is added as an admin.</param>
		/// <param name="isEditor">if set to <c>true</c> the user is added as an editor.</param>
		/// <returns>
		/// true if the user was added; false if the user already exists.
		/// </returns>
		/// <exception cref="SecurityException">An databaseerror occurred while adding the new user.</exception>
		public override bool AddUser(string email, string username, string password, bool isAdmin, bool isEditor)
		{
			try
			{
				User user = UserRepository.GetUserByUsernameOrEmail(username, email);
				if (user == null)
				{
					user = new User();
					user.Email = email;
					user.Username = username;
					user.SetPassword(password);
					user.IsAdmin = isAdmin;
					user.IsEditor = isEditor;
					user.IsActivated = true;
					UserRepository.SaveOrUpdateUser(user);

					return true;
				}
				else
				{
					return false;
				}
			}
			catch (DatabaseException ex)
			{
				throw new SecurityException(ex, "An error occurred while adding the new user {0}", email);
			}
		}
        public void Constructor_Should_Fill_Properties_From_User_Object()
        {
            // Arrange + Act
            User user = new User();
            user.ActivationKey = Guid.NewGuid().ToString();
            user.Id = Guid.NewGuid();
            user.Email = "*****@*****.**";
            user.Username = "******";
            user.Firstname = "firstname";
            user.Lastname = user.Lastname;
            user.PasswordResetKey = user.PasswordResetKey;

            UserViewModel model = new UserViewModel(user);

            // Assert
            Assert.That(model.Id, Is.EqualTo(user.Id));
            Assert.That(model.Firstname, Is.EqualTo(user.Firstname));
            Assert.That(model.Lastname, Is.EqualTo(user.Lastname));
            Assert.That(model.ExistingUsername, Is.EqualTo(user.Username));
            Assert.That(model.NewUsername, Is.EqualTo(user.Username));
            Assert.That(model.ExistingEmail, Is.EqualTo(user.Email));
            Assert.That(model.NewEmail, Is.EqualTo(user.Email));
            Assert.That(model.PasswordResetKey, Is.EqualTo(user.PasswordResetKey));
            Assert.That(model.UsernameHasChanged, Is.False);
            Assert.That(model.EmailHasChanged, Is.False);
        }
		public void AddAdminUser(string email, string username, string password)
		{
			try
			{
				using (IUnitOfWork unitOfWork = _context.CreateUnitOfWork())
				{
					var user = new User();
					user.Email = email;
					user.Username = username;
					user.SetPassword(password);
					user.IsAdmin = true;
					user.IsEditor = true;
					user.IsActivated = true;

					var entity = new UserEntity();
					ToEntity.FromUser(user, entity);

					unitOfWork.Add(entity);
					unitOfWork.SaveChanges();
				}
			}
			catch (Exception e)
			{
				throw new DatabaseException(e, "Install failed: unable to create the admin user using '{0}' - {1}", ConnectionString, e.Message);
			}
		}
Exemple #5
0
		public void Setup()
		{
			_container = new MocksAndStubsContainer();

			_applicationSettings = _container.ApplicationSettings;
			_applicationSettings.ConnectionString = "connstring";
			_context = _container.UserContext;
			_repository = _container.Repository;
			_pluginFactory = _container.PluginFactory;
			_settingsService = _container.SettingsService;
			_userService = _container.UserService;
			_historyService = _container.HistoryService;
			_pageService = _container.PageService;
			_listCache = _container.ListCache;
			_pageViewModelCache = _container.PageViewModelCache;

			// User setup
			_editorUser = new User();
			_editorUser.Id = Guid.NewGuid();
			_editorUser.Email = EditorEmail;
			_editorUser.Username = EditorUsername;
			_editorUser.IsAdmin = false;
			_editorUser.IsEditor = true;

			_adminUser = new User();
			_adminUser.Id = Guid.NewGuid();
			_adminUser.Email = AdminEmail;
			_adminUser.Username = AdminUsername;
			_adminUser.IsAdmin = true;
			_adminUser.IsEditor = true;

			_userService.Users.Add(_editorUser);
			_userService.Users.Add(_adminUser);
			SetUserContext(_adminUser);
		}
Exemple #6
0
		public void SetUp()
		{
			_adminUser = NewUser("admin@localhost", "admin", true, true);
			_adminUser = Repository.SaveOrUpdateUser(_adminUser);

			_editor = NewUser("editor1@localhost", "editor1", false, true);
			_editor = Repository.SaveOrUpdateUser(_editor);

			_inactiveUser = NewUser("editor2@localhost", "editor2", false, true, false);
			_inactiveUser = Repository.SaveOrUpdateUser(_inactiveUser);
		}
Exemple #7
0
        public void User_ObjectId_Should_Match_Id()
        {
            // Arrange
            User user = new User();
            user.Id = Guid.NewGuid();

            // Act
            Guid objectId = user.ObjectId;

            // Assert
            Assert.That(objectId, Is.EqualTo(user.Id));
        }
Exemple #8
0
		public void user_objectid_should_match_id()
		{
			// Arrange
			User user = new User();
			user.Id = Guid.NewGuid();

			// Act
			Guid objectId = user.ObjectId;

			// Assert
			Assert.That(objectId, Is.EqualTo(user.Id));
		}
		public void SetUp()
		{
			// Setup the repository
			Repository = GetRepository();
			Clearup();
			_adminUser = NewUser("admin@localhost", "admin", true, true);
			_adminUser = Repository.SaveOrUpdateUser(_adminUser);

			_editor = NewUser("editor1@localhost", "editor1", false, true);
			_editor = Repository.SaveOrUpdateUser(_editor);

			_inactiveUser = NewUser("editor2@localhost", "editor2", false, true, false);
			_inactiveUser = Repository.SaveOrUpdateUser(_inactiveUser);
		}
Exemple #10
0
		public override bool AddUser(string email, string username, string password, bool isAdmin, bool isEditor)
		{
			User user = new User();
			user.Id = Guid.NewGuid();
			user.Email = email;
			user.Username = username;
			user.SetPassword(password);
			user.IsAdmin = isAdmin;
			user.IsEditor = isEditor;

			Users.Add(user);

			return true;
		}
		public void Setup()
		{
			_container = new MocksAndStubsContainer();

			_applicationSettings = _container.ApplicationSettings;
			_context = _container.UserContext;	
			_repository = _container.Repository;
			_userService = _container.UserService;
			_historyService = _container.HistoryService;

			_testUser = new User();
			_testUser.IsActivated = true;
			_testUser.Id = Guid.NewGuid();
			_testUser.Email = AdminEmail;
			_testUser.Username = AdminUsername;
			_userService.Users.Add(_testUser);

			_context.CurrentUser = _testUser.Id.ToString();
		}
		/// <summary>
		/// Imports all users from the users table.
		/// </summary>
		private void ImportUsers()
		{
			try
			{
				using (SqlConnection connection = new SqlConnection(_connectionString))
				{
					using (SqlCommand command = connection.CreateCommand())
					{
						connection.Open();
						command.CommandText = "SELECT * FROM [User]";

						using (SqlDataReader reader = command.ExecuteReader())
						{
							while (reader.Read())
							{
								string username = reader["Username"].ToString();
								if (!string.IsNullOrEmpty(username) && !string.Equals(username, "admin", StringComparison.OrdinalIgnoreCase))
								{
									string email = reader["Email"].ToString();
                                    int orgID = (int)reader["orgID"];

									User user = new User();
									user.Id = Guid.NewGuid();
									user.IsEditor = true;
									user.IsAdmin = false;
                                    user.orgID = orgID;
									user.Email = email;
									user.Username = username;
									user.IsActivated = false;
									user.SetPassword("password");

									Repository.SaveOrUpdateUser(user);
								}
							}
						}
					}
				}
			}
			catch (SqlException ex)
			{
				throw new DatabaseException(ex, "Unable to import the pages from Screwturn - have you configured it to use the SQL Server users provider? \n{0}", ex.Message);
			}
		}
Exemple #13
0
 private void SetUserContext(User user)
 {
     _context.CurrentUser = user.Id.ToString();
 }
		public void Should_Export_Users_With_All_FieldValues()
		{
			// Arrange
			RepositoryMock repository = new RepositoryMock();
			repository.SiteSettings.PluginLastSaveDate = DateTime.Today;

			Guid user1Id = new Guid("29a8ad19-b203-46f5-be10-11e0ebf6f812");
			Guid user2Id = new Guid("e63b0023-329a-49b9-97a4-5094a0e378a2");
			Guid user3Id = new Guid("a6ee19ef-c093-47de-97d2-83dec406d92d");

			Guid user1Activationkey = new Guid("0953cf95-f357-4e5b-ae2b-7541844d3b6b");
			Guid user2Activationkey = new Guid("aa87fe31-9781-4c93-b7e3-9092ed095810");
			Guid user3Activationkey = new Guid("b8ef994d-87f5-4543-85de-66b41244a20a");

			User user1 = new User()
			{
				Id = user1Id,
				ActivationKey = user1Activationkey.ToString(),
				Firstname = "firstname1",
				Lastname = "lastname1",
				Email = "user1@localhost", 
				Password = "******",
				Salt = "salt1",
				IsActivated = true,
				IsAdmin = true,
				Username = "******"
			};

			User user2 = new User()
			{
				Id = user2Id,
				ActivationKey = user2Activationkey.ToString(),
				Firstname = "firstname2",
				Lastname = "lastname2",
				Email = "user2@localhost",
				Password = "******",
				Salt = "salt2",
				IsActivated = true,
				IsEditor = true,
				Username = "******"
			};

			User user3 = new User()
			{
				Id = user3Id,
				ActivationKey = user3Activationkey.ToString(),
				Firstname = "firstname3",
				Lastname = "lastname3",
				Email = "user3@localhost",
				Password = "******",
				Salt = "salt3",
				IsActivated = false,
				IsEditor = true,
				Username = "******"
			};

			repository.Users.Add(user1);
			repository.Users.Add(user2);
			repository.Users.Add(user3);

			SqlExportBuilder builder = new SqlExportBuilder(repository, new PluginFactoryMock());
			builder.IncludeConfiguration = false;
			builder.IncludePages = false;
			string expectedSql = ReadEmbeddedResource("expected-users-export.sql");

			// Act
			string actualSql = builder.Export();

			// Assert
			Assert.That(actualSql, Is.EqualTo(expectedSql), actualSql);
		}
		/// <summary>
		/// Creates a user in the system without setting the <see cref="User.IsActivated"/>, in other words for a user confirmation email.
		/// </summary>
		/// <param name="user">The user details to signup.</param>
		/// <param name="completed">Called once the signup (e.g. email is sent) is complete. Pass Null for no action.</param>
		/// <returns>
		/// The activation key for the signup.
		/// </returns>
		public override string Signup(UserViewModel model, Action completed)
		{
			if (model == null)
				throw new SecurityException("The summary provided to Signup is null.", null);

			try
			{
				// Create the new user
				model.ActivationKey = Guid.NewGuid().ToString();
				User user = new User();
				user.Username = model.NewUsername;
				user.ActivationKey = model.ActivationKey;
				user.Email = model.NewEmail;
				user.Firstname = model.Firstname;
				user.Lastname = model.Lastname;
				user.SetPassword(model.Password);
				user.IsEditor = true;
				user.IsAdmin = false;
				user.IsActivated = false;
				UserRepository.SaveOrUpdateUser(user);

				if (completed != null)
					completed();

				return user.ActivationKey;
			}
			catch (DatabaseException ex)
			{
				throw new SecurityException(ex, "An error occurred with the signup of {0}", model.NewEmail);
			}
		}
		public void Index_Should_Return_View_And_ViewModel_With_Both_User_Types()
		{
			// Arrange
			User admin = new User() { Id = Guid.NewGuid() };
			_userService.Users.Add(admin);

			User editor = new User() { Id = Guid.NewGuid() };
			_userService.Users.Add(editor);

			// Act
			ViewResult result = _controller.Index() as ViewResult;

			// Assert
			Assert.That(result, Is.Not.Null, "ViewResult");
			List<IEnumerable<UserViewModel>> model = result.ModelFromActionResult<List<IEnumerable<UserViewModel>>>();
			Assert.That(model, Is.Not.Null, "model");
			Assert.That(model.Count, Is.EqualTo(2));
		}
		public void EditUser_POST_Should_Update_Password_When_Password_Is_Not_Empty_And_Redirect_To_Index()
		{
			// Arrange
			User user = new User()
			{
				Id = Guid.NewGuid(),
				IsActivated = true,
				Lastname = "Lastname",
				Firstname = "Firstname",
				Email = "email@localhost",
				Username = "******"
			};
			_userService.Users.Add(user);

			UserViewModel model = new UserViewModel(user);
			model.Password = "******";
			model.PasswordConfirmation = "NewPassword";

			// Act
			RedirectToRouteResult result = _controller.EditUser(model) as RedirectToRouteResult;

			// Assert
			Assert.That(result, Is.Not.Null, "RedirectToRouteResult");
			Assert.That(result.RouteValues["action"], Is.EqualTo("Index"));

			bool passwordChanged = _userService.Authenticate(user.Email, "NewPassword");
			Assert.That(passwordChanged, Is.True);
		}
		public void EditUser_POST_Should_Update_User_When_Username_And_Email_Changes_And_Redirect_To_Index()
		{
			// Arrange
			User user = new User()
			{ 
				Id = Guid.NewGuid(), 
				IsActivated = true, 
				Lastname = "Lastname",
				Firstname = "Firstname",
				Email = "email@localhost",
				Username = "******"
			};
			_userService.Users.Add(user);

			UserViewModel model = new UserViewModel(user);
			model.Lastname = "new lastname";
			model.Firstname = "new Firstname";
			model.ExistingEmail = "email@localhost";
			model.NewEmail = "newemail@localhost";
			model.NewUsername = "******";

			// Act
			RedirectToRouteResult result = _controller.EditUser(model) as RedirectToRouteResult;

			// Assert
			Assert.That(result, Is.Not.Null, "RedirectToRouteResult");
			Assert.That(result.RouteValues["action"], Is.EqualTo("Index"));

			User savedUser = _userService.Users.First();
			Assert.That(savedUser.Lastname, Is.EqualTo(model.Lastname));
			Assert.That(savedUser.Firstname, Is.EqualTo(model.Firstname));
			Assert.That(savedUser.Email, Is.EqualTo(model.NewEmail));
			Assert.That(savedUser.Username, Is.EqualTo(model.NewUsername));
		}
		public void EditUser_GET_Should_Return_View_And_ViewModel()
		{
			// Arrange
			User user = new User() { Id = Guid.NewGuid(), IsActivated = true };
			_userService.Users.Add(user);

			// Act
			ViewResult result = _controller.EditUser(user.Id) as ViewResult;

			// Assert
			Assert.That(result, Is.Not.Null, "ViewResult");
			UserViewModel model = result.ModelFromActionResult<UserViewModel>();
			Assert.That(model, Is.Not.Null, "model");
			Assert.That(model.Id, Is.EqualTo(user.Id), "model");
		}
		public void DeleteUser_Should_Remove_User_And_Redirect_To_Index()
		{
			// Arrange
			User user = new User() { Id = Guid.NewGuid(), Email="blah@localhost", IsActivated = true };
			_userService.Users.Add(user);

			// Act
			RedirectToRouteResult result = _controller.DeleteUser(user.Email) as RedirectToRouteResult;

			// Assert
			Assert.That(_userService.Users.Count, Is.EqualTo(0));
			Assert.That(result, Is.Not.Null, "RedirectToRouteResult");
			Assert.That(result.RouteValues["action"], Is.EqualTo("Index"));
		}
Exemple #21
0
		public User SaveOrUpdateUser(User user)
		{
			User existingUser = Users.FirstOrDefault(x => x.Id == user.Id);

			if (existingUser == null)
			{
				user.Id = Guid.NewGuid();
				Users.Add(user);
			}
			else
			{
				user.ActivationKey = user.ActivationKey;
				user.Email = user.Email;
				user.Firstname = user.Firstname;
				user.IsActivated = user.IsActivated;
				user.IsAdmin = user.IsAdmin;
				user.IsEditor = user.IsEditor;
				user.Lastname = user.Lastname;
				user.Password = user.Password;
				user.PasswordResetKey = user.PasswordResetKey;
				user.Username = user.Username;
				user.Salt = user.Salt;
			}

			return user;
		}
        /// <summary>
        /// Retrieves a full <see cref="User"/> object for the email address provided, or null if the user doesn't exist.
        /// </summary>
        /// <param name="email">The username of the user to get</param>
        /// <returns>
        /// A <see cref="User"/> object
        /// </returns>
        public override User GetUser(string email, bool? isActivated = null)
        {
            User user = new User()
            {
                Email = email,
                Username = email,
                IsEditor = IsEditor(email),
                IsAdmin = IsAdmin(email),
            };

            if (isActivated.HasValue)
                user.IsActivated = isActivated.Value;
            else
                user.IsActivated = true;

            return user;
        }
Exemple #23
0
		public void DeleteUser(User user)
		{
			Users.Remove(user);
		}