forked from ToolJet/ToolJet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugins.controller.ts
More file actions
87 lines (74 loc) · 2.75 KB
/
plugins.controller.ts
File metadata and controls
87 lines (74 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import {
Controller,
Get,
Post,
Body,
Patch,
Param,
Delete,
UseInterceptors,
ClassSerializerInterceptor,
UseGuards,
ForbiddenException,
} from '@nestjs/common';
import { Plugin } from 'src/entities/plugin.entity';
import { PluginsService } from '../services/plugins.service';
import { CreatePluginDto } from '../dto/create-plugin.dto';
import { UpdatePluginDto } from '../dto/update-plugin.dto';
import { decode } from 'js-base64';
import { PluginsAbilityFactory } from 'src/modules/casl/abilities/plugins-ability.factory';
import { JwtAuthGuard } from 'src/modules/auth/jwt-auth.guard';
import { User } from 'src/decorators/user.decorator';
import { IsPluginApiEnabledGuard } from 'src/modules/casl/plugins.guard';
@Controller('plugins')
@UseInterceptors(ClassSerializerInterceptor)
@UseGuards(JwtAuthGuard)
export class PluginsController {
constructor(private readonly pluginsService: PluginsService, private pluginsAbilityFactory: PluginsAbilityFactory) {}
@Post('install')
@UseGuards(JwtAuthGuard)
async install(@User() user, @Body() createPluginDto: CreatePluginDto) {
const ability = await this.pluginsAbilityFactory.pluginActions(user);
if (!ability.can('installPlugin', Plugin)) {
throw new ForbiddenException('You do not have permissions to perform this action');
}
return this.pluginsService.install(createPluginDto);
}
@Get()
async findAll() {
const plugins = await this.pluginsService.findAll();
return plugins.map((plugin) => {
plugin.iconFile.data = plugin.iconFile.data.toString('utf8');
plugin.manifestFile.data = JSON.parse(decode(plugin.manifestFile.data.toString('utf8')));
return plugin;
});
}
@Get(':id')
@UseGuards(IsPluginApiEnabledGuard)
findOne(@Param('id') id: string) {
return this.pluginsService.findOne(id);
}
@Patch(':id')
@UseGuards(IsPluginApiEnabledGuard)
async update(@User() user, @Param('id') id: string, @Body() updatePluginDto: UpdatePluginDto) {
const ability = await this.pluginsAbilityFactory.pluginActions(user);
if (!ability.can('updatePlugin', Plugin)) {
throw new ForbiddenException('You do not have permissions to perform this action');
}
return this.pluginsService.update(id, updatePluginDto);
}
@Delete(':id')
@UseGuards(IsPluginApiEnabledGuard)
async remove(@User() user, @Param('id') id: string) {
const ability = await this.pluginsAbilityFactory.pluginActions(user);
if (!ability.can('deletePlugin', Plugin)) {
throw new ForbiddenException('You do not have permissions to perform this action');
}
return this.pluginsService.remove(id);
}
@Post(':id/reload')
@UseGuards(IsPluginApiEnabledGuard)
async reload(@Param('id') id: string) {
return this.pluginsService.reload(id);
}
}