Esempio n. 1
0
        public async Task <IActionResult> Index(ApiConfigurationModel model)
        {
            if (!await _permissionService.AuthorizeAsync(StandardPermissionProvider.ManagePlugins))
            {
                return(AccessDeniedView());
            }

            //load settings for a chosen tenant scope
            var tenantScope = _tenantContext.ActiveTenantScopeConfiguration;
            var apiSettings = model.ToEntity();

            await _settingService.SaveSettingOverridablePerTenant(apiSettings, x => x.EnableApi,
                                                                  model.EnableApi_OverrideForTenant, tenantScope, false);

            await _settingService.SaveSettingOverridablePerTenant(apiSettings, x => x.EnableLogging,
                                                                  model.EnableLogging_OverrideForTenant, tenantScope, false);

            _settingService.ClearCache();

            //activity log
            await _userActivityService.InsertActivityAsync("EditApiSettings", "Edited api settings");

            _notificationService.SuccessNotification("The settings have been updated successfully.");

            //selected tab
            SaveSelectedTabName();

            return(RedirectToAction("Index"));
        }
        protected void PrepareConfigurationModel(ApiConfigurationModel model, int storeScope)
        {
            var store = storeScope == 0
                                ? Services.StoreContext.CurrentStore
                                : Services.StoreService.GetStoreById(storeScope);

            model.PrimaryStoreCurrencyCode = store.PrimaryStoreCurrency.CurrencyCode;
        }
Esempio n. 3
0
        public LogServiceTests()
        {
            var values = new ApiConfigurationModel
            {
                ApiKey     = "key46INqjpp7lMzjd",
                ApiGetUrl  = "https://api.airtable.com/v0/appD1b1YjWoXkUJwR/Messages?maxRecords=3&view=Grid%20view",
                ApiPostUrl = "https://api.airtable.com/v0/appD1b1YjWoXkUJwR/Messages"
            };

            var mockSettings = new Mock <IOptions <ApiConfigurationModel> >();

            mockSettings.Setup(v => v.Value).Returns(values);

            config = mockSettings.Object;

            logger = new Mock <ILogger <LogService> >().Object;
        }
Esempio n. 4
0
 public LogService(IOptions <ApiConfigurationModel> option, ILogger <LogService> _logger)
 {
     apiConfigurationModel = option.Value;
     logger = _logger;
 }
Esempio n. 5
0
        protected bool SaveConfigurationModel <TSetting>(ApiConfigurationModel model, FormCollection form, Action <TSetting> map = null) where TSetting : PayPalApiSettingsBase, ISettings, new()
        {
            var storeDependingSettingHelper = new StoreDependingSettingHelper(ViewData);
            var storeScope = this.GetActiveStoreScopeConfiguration(Services.StoreService, Services.WorkContext);
            var settings   = Services.Settings.LoadSetting <TSetting>(storeScope);

            var oldClientId  = settings.ClientId;
            var oldSecret    = settings.Secret;
            var oldProfileId = settings.ExperienceProfileId;

            var validator = new PayPalApiConfigValidator(T, x =>
            {
                return(storeScope == 0 || storeDependingSettingHelper.IsOverrideChecked(settings, x, form));
            });

            validator.Validate(model, ModelState);

            if (!ModelState.IsValid)
            {
                return(false);
            }

            ModelState.Clear();
            model.TransactMode = TransactMode.AuthorizeAndCapture;

            MiniMapper.Map(model, settings);
            settings.ApiAccountName      = model.ApiAccountName.TrimSafe();
            settings.ApiAccountPassword  = model.ApiAccountPassword.TrimSafe();
            settings.ClientId            = model.ClientId.TrimSafe();
            settings.ExperienceProfileId = model.ExperienceProfileId.TrimSafe();
            settings.Secret    = model.Secret.TrimSafe();
            settings.Signature = model.Signature.TrimSafe();
            settings.WebhookId = model.WebhookId.TrimSafe();

            // Additional mapping.
            map?.Invoke(settings);

            // Credentials changed: reset profile and webhook id to avoid errors.
            if (!oldClientId.IsCaseInsensitiveEqual(settings.ClientId) || !oldSecret.IsCaseInsensitiveEqual(settings.Secret))
            {
                if (oldProfileId.IsCaseInsensitiveEqual(settings.ExperienceProfileId))
                {
                    settings.ExperienceProfileId = null;
                }

                settings.WebhookId = null;
            }

            using (Services.Settings.BeginScope())
            {
                storeDependingSettingHelper.UpdateSettings(settings, form, storeScope, Services.Settings);
            }

            using (Services.Settings.BeginScope())
            {
                // Multistore context not possible, see IPN handling.
                Services.Settings.SaveSetting(settings, x => x.UseSandbox, 0, false);
            }

            NotifySuccess(T("Admin.Common.DataSuccessfullySaved"));

            return(true);
        }
        protected bool SaveConfigurationModel(
            ApiConfigurationModel model,
            FormCollection form,
            Action <TSetting> map = null)
        {
            var storeDependingSettingHelper = new StoreDependingSettingHelper(ViewData);
            var storeScope = this.GetActiveStoreScopeConfiguration(Services.StoreService, Services.WorkContext);
            var settings   = Services.Settings.LoadSetting <TSetting>(storeScope);

            var oldClientId  = settings.ClientId;
            var oldSecret    = settings.Secret;
            var oldProfileId = settings.ExperienceProfileId;

            var validator = new PayPalApiConfigValidator(T, x =>
            {
                return(storeScope == 0 || storeDependingSettingHelper.IsOverrideChecked(settings, x, form));
            });

            validator.Validate(model, ModelState);

            // Additional validation.
            if (ModelState.IsValid)
            {
                // PayPal review: check if credentials are valid.
                var credentialChanged = model.ClientId.HasValue() && model.Secret.HasValue() &&
                                        (!model.ClientId.IsCaseInsensitiveEqual(settings.ClientId) || !model.Secret.IsCaseInsensitiveEqual(settings.Secret));

                if (credentialChanged)
                {
                    var session = new PayPalSessionData {
                        ProviderSystemName = ProviderSystemName
                    };
                    var tmpSettings = new PayPalApiSettingsBase
                    {
                        UseSandbox = model.UseSandbox,
                        ClientId   = model.ClientId,
                        Secret     = model.Secret
                    };

                    var result = PayPalService.EnsureAccessToken(session, tmpSettings);
                    if (!result.Success)
                    {
                        ModelState.AddModelError("", T("Plugins.SmartStore.PayPal.InvalidCredentials"));
                        ModelState.AddModelError("", result.ErrorMessage);
                    }
                }
            }

            if (!ModelState.IsValid)
            {
                return(false);
            }

            ModelState.Clear();
            model.TransactMode = TransactMode.AuthorizeAndCapture;

            MiniMapper.Map(model, settings);
            settings.ApiAccountName      = model.ApiAccountName.TrimSafe();
            settings.ApiAccountPassword  = model.ApiAccountPassword.TrimSafe();
            settings.ClientId            = model.ClientId.TrimSafe();
            settings.ExperienceProfileId = model.ExperienceProfileId.TrimSafe();
            settings.Secret    = model.Secret.TrimSafe();
            settings.Signature = model.Signature.TrimSafe();
            settings.WebhookId = model.WebhookId.TrimSafe();

            // Additional mapping.
            map?.Invoke(settings);

            // Credentials changed: reset profile and webhook id to avoid errors.
            if (!oldClientId.IsCaseInsensitiveEqual(settings.ClientId) || !oldSecret.IsCaseInsensitiveEqual(settings.Secret))
            {
                if (oldProfileId.IsCaseInsensitiveEqual(settings.ExperienceProfileId))
                {
                    settings.ExperienceProfileId = null;
                }

                settings.WebhookId = null;
            }

            using (Services.Settings.BeginScope())
            {
                storeDependingSettingHelper.UpdateSettings(settings, form, storeScope, Services.Settings);
            }

            using (Services.Settings.BeginScope())
            {
                // Multistore context not possible, see IPN handling.
                Services.Settings.SaveSetting(settings, x => x.UseSandbox, 0, false);
            }

            NotifySuccess(T("Admin.Common.DataSuccessfullySaved"));
            return(true);
        }
Esempio n. 7
0
 public TokenService(IOptions <ApiConfigurationModel> options, ILogger <TokenService> _logger)
 {
     apiConfigurationModel = options.Value;
     logger = _logger;
 }