Browse Source

refactor: add conditional service function logic

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5239/head
Pranav C 2 years ago
parent
commit
ac9596ded5
  1. 12
      packages/nocodb/src/lib/controllers/orgTokenController.ts
  2. 22
      packages/nocodb/src/lib/meta/api/ee/orgTokenApis.ts
  3. 17
      packages/nocodb/src/lib/meta/helpers/getHandler.ts
  4. 20
      packages/nocodb/src/lib/services/ee/orgTokenService.ts
  5. 3
      packages/nocodb/src/lib/services/index.ts

12
packages/nocodb/src/lib/controllers/orgTokenController.ts

@ -1,13 +1,15 @@
import { Request, Response, Router } from 'express'; import { Request, Response, Router } from 'express';
import { metaApiMetrics } from '../meta/helpers/apiMetrics'; 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 ncMetaAclMw from '../meta/helpers/ncMetaAclMw';
import { apiTokenListEE } from '../meta/api/ee/orgTokenApis'; import { orgTokenService, orgTokenServiceEE } from '../services';
import { orgTokenService } from '../services';
async function apiTokenList(req, res) { async function apiTokenList(req, res) {
res.json( res.json(
await orgTokenService.apiTokenList({ await getConditionalHandler(
orgTokenService.apiTokenList,
orgTokenServiceEE.apiTokenListEE
)({
query: req.query, query: req.query,
user: req['user'], user: req['user'],
}) })
@ -37,7 +39,7 @@ const router = Router({ mergeParams: true });
router.get( router.get(
'/api/v1/tokens', '/api/v1/tokens',
metaApiMetrics, metaApiMetrics,
ncMetaAclMw(getHandler(apiTokenList, apiTokenListEE), 'apiTokenList', { ncMetaAclMw(apiTokenList, 'apiTokenList', {
// allowedRoles: [OrgUserRoles.SUPER], // allowedRoles: [OrgUserRoles.SUPER],
blockApiTokenAccess: true, blockApiTokenAccess: true,
}) })

22
packages/nocodb/src/lib/meta/api/ee/orgTokenApis.ts

@ -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({}),
}
)
);
}

17
packages/nocodb/src/lib/meta/helpers/getHandler.ts

@ -12,3 +12,20 @@ export default function getHandler(
return eeHandler(...args); return eeHandler(...args);
}; };
} }
export function getConditionalHandler<
T extends (...args: any[]) => any,
U extends (...args: any[]) => any
>(
defaultHandler: T,
eeHandler: U
): (
...args: Parameters<T> | Parameters<U>
) => Promise<ReturnType<T> | ReturnType<U>> {
return async (...args: Parameters<T> | Parameters<U>) => {
if (Noco.isEE()) {
return defaultHandler(...args);
}
return eeHandler(...args);
};
}

20
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({}),
}
);
}

3
packages/nocodb/src/lib/services/index.ts

@ -21,7 +21,6 @@ export * as mapViewService from './mapViewService';
export * as modelVisibilityService from './modelVisibilityService'; export * as modelVisibilityService from './modelVisibilityService';
export * as sharedBaseService from './sharedBaseService'; export * as sharedBaseService from './sharedBaseService';
export * as orgUserService from './orgUserService'; export * as orgUserService from './orgUserService';
export * as orgTokenService from './orgTokenService';
export * as orgLicenseService from './orgLicenseService'; export * as orgLicenseService from './orgLicenseService';
export * as projectUserService from './projectUserService'; export * as projectUserService from './projectUserService';
export * as attachmentService from './attachmentService'; export * as attachmentService from './attachmentService';
@ -34,3 +33,5 @@ export * as swaggerService from './swaggerService';
export * as userService from './userService'; export * as userService from './userService';
export * as syncService from './syncService'; export * as syncService from './syncService';
export * from './public'; export * from './public';
export * as orgTokenService from './orgTokenService';
export * as orgTokenServiceEE from './ee/orgTokenService';

Loading…
Cancel
Save