diff --git a/packages/nocodb-nest/src/app.module.ts b/packages/nocodb-nest/src/app.module.ts index 4d2176a739..3d6bbbff1b 100644 --- a/packages/nocodb-nest/src/app.module.ts +++ b/packages/nocodb-nest/src/app.module.ts @@ -6,6 +6,7 @@ import { GlobalExceptionFilter } from './filters/global-exception/global-excepti import { GlobalMiddleware } from './middlewares/global/global.middleware'; import { AuthModule } from './modules/auth/auth.module'; import { ExtractProjectIdMiddleware } from './middlewares/extract-project-id/extract-project-id.middleware'; +import { AuthService } from './modules/auth/auth.service' import { UsersModule } from './modules/users/users.module'; import { MetaService } from './meta/meta.service'; import { UsersService } from './modules/users/users.service'; @@ -50,6 +51,7 @@ import { CachesModule } from './modules/caches/caches.module'; import { TestModule } from './modules/test/test.module'; import { PluginsModule } from './modules/plugins/plugins.module'; import { GlobalModule } from './modules/global/global.module'; +import { LocalStrategy } from './strategies/local.strategy' import NcConfigFactory from './utils/NcConfigFactory' import NcUpgrader from './version-upgrader/NcUpgrader'; import type { @@ -123,11 +125,13 @@ export const JwtStrategyProvider: Provider = { ], controllers: [], providers: [ + AuthService, { provide: APP_FILTER, useClass: GlobalExceptionFilter, }, JwtStrategyProvider, + LocalStrategy, ExtractProjectIdMiddleware, ], }) diff --git a/packages/nocodb-nest/src/modules/auth/auth.service.ts b/packages/nocodb-nest/src/modules/auth/auth.service.ts index f5161c002d..c9f300ec2a 100644 --- a/packages/nocodb-nest/src/modules/auth/auth.service.ts +++ b/packages/nocodb-nest/src/modules/auth/auth.service.ts @@ -5,6 +5,8 @@ import * as bcrypt from 'bcryptjs'; import { JwtService } from '@nestjs/jwt'; import { v4 as uuidv4 } from 'uuid'; +import Noco from '../../Noco' +import { genJwt } from '../users/helpers' import { UsersService } from '../users/users.service'; import { Connection } from '../../connection/connection'; import type { CreateUserDto } from './auth.controller'; @@ -13,7 +15,7 @@ import type { CreateUserDto } from './auth.controller'; export class AuthService { constructor( private usersService: UsersService, - private jwtService: JwtService, + // private jwtService: JwtService, private connection: Connection, ) {} @@ -35,7 +37,7 @@ export class AuthService { delete user.salt; const payload = user; return { - token: this.jwtService.sign(payload), + token: genJwt(payload, Noco.getConfig()), }; } diff --git a/packages/nocodb-nest/src/modules/users/users.controller.ts b/packages/nocodb-nest/src/modules/users/users.controller.ts index a650f79d1f..99f1c96f8f 100644 --- a/packages/nocodb-nest/src/modules/users/users.controller.ts +++ b/packages/nocodb-nest/src/modules/users/users.controller.ts @@ -106,6 +106,7 @@ export class UsersController { '/api/v1/auth/user/signin', ]) @UseGuards(AuthGuard('local')) + @HttpCode(200) async signin(@Request() req) { return this.usersService.login(req.user); } diff --git a/packages/nocodb-nest/src/modules/utils/utils.controller.ts b/packages/nocodb-nest/src/modules/utils/utils.controller.ts index 32ffe2a71a..76fa348988 100644 --- a/packages/nocodb-nest/src/modules/utils/utils.controller.ts +++ b/packages/nocodb-nest/src/modules/utils/utils.controller.ts @@ -1,4 +1,16 @@ -import { Controller, Get, Request } from '@nestjs/common'; +import { + Body, + Controller, + Get, + Post, + Request, + UseGuards, +} from '@nestjs/common'; +import { AuthGuard } from '@nestjs/passport'; +import { + Acl, + ExtractProjectIdMiddleware, +} from '../../middlewares/extract-project-id/extract-project-id.middleware'; import { UtilsService } from './utils.service'; @Controller() @@ -16,4 +28,40 @@ export class UtilsController { version() { return this.utilsService.versionInfo(); } + + @UseGuards(ExtractProjectIdMiddleware, AuthGuard('jwt')) + @Post('/api/v1/db/meta/connection/test') + @Acl('testConnection') + async testConnection(@Body() body: any) { + return await this.utilsService.testConnection({ body }); + } + @Get('/api/v1/db/meta/nocodb/info') + async appInfo(@Request() req) { + return await this.utilsService.appInfo({ + req: { + ncSiteUrl: (req as any).ncSiteUrl, + }, + }); + } + + @Get('/api/v1/health') + async appHealth() { + return await this.utilsService.appHealth(); + } + + @Post('/api/v1/db/meta/axiosRequestMake') + async axiosRequestMake(@Body() body: any) { + return await this.utilsService.axiosRequestMake({ body }); + } + @Get('/api/v1/aggregated-meta-info') + async aggregatedMetaInfo() { + return await this.utilsService.aggregatedMetaInfo(); + } + + @Post('/api/v1/url_to_config') + async urlToDbConfig(@Body() body: any) { + return await this.utilsService.urlToDbConfig({ + body, + }); + } } diff --git a/packages/nocodb-nest/src/modules/utils/utils.service.ts b/packages/nocodb-nest/src/modules/utils/utils.service.ts index ad67cae3a7..36d0b05980 100644 --- a/packages/nocodb-nest/src/modules/utils/utils.service.ts +++ b/packages/nocodb-nest/src/modules/utils/utils.service.ts @@ -2,8 +2,10 @@ import { Injectable } from '@nestjs/common'; import axios from 'axios'; import { compareVersions, validate } from 'compare-versions'; import { NC_ATTACHMENT_FIELD_SIZE } from '../../constants'; +import SqlMgrv2 from '../../db/sql-mgr/v2/SqlMgrv2' import { NcError } from '../../helpers/catchError'; import { User } from '../../models'; +import Noco from '../../Noco' import NcConfigFactory from '../../utils/NcConfigFactory'; import { packageVersion } from '../../utils/packageVersion'; // import { packageVersion } from '../packageVersion'; @@ -381,4 +383,47 @@ export class UtilsService { return null; }); }; + + async testConnection(param: { body: any }) { + return await SqlMgrv2.testConnection(param.body); + } + + async appInfo(param: { req: { ncSiteUrl: string } }) { + const projectHasAdmin = !(await User.isFirst()); + const result = { + authType: 'jwt', + projectHasAdmin, + firstUser: !projectHasAdmin, + type: 'rest', + env: process.env.NODE_ENV, + googleAuthEnabled: !!( + process.env.NC_GOOGLE_CLIENT_ID && process.env.NC_GOOGLE_CLIENT_SECRET + ), + githubAuthEnabled: !!( + process.env.NC_GITHUB_CLIENT_ID && process.env.NC_GITHUB_CLIENT_SECRET + ), + oneClick: !!process.env.NC_ONE_CLICK, + connectToExternalDB: !process.env.NC_CONNECT_TO_EXTERNAL_DB_DISABLED, + version: packageVersion, + defaultLimit: Math.max( + Math.min( + +process.env.DB_QUERY_LIMIT_DEFAULT || 25, + +process.env.DB_QUERY_LIMIT_MAX || 100, + ), + +process.env.DB_QUERY_LIMIT_MIN || 1, + ), + timezone: defaultConnectionConfig.timezone, + ncMin: !!process.env.NC_MIN, + teleEnabled: process.env.NC_DISABLE_TELE !== 'true', + auditEnabled: process.env.NC_DISABLE_AUDIT !== 'true', + ncSiteUrl: (param.req as any).ncSiteUrl, + ee: Noco.isEE(), + ncAttachmentFieldSize: NC_ATTACHMENT_FIELD_SIZE, + ncMaxAttachmentsAllowed: +(process.env.NC_MAX_ATTACHMENTS_ALLOWED || 10), + isCloud: process.env.NC_CLOUD === 'true', + automationLogLevel: process.env.NC_AUTOMATION_LOG_LEVEL || 'OFF', + }; + + return result; + } }