From ec6cf8defbc0de17ae4705af04f4579a69c659ea Mon Sep 17 00:00:00 2001 From: Pranav C Date: Fri, 7 Apr 2023 00:10:53 +0530 Subject: [PATCH] feat: project create api (WIP) Signed-off-by: Pranav C --- .../nocodb-nest/src/helpers/populateMeta.ts | 25 +++++++++---------- .../src/projects/projects.controller.ts | 14 ++++++----- .../src/strategies/jwt.strategy.ts | 9 +++---- .../nocodb-nest/src/utils/utils.service.ts | 3 ++- 4 files changed, 26 insertions(+), 25 deletions(-) diff --git a/packages/nocodb-nest/src/helpers/populateMeta.ts b/packages/nocodb-nest/src/helpers/populateMeta.ts index 2e5b3a8aa8..2e8a92a384 100644 --- a/packages/nocodb-nest/src/helpers/populateMeta.ts +++ b/packages/nocodb-nest/src/helpers/populateMeta.ts @@ -1,19 +1,18 @@ import { ModelTypes, UITypes, ViewTypes } from 'nocodb-sdk'; -import Column from '../../../models/Column'; -import Model from '../../../models/Model'; -import NcHelp from '../../../utils/NcHelp'; -import View from '../../../models/View'; -import NcConnectionMgrv2 from '../../../utils/common/NcConnectionMgrv2'; +import Column from '../models/Column'; +import Model from '../models/Model'; +import NcHelp from '../utils/NcHelp'; +import View from '../models/View'; import getTableNameAlias, { getColumnNameAlias, -} from '../../helpers/getTableName'; -import getColumnUiType from '../../helpers/getColumnUiType'; -import mapDefaultDisplayValue from '../../helpers/mapDefaultDisplayValue'; -import { extractAndGenerateManyToManyRelations } from '../../../services/metaDiff.svc'; -import { IGNORE_TABLES } from '../../../utils/common/BaseApiBuilder'; -import type LinkToAnotherRecordColumn from '../../../models/LinkToAnotherRecordColumn'; -import type Base from '../../../models/Base'; -import type Project from '../../../models/Project'; +} from '../helpers/getTableName'; +import getColumnUiType from '../helpers/getColumnUiType'; +import mapDefaultDisplayValue from '../helpers/mapDefaultDisplayValue'; +import { extractAndGenerateManyToManyRelations } from '../../services/metaDiff.svc'; +import { IGNORE_TABLES } from '../utils/common/BaseApiBuilder'; +import type LinkToAnotherRecordColumn from '../../models/LinkToAnotherRecordColumn'; +import type Base from '../models/Base'; +import type Project from '../models/Project'; export async function populateMeta(base: Base, project: Project): Promise { const info = { diff --git a/packages/nocodb-nest/src/projects/projects.controller.ts b/packages/nocodb-nest/src/projects/projects.controller.ts index f9073d22ff..d1338559b2 100644 --- a/packages/nocodb-nest/src/projects/projects.controller.ts +++ b/packages/nocodb-nest/src/projects/projects.controller.ts @@ -1,17 +1,20 @@ -import { Body, Controller, Delete, Get, Param, Patch, Post, Query } from '@nestjs/common' +import { Body, Controller, Delete, Get, Param, Patch, Post, Query, Request, UseGuards } from '@nestjs/common' +import { AuthGuard } from '@nestjs/passport'; import isDocker from 'is-docker' import Noco from '../Noco' import { packageVersion } from '../utils/packageVersion' import { ProjectsService } from './projects.service'; + +@UseGuards(AuthGuard('jwt')) @Controller() export class ProjectsController { constructor(private readonly projectsService: ProjectsService) {} @Get('/api/v1/db/meta/projects/') - list(@Query() queryParams: Record){ + list(@Query() queryParams: Record, @Request() req){ return this.projectsService.list({ - user: { id: '1', roles: 'SUPER_ADMIN' }, + user: req.user, query: queryParams }); } @@ -76,11 +79,10 @@ export class ProjectsController { @Post( '/api/v1/db/meta/projects' ) - async projectCreate(@Body() projectBody: Record){ + async projectCreate(@Body() projectBody: Record, @Request() req){ const project = await this.projectsService.projectCreate({ project: projectBody, - // todo: user - // user: req['user'], + user: req['user'], }); return project; diff --git a/packages/nocodb-nest/src/strategies/jwt.strategy.ts b/packages/nocodb-nest/src/strategies/jwt.strategy.ts index a1fa1124d5..b2466aa78b 100644 --- a/packages/nocodb-nest/src/strategies/jwt.strategy.ts +++ b/packages/nocodb-nest/src/strategies/jwt.strategy.ts @@ -1,20 +1,19 @@ import { Injectable, UnauthorizedException } from '@nestjs/common' import { PassportStrategy } from '@nestjs/passport'; import { Strategy, ExtractJwt } from 'passport-jwt'; -import { AuthService } from './auth.service'; +import { UsersService } from '../users/users.service' @Injectable() export class JwtStrategy extends PassportStrategy(Strategy) { - constructor(private authService: AuthService) { + constructor(private userService: UsersService) { super({ - jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), ignoreExpiration: false, - secretOrKey: 'your-secret-key', + jwtFromRequest: ExtractJwt.fromHeader('xc-auth'), }); } async validate(payload: any) { - const user = await this.authService.validateUser(payload); + const user = await this.userService.findOne(payload?.email); if (!user) { throw new UnauthorizedException(); } diff --git a/packages/nocodb-nest/src/utils/utils.service.ts b/packages/nocodb-nest/src/utils/utils.service.ts index 24ef29b7b7..f471adea62 100644 --- a/packages/nocodb-nest/src/utils/utils.service.ts +++ b/packages/nocodb-nest/src/utils/utils.service.ts @@ -3,6 +3,7 @@ import axios from 'axios'; import { compareVersions, validate } from 'compare-versions'; import { NC_ATTACHMENT_FIELD_SIZE } from '../constants'; import { NcError } from '../helpers/catchError' +import { User } from '../models' import NcConfigFactory from './NcConfigFactory'; import { packageVersion } from './packageVersion'; // import { packageVersion } from '../packageVersion'; @@ -57,7 +58,7 @@ interface AllMeta { @Injectable() export class UtilsService { async info() { - const projectHasAdmin = false; //!(await User.isFirst()); + const projectHasAdmin = !(await User.isFirst()); const result = { authType: 'jwt', projectHasAdmin,