2021-08-30 11:43:27 +00:00
|
|
|
import {
|
|
|
|
|
Controller,
|
|
|
|
|
ForbiddenException,
|
|
|
|
|
Get,
|
|
|
|
|
Param,
|
|
|
|
|
Post,
|
|
|
|
|
Put,
|
|
|
|
|
Delete,
|
|
|
|
|
Query,
|
|
|
|
|
Request,
|
|
|
|
|
UseGuards,
|
2022-01-12 12:34:39 +00:00
|
|
|
Body,
|
2021-08-30 11:43:27 +00:00
|
|
|
} from '@nestjs/common';
|
2021-07-14 14:14:35 +00:00
|
|
|
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-10-11 15:15:58 +00:00
|
|
|
import { App } from 'src/entities/app.entity';
|
2021-10-19 11:22:00 +00:00
|
|
|
import { AppImportExportService } from '@services/app_import_export.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-10-19 11:22:00 +00:00
|
|
|
private appImportExportService: AppImportExportService,
|
2021-07-26 12:45:10 +00:00
|
|
|
private foldersService: FoldersService,
|
2021-07-24 18:09:25 +00:00
|
|
|
private appsAbilityFactory: AppsAbilityFactory
|
2021-08-30 11:43:27 +00:00
|
|
|
) {}
|
2021-07-10 13:54:32 +00:00
|
|
|
|
2021-07-10 14:07:47 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Post()
|
|
|
|
|
async create(@Request() req) {
|
2021-09-15 15:42:04 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, {});
|
|
|
|
|
|
2021-10-11 15:15:58 +00:00
|
|
|
if (!ability.can('createApp', App)) {
|
|
|
|
|
throw new ForbiddenException('You do not have permissions to perform this action');
|
2021-09-15 15:42:04 +00:00
|
|
|
}
|
2021-10-11 15:15:58 +00:00
|
|
|
const app = await this.appsService.create(req.user);
|
2021-09-15 15:42:04 +00:00
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
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);
|
2021-10-11 15:15:58 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, params);
|
|
|
|
|
|
|
|
|
|
if (!ability.can('viewApp', app)) {
|
|
|
|
|
throw new ForbiddenException('You do not have permissions to perform this action');
|
|
|
|
|
}
|
2021-09-21 13:48:28 +00:00
|
|
|
const response = decamelizeKeys(app);
|
2021-07-11 08:22:05 +00:00
|
|
|
|
2021-08-10 09:04:27 +00:00
|
|
|
const seralizedQueries = [];
|
2022-03-03 16:07:00 +00:00
|
|
|
const dataQueriesForVersion = app.editingVersion
|
|
|
|
|
? await this.appsService.findDataQueriesForVersion(app.editingVersion.id)
|
|
|
|
|
: [];
|
2021-08-10 09:04:27 +00:00
|
|
|
|
|
|
|
|
// serialize queries
|
2022-03-03 16:07:00 +00:00
|
|
|
for (const query of dataQueriesForVersion) {
|
2021-09-21 13:48:28 +00:00
|
|
|
const decamelizedQuery = decamelizeKeys(query);
|
2021-08-10 09:04:27 +00:00
|
|
|
decamelizedQuery['options'] = query.options;
|
|
|
|
|
seralizedQueries.push(decamelizedQuery);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
response['data_queries'] = seralizedQueries;
|
2021-08-30 11:43:27 +00:00
|
|
|
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) {
|
2021-08-30 11:43:27 +00:00
|
|
|
if (req.user) {
|
2021-07-24 04:13:45 +00:00
|
|
|
const app = await this.appsService.findBySlug(params.slug);
|
2021-10-19 11:22:00 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, {
|
|
|
|
|
id: app.id,
|
|
|
|
|
});
|
2021-07-24 04:13:45 +00:00
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('viewApp', app)) {
|
2021-10-11 15:15:58 +00:00
|
|
|
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);
|
2022-03-03 16:07:00 +00:00
|
|
|
const versionToLoad = app.currentVersionId
|
|
|
|
|
? await this.appsService.findVersion(app.currentVersionId)
|
2022-03-03 16:35:19 +00:00
|
|
|
: await this.appsService.findVersion(app.editingVersion?.id);
|
2021-07-24 04:13:45 +00:00
|
|
|
|
|
|
|
|
// serialize
|
|
|
|
|
return {
|
2022-01-31 08:41:58 +00:00
|
|
|
current_version_id: app['currentVersionId'],
|
2022-03-03 16:35:19 +00:00
|
|
|
data_queries: versionToLoad?.dataQueries,
|
2022-03-03 16:07:00 +00:00
|
|
|
definition: versionToLoad?.definition,
|
2021-07-24 04:13:45 +00:00
|
|
|
is_public: app.isPublic,
|
|
|
|
|
name: app.name,
|
2021-08-30 11:43:27 +00:00
|
|
|
slug: app.slug,
|
|
|
|
|
};
|
2021-07-24 04:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-22 14:24:18 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Put(':id')
|
2022-01-12 12:34:39 +00:00
|
|
|
async update(@Request() req, @Param() params, @Body('app') appChanges) {
|
2021-07-22 14:24:18 +00:00
|
|
|
const app = await this.appsService.find(params.id);
|
2021-10-11 15:15:58 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, params);
|
2021-07-22 14:24:18 +00:00
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('updateParams', app)) {
|
2021-10-11 15:15:58 +00:00
|
|
|
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
|
|
|
|
2022-01-12 12:34:39 +00:00
|
|
|
const result = await this.appsService.update(req.user, params.id, appChanges);
|
2021-09-21 13:48:28 +00:00
|
|
|
const response = decamelizeKeys(result);
|
2021-07-22 14:24:18 +00:00
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-15 15:47:44 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Post(':id/clone')
|
|
|
|
|
async clone(@Request() req, @Param() params) {
|
|
|
|
|
const existingApp = await this.appsService.find(params.id);
|
2021-10-11 15:15:58 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, params);
|
2021-09-15 15:47:44 +00:00
|
|
|
|
|
|
|
|
if (!ability.can('cloneApp', existingApp)) {
|
2021-10-11 15:15:58 +00:00
|
|
|
throw new ForbiddenException('You do not have permissions to perform this action');
|
2021-09-15 15:47:44 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const result = await this.appsService.clone(existingApp, req.user);
|
|
|
|
|
const response = decamelizeKeys(result);
|
2021-09-15 15:47:44 +00:00
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-19 11:22:00 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Get(':id/export')
|
|
|
|
|
async export(@Request() req, @Param() params) {
|
|
|
|
|
const appToExport = await this.appsService.find(params.id);
|
|
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, params);
|
|
|
|
|
|
|
|
|
|
if (!ability.can('viewApp', appToExport)) {
|
|
|
|
|
throw new ForbiddenException('You do not have permissions to perform this action');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const app = await this.appImportExportService.export(req.user, params.id);
|
2021-10-26 07:25:40 +00:00
|
|
|
return {
|
|
|
|
|
...app,
|
|
|
|
|
tooljetVersion: globalThis.TOOLJET_VERSION,
|
|
|
|
|
};
|
2021-10-19 11:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Post('/import')
|
2022-01-12 12:34:39 +00:00
|
|
|
async import(@Request() req, @Body() body) {
|
2021-10-19 11:22:00 +00:00
|
|
|
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');
|
|
|
|
|
}
|
2022-01-12 12:34:39 +00:00
|
|
|
await this.appImportExportService.import(req.user, body);
|
2021-10-19 11:22:00 +00:00
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 14:06:37 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Delete(':id')
|
|
|
|
|
async delete(@Request() req, @Param() params) {
|
|
|
|
|
const app = await this.appsService.find(params.id);
|
2021-10-11 15:15:58 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, params);
|
2021-08-10 14:06:37 +00:00
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('deleteApp', app)) {
|
2021-09-21 13:48:28 +00:00
|
|
|
throw new ForbiddenException('Only administrators are allowed to delete apps.');
|
2021-08-10 14:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const result = await this.appsService.delete(params.id);
|
2021-09-21 13:48:28 +00:00
|
|
|
const response = decamelizeKeys(result);
|
2021-08-10 14:06:37 +00:00
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-10 13:54:32 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Get()
|
|
|
|
|
async index(@Request() req, @Query() query) {
|
2021-12-22 09:07:37 +00:00
|
|
|
const page = query.page;
|
|
|
|
|
const folderId = query.folder;
|
|
|
|
|
const searchKey = query.searchKey || '';
|
2021-07-26 12:45:10 +00:00
|
|
|
|
|
|
|
|
let apps = [];
|
2021-11-15 06:13:48 +00:00
|
|
|
let totalFolderCount = 0;
|
2021-07-26 12:45:10 +00:00
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (folderId) {
|
2021-07-26 12:45:10 +00:00
|
|
|
const folder = await this.foldersService.findOne(folderId);
|
2021-12-22 09:07:37 +00:00
|
|
|
apps = await this.foldersService.getAppsFor(req.user, folder, page, searchKey);
|
|
|
|
|
totalFolderCount = await this.foldersService.userAppCount(req.user, folder, searchKey);
|
2021-07-26 12:45:10 +00:00
|
|
|
} else {
|
2021-12-22 09:07:37 +00:00
|
|
|
apps = await this.appsService.all(req.user, page, searchKey);
|
2021-08-30 11:43:27 +00:00
|
|
|
}
|
2022-03-09 03:05:33 +00:00
|
|
|
//remove password from user info
|
|
|
|
|
apps.forEach((app) => (app.user.password = undefined));
|
2021-08-24 05:44:16 +00:00
|
|
|
|
2021-12-22 09:07:37 +00:00
|
|
|
const totalCount = await this.appsService.count(req.user, searchKey);
|
2021-07-10 13:54:32 +00:00
|
|
|
|
2021-11-15 06:13:48 +00:00
|
|
|
const totalPageCount = folderId ? totalFolderCount : totalCount;
|
2021-07-10 13:54:32 +00:00
|
|
|
|
|
|
|
|
const meta = {
|
2021-08-30 11:43:27 +00:00
|
|
|
total_pages: Math.ceil(totalPageCount / 10),
|
2021-07-10 13:54:32 +00:00
|
|
|
total_count: totalCount,
|
2021-11-15 06:13:48 +00:00
|
|
|
folder_count: totalFolderCount,
|
2021-08-30 11:43:27 +00:00
|
|
|
current_page: parseInt(page || 1),
|
|
|
|
|
};
|
2021-07-10 13:54:32 +00:00
|
|
|
|
|
|
|
|
const response = {
|
|
|
|
|
meta,
|
2021-08-30 11:43:27 +00:00
|
|
|
apps,
|
|
|
|
|
};
|
2021-07-10 13:54:32 +00:00
|
|
|
|
|
|
|
|
return decamelizeKeys(response);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-11 15:15:58 +00:00
|
|
|
// deprecated
|
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);
|
2021-10-11 15:15:58 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, params);
|
2021-07-23 05:32:49 +00:00
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('fetchUsers', app)) {
|
2021-10-11 15:15:58 +00:00
|
|
|
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);
|
2021-10-11 15:15:58 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, params);
|
2021-07-23 14:51:24 +00:00
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('fetchVersions', app)) {
|
2021-10-11 15:15:58 +00:00
|
|
|
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);
|
2021-08-30 11:43:27 +00:00
|
|
|
return { versions: result };
|
2021-07-23 14:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 15:59:01 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Post(':id/versions')
|
2022-01-24 05:51:54 +00:00
|
|
|
async createVersion(
|
|
|
|
|
@Request() req,
|
|
|
|
|
@Param() params,
|
|
|
|
|
@Body('versionName') versionName,
|
|
|
|
|
@Body('versionFromId') versionFromId
|
|
|
|
|
) {
|
2021-07-23 15:59:01 +00:00
|
|
|
const app = await this.appsService.find(params.id);
|
2021-10-11 15:15:58 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, params);
|
2021-07-23 15:59:01 +00:00
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('createVersions', app)) {
|
2021-10-11 15:15:58 +00:00
|
|
|
throw new ForbiddenException('You do not have permissions to perform this action');
|
2021-07-23 15:59:01 +00:00
|
|
|
}
|
|
|
|
|
|
2022-01-24 05:51:54 +00:00
|
|
|
const appUser = await this.appsService.createVersion(req.user, app, versionName, versionFromId);
|
2021-07-23 15:59:01 +00:00
|
|
|
return decamelizeKeys(appUser);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Get(':id/versions/:versionId')
|
|
|
|
|
async version(@Request() req, @Param() params) {
|
|
|
|
|
const app = await this.appsService.find(params.id);
|
2021-10-11 15:15:58 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, params);
|
2021-08-30 11:43:27 +00:00
|
|
|
|
|
|
|
|
if (!ability.can('fetchVersions', app)) {
|
2021-10-11 15:15:58 +00:00
|
|
|
throw new ForbiddenException('You do not have permissions to perform this action');
|
2021-08-30 11:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const appVersion = await this.appsService.findVersion(params.versionId);
|
|
|
|
|
|
2022-01-04 08:04:12 +00:00
|
|
|
return { ...appVersion, data_queries: appVersion.dataQueries };
|
2021-08-30 11:43:27 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 16:57:59 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Put(':id/versions/:versionId')
|
2022-01-12 12:34:39 +00:00
|
|
|
async updateVersion(@Request() req, @Param() params, @Body('definition') definition) {
|
2021-08-10 11:56:16 +00:00
|
|
|
const version = await this.appsService.findVersion(params.versionId);
|
2021-10-11 15:15:58 +00:00
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, params);
|
2021-07-23 16:57:59 +00:00
|
|
|
|
2021-08-30 11:43:27 +00:00
|
|
|
if (!ability.can('updateVersions', version.app)) {
|
2021-10-11 15:15:58 +00:00
|
|
|
throw new ForbiddenException('You do not have permissions to perform this action');
|
2021-07-23 16:57:59 +00:00
|
|
|
}
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
const appUser = await this.appsService.updateVersion(req.user, version, definition);
|
2021-07-23 16:57:59 +00:00
|
|
|
return decamelizeKeys(appUser);
|
|
|
|
|
}
|
2022-01-12 12:34:39 +00:00
|
|
|
|
2022-03-10 20:14:37 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Delete(':id/versions/:versionId')
|
|
|
|
|
async deleteVersion(@Request() req, @Param() params) {
|
|
|
|
|
const version = await this.appsService.findVersion(params.versionId);
|
|
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, params);
|
|
|
|
|
|
|
|
|
|
if (!version || !ability.can('deleteVersions', version.app)) {
|
|
|
|
|
throw new ForbiddenException('You do not have permissions to perform this action');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await this.appsService.deleteVersion(version.app, version);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-12 12:34:39 +00:00
|
|
|
@UseGuards(JwtAuthGuard)
|
|
|
|
|
@Put(':id/icons')
|
|
|
|
|
async updateIcon(@Request() req, @Param() params, @Body('icon') icon) {
|
|
|
|
|
const app = await this.appsService.find(params.id);
|
|
|
|
|
const ability = await this.appsAbilityFactory.appsActions(req.user, params);
|
|
|
|
|
|
|
|
|
|
if (!ability.can('updateIcon', app)) {
|
|
|
|
|
throw new ForbiddenException('You do not have permissions to perform this action');
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-03 16:07:00 +00:00
|
|
|
const appUser = await this.appsService.update(req.user, params.id, {
|
|
|
|
|
icon,
|
|
|
|
|
});
|
2022-01-12 12:34:39 +00:00
|
|
|
return decamelizeKeys(appUser);
|
|
|
|
|
}
|
2021-07-10 13:54:32 +00:00
|
|
|
}
|