diff --git a/packages/api/src/controllers/config.js b/packages/api/src/controllers/config.js index 941994889..67379e296 100644 --- a/packages/api/src/controllers/config.js +++ b/packages/api/src/controllers/config.js @@ -13,7 +13,7 @@ const platformInfo = require('../utility/platformInfo'); const connections = require('../controllers/connections'); const { getAuthProviderFromReq } = require('../auth/authProvider'); const isElectron = require('is-electron'); -const { checkLicenseApp, checkLicenseWeb } = require('../utility/checkLicense'); +const { checkLicense, checkLicenseKey } = require('../utility/checkLicense'); const lock = new AsyncLock(); @@ -47,7 +47,7 @@ module.exports = { 'Basic authentization is not allowed, when using storage. Cannot use both STORAGE_DATABASE and BASIC_AUTH'; } - const checkedLicense = isElectron() ? checkLicenseApp() : checkLicenseWeb(); + const checkedLicense = await checkLicense(); const isLicenseValid = checkedLicense?.status == 'ok'; return { @@ -124,7 +124,10 @@ module.exports = { async loadSettings() { try { const settingsText = await fs.readFile(path.join(datadir(), 'settings.json'), { encoding: 'utf-8' }); - return this.fillMissingSettings(JSON.parse(settingsText)); + return { + ...this.fillMissingSettings(JSON.parse(settingsText)), + 'other.licenseKey': await this.loadLicenseKey(), + }; } catch (err) { return this.fillMissingSettings({}); } @@ -158,10 +161,16 @@ module.exports = { try { const updated = { ...currentValue, - ...values, + ..._.omit(values, ['other.licenseKey']), }; await fs.writeFile(path.join(datadir(), 'settings.json'), JSON.stringify(updated, undefined, 2)); // this.settingsValue = updated; + + if (currentValue['other.licenseKey'] != values['other.licenseKey']) { + await this.saveLicenseKey({ licenseKey: values['other.licenseKey'] }); + socket.emitChanged(`config-changed`); + } + socket.emitChanged(`settings-changed`); return updated; } catch (err) { @@ -176,4 +185,10 @@ module.exports = { const resp = await axios.default.get('https://raw.githubusercontent.com/dbgate/dbgate/master/CHANGELOG.md'); return resp.data; }, + + checkLicense_meta: true, + async checkLicense({ licenseKey }) { + const resp = await checkLicenseKey(licenseKey); + return resp; + }, }; diff --git a/packages/api/src/utility/platformInfo.js b/packages/api/src/utility/platformInfo.js index 4f0299f4e..d6af1323a 100644 --- a/packages/api/src/utility/platformInfo.js +++ b/packages/api/src/utility/platformInfo.js @@ -3,7 +3,6 @@ const os = require('os'); const path = require('path'); const processArgs = require('./processArgs'); const isElectron = require('is-electron'); -const { checkLicenseWeb, checkLicenseApp } = require('./checkLicense'); const platform = process.env.OS_OVERRIDE ? process.env.OS_OVERRIDE : process.platform; const isWindows = platform === 'win32'; diff --git a/packages/web/src/forms/FormTextAreaFieldRaw.svelte b/packages/web/src/forms/FormTextAreaFieldRaw.svelte index 041c8ced7..dc624a056 100644 --- a/packages/web/src/forms/FormTextAreaFieldRaw.svelte +++ b/packages/web/src/forms/FormTextAreaFieldRaw.svelte @@ -5,6 +5,7 @@ export let name; export let defaultValue = undefined; export let saveOnInput = false; + export let onChange = null; const { values, setFieldValue } = getFormContext(); @@ -17,5 +18,8 @@ if (saveOnInput) { setFieldValue(name, e.target['value']); } + if (onChange) { + onChange(e.target['value']); + } }} /> diff --git a/packages/web/src/settings/SettingsModal.svelte b/packages/web/src/settings/SettingsModal.svelte index 7b6bdd5dd..5f384e00e 100644 --- a/packages/web/src/settings/SettingsModal.svelte +++ b/packages/web/src/settings/SettingsModal.svelte @@ -33,9 +33,13 @@ import ThemeSkeleton from './ThemeSkeleton.svelte'; import { isProApp } from '../utility/proTools'; import FormTextAreaField from '../forms/FormTextAreaField.svelte'; + import { apiCall } from '../utility/api'; + import { useSettings } from '../utility/metadataLoaders'; + import { derived } from 'svelte/store'; const electron = getElectron(); let restartWarning = false; + let licenseKeyCheckResult = null; export let selectedTab = 0; @@ -60,6 +64,23 @@ ORDER BY $selectedWidget = 'plugins'; $visibleWidgetSideBar = true; } + + const settings = useSettings(); + const settingsValues = derived(settings, $settings => { + if (!$settings) { + return {}; + } + return $settings; + }); + + $: licenseKey = $settingsValues['other.licenseKey']; + let checkedLicenseKey = false; + $: if (licenseKey && !checkedLicenseKey) { + checkedLicenseKey = true; + apiCall('config/check-license', { licenseKey }).then(result => { + licenseKeyCheckResult = result; + }); + } @@ -72,7 +93,7 @@ ORDER BY isInline tabs={[ { label: 'General', slot: 1 }, - isProApp() && { label: 'License', slot: 7 }, + isProApp() && electron && { label: 'License', slot: 7 }, { label: 'Connection', slot: 2 }, { label: 'Themes', slot: 3 }, { label: 'Default Actions', slot: 4 }, @@ -325,7 +346,29 @@ ORDER BY
License
- + { + licenseKeyCheckResult = await apiCall('config/check-license', { licenseKey: value }); + }} + /> + {#if licenseKeyCheckResult} +
+ {#if licenseKeyCheckResult.status == 'ok'} +
+ License key is valid +
+
+ License valid to: {licenseKeyCheckResult.validTo} +
+
License key expiration: {licenseKeyCheckResult.expiration}
+ {:else if licenseKeyCheckResult.status == 'error'} + License key is invalid + {/if} +
+ {/if}