-
Notifications
You must be signed in to change notification settings - Fork 0
/
collection.controller.ts
162 lines (153 loc) · 4.54 KB
/
collection.controller.ts
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {
Body,
Controller,
Get,
Param,
Post,
Query,
Request,
UseGuards,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { Request as ExpressRequest } from 'express';
import { ApiBearerAuth, ApiOAuth2, ApiQuery } from '@nestjs/swagger';
import { OAUTH2_CLIENT_MAP_GUID } from '../constants';
import { CollectionService } from './collection.service';
import { BrokerOidcAuthGuard } from '../auth/broker-oidc-auth.guard';
import { BrokerCombinedAuthGuard } from '../auth/broker-combined-auth.guard';
import { AccountService } from './account.service';
import { Roles } from '../roles.decorator';
import { AllowOwner } from '../allow-owner.decorator';
import { BrokerAccountDto } from '../persistence/dto/broker-account.dto';
import { CollectionConfigDto } from '../persistence/dto/collection-config.dto';
import { UserImportDto } from './dto/user-import.dto';
import { UserRolesDto } from './dto/user-roles.dto';
import { AccountPermission } from '../account-permission.decorator';
import { CollectionSearchQuery } from './dto/collection-search-query.dto';
import { get } from 'radash';
import { UserCollectionService } from './user-collection.service';
@Controller({
path: 'collection',
version: '1',
})
export class CollectionController {
constructor(
private readonly accountService: AccountService,
private readonly service: CollectionService,
private readonly userCollectionService: UserCollectionService,
) {}
@Get('user/self')
@UseGuards(BrokerOidcAuthGuard)
@ApiOAuth2(['openid', 'profile'])
async user(@Request() req: ExpressRequest): Promise<UserRolesDto> {
return await this.userCollectionService.extractUserFromRequest(req);
}
@Post('user/import')
@UseGuards(BrokerCombinedAuthGuard)
@Roles('admin')
@AccountPermission('enableUserImport')
@ApiBearerAuth()
async userImport(
@Request() req: ExpressRequest,
@Body() userDto: UserImportDto,
): Promise<void> {
await this.userCollectionService.upsertUser(req, userDto);
}
@Get('config')
@UseGuards(BrokerCombinedAuthGuard)
@ApiBearerAuth()
getCollectionConfig(): Promise<CollectionConfigDto[]> {
return this.service.getCollectionConfig();
}
@Get('broker-account')
@UseGuards(BrokerCombinedAuthGuard)
async getAccountByVertexId(
@Query('vertex') vertexId: string,
): Promise<BrokerAccountDto> {
return this.service.getCollectionByVertexId('brokerAccount', vertexId);
}
@Get('broker-account/:id/token')
@UseGuards(BrokerOidcAuthGuard)
async getAccounts(@Param('id') id: string) {
return this.accountService.getRegisteryJwts(id);
}
@Post('broker-account/:id/token')
@Roles('admin')
@AllowOwner({
graphObjectType: 'collection',
graphObjectCollection: 'brokerAccount',
graphIdFromParamKey: 'id',
requiredEdgeNames: ['administrator', 'lead-developer'],
upstreamRecursive: true,
})
@UseGuards(BrokerOidcAuthGuard)
async generateAccountToken(
@Param('id') id: string,
@Request() req: ExpressRequest,
) {
return this.accountService.generateAccountToken(
req,
id,
get((req.user as any).userinfo, OAUTH2_CLIENT_MAP_GUID),
);
}
@Get(':collection')
@UseGuards(BrokerCombinedAuthGuard)
async getCollectionByVertexId(
@Param('collection') collection: string,
@Query('vertex') vertexId: string,
) {
switch (collection) {
case 'environment':
case 'project':
case 'service':
case 'team':
case 'user':
return this.service.getCollectionByVertexId(collection, vertexId);
case 'service-instance':
return this.service.getCollectionByVertexId(
'serviceInstance',
vertexId,
);
}
}
@Post(':collection/search')
@UseGuards(BrokerCombinedAuthGuard)
@UsePipes(new ValidationPipe({ transform: true }))
@ApiQuery({
name: 'upstreamVertex',
required: false,
})
@ApiQuery({
name: 'vertexId',
required: false,
})
async getCollections(
@Param('collection') collection: string,
@Query() query: CollectionSearchQuery,
) {
switch (collection) {
case 'environment':
case 'project':
case 'service':
case 'team':
case 'user':
return this.service.searchCollection(
collection,
query.upstreamVertex,
query.vertexId,
query.offset,
query.limit,
);
case 'service-instance':
return this.service.searchCollection(
'serviceInstance',
query.upstreamVertex,
query.vertexId,
query.offset,
query.limit,
);
}
}
}