Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add deleteAccount endpoint #38

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/account/account.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ describe('AccountController', () => {
});
});

it('should delete a user account', (done) => {
mockHttp.delete.mockReturnValue(of(""));
const id = 'my-id';

controller
.deleteAccount({ user }, id )
.subscribe(() => {
// delete user
expect(mockHttp.delete).toHaveBeenCalledWith(
expect.stringContaining("/ndb-dev/users/my-id"),
);
done();
});
});

it('should return a user with the assigned roles', async () => {
const requestedUser: KeycloakUser = { username: 'my-user', id: 'user-id' };
const roles = ['user-role-1', 'user-role-2'];
Expand Down
23 changes: 23 additions & 0 deletions src/account/account.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
Body,
Controller,
Delete,
Get,
Headers,
Param,
Expand Down Expand Up @@ -149,6 +150,28 @@ export class AccountController {
);
}

@ApiOperation({
summary: 'delete an user account',
description:
'Looks if an account with given id exist in realm and deletes it',
})
@ApiBearerAuth()
@ApiHeader({ name: 'Accept-Language', required: false })
@UseGuards(BearerGuard, RolesGuard)
@Roles(AccountController.ACCOUNT_MANAGEMENT_ROLE)
@Delete('/:userId')
deleteAccount(
@Req() req,
@Param('userId') userId: string,
) {
const user = req.user as User;

return this.keycloak.deleteUser(
user.realm,
userId,
)
}

@ApiOperation({
summary: 'get account details',
description:
Expand Down
19 changes: 18 additions & 1 deletion src/account/keycloak.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, NotFoundException } from '@nestjs/common';
import { HttpService } from '@nestjs/axios';
import { ConfigService } from '@nestjs/config';
import { KeycloakUser } from './keycloak-user.dto';
import { map, Observable } from 'rxjs';
import { catchError, map, Observable, of } from 'rxjs';

/**
* This service provides endpoints for interacting with Keycloak.
Expand All @@ -17,6 +17,7 @@ export class KeycloakService {
];

private readonly keycloakUrl: string;

constructor(private http: HttpService, configService: ConfigService) {
this.keycloakUrl = configService.get('KEYCLOAK_URL');
}
Expand All @@ -35,6 +36,22 @@ export class KeycloakService {
);
}

/**
* Delete a user in the realm with the given id
* @param realm
* @param id
*/
deleteUser(realm: string, id: string) {
return this.perform(
this.http.delete,
`${realm}/users/${id}`,
).pipe(
catchError(() => {
return of("")
tomwwinter marked this conversation as resolved.
Show resolved Hide resolved
}),
);
}

/**
* Update the user with the given id in the realm.
* @param realm
Expand Down
Loading