コード例 #1
0
        public DataResult <int> CreateUser(UserModel userModel, bool generateVerificationCode = true)
        {
            var now = m_dateTimeProvider.UtcNow;

            var userData = MapUserData(userModel.UserData);

            var user = new UserEntity
            {
                Username          = userModel.Username,
                PasswordHash      = userModel.PasswordHash,
                TwoFactorEnabled  = userModel.TwoFactorEnabled,
                LockoutEndDateUtc = userModel.LockoutEndDateUtc,
                LockoutEnabled    = userModel.LockoutEnabled,
                AccessFailedCount = userModel.AccessFailedCount,
                SecurityStamp     = userModel.SecurityStamp,
                LastChange        = now,
                TwoFactorProvider = userModel.TwoFactorProvider,
            };

            userData.Add(CreateMasterUserId(user));

            var formattedContacts = m_contactManager.ValidateAndFormatContactList(userModel.UserContacts, UserAction.Create);

            if (formattedContacts.HasError)
            {
                return(Error <int>(formattedContacts.Error));
            }

            var userContacts = formattedContacts.Result?.Select(x => new UserContactEntity
            {
                Type        = (ContactTypeEnum)x.Type,
                User        = user,
                Value       = x.Value,
                ConfirmCode = x.ConfirmCode
            })
                               .ToList();

            var userExternalIdentityModels = m_externalIdentityResolver.Resolve(userModel);

            var userExternalIdentities = userExternalIdentityModels?.Select(x => new UserExternalIdentityEntity
            {
                User = user,
                ExternalIdentityType = new ExternalIdentityEntity {
                    Name = x.ExternalIdentityType.Name
                },
                ExternalIdentity = x.ExternalIdentity
            })
                                         .ToList();

            try
            {
                var userId = m_userUoW.CreateUser(now, user, userData.Select(x => new UserDataLoAModel
                {
                    UserData             = x,
                    LevelOfAssuranceEnum = LevelOfAssuranceEnum.Low
                }).ToList(), userContacts.Select(x => new UserContactLoAModel
                {
                    UserContact          = x,
                    LevelOfAssuranceEnum = LevelOfAssuranceEnum.Low
                }).ToList(), userExternalIdentities);

                if (generateVerificationCode)
                {
                    var generaCodeResult = RegenerateVerificationCode(userId);

                    if (generaCodeResult.HasError)
                    {
                        m_userUoW.DeleteUserById(userId);
                        return(Error <int>(generaCodeResult.Error.Message, DataResultErrorCode.CreateUserErrorGenerateVerificationCode));
                    }
                }

                return(Success(userId));
            }
            catch (SaveEntityException <UserEntity> e)
            {
                m_logger.LogWarning(e);
                return(Error <int>(m_translator.Translate("save-user-failed"), DataResultErrorCode.CreateUserError));
            }
            catch (SaveEntityException <UserDataEntity> e)
            {
                m_logger.LogWarning(e);
                return(Error <int>(m_translator.Translate("save-user-data-failed"), DataResultErrorCode.CreateUserErrorUserData));
            }
            catch (SaveContactEntityException e)
            {
                m_logger.LogWarning(e);
                return(Error <int>(m_translator.Translate("save-user-contact-failed"),
                                   e.ContactType == ContactTypeEnum.Email
                        ? DataResultErrorCode.CreateUserErrorUserContactEmail
                        : DataResultErrorCode.CreateUserErrorUserContactPhone));
            }
            catch (SaveEntityException <UserExternalIdentityEntity> e)
            {
                m_logger.LogWarning(e);
                return(Error <int>(m_translator.Translate("save-user-external-identity-failed"),
                                   DataResultErrorCode.CreateUserErrorExternalIdentity));
            }
            catch (UserDataAlreadyExistsException e)
            {
                m_logger.LogWarning(e);
                return(Error <int>(m_translator.Translate("save-user-external-identity-failed"),
                                   UserDataTypeErrorCodeHelper.GetCodeForUniqueUserDataTypeException(e.DataType)));
            }
            catch (DatabaseException e)
            {
                m_logger.LogWarning(e);
                return(Error <int>(e.Message));
            }
        }