@@ -5,18 +5,17 @@ import {
5
5
Patch ,
6
6
Param ,
7
7
UseGuards ,
8
- Req ,
9
8
Logger ,
10
9
Post ,
11
10
Delete ,
11
+ ForbiddenException ,
12
12
} from '@nestjs/common'
13
13
import {
14
14
ApiBearerAuth ,
15
15
ApiOperation ,
16
16
ApiResponse ,
17
17
ApiTags ,
18
18
} from '@nestjs/swagger'
19
- import { IRequest } from '../utils/interface'
20
19
import { JwtAuthGuard } from '../authentication/jwt.auth.guard'
21
20
import {
22
21
ApiResponseArray ,
@@ -49,6 +48,11 @@ import { ResourceService } from 'src/billing/resource.service'
49
48
import { RuntimeDomainService } from 'src/gateway/runtime-domain.service'
50
49
import { BindCustomDomainDto } from 'src/website/dto/update-website.dto'
51
50
import { RuntimeDomain } from 'src/gateway/entities/runtime-domain'
51
+ import { GroupRole , getRoleLevel } from 'src/group/entities/group-member'
52
+ import { GroupRoles } from 'src/group/group-role.decorator'
53
+ import { InjectApplication , InjectGroup , InjectUser } from 'src/utils/decorator'
54
+ import { User } from 'src/user/entities/user'
55
+ import { GroupWithRole } from 'src/group/entities/group'
52
56
53
57
@ApiTags ( 'Application' )
54
58
@Controller ( 'applications' )
@@ -73,14 +77,12 @@ export class ApplicationController {
73
77
@ApiOperation ( { summary : 'Create application' } )
74
78
@ApiResponseObject ( ApplicationWithRelations )
75
79
@Post ( )
76
- async create ( @Req ( ) req : IRequest , @Body ( ) dto : CreateApplicationDto ) {
80
+ async create ( @Body ( ) dto : CreateApplicationDto , @InjectUser ( ) user : User ) {
77
81
const error = dto . autoscaling . validate ( )
78
82
if ( error ) {
79
83
return ResponseUtil . error ( error )
80
84
}
81
85
82
- const user = req . user
83
-
84
86
// check regionId exists
85
87
const region = await this . region . findOneDesensitized (
86
88
new ObjectId ( dto . regionId ) ,
@@ -141,8 +143,7 @@ export class ApplicationController {
141
143
@Get ( )
142
144
@ApiOperation ( { summary : 'Get user application list' } )
143
145
@ApiResponseArray ( ApplicationWithRelations )
144
- async findAll ( @Req ( ) req : IRequest ) {
145
- const user = req . user
146
+ async findAll ( @InjectUser ( ) user : User ) {
146
147
const data = await this . application . findAllByUser ( user . _id )
147
148
return ResponseUtil . ok ( data )
148
149
}
@@ -207,6 +208,7 @@ export class ApplicationController {
207
208
*/
208
209
@ApiOperation ( { summary : 'Update application name' } )
209
210
@ApiResponseObject ( Application )
211
+ @GroupRoles ( GroupRole . Admin )
210
212
@UseGuards ( JwtAuthGuard , ApplicationAuthGuard )
211
213
@Patch ( ':appid/name' )
212
214
async updateName (
@@ -227,13 +229,16 @@ export class ApplicationController {
227
229
async updateState (
228
230
@Param ( 'appid' ) appid : string ,
229
231
@Body ( ) dto : UpdateApplicationStateDto ,
230
- @Req ( ) req : IRequest ,
232
+ @InjectApplication ( ) app : Application ,
233
+ @InjectGroup ( ) group : GroupWithRole ,
231
234
) {
232
- const app = req . application
233
- const user = req . user
235
+ if ( dto . state === ApplicationState . Deleted ) {
236
+ throw new ForbiddenException ( 'cannot update state to deleted' )
237
+ }
238
+ const userid = app . createdBy
234
239
235
240
// check account balance
236
- const account = await this . account . findOne ( user . _id )
241
+ const account = await this . account . findOne ( userid )
237
242
const balance = account ?. balance || 0
238
243
if ( balance < 0 ) {
239
244
return ResponseUtil . error ( `account balance is not enough` )
@@ -272,6 +277,15 @@ export class ApplicationController {
272
277
)
273
278
}
274
279
280
+ if (
281
+ [ ApplicationState . Stopped , ApplicationState . Running ] . includes (
282
+ dto . state ,
283
+ ) &&
284
+ getRoleLevel ( group . role ) < getRoleLevel ( GroupRole . Admin )
285
+ ) {
286
+ return ResponseUtil . error ( 'no permission' )
287
+ }
288
+
275
289
const doc = await this . application . updateState ( appid , dto . state )
276
290
return ResponseUtil . ok ( doc )
277
291
}
@@ -281,20 +295,20 @@ export class ApplicationController {
281
295
*/
282
296
@ApiOperation ( { summary : 'Update application bundle' } )
283
297
@ApiResponseObject ( ApplicationBundle )
298
+ @GroupRoles ( GroupRole . Admin )
284
299
@UseGuards ( JwtAuthGuard , ApplicationAuthGuard )
285
300
@Patch ( ':appid/bundle' )
286
301
async updateBundle (
287
302
@Param ( 'appid' ) appid : string ,
288
303
@Body ( ) dto : UpdateApplicationBundleDto ,
289
- @Req ( ) req : IRequest ,
304
+ @InjectApplication ( ) app : ApplicationWithRelations ,
290
305
) {
291
306
const error = dto . autoscaling . validate ( )
292
307
if ( error ) {
293
308
return ResponseUtil . error ( error )
294
309
}
295
310
296
- const app = await this . application . findOne ( appid )
297
- const user = req . user
311
+ const userid = app . createdBy
298
312
const regionId = app . regionId
299
313
300
314
// check if trial tier
@@ -304,7 +318,7 @@ export class ApplicationController {
304
318
} )
305
319
if ( isTrialTier ) {
306
320
const bundle = await this . resource . findTrialBundle ( regionId )
307
- const trials = await this . application . findTrialApplications ( user . _id )
321
+ const trials = await this . application . findTrialApplications ( userid )
308
322
const limitOfFreeTier = bundle ?. limitCountOfFreeTierPerUser || 0
309
323
if ( trials . length >= ( limitOfFreeTier || 0 ) ) {
310
324
return ResponseUtil . error (
@@ -334,6 +348,7 @@ export class ApplicationController {
334
348
*/
335
349
@ApiResponseObject ( RuntimeDomain )
336
350
@ApiOperation ( { summary : 'Bind custom domain to application' } )
351
+ @GroupRoles ( GroupRole . Admin )
337
352
@UseGuards ( JwtAuthGuard , ApplicationAuthGuard )
338
353
@Patch ( ':appid/domain' )
339
354
async bindDomain (
@@ -368,6 +383,7 @@ export class ApplicationController {
368
383
*/
369
384
@ApiResponse ( { type : ResponseUtil < boolean > } )
370
385
@ApiOperation ( { summary : 'Check if domain is resolved' } )
386
+ @GroupRoles ( GroupRole . Admin )
371
387
@UseGuards ( JwtAuthGuard , ApplicationAuthGuard )
372
388
@Post ( ':appid/domain/resolved' )
373
389
async checkResolved (
@@ -387,6 +403,7 @@ export class ApplicationController {
387
403
*/
388
404
@ApiResponseObject ( RuntimeDomain )
389
405
@ApiOperation ( { summary : 'Remove custom domain of application' } )
406
+ @GroupRoles ( GroupRole . Admin )
390
407
@UseGuards ( JwtAuthGuard , ApplicationAuthGuard )
391
408
@Delete ( ':appid/domain' )
392
409
async remove ( @Param ( 'appid' ) appid : string ) {
@@ -408,11 +425,13 @@ export class ApplicationController {
408
425
*/
409
426
@ApiOperation ( { summary : 'Delete an application' } )
410
427
@ApiResponseObject ( Application )
428
+ @GroupRoles ( GroupRole . Owner )
411
429
@UseGuards ( JwtAuthGuard , ApplicationAuthGuard )
412
430
@Delete ( ':appid' )
413
- async delete ( @Param ( 'appid' ) appid : string , @Req ( ) req : IRequest ) {
414
- const app = req . application
415
-
431
+ async delete (
432
+ @Param ( 'appid' ) appid : string ,
433
+ @InjectApplication ( ) app : ApplicationWithRelations ,
434
+ ) {
416
435
// check: only stopped application can be deleted
417
436
if (
418
437
app . state !== ApplicationState . Stopped &&
0 commit comments