/// <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 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); } }
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)); }
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; }
/// <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); } }
/// <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); } }