This repository has been archived by the owner on Apr 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 205
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89af802
commit bf9976d
Showing
3 changed files
with
114 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Param, | ||
ParseIntPipe, | ||
Patch, | ||
Post, | ||
Put, | ||
Query, | ||
} from '@nestjs/common'; | ||
import { apiKeys } from '@prisma/client'; | ||
import { CursorPipe } from '../../pipes/cursor.pipe'; | ||
import { OptionalIntPipe } from '../../pipes/optional-int.pipe'; | ||
import { OrderByPipe } from '../../pipes/order-by.pipe'; | ||
import { WherePipe } from '../../pipes/where.pipe'; | ||
import { AuditLog } from '../audit-logs/audit-log.decorator'; | ||
import { Scopes } from '../auth/scope.decorator'; | ||
import { Expose } from '../prisma/prisma.interface'; | ||
import { | ||
CreateApiKeyDto, | ||
ReplaceApiKeyDto, | ||
UpdateApiKeyDto, | ||
} from './api-keys.dto'; | ||
import { ApiKeysService } from './api-keys.service'; | ||
|
||
@Controller('users/:userId/api-keys') | ||
export class ApiKeyUserController { | ||
constructor(private apiKeysService: ApiKeysService) {} | ||
|
||
@Post() | ||
@AuditLog('create-api-key') | ||
@Scopes('user-{userId}:write-api-key-*') | ||
async create( | ||
@Param('userId', ParseIntPipe) userId: number, | ||
@Body() data: CreateApiKeyDto, | ||
): Promise<Expose<apiKeys>> { | ||
return this.apiKeysService.createApiKeyForUser(userId, data); | ||
} | ||
|
||
@Get() | ||
@Scopes('user-{userId}:read-api-key-*') | ||
async getAll( | ||
@Param('userId', ParseIntPipe) userId: number, | ||
@Query('skip', OptionalIntPipe) skip?: number, | ||
@Query('take', OptionalIntPipe) take?: number, | ||
@Query('cursor', CursorPipe) cursor?: Record<string, number | string>, | ||
@Query('where', WherePipe) where?: Record<string, number | string>, | ||
@Query('orderBy', OrderByPipe) orderBy?: Record<string, 'asc' | 'desc'>, | ||
): Promise<Expose<apiKeys>[]> { | ||
return this.apiKeysService.getApiKeysForUser(userId, { | ||
skip, | ||
take, | ||
orderBy, | ||
cursor, | ||
where, | ||
}); | ||
} | ||
|
||
@Get('scopes') | ||
@Scopes('user-{userId}:write-api-key-*') | ||
async scopes( | ||
@Param('userId', ParseIntPipe) userId: number, | ||
): Promise<Record<string, string>> { | ||
return this.apiKeysService.getApiKeyScopesForUser(userId); | ||
} | ||
|
||
@Get(':id') | ||
@Scopes('user-{userId}:read-api-key-{id}') | ||
async get( | ||
@Param('userId', ParseIntPipe) userId: number, | ||
@Param('id', ParseIntPipe) id: number, | ||
): Promise<Expose<apiKeys>> { | ||
return this.apiKeysService.getApiKeyForUser(userId, Number(id)); | ||
} | ||
|
||
@Patch(':id') | ||
@AuditLog('update-api-key') | ||
@Scopes('user-{userId}:write-api-key-{id}') | ||
async update( | ||
@Body() data: UpdateApiKeyDto, | ||
@Param('userId', ParseIntPipe) userId: number, | ||
@Param('id', ParseIntPipe) id: number, | ||
): Promise<Expose<apiKeys>> { | ||
return this.apiKeysService.updateApiKeyForUser(userId, Number(id), data); | ||
} | ||
|
||
@Put(':id') | ||
@AuditLog('update-api-key') | ||
@Scopes('user-{userId}:write-api-key-{id}') | ||
async replace( | ||
@Body() data: ReplaceApiKeyDto, | ||
@Param('userId', ParseIntPipe) userId: number, | ||
@Param('id', ParseIntPipe) id: number, | ||
): Promise<Expose<apiKeys>> { | ||
return this.apiKeysService.updateApiKeyForUser(userId, Number(id), data); | ||
} | ||
|
||
@Delete(':id') | ||
@AuditLog('delete-api-key') | ||
@Scopes('user-{userId}:delete-api-key-{id}') | ||
async remove( | ||
@Param('userId', ParseIntPipe) userId: number, | ||
@Param('id', ParseIntPipe) id: number, | ||
): Promise<Expose<apiKeys>> { | ||
return this.apiKeysService.deleteApiKeyForUser(userId, Number(id)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters