示例#1
0
        public DataResult <bool> UpdateUser(int userId, UserModel userModel)
        {
            var userData = MapUserData(userModel.UserData);

            var now = m_dateTimeProvider.UtcNow;

            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,
                VerificationCode           = userModel.VerificationCode,
                VerificationCodeCreateTime = userModel.VerificationCodeCreateTime
            };

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

            if (formattedContacts.HasError)
            {
                return(Error <bool>(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 userDataLowLoa = userData.Select(x => new UserDataLoAModel
                {
                    UserData             = x,
                    LevelOfAssuranceEnum = LevelOfAssuranceEnum.Low
                }).ToList();
                var userContractsLowLoa = userContacts.Select(x => new UserContactLoAModel
                {
                    UserContact          = x,
                    LevelOfAssuranceEnum = LevelOfAssuranceEnum.Low
                }).ToList();

                m_userUoW.UpdateUser(now, userId, user, userDataLowLoa, userContractsLowLoa, userExternalIdentities);

                return(Success(true));
            }
            catch (NoResultException <UserEntity> e)
            {
                m_logger.LogWarning(e);
                return(Error <bool>(m_translator.Translate("invalid-user-id"), DataResultErrorCode.UserNotExistId));
            }
            catch (SaveEntityException <UserEntity> e)
            {
                m_logger.LogWarning(e);
                return(Error <bool>(m_translator.Translate("save-user-failed"), DataResultErrorCode.CreateUserError));
            }
            catch (SaveEntityException <UserDataEntity> e)
            {
                m_logger.LogWarning(e);
                return(Error <bool>(m_translator.Translate("save-user-data-failed"), DataResultErrorCode.CreateUserErrorUserData));
            }
            catch (SaveContactEntityException e)
            {
                m_logger.LogWarning(e);
                return(Error <bool>(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 <bool>(m_translator.Translate("save-user-external-identity-failed"),
                                    DataResultErrorCode.CreateUserErrorExternalIdentity));
            }
            catch (UserDataAlreadyExistsException e)
            {
                m_logger.LogWarning(e);
                return(Error <bool>(m_translator.Translate("save-user-external-identity-failed"),
                                    UserDataTypeErrorCodeHelper.GetCodeForUniqueUserDataTypeException(e.DataType)));
            }
            catch (DatabaseException e)
            {
                m_logger.LogWarning(e);
                return(Error <bool>(e.Message));
            }
        }