diff --git a/packages/nocodb/src/lib/controllers/orgTokenController.ts b/packages/nocodb/src/lib/controllers/orgTokenController.ts index 293ccd8c38..75e406fc42 100644 --- a/packages/nocodb/src/lib/controllers/orgTokenController.ts +++ b/packages/nocodb/src/lib/controllers/orgTokenController.ts @@ -1,13 +1,15 @@ import { Request, Response, Router } from 'express'; import { metaApiMetrics } from '../meta/helpers/apiMetrics'; -import getHandler from '../meta/helpers/getHandler'; +import { getConditionalHandler } from '../meta/helpers/getHandler'; import ncMetaAclMw from '../meta/helpers/ncMetaAclMw'; -import { apiTokenListEE } from '../meta/api/ee/orgTokenApis'; -import { orgTokenService } from '../services'; +import { orgTokenService, orgTokenServiceEE } from '../services'; async function apiTokenList(req, res) { res.json( - await orgTokenService.apiTokenList({ + await getConditionalHandler( + orgTokenService.apiTokenList, + orgTokenServiceEE.apiTokenListEE + )({ query: req.query, user: req['user'], }) @@ -37,7 +39,7 @@ const router = Router({ mergeParams: true }); router.get( '/api/v1/tokens', metaApiMetrics, - ncMetaAclMw(getHandler(apiTokenList, apiTokenListEE), 'apiTokenList', { + ncMetaAclMw(apiTokenList, 'apiTokenList', { // allowedRoles: [OrgUserRoles.SUPER], blockApiTokenAccess: true, }) diff --git a/packages/nocodb/src/lib/meta/api/ee/orgTokenApis.ts b/packages/nocodb/src/lib/meta/api/ee/orgTokenApis.ts deleted file mode 100644 index 410d040c11..0000000000 --- a/packages/nocodb/src/lib/meta/api/ee/orgTokenApis.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { OrgUserRoles } from 'nocodb-sdk'; -import ApiToken from '../../../models/ApiToken'; -import { PagedResponseImpl } from '../../helpers/PagedResponse'; - -export async function apiTokenListEE(req, res) { - let fk_user_id = req.user.id; - - // if super admin get all tokens - if (req.user.roles.includes(OrgUserRoles.SUPER_ADMIN)) { - fk_user_id = undefined; - } - - res.json( - new PagedResponseImpl( - await ApiToken.listWithCreatedBy({ ...req.query, fk_user_id }), - { - ...req.query, - count: await ApiToken.count({}), - } - ) - ); -} diff --git a/packages/nocodb/src/lib/meta/helpers/getHandler.ts b/packages/nocodb/src/lib/meta/helpers/getHandler.ts index 1f40f45b2f..07c57a08eb 100644 --- a/packages/nocodb/src/lib/meta/helpers/getHandler.ts +++ b/packages/nocodb/src/lib/meta/helpers/getHandler.ts @@ -12,3 +12,20 @@ export default function getHandler( return eeHandler(...args); }; } + +export function getConditionalHandler< + T extends (...args: any[]) => any, + U extends (...args: any[]) => any +>( + defaultHandler: T, + eeHandler: U +): ( + ...args: Parameters | Parameters +) => Promise | ReturnType> { + return async (...args: Parameters | Parameters) => { + if (Noco.isEE()) { + return defaultHandler(...args); + } + return eeHandler(...args); + }; +} diff --git a/packages/nocodb/src/lib/services/ee/orgTokenService.ts b/packages/nocodb/src/lib/services/ee/orgTokenService.ts new file mode 100644 index 0000000000..9dd491144c --- /dev/null +++ b/packages/nocodb/src/lib/services/ee/orgTokenService.ts @@ -0,0 +1,20 @@ +import { OrgUserRoles, UserType } from 'nocodb-sdk'; +import { PagedResponseImpl } from '../../meta/helpers/PagedResponse'; +import { ApiToken } from '../../models'; + +export async function apiTokenListEE(param: { user: UserType; query: any }) { + let fk_user_id = param.user.id; + + // if super admin get all tokens + if (param.user.roles.includes(OrgUserRoles.SUPER_ADMIN)) { + fk_user_id = undefined; + } + + return new PagedResponseImpl( + await ApiToken.listWithCreatedBy({ ...param.query, fk_user_id }), + { + ...(param.query || {}), + count: await ApiToken.count({}), + } + ); +} diff --git a/packages/nocodb/src/lib/services/index.ts b/packages/nocodb/src/lib/services/index.ts index f5ffe2f46f..f99398753c 100644 --- a/packages/nocodb/src/lib/services/index.ts +++ b/packages/nocodb/src/lib/services/index.ts @@ -21,7 +21,6 @@ export * as mapViewService from './mapViewService'; export * as modelVisibilityService from './modelVisibilityService'; export * as sharedBaseService from './sharedBaseService'; export * as orgUserService from './orgUserService'; -export * as orgTokenService from './orgTokenService'; export * as orgLicenseService from './orgLicenseService'; export * as projectUserService from './projectUserService'; export * as attachmentService from './attachmentService'; @@ -34,3 +33,5 @@ export * as swaggerService from './swaggerService'; export * as userService from './userService'; export * as syncService from './syncService'; export * from './public'; +export * as orgTokenService from './orgTokenService'; +export * as orgTokenServiceEE from './ee/orgTokenService';