示例#1
0
 public void Staff_NoSecurityAnswer()
 {
     var user = new User();
     user.AddRole(ProfileRole);
     user.AddRole(StaffRole);
     Assert.IsFalse(user.RequiresSecurityQuestionToRetrieveCredentials);
 }
示例#2
0
 public void Staff_WithSecurityAnswser()
 {
     var user = new User();
     user.AddRole(ProfileRole);
     user.AddRole(StaffRole);
     user.SecurityAnswer = "Test";
     Assert.IsTrue(user.RequiresSecurityQuestionToRetrieveCredentials);
 }
示例#3
0
 public void NonStaff()
 {
     var user = new User();
     user.AddRole(ProfileRole);
     Assert.IsTrue(user.RequiresSecurityQuestionToRetrieveCredentials);
 }
示例#4
0
        /// <summary>
        /// Creates and persists a new user.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns>The new user.</returns>
        public User CreateUser(CreateUserRequest request)
        {
            lock (_threadLocker)
            {
                using (var entityContext = _entityContextFactory.CreateEntityContext())
                {
                    if (!_passwordValidator.IsPasswordValid(request.Password))
                        throw new InvalidUserException(MembershipCreateStatus.InvalidPassword);

                    MembershipCreateStatus createStatus;
                    if (request.AllowDuplicateEmails)
                        createStatus = _statusChecker.CheckCreateStatus(request.UserName);
                    else
                        createStatus = _statusChecker.CheckCreateStatus(request.UserName, request.Email);

                    if (createStatus != MembershipCreateStatus.Success)
                        throw new InvalidUserException(createStatus);

                    var user = new User(request.UserName, request.Email, request.Password)
                    {
                        ApplicationName = Utils.Common.Constants.MembershipProviderApplicationName,
                        IsApproved = request.IsApproved,
                        NamePrefix = request.NamePrefix,
                        FirstName = request.FirstName,
                        MiddleName = request.MiddleName,
                        LastName = request.LastName,
                        NameSuffix = request.NameSuffix,
                        IsVerified = request.IsVerified,
                        IsSocialMediaAccount = request.IsSocialMediaAccount
                    };

                    int questionId;
                    if (Int32.TryParse(request.PasswordQuestion, out questionId))
                    {
                        user.SecurityQuestionId = questionId;
                        user.SecurityAnswer = SymmetricUtility.Encrypt(request.PasswordAnswer, _symmetricKeyStore.ReadKey(), _symmetricKeyStore.ReadIV());
                    }

                    if (request.RoleIds != null && request.RoleIds.Any())
                    {
                        var roles = entityContext.EntitySet<Role>();
                        foreach (var roleId in request.RoleIds)
                        {
                            var role = roles.SingleOrDefault(r => r.Id == roleId);
                            if (role != null)
                                user.AddRole(role);
                        }
                    }
                    entityContext.EntitySet<User>().Add(user);
                    entityContext.SaveChanges();

                    return user;
                }
            }
        }