/// <summary>
        /// Gets all the settings data required for the client.
        /// </summary>
        /// <param name="id">Id of the homepage or -1 if global settings.</param>
        public GigyaDsSettingsApiResponseModel Get(int id)
        {
            var settingsHelper = new GigyaUmbracoDsSettingsHelper(_logger);
            var data           = settingsHelper.Get(id, false) ?? new GigyaDsSettings {
                SiteId = new string[] { id.ToString() }, Mappings = new List <GigyaDsMapping>()
            };
            var model = GetModel(id, data);

            var memberType = this.ApplicationContext.Services.MemberTypeService.Get(Constants.MemberTypeAlias);

            var wrappedModel = new GigyaDsSettingsApiResponseModel
            {
                Settings = model,
                Data     = new GigyaDsConfigViewModel
                {
                    MemberProperties = memberType.PropertyTypes.Select(i => new GigyaDsMemberPropertyViewModel
                    {
                        Alias = i.Alias,
                        Name  = i.Name
                    }).ToList()
                }
            };

            return(wrappedModel);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Event that is called whenever the user's data is retrieved with getAccountInfo.
        /// This method retrieves the ds data and merges it with the getAccountInfo object.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void GigyaEventHub_GetAccountInfoCompleted(object sender, GetAccountInfoCompletedEventArgs e)
        {
            var settingsHelper = new GigyaUmbracoDsSettingsHelper(e.Logger);
            var settings       = settingsHelper.Get(Convert.ToInt32(e.CurrentSiteId));

            // merge ds data with account info
            var helper = new GigyaDsHelper(e.Settings, e.Logger, settings);

            e.GigyaModel = helper.Merge(e.GigyaModel, e.MappingFields);
        }
        /// <summary>
        /// Saves Gigya settings to the db.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public GigyaDsSettingsResponseModel Save(GigyaDsSettingsViewModel model)
        {
            var response = new GigyaDsSettingsResponseModel();

            if (!ModelState.IsValid)
            {
                var errorList = ModelState.Values.SelectMany(m => m.Errors)
                                .Select(e => e.ErrorMessage)
                                .ToList();

                response.Error = string.Join(" ", errorList);
                _logger.Error(response.Error);
                return(response);
            }

            var settingsHelper = new GigyaUmbracoDsSettingsHelper(_logger);
            var settings       = settingsHelper.GetRaw(model.Id);

            if (settings == null)
            {
                response.Error = "Settings not found for id: " + model.Id;
                _logger.Error(response.Error);
                return(response);
            }

            if (model.Inherited && model.Id > 0)
            {
                // don't delete global settings
                if (settings.Id > -1)
                {
                    settingsHelper.Delete(settings);
                }

                settingsHelper.ClearCache(model.Id);
                response.Success = true;

                // return global settings to refresh client
                var globalData  = settingsHelper.Get(model.Id);
                var globalModel = GetModel(model.Id, globalData);
                response.Settings = globalModel;
                return(response);
            }

            if (model.Mappings == null)
            {
                response.Error = "Invalid mapping param supplied. Please check your mappings.";
                _logger.Error(response.Error);
                return(response);
            }

            if (model.Mappings.Any(i => string.IsNullOrEmpty(i.CmsName)))
            {
                response.Error = "Umbraco field is required.";
                _logger.Error(response.Error);
                return(response);
            }

            if (model.Mappings.Any(i => string.IsNullOrEmpty(i.GigyaName)))
            {
                response.Error = "Gigya DS field is required.";
                _logger.Error(response.Error);
                return(response);
            }

            if (model.Mappings.Any(i => string.IsNullOrEmpty(i.Oid)))
            {
                response.Error = "Gigya DS OID field is required.";
                _logger.Error(response.Error);
                return(response);
            }

            if (model.Mappings.Any(i => !i.GigyaName.StartsWith("ds.") || i.GigyaName.Split('.').Length < 3))
            {
                response.Error = "Gigya DS fields must be in the format ds.type.fieldName";
                _logger.Error(response.Error);
                return(response);
            }

            // map back to db models
            settings.Method   = (int)model.Method;
            settings.Mappings = model.Mappings.Select(i => new Data.GigyaUmbracoDsMapping
            {
                CmsName     = i.CmsName,
                DsSettingId = settings.Id,
                GigyaName   = i.GigyaName,
                Oid         = i.Oid
            }).ToList();

            var mappedSettings = settingsHelper.Map(settings.Mappings, settings);

            if (_validateDsSettings && !Validate(mappedSettings, ref response))
            {
                return(response);
            }

            settingsHelper.Save(settings);

            response.Success = true;
            settingsHelper.ClearCache(model.Id);
            return(response);
        }