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

214 lines
6.1 KiB
TypeScript
Raw Normal View History

import {
Controller,
Get,
Param,
Post,
Patch,
Delete,
Query,
Request,
UseGuards,
ForbiddenException,
} from '@nestjs/common';
import { JwtAuthGuard } from '../../src/modules/auth/jwt-auth.guard';
import { decamelizeKeys } from 'humps';
import { DataQueriesService } from '../../src/services/data_queries.service';
import { DataSourcesService } from '../../src/services/data_sources.service';
2021-07-17 04:41:02 +00:00
import { QueryError } from 'src/modules/data_sources/query.error';
2021-07-24 06:13:21 +00:00
import { QueryAuthGuard } from 'src/modules/auth/query-auth.guard';
import { AppsAbilityFactory } from 'src/modules/casl/abilities/apps-ability.factory';
2021-07-24 18:09:25 +00:00
import { AppsService } from '@services/apps.service';
@Controller('data_queries')
export class DataQueriesController {
constructor(
2021-07-24 18:09:25 +00:00
private appsService: AppsService,
private dataQueriesService: DataQueriesService,
2021-07-24 06:13:21 +00:00
private dataSourcesService: DataSourcesService,
private appsAbilityFactory: AppsAbilityFactory
) { }
@UseGuards(JwtAuthGuard)
@Get()
async index(@Request() req, @Query() query) {
2021-07-24 18:09:25 +00:00
const app = await this.appsService.find(query.app_id);
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
const ability = await this.appsAbilityFactory.appsActions(req.user, {
id: query.app_id,
});
2021-07-24 18:09:25 +00:00
if (!ability.can('getQueries', app)) {
2021-07-24 18:09:25 +00:00
throw new ForbiddenException('you do not have permissions to perform this action');
}
const queries = await this.dataQueriesService.all(req.user, query.app_id);
const seralizedQueries = [];
// serialize
for (const query of queries) {
const decamelizedQuery = decamelizeKeys(query);
decamelizedQuery['options'] = query.options;
seralizedQueries.push(decamelizedQuery);
}
const response = { data_queries: seralizedQueries };
return response;
}
2021-07-12 14:12:34 +00:00
@UseGuards(JwtAuthGuard)
@Post()
async create(@Request() req) {
const { kind, name, options } = req.body;
const appId = req.body.app_id;
2021-07-24 18:09:25 +00:00
const app = await this.appsService.find(appId);
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
const ability = await this.appsAbilityFactory.appsActions(req.user, {
id: appId,
});
2021-07-24 18:09:25 +00:00
if (!ability.can('createQuery', app)) {
2021-07-24 18:09:25 +00:00
throw new ForbiddenException('you do not have permissions to perform this action');
}
2021-07-12 14:12:34 +00:00
const dataSourceId = req.body.data_source_id;
2021-07-24 18:09:25 +00:00
// Make sure that the data source belongs ot the app
if (dataSourceId) {
2021-07-24 18:09:25 +00:00
const dataSource = await this.dataSourcesService.findOne(dataSourceId);
if (dataSource.appId !== appId) {
2021-07-24 18:09:25 +00:00
throw new ForbiddenException('you do not have permissions to perform this action');
}
}
2021-07-12 14:12:34 +00:00
const dataQuery = await this.dataQueriesService.create(req.user, name, kind, options, appId, dataSourceId);
return decamelizeKeys(dataQuery);
}
@UseGuards(JwtAuthGuard)
2021-07-24 18:09:25 +00:00
@Patch(':id')
2021-07-12 14:12:34 +00:00
async update(@Request() req, @Param() params) {
const { name, options } = req.body;
const dataQueryId = params.id;
2021-07-24 18:09:25 +00:00
const dataQuery = await this.dataQueriesService.findOne(dataQueryId);
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
const ability = await this.appsAbilityFactory.appsActions(req.user, {
id: dataQuery.appId,
});
2021-07-24 18:09:25 +00:00
if (!ability.can('updateQuery', dataQuery.app)) {
2021-07-24 18:09:25 +00:00
throw new ForbiddenException('you do not have permissions to perform this action');
}
2021-07-24 18:09:25 +00:00
const result = await this.dataQueriesService.update(req.user, dataQueryId, name, options);
return decamelizeKeys(result);
2021-07-12 14:12:34 +00:00
}
@UseGuards(JwtAuthGuard)
@Delete(':id')
async delete(@Request() req, @Param() params) {
const dataQueryId = params.id;
const dataQuery = await this.dataQueriesService.findOne(dataQueryId);
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
const ability = await this.appsAbilityFactory.appsActions(req.user, {
id: dataQuery.appId,
});
if (!ability.can('deleteQuery', dataQuery.app)) {
throw new ForbiddenException('you do not have permissions to perform this action');
}
const result = await this.dataQueriesService.delete(params.id);
return decamelizeKeys(result);
}
2021-07-24 06:13:21 +00:00
@UseGuards(QueryAuthGuard)
@Post(':id/run')
async runQuery(@Request() req, @Param() params) {
const dataQueryId = params.id;
const { options } = req.body;
const dataQuery = await this.dataQueriesService.findOne(dataQueryId);
if (req.user) {
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
const ability = await this.appsAbilityFactory.appsActions(req.user, {
id: dataQuery.appId,
});
2021-07-24 06:13:21 +00:00
if (!ability.can('runQuery', dataQuery.app)) {
2021-07-24 06:13:21 +00:00
throw new ForbiddenException('you do not have permissions to perform this action');
}
}
2021-07-17 07:38:02 +00:00
let result = {};
try {
result = await this.dataQueriesService.runQuery(req.user, dataQuery, options);
} catch (error) {
if (error instanceof QueryError) {
result = {
status: 'failed',
message: error.message,
description: error.description,
data: error.data,
};
2021-07-17 07:38:02 +00:00
} else {
2021-07-17 14:22:37 +00:00
console.log(error);
2021-07-17 07:38:02 +00:00
result = {
status: 'failed',
message: 'Internal server error',
description: error.message,
data: {},
};
2021-07-17 07:38:02 +00:00
}
}
return result;
}
@UseGuards(JwtAuthGuard)
@Post('/preview')
async previewQuery(@Request() req, @Param() params) {
const { options, query } = req.body;
const dataQueryEntity = {
...query,
dataSource: await this.dataSourcesService.findOne(query['data_source_id']),
};
if (dataQueryEntity.dataSource) {
Feature: User access management 🔥 (#918) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * Feature: User access permission group usage (#883) * create migrations for group permissions setup * define new entities and relationships * revise migrations * rename columns * add migration to populate permission groups for existing users * revise migrations * hide roles usage * setup group permissions for apps and users * fix defaultChecked * fix update permission checkbox * fix casl ability check to have params passed * fix casl apps abilities to check with app specific permission * add ability to delete groups * conditionally render edit and delete options for all and admin users * fix user role to group migration * revise group management pages to disallow updating default group * move manage users and groups to navbar dropdown * show only addable apps and users on dropdowns * rename header as profile settings * scope addable apps and users by organization * scope viewable apps on homepage * hide manage groups link from non admins * make permissions to be used with radio input * add loading state for add apps/users buttons * revise unit tests * revise migrations * fix e2e tests * comment out dead code * fix seeds script * handle folder count * captalize error toast * hide manage users dropdown for non admins * show fobidden error on blank homepage * fix folder app count * fix invalid state set * make group name clickable for edit instead * users with edit permission can deploy apps * not show edit link on homepage if user dont have update permission * remove unused entity from merge * remove roles usage from manage org users page * fix folder count and blank slate on homepage * disable add buttons if there is no selections * humanize default groups on view * make app added onto groups have read permission by default * not show app menu if user is not admin * remove admin users from group user addition dropdown * create default permissions for app cloned * fix querying index page without page params * fix admin scoped out from group add * remove apps from header * fix invitation url not shown * scope admin deletion check by org * scope public apps by organization * add specs for group permissions e2e * removed unused entity and add group permissions spec * remove console logs * remove unused permission * scope public app count by org * remove console log * refactor manage group permission resources component * update group permssion in org scope
2021-10-11 15:15:58 +00:00
const ability = await this.appsAbilityFactory.appsActions(req.user, {
id: dataQueryEntity.dataSource.appId,
});
2021-07-24 18:09:25 +00:00
if (!ability.can('previewQuery', dataQueryEntity.dataSource.app)) {
2021-07-24 18:09:25 +00:00
throw new ForbiddenException('you do not have permissions to perform this action');
}
}
2021-07-17 04:41:02 +00:00
let result = {};
try {
result = await this.dataQueriesService.runQuery(req.user, dataQueryEntity, options);
} catch (error) {
if (error instanceof QueryError) {
result = {
status: 'failed',
message: error.message,
description: error.description,
data: error.data,
};
2021-07-17 04:41:02 +00:00
} else {
2021-07-17 14:22:37 +00:00
console.log(error);
2021-07-17 04:41:02 +00:00
result = {
status: 'failed',
message: 'Internal server error',
description: error.message,
data: {},
};
2021-07-17 04:41:02 +00:00
}
}
2021-07-16 12:37:30 +00:00
return result;
}
}