Browse Source

feat: org api tokens apis

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/5444/head
Pranav C 2 years ago
parent
commit
0e63ba8584
  1. 3
      packages/nocodb-nest/src/app.module.ts
  2. 18
      packages/nocodb-nest/src/modules/org-tokens/ee/org-tokens/org-tokens-ee.service.spec.ts
  3. 24
      packages/nocodb-nest/src/modules/org-tokens/ee/org-tokens/org-tokens-ee.service.ts
  4. 20
      packages/nocodb-nest/src/modules/org-tokens/org-tokens.controller.spec.ts
  5. 66
      packages/nocodb-nest/src/modules/org-tokens/org-tokens.controller.ts
  6. 10
      packages/nocodb-nest/src/modules/org-tokens/org-tokens.module.ts
  7. 18
      packages/nocodb-nest/src/modules/org-tokens/org-tokens.service.spec.ts
  8. 59
      packages/nocodb-nest/src/modules/org-tokens/org-tokens.service.ts

3
packages/nocodb-nest/src/app.module.ts

@ -31,9 +31,10 @@ import { HookFiltersModule } from './modules/hook-filters/hook-filters.module';
import { ApiTokensModule } from './modules/api-tokens/api-tokens.module'; import { ApiTokensModule } from './modules/api-tokens/api-tokens.module';
import { AttachmentsModule } from './modules/attachments/attachments.module'; import { AttachmentsModule } from './modules/attachments/attachments.module';
import { OrgLcenseModule } from './modules/org-lcense/org-lcense.module'; import { OrgLcenseModule } from './modules/org-lcense/org-lcense.module';
import { OrgTokensModule } from './modules/org-tokens/org-tokens.module';
@Module({ @Module({
imports: [AuthModule, UsersModule, UtilsModule, ProjectsModule, TablesModule, ViewsModule, FiltersModule, SortsModule, ColumnsModule, ViewColumnsModule, BasesModule, HooksModule, SharedBasesModule, FormsModule, GridsModule, KanbansModule, GalleriesModule, FormColumnsModule, GridColumnsModule, MapsModule, ProjectUsersModule, ModelVisibilitiesModule, HookFiltersModule, ApiTokensModule, AttachmentsModule, OrgLcenseModule], imports: [AuthModule, UsersModule, UtilsModule, ProjectsModule, TablesModule, ViewsModule, FiltersModule, SortsModule, ColumnsModule, ViewColumnsModule, BasesModule, HooksModule, SharedBasesModule, FormsModule, GridsModule, KanbansModule, GalleriesModule, FormColumnsModule, GridColumnsModule, MapsModule, ProjectUsersModule, ModelVisibilitiesModule, HookFiltersModule, ApiTokensModule, AttachmentsModule, OrgLcenseModule, OrgTokensModule],
controllers: [], controllers: [],
providers: [Connection, MetaService, JwtStrategy, ExtractProjectIdMiddleware], providers: [Connection, MetaService, JwtStrategy, ExtractProjectIdMiddleware],
exports: [Connection, MetaService], exports: [Connection, MetaService],

18
packages/nocodb-nest/src/modules/org-tokens/ee/org-tokens/org-tokens-ee.service.spec.ts

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { OrgTokensEeService } from './org-tokens.service';
describe('OrgTokensService', () => {
let service: OrgTokensEeService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [OrgTokensEeService],
}).compile();
service = module.get<OrgTokensEeService>(OrgTokensEeService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

24
packages/nocodb-nest/src/modules/org-tokens/ee/org-tokens/org-tokens-ee.service.ts

@ -0,0 +1,24 @@
import { Injectable } from '@nestjs/common';
import { OrgUserRoles, UserType } from 'nocodb-sdk';
import { PagedResponseImpl } from '../../../../helpers/PagedResponse';
import { ApiToken } from '../../../../models';
@Injectable()
export class OrgTokensEeService {
async 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({}),
},
);
}
}

20
packages/nocodb-nest/src/modules/org-tokens/org-tokens.controller.spec.ts

@ -0,0 +1,20 @@
import { Test, TestingModule } from '@nestjs/testing';
import { OrgTokensController } from './org-tokens.controller';
import { OrgTokensService } from './org-tokens.service';
describe('OrgTokensController', () => {
let controller: OrgTokensController;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [OrgTokensController],
providers: [OrgTokensService],
}).compile();
controller = module.get<OrgTokensController>(OrgTokensController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
});

66
packages/nocodb-nest/src/modules/org-tokens/org-tokens.controller.ts

@ -0,0 +1,66 @@
import {
Body,
Controller,
Delete,
Get,
Param,
Post,
Request,
UseGuards,
} from '@nestjs/common';
import { ApiTokenReqType } from 'nocodb-sdk';
import { getConditionalHandler } from '../../helpers/getHandler';
import {
Acl,
ExtractProjectIdMiddleware,
} from '../../middlewares/extract-project-id/extract-project-id.middleware';
import { OrgTokensEeService } from './ee/org-tokens/org-tokens-ee.service';
import { OrgTokensService } from './org-tokens.service';
import { AuthGuard } from '@nestjs/passport';
@UseGuards(ExtractProjectIdMiddleware, AuthGuard('jwt'))
@Controller('org-tokens')
export class OrgTokensController {
constructor(
private readonly orgTokensService: OrgTokensService,
private readonly orgTokensEeService: OrgTokensEeService,
) {}
@Get('/api/v1/tokens')
@Acl('apiTokenList', {
blockApiTokenAccess: true,
})
async apiTokenList(@Request() req) {
return await getConditionalHandler(
this.orgTokensService.apiTokenList,
this.orgTokensEeService.apiTokenListEE,
)({
query: req.query,
user: req['user'],
});
}
@Post('/api/v1/tokens')
@Acl('apiTokenCreate', {
blockApiTokenAccess: true,
})
async apiTokenCreate(@Request() req, @Body() body: ApiTokenReqType) {
return await this.orgTokensService.apiTokenCreate({
apiToken: body,
user: req['user'],
});
}
@Delete('/api/v1/tokens/:token')
@Acl('apiTokenDelete', {
// allowedRoles: [OrgUserRoles.SUPER],
blockApiTokenAccess: true,
})
async apiTokenDelete(@Request() req, @Param('token') token: string) {
return;
await this.orgTokensService.apiTokenDelete({
token,
user: req['user'],
});
}
}

10
packages/nocodb-nest/src/modules/org-tokens/org-tokens.module.ts

@ -0,0 +1,10 @@
import { Module } from '@nestjs/common';
import { OrgTokensService } from './org-tokens.service';
import { OrgTokensController } from './org-tokens.controller';
import { OrgTokensEeService } from './ee/org-tokens/org-tokens.service';
@Module({
controllers: [OrgTokensController],
providers: [OrgTokensEeService]
})
export class OrgTokensModule {}

18
packages/nocodb-nest/src/modules/org-tokens/org-tokens.service.spec.ts

@ -0,0 +1,18 @@
import { Test, TestingModule } from '@nestjs/testing';
import { OrgTokensService } from './org-tokens.service';
describe('OrgTokensService', () => {
let service: OrgTokensService;
beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [OrgTokensService],
}).compile();
service = module.get<OrgTokensService>(OrgTokensService);
});
it('should be defined', () => {
expect(service).toBeDefined();
});
});

59
packages/nocodb-nest/src/modules/org-tokens/org-tokens.service.ts

@ -0,0 +1,59 @@
import { Injectable } from '@nestjs/common';
import { ApiTokenReqType, OrgUserRoles } from 'nocodb-sdk';
import { validatePayload } from '../../helpers';
import { NcError } from '../../helpers/catchError';
import { PagedResponseImpl } from '../../helpers/PagedResponse';
import { ApiToken, User } from '../../models';
import { T } from 'nc-help';
@Injectable()
export class OrgTokensService {
async apiTokenList(param: { user: User; query: any }) {
const fk_user_id = param.user.id;
let includeUnmappedToken = false;
if (param.user.roles.includes(OrgUserRoles.SUPER_ADMIN)) {
includeUnmappedToken = true;
}
return new PagedResponseImpl(
await ApiToken.listWithCreatedBy({
...param.query,
fk_user_id,
includeUnmappedToken,
}),
{
...param.query,
count: await ApiToken.count({
includeUnmappedToken,
fk_user_id,
}),
},
);
}
async apiTokenCreate(param: { user: User; apiToken: ApiTokenReqType }) {
validatePayload(
'swagger.json#/components/schemas/ApiTokenReq',
param.apiToken,
);
T.emit('evt', { evt_type: 'org:apiToken:created' });
return await ApiToken.insert({
...param.apiToken,
fk_user_id: param['user'].id,
});
}
async apiTokenDelete(param: { user: User; token: string }) {
const fk_user_id = param.user.id;
const apiToken = await ApiToken.getByToken(param.token);
if (
!param.user.roles.includes(OrgUserRoles.SUPER_ADMIN) &&
apiToken.fk_user_id !== fk_user_id
) {
NcError.notFound('Token not found');
}
T.emit('evt', { evt_type: 'org:apiToken:deleted' });
return await ApiToken.delete(param.token);
}
}
Loading…
Cancel
Save