Esempio n. 1
0
        private void HydrateUserData(
            DateTime now,
            UserEntity userEntity,
            IList <UserDataLoAModel> userData,
            IList <UserDataEntity> userDataToUpdate,
            IDictionary <string, UserDataEntity> userDataDict,
            IDictionary <string, UserDataTypeEntity> userDataTypesDict,
            string externalProviderName,
            UserDataEntity parentEntity = null
            )
        {
            if (userData == null)
            {
                return;
            }

            var levelOfAssuranceCache = new LevelOfAssuranceCache(m_levelOfAssuranceRepository);

            var dataSource = Lazy.Of(() =>
            {
                if (string.IsNullOrEmpty(externalProviderName))
                {
                    return(m_dataSourceRepository.GetDataSourceByDataSource(
                               DataSourceEnum.User
                               ));
                }

                var externalLoginProviderEntity = m_externalLoginProviderRepository.GetExternalLoginProviderByName(
                    externalProviderName
                    );

                return(m_dataSourceRepository.GetDataSourceByDataSource(
                           DataSourceEnum.ExternalLoginProvider,
                           externalLoginProviderEntity.Id
                           ));
            }
                                     );

            foreach (var dataEntityModel in userData)
            {
                var dataEntity = dataEntityModel.UserData;

                if (userDataDict != null && userDataDict.ContainsKey(dataEntity.UserDataType.DataTypeValue))
                {
                    var userDataEntity = userDataDict[dataEntity.UserDataType.DataTypeValue];

                    if (dataEntity.Value != userDataEntity.Value)
                    {
                        userDataEntity.LevelOfAssurance = levelOfAssuranceCache.GetByEnum(dataEntityModel.LevelOfAssuranceEnum);
                        userDataEntity.DataSource       = dataEntityModel.UserData.DataSource ?? dataSource.Value; //UserData generated by system, e.g. MasterUserId, has DataSource already set
                    }

                    userDataEntity.Value = dataEntity.Value;

                    if (dataEntity.ActiveTo == null || dataEntity.ActiveTo > now)
                    {
                        userDataEntity.ActiveTo = dataEntity.ActiveTo;
                    }
                    else
                    {
                        throw new InvalidOperationException("ActiveTo property of new UserData version must be null or greater than UtcNow");
                    }

                    userDataEntity.ParentUserData = parentEntity;

                    userDataToUpdate.Add(userDataEntity);

                    HydrateUserData(now,
                                    userEntity, dataEntity.ChildrenUserData?.Select(x => new UserDataLoAModel
                    {
                        UserData             = x,
                        LevelOfAssuranceEnum = dataEntityModel.LevelOfAssuranceEnum
                    }).ToList(), userDataToUpdate,
                                    userDataDict, userDataTypesDict, externalProviderName, userDataEntity
                                    );
                }
                else
                {
                    // expect this mean it is new "row"

                    dataEntity.User           = userEntity;
                    dataEntity.UserDataType   = userDataTypesDict[dataEntity.UserDataType.DataTypeValue];
                    dataEntity.ParentUserData = parentEntity;

                    dataEntity.LevelOfAssurance = levelOfAssuranceCache.GetByEnum(dataEntityModel.LevelOfAssuranceEnum);
                    dataEntity.DataSource       = dataEntityModel.UserData.DataSource ?? dataSource.Value;

                    userDataToUpdate.Add(dataEntity);

                    HydrateUserData(now,
                                    userEntity, dataEntity.ChildrenUserData?.Select(x => new UserDataLoAModel
                    {
                        UserData             = x,
                        LevelOfAssuranceEnum = dataEntityModel.LevelOfAssuranceEnum
                    }).ToList(), userDataToUpdate,
                                    userDataDict, userDataTypesDict, externalProviderName, dataEntity
                                    );
                }
            }
        }
Esempio n. 2
0
        private void UpdateUserContacts(DateTime now,
                                        UserEntity userEntity,
                                        IList <UserContactLoAModel> userContacts,
                                        UserAction action,
                                        string externalProviderName = null
                                        )
        {
            var userContactsDict     = m_userContactRepository.GetUserContacts(userEntity.Id)?.ToDictionary(x => x.Type);
            var userContactsToUpdate = new List <UserContactEntity>();

            var levelOfAssuranceCache = new LevelOfAssuranceCache(m_levelOfAssuranceRepository);
            var dataSourceCache       = new DataSourceCache(m_dataSourceRepository, m_externalLoginProviderRepository);

            foreach (var userContactModel in userContacts)
            {
                var userContact = userContactModel.UserContact;

                if (userContactsDict != null && userContactsDict.ContainsKey(userContact.Type))
                {
                    var userContactEntity = userContactsDict[userContact.Type];

                    if (!userContact.Value.Equals(userContactEntity.Value))
                    {
                        userContactEntity.LevelOfAssurance = levelOfAssuranceCache.GetByEnum(userContactModel.LevelOfAssuranceEnum);
                        userContactEntity.DataSource       = dataSourceCache.GetByExternalProvider(externalProviderName);
                    }

                    userContactEntity.Value = userContact.Value;

                    if (
                        userContact.ConfirmCode != null &&
                        !userContact.ConfirmCode.Equals(userContactEntity.ConfirmCode)
                        )
                    {
                        userContactEntity.ConfirmCode           = userContact.ConfirmCode;
                        userContactEntity.ConfirmCodeChangeTime = now;
                    }

                    userContactsToUpdate.Add(userContactEntity);
                }
                else
                {
                    // expect this mean it is new "row"

                    userContact.User = userEntity;
                    userContact.ConfirmCodeChangeTime = now;

                    userContact.LevelOfAssurance = levelOfAssuranceCache.GetByEnum(userContactModel.LevelOfAssuranceEnum);
                    userContact.DataSource       = dataSourceCache.GetByExternalProvider(externalProviderName);

                    userContactsToUpdate.Add(userContact);
                }
            }

            switch (action)
            {
            case UserAction.Create:
                m_userContactRepository.CreateUserContacts(userContactsToUpdate);
                break;

            case UserAction.Update:
                m_userContactRepository.UpdateUserContacts(userContactsToUpdate);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(action), action, null);
            }
        }