mirror of https://github.com/nocodb/nocodb
Pranav C
2 years ago
10 changed files with 187 additions and 40 deletions
@ -0,0 +1,39 @@
|
||||
import { Router } from 'express'; |
||||
import { OrgUserRoles } from '../../../enums/OrgUserRoles'; |
||||
import Store from '../../models/Store'; |
||||
import { metaApiMetrics } from '../helpers/apiMetrics'; |
||||
import ncMetaAclMw from '../helpers/ncMetaAclMw'; |
||||
|
||||
const LICENSE_KEY = 'nc-license-key'; |
||||
|
||||
async function licenseGet(_req, res) { |
||||
const license = await Store.get(LICENSE_KEY); |
||||
|
||||
res.json({ key: license?.value }); |
||||
} |
||||
|
||||
async function licenseSet(req, res) { |
||||
await Store.saveOrUpdate({ value: req.body.key, key: LICENSE_KEY }); |
||||
|
||||
res.json({ msg: 'License key saved' }); |
||||
} |
||||
|
||||
const router = Router({ mergeParams: true }); |
||||
router.get( |
||||
'/api/v1/license', |
||||
metaApiMetrics, |
||||
ncMetaAclMw(licenseGet, 'licenseGet', { |
||||
allowedRoles: [OrgUserRoles.SUPER], |
||||
blockApiTokenAccess: true, |
||||
}) |
||||
); |
||||
router.post( |
||||
'/api/v1/license', |
||||
metaApiMetrics, |
||||
ncMetaAclMw(licenseSet, 'licenseSet', { |
||||
allowedRoles: [OrgUserRoles.SUPER], |
||||
blockApiTokenAccess: true, |
||||
}) |
||||
); |
||||
|
||||
export default router; |
@ -0,0 +1,46 @@
|
||||
import { NcError } from '../meta/helpers/catchError'; |
||||
import { extractProps } from '../meta/helpers/extractProps'; |
||||
import Noco from '../Noco'; |
||||
import { MetaTable } from '../utils/globals'; |
||||
import { SortType } from 'nocodb-sdk'; |
||||
|
||||
export default class Store { |
||||
key?: string; |
||||
value?: string; |
||||
type?: string; |
||||
env?: string; |
||||
tag?: string; |
||||
project_id?: string; |
||||
db_alias?: string; |
||||
|
||||
constructor(data: Partial<SortType>) { |
||||
Object.assign(this, data); |
||||
} |
||||
|
||||
public static get(key: string, ncMeta = Noco.ncMeta): Promise<Store> { |
||||
return ncMeta.metaGet(null, null, MetaTable.STORE, { key }); |
||||
} |
||||
|
||||
static async saveOrUpdate(store: Store, ncMeta = Noco.ncMeta) { |
||||
if (!store.key) { |
||||
NcError.badRequest('Key is required'); |
||||
} |
||||
|
||||
const insertObj = extractProps(store, [ |
||||
'key', |
||||
'value', |
||||
'type', |
||||
'env', |
||||
'tag', |
||||
]); |
||||
|
||||
const existing = await Store.get(store.key, ncMeta); |
||||
if (existing) { |
||||
await ncMeta.metaUpdate(null, null, MetaTable.STORE, insertObj, { |
||||
key: store.key, |
||||
}); |
||||
} else { |
||||
await ncMeta.metaInsert(null, null, MetaTable.STORE, insertObj); |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue