ToolJet/server/src/controllers/apps.controller.ts

265 lines
7.3 KiB
TypeScript
Raw Normal View History

import {
Controller,
ForbiddenException,
Get,
Param,
Post,
Put,
Delete,
Query,
Request,
UnauthorizedException,
UseGuards,
} from '@nestjs/common';
import { JwtAuthGuard } from '../../src/modules/auth/jwt-auth.guard';
2021-07-11 05:41:45 +00:00
import { AppsService } from '../services/apps.service';
2021-07-10 13:54:32 +00:00
import { decamelizeKeys } from 'humps';
2021-07-22 14:24:18 +00:00
import { AppsAbilityFactory } from 'src/modules/casl/abilities/apps-ability.factory';
2021-07-24 04:13:45 +00:00
import { AppAuthGuard } from 'src/modules/auth/app-auth.guard';
2021-07-26 12:45:10 +00:00
import { FoldersService } from '@services/folders.service';
2021-07-10 13:54:32 +00:00
@Controller('apps')
export class AppsController {
constructor(
2021-07-22 14:24:18 +00:00
private appsService: AppsService,
2021-07-26 12:45:10 +00:00
private foldersService: FoldersService,
2021-07-24 18:09:25 +00:00
private appsAbilityFactory: AppsAbilityFactory
) {}
2021-07-10 13:54:32 +00:00
2021-07-10 14:07:47 +00:00
@UseGuards(JwtAuthGuard)
@Post()
async create(@Request() req) {
const app = await this.appsService.create(req.user);
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
if (!ability.can('createApp', app)) {
throw new ForbiddenException(
'you do not have permissions to perform this action',
);
}
await this.appsService.update(req.user, app.id, {
slug: app.id,
});
2021-07-10 14:07:47 +00:00
return decamelizeKeys(app);
}
2021-07-11 08:22:05 +00:00
@UseGuards(JwtAuthGuard)
@Get(':id')
async show(@Request() req, @Param() params) {
const app = await this.appsService.find(params.id);
let response = decamelizeKeys(app);
2021-08-10 09:04:27 +00:00
const seralizedQueries = [];
// serialize queries
for (const query of app.dataQueries) {
2021-08-10 09:04:27 +00:00
let decamelizedQuery = decamelizeKeys(query);
decamelizedQuery['options'] = query.options;
seralizedQueries.push(decamelizedQuery);
}
response['data_queries'] = seralizedQueries;
response['definition'] = app.editingVersion?.definition;
2021-07-11 08:22:05 +00:00
return response;
}
2021-07-24 04:13:45 +00:00
@UseGuards(AppAuthGuard) // This guard will allow access for unauthenticated user if the app is public
@Get('slugs/:slug')
async appFromSlug(@Request() req, @Param() params) {
if (req.user) {
2021-07-24 04:13:45 +00:00
const app = await this.appsService.findBySlug(params.slug);
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
if (!ability.can('viewApp', app)) {
throw new ForbiddenException(
'you do not have permissions to perform this action',
);
2021-07-24 04:13:45 +00:00
}
}
const app = await this.appsService.findBySlug(params.slug);
// serialize
return {
current_version_id: app['current_version_id'],
data_queries: app.dataQueries,
definition: app.editingVersion?.definition,
2021-07-24 04:13:45 +00:00
is_public: app.isPublic,
name: app.name,
slug: app.slug,
};
2021-07-24 04:13:45 +00:00
}
2021-07-22 14:24:18 +00:00
@UseGuards(JwtAuthGuard)
@Put(':id')
async update(@Request() req, @Param() params) {
const app = await this.appsService.find(params.id);
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
if (!ability.can('updateParams', app)) {
throw new ForbiddenException(
'you do not have permissions to perform this action',
);
2021-07-22 14:24:18 +00:00
}
2021-08-24 05:44:16 +00:00
const result = await this.appsService.update(
req.user,
params.id,
req.body.app,
);
2021-07-22 14:24:18 +00:00
let response = decamelizeKeys(result);
return response;
}
@UseGuards(JwtAuthGuard)
@Delete(':id')
async delete(@Request() req, @Param() params) {
const app = await this.appsService.find(params.id);
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
if (!ability.can('deleteApp', app)) {
throw new ForbiddenException(
'Only administrators are allowed to delete apps.',
);
}
const result = await this.appsService.delete(params.id);
let response = decamelizeKeys(result);
return response;
}
2021-07-10 13:54:32 +00:00
@UseGuards(JwtAuthGuard)
@Get()
async index(@Request() req, @Query() query) {
const page = req.query.page;
2021-07-26 12:45:10 +00:00
const folderId = req.query.folder;
let apps = [];
2021-08-24 05:44:16 +00:00
let folderCount = 0;
2021-07-26 12:45:10 +00:00
if (folderId) {
2021-07-26 12:45:10 +00:00
const folder = await this.foldersService.findOne(folderId);
apps = await this.foldersService.getAppsFor(req.user, folder, page);
2021-08-24 05:44:16 +00:00
folderCount = await this.foldersService.userAppCount(req.user, folder);
2021-07-26 12:45:10 +00:00
} else {
apps = await this.appsService.all(req.user, page);
}
2021-08-24 05:44:16 +00:00
let totalCount = await this.appsService.count(req.user);
2021-07-10 13:54:32 +00:00
2021-08-24 05:44:16 +00:00
const totalPageCount = folderId ? folderCount : totalCount;
2021-07-10 13:54:32 +00:00
const meta = {
total_pages: Math.ceil(totalPageCount / 10),
2021-07-10 13:54:32 +00:00
total_count: totalCount,
2021-08-24 05:44:16 +00:00
folder_count: folderCount,
current_page: parseInt(page || 1),
};
2021-07-10 13:54:32 +00:00
const response = {
meta,
apps,
};
2021-07-10 13:54:32 +00:00
return decamelizeKeys(response);
}
2021-07-23 05:32:49 +00:00
@UseGuards(JwtAuthGuard)
@Get(':id/users')
async fetchUsers(@Request() req, @Param() params) {
const app = await this.appsService.find(params.id);
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
if (!ability.can('fetchUsers', app)) {
throw new ForbiddenException(
'you do not have permissions to perform this action',
);
2021-07-23 05:32:49 +00:00
}
const result = await this.appsService.fetchUsers(req.user, params.id);
return decamelizeKeys({ users: result });
}
2021-07-23 14:51:24 +00:00
@UseGuards(JwtAuthGuard)
@Get(':id/versions')
async fetchVersions(@Request() req, @Param() params) {
const app = await this.appsService.find(params.id);
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
if (!ability.can('fetchVersions', app)) {
throw new ForbiddenException(
'you do not have permissions to perform this action',
);
2021-07-23 14:51:24 +00:00
}
const result = await this.appsService.fetchVersions(req.user, params.id);
return { versions: result };
2021-07-23 14:51:24 +00:00
}
2021-07-23 15:59:01 +00:00
@UseGuards(JwtAuthGuard)
@Post(':id/versions')
async createVersion(@Request() req, @Param() params) {
const versionName = req.body['versionName'];
2021-07-23 15:59:01 +00:00
const app = await this.appsService.find(params.id);
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
if (!ability.can('createVersions', app)) {
throw new ForbiddenException(
'you do not have permissions to perform this action',
);
2021-07-23 15:59:01 +00:00
}
const appUser = await this.appsService.createVersion(
req.user,
app,
versionName,
);
2021-07-23 15:59:01 +00:00
return decamelizeKeys(appUser);
}
@UseGuards(JwtAuthGuard)
@Get(':id/versions/:versionId')
async version(@Request() req, @Param() params) {
const app = await this.appsService.find(params.id);
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
if (!ability.can('fetchVersions', app)) {
throw new ForbiddenException(
'you do not have permissions to perform this action',
);
}
const appVersion = await this.appsService.findVersion(params.versionId);
return { ...appVersion, data_queries: app.dataQueries };
}
2021-07-23 16:57:59 +00:00
@UseGuards(JwtAuthGuard)
@Put(':id/versions/:versionId')
2021-08-10 11:56:16 +00:00
async updateVersion(@Request() req, @Param() params) {
const definition = req.body['definition'];
2021-07-23 16:57:59 +00:00
2021-08-10 11:56:16 +00:00
const version = await this.appsService.findVersion(params.versionId);
2021-07-23 16:57:59 +00:00
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
if (!ability.can('updateVersions', version.app)) {
throw new ForbiddenException(
'you do not have permissions to perform this action',
);
2021-07-23 16:57:59 +00:00
}
const appUser = await this.appsService.updateVersion(
req.user,
version,
definition,
);
2021-07-23 16:57:59 +00:00
return decamelizeKeys(appUser);
}
2021-07-10 13:54:32 +00:00
}