Example #1
0
 public void CreateUser(UserData user)
 {
     using (var context = new DatabaseContext())
     {
         var entity = new UserEntity(user);
         context.Users.Add(entity);
         context.SaveChanges();
     }
 }
Example #2
0
 public void UpdateUser(UserData user)
 {
     using (var context = new DatabaseContext())
     {
         var entity = context.Users.FirstOrDefault(x => x.Email == user.Email);
         entity.Update(user);
         context.SaveChanges();
     }
 }
Example #3
0
 public void Update(UserData user)
 {
     FirstName = user.FirstName;
     LastName = user.LastName;
     Role = user.Role;
     HashedPassword = user.HashedPassword;
     PasswordSalt = user.PasswordSalt;
     EncryptedSecurePhrase = user.EncryptedSecurePhrase;
     FirstSecurePhraseQuestionCharacterIndex = user.FirstSecurePhraseQuestionCharacterIndex;
     SecondSecurePhraseQuestionCharacterIndex = user.SecondSecurePhraseQuestionCharacterIndex;
     UserState = user.UserState;
 }
Example #4
0
 public UserHeader CreateUser(UserHeader user)
 {
     using (var transaction = new TransactionScope())
     {
         var userData = _database.GetUserByEmail(user.Email);
         if (userData != null)
         {
             if (userData.UserState != UserState.Deleted) throw new DuplicateUserException();
             userData.UserState = UserState.Created;
             userData.FirstName = user.FirstName;
             userData.LastName = user.LastName;
             userData.HashedPassword = null;
             userData.PasswordSalt = null;
             userData.EncryptedSecurePhrase = null;
             userData.FirstSecurePhraseQuestionCharacterIndex = null;
             userData.SecondSecurePhraseQuestionCharacterIndex = null;
             userData.Role = user.Role;
             _database.UpdateUser(userData);
         }
         else
         {
             userData = new UserData(user);
             if (userData.Id == Guid.Empty) userData.Id = Guid.NewGuid();
             _database.CreateUser(userData);
         }
         var resetToken = new ResetToken
         {
             UserId = userData.Id,
             Token = Guid.NewGuid().ToString(),
             ExpirationTime = DateTime.Now.Add(_configuration.ResetUserTokenLifespan),
             Type = ResetTokenType.Both,
         };
         _database.DeleteExpiredResetTokens();
         _database.DeleteResetTokenIfExists(userData.Id, ResetTokenType.Both);
         _database.AddResetToken(resetToken);
         transaction.Complete();
         _mailNotifier.SendNewUserInstruction(new ResetNotification
         {
             UserId = userData.Id,
             UserEmail = userData.Email,
             UserFirstName = userData.FirstName,
             UserLastName = userData.LastName,
             ResetToken = resetToken.Token,
             ExpirationTime = resetToken.ExpirationTime,
         });
         return userData;
     }
 }
Example #5
0
        public UserHeader CreateActiveUser(string id, string password, string phrase, Role role)
        {
            var email = id + "@arb.org.uk";
            var salt = _passwordManager.GenerateSalt();
            var hashedPassword = _passwordManager.HashPassword(password, salt);
            var enctyptedSecurePhrase = _passwordManager.EncryptSecurePhrase(phrase);

            var existingUser = _database.GetUserByEmail(email);
            if (existingUser == null)
            {
                var user = new UserData
                {
                    Id = Guid.NewGuid(),
                    Email = email,
                    FirstName = "User",
                    LastName = id,
                    Role = role,
                    UserState = UserState.Activated,
                    HashedPassword = hashedPassword,
                    PasswordSalt = salt,
                    EncryptedSecurePhrase = enctyptedSecurePhrase,
                    FirstSecurePhraseQuestionCharacterIndex = 0,
                    SecondSecurePhraseQuestionCharacterIndex = 1
                };
                _database.CreateUser(user);
                return user;
            }
            else
            {
                existingUser.HashedPassword = hashedPassword;
                existingUser.PasswordSalt = salt;
                existingUser.EncryptedSecurePhrase = enctyptedSecurePhrase;
                existingUser.FirstSecurePhraseQuestionCharacterIndex = 0;
                existingUser.SecondSecurePhraseQuestionCharacterIndex = 1;
                existingUser.UserState = UserState.Activated;
                _database.UpdateUser(existingUser);
                return existingUser;
            }
        }
Example #6
0
        public UserHeader CreateDefaultUser(string password, string phrase)
        {
            const string defaultUserEmail = "*****@*****.**";
            var defaultUserId = Guid.Empty;

            var salt = _passwordManager.GenerateSalt();
            var hashedPassword = _passwordManager.HashPassword(password, salt);
            var enctyptedSecurePhrase = _passwordManager.EncryptSecurePhrase(phrase);

            var existingUser = _database.GetUserById(defaultUserId);
            if (existingUser == null)
            {
                var user = new UserData
                {
                    Id = defaultUserId,
                    Email = defaultUserEmail,
                    FirstName = "Default",
                    LastName = "User",
                    Role = Role.Admin,
                    UserState = UserState.Activated,
                    HashedPassword = hashedPassword,
                    PasswordSalt = salt,
                    EncryptedSecurePhrase = enctyptedSecurePhrase,
                    FirstSecurePhraseQuestionCharacterIndex = 0,
                    SecondSecurePhraseQuestionCharacterIndex = 1
                };
                _database.CreateUser(user);
                return user;
            }
            else
            {
                existingUser.HashedPassword = hashedPassword;
                existingUser.PasswordSalt = salt;
                existingUser.EncryptedSecurePhrase = enctyptedSecurePhrase;
                existingUser.FirstSecurePhraseQuestionCharacterIndex = 0;
                existingUser.SecondSecurePhraseQuestionCharacterIndex = 1;
                existingUser.UserState = UserState.Activated;
                _database.UpdateUser(existingUser);
                return existingUser;
            }
        }
Example #7
0
 public UserEntity(UserData user)
 {
     Id = user.Id;
     Email = user.Email;
     Update(user);
 }