Esempio n. 1
0
        public void GivenTheFollowingUsersAreSetupInTheDatabase(Table table)
        {
            var usersToCreate = table.CreateSet <UserToCreate>().ToList();
            var users         = new List <User>();
            var hashStrategy  = (HashStrategyKind)Convert.ToInt32(ConfigurationManager.AppSettings["DefaultHashStrategy"]);
            var encryptor     = new Encryption();
            var adminRole     = SeDatabase.GetRoleByName("Admin");

            foreach (var userToCreate in usersToCreate)
            {
                string encryptedSecurityAnswer;
                string encryptedSecurityAnswerSalt;
                var    securePassword   = new SecuredPassword(userToCreate.Password, hashStrategy);
                var    securityQuestion = SeDatabase.GetLookupItemsByLookupType(Consts.LookupTypeId.SecurityQuestion).Single(a => a.Description == userToCreate.SecurityQuestion);
                encryptor.Encrypt(ConfigurationManager.AppSettings["EncryptionPassword"], Convert.ToInt32(ConfigurationManager.AppSettings["EncryptionIterationCount"]), userToCreate.SecurityAnswer, out encryptedSecurityAnswerSalt, out encryptedSecurityAnswer);

                var user = new User
                {
                    UserName     = userToCreate.UserName,
                    FirstName    = userToCreate.FirstName,
                    LastName     = userToCreate.LastName,
                    TelNoWork    = userToCreate.WorkTelephoneNumber,
                    TelNoHome    = userToCreate.HomeTelephoneNumber,
                    TelNoMobile  = userToCreate.MobileTelephoneNumber,
                    Title        = userToCreate.Title,
                    Town         = userToCreate.Town,
                    Postcode     = userToCreate.Postcode,
                    SkypeName    = userToCreate.SkypeName,
                    HashStrategy = hashStrategy,
                    PasswordHash = Convert.ToBase64String(securePassword.Hash),
                    PasswordSalt = Convert.ToBase64String(securePassword.Salt),
                    SecurityQuestionLookupItemId = securityQuestion.Id,
                    SecurityAnswer                      = encryptedSecurityAnswer,
                    SecurityAnswerSalt                  = encryptedSecurityAnswerSalt,
                    Approved                            = true,
                    EmailVerified                       = true,
                    Enabled                             = true,
                    PasswordLastChangedDateUtc          = DateTime.UtcNow,
                    PasswordResetToken                  = string.IsNullOrWhiteSpace(userToCreate.PasswordResetToken) ? null : userToCreate.PasswordResetToken,
                    PasswordResetExpiryDateUtc          = userToCreate.PasswordResetExpiry == "[One day from now]" ? (DateTime?)DateTime.UtcNow.AddDays(1) : null,
                    NewEmailAddress                     = string.IsNullOrWhiteSpace(userToCreate.NewEmailAddress) ? null : userToCreate.NewEmailAddress,
                    NewEmailAddressToken                = string.IsNullOrWhiteSpace(userToCreate.NewEmailAddressToken) ? null : userToCreate.NewEmailAddressToken,
                    NewEmailAddressRequestExpiryDateUtc = userToCreate.NewEmailAddressRequestExpiryDate == "[One day from now]" ? (DateTime?)DateTime.UtcNow.AddDays(1) : null,
                    PasswordExpiryDateUtc               = userToCreate.PasswordExpiryDate == "[Expired]" ? (DateTime?)DateTime.Now.AddDays(-1) : null
                };
                if (userToCreate.IsAdmin)
                {
                    user.UserRoles = new List <UserRole> {
                        new UserRole {
                            RoleId = adminRole.Id, UserId = 1
                        }
                    };
                }
                users.Add(user);
            }
            SeDatabase.SetUsers(users);
        }