diff --git a/apps/api/src/app/access/access.controller.ts b/apps/api/src/app/access/access.controller.ts index a24ac5d1a..372c7f34a 100644 --- a/apps/api/src/app/access/access.controller.ts +++ b/apps/api/src/app/access/access.controller.ts @@ -34,6 +34,21 @@ export class AccessController { @Inject(REQUEST) private readonly request: RequestWithUser ) {} + @Get(':id') + @UseGuards(AuthGuard('jwt'), HasPermissionGuard) + public async getAccess(@Param('id') id: string): Promise { + const access = await this.accessService.access({ id }); + + if (!access || access.userId !== this.request.user.id) { + throw new HttpException( + getReasonPhrase(StatusCodes.FORBIDDEN), + StatusCodes.FORBIDDEN + ); + } + + return access; + } + @Get() @UseGuards(AuthGuard('jwt'), HasPermissionGuard) public async getAllAccesses(): Promise { @@ -68,21 +83,6 @@ export class AccessController { ); } - @Get(':id') - @UseGuards(AuthGuard('jwt'), HasPermissionGuard) - public async getAccess(@Param('id') id: string): Promise { - const access = await this.accessService.access({ id }); - - if (!access || access.userId !== this.request.user.id) { - throw new HttpException( - getReasonPhrase(StatusCodes.FORBIDDEN), - StatusCodes.FORBIDDEN - ); - } - - return access; - } - @HasPermission(permissions.createAccess) @Post() @UseGuards(AuthGuard('jwt'), HasPermissionGuard) @@ -116,6 +116,24 @@ export class AccessController { } } + @Delete(':id') + @HasPermission(permissions.deleteAccess) + @UseGuards(AuthGuard('jwt'), HasPermissionGuard) + public async deleteAccess(@Param('id') id: string): Promise { + const access = await this.accessService.access({ id }); + + if (!access || access.userId !== this.request.user.id) { + throw new HttpException( + getReasonPhrase(StatusCodes.FORBIDDEN), + StatusCodes.FORBIDDEN + ); + } + + return this.accessService.deleteAccess({ + id + }); + } + @Put(':id') @UseGuards(AuthGuard('jwt'), HasPermissionGuard) public async updateAccess( @@ -159,22 +177,4 @@ export class AccessController { ); } } - - @Delete(':id') - @HasPermission(permissions.deleteAccess) - @UseGuards(AuthGuard('jwt'), HasPermissionGuard) - public async deleteAccess(@Param('id') id: string): Promise { - const access = await this.accessService.access({ id }); - - if (!access || access.userId !== this.request.user.id) { - throw new HttpException( - getReasonPhrase(StatusCodes.FORBIDDEN), - StatusCodes.FORBIDDEN - ); - } - - return this.accessService.deleteAccess({ - id - }); - } } diff --git a/apps/client/src/app/components/user-account-access/create-or-update-access-dialog/create-or-update-access-dialog.component.ts b/apps/client/src/app/components/user-account-access/create-or-update-access-dialog/create-or-update-access-dialog.component.ts index 3fd3aa54f..84d62c3f9 100644 --- a/apps/client/src/app/components/user-account-access/create-or-update-access-dialog/create-or-update-access-dialog.component.ts +++ b/apps/client/src/app/components/user-account-access/create-or-update-access-dialog/create-or-update-access-dialog.component.ts @@ -66,18 +66,57 @@ export class GfCreateOrUpdateAccessDialog implements OnInit, OnDestroy { this.isEditMode = !!data.accessId; } - public ngOnInit() { - console.log('Dialog init - Edit mode:', this.isEditMode); - console.log('Dialog data:', this.data); + private async createAccess() { + console.log('Creating access...'); + const access: CreateAccessDto = { + alias: this.accessForm.get('alias').value, + granteeUserId: this.accessForm.get('granteeUserId').value, + permissions: [this.accessForm.get('permissions').value] + }; + + try { + await validateObjectForForm({ + classDto: CreateAccessDto, + form: this.accessForm, + object: access + }); + + this.dataService + .postAccess(access) + .pipe( + catchError((error) => { + if (error.status === StatusCodes.BAD_REQUEST) { + this.notificationService.alert({ + title: $localize`Oops! Could not grant access.` + }); + } + + return EMPTY; + }), + takeUntil(this.unsubscribeSubject) + ) + .subscribe(() => { + this.dialogRef.close(access); + }); + } catch (error) { + console.error(error); + } + } + public ngOnDestroy() { + this.unsubscribeSubject.next(); + this.unsubscribeSubject.complete(); + } + + public ngOnInit() { this.accessForm = this.formBuilder.group({ alias: [this.data.access.alias], + granteeUserId: [this.data.access.grantee, Validators.required], permissions: [this.data.access.permissions[0], Validators.required], type: [ { value: this.data.access.type, disabled: this.isEditMode }, Validators.required - ], - granteeUserId: [this.data.access.grantee, Validators.required] + ] }); this.accessForm.get('type').valueChanges.subscribe((accessType) => { @@ -123,45 +162,6 @@ export class GfCreateOrUpdateAccessDialog implements OnInit, OnDestroy { } } - private async createAccess() { - console.log('Creating access...'); - const access: CreateAccessDto = { - alias: this.accessForm.get('alias').value, - granteeUserId: this.accessForm.get('granteeUserId').value, - permissions: [this.accessForm.get('permissions').value] - }; - - console.log('Access data:', access); - - try { - await validateObjectForForm({ - classDto: CreateAccessDto, - form: this.accessForm, - object: access - }); - - this.dataService - .postAccess(access) - .pipe( - catchError((error) => { - if (error.status === StatusCodes.BAD_REQUEST) { - this.notificationService.alert({ - title: $localize`Oops! Could not grant access.` - }); - } - - return EMPTY; - }), - takeUntil(this.unsubscribeSubject) - ) - .subscribe(() => { - this.dialogRef.close(access); - }); - } catch (error) { - console.error(error); - } - } - private async updateAccess() { console.log('Updating access...'); const access: UpdateAccessDto = { @@ -201,9 +201,4 @@ export class GfCreateOrUpdateAccessDialog implements OnInit, OnDestroy { console.error(error); } } - - public ngOnDestroy() { - this.unsubscribeSubject.next(); - this.unsubscribeSubject.complete(); - } } diff --git a/apps/client/src/app/services/data.service.ts b/apps/client/src/app/services/data.service.ts index eb6f55cbb..4675b2388 100644 --- a/apps/client/src/app/services/data.service.ts +++ b/apps/client/src/app/services/data.service.ts @@ -749,13 +749,6 @@ export class DataService { return this.http.post('/api/v1/access', aAccess); } - public putAccess(id: string, aAccess: UpdateAccessDto) { - return this.http.put( - `/api/v1/access/${id}`, - aAccess - ); - } - public postAccount(aAccount: CreateAccountDto) { return this.http.post('/api/v1/account', aAccount); } @@ -805,6 +798,13 @@ export class DataService { return this.http.post('/api/v1/watchlist', watchlistItem); } + public putAccess(id: string, aAccess: UpdateAccessDto) { + return this.http.put( + `/api/v1/access/${id}`, + aAccess + ); + } + public putAccount(aAccount: UpdateAccountDto) { return this.http.put(`/api/v1/account/${aAccount.id}`, aAccount); }