Skip to content

Commit

Permalink
Merge pull request #723 from bcgov/dev
Browse files Browse the repository at this point in the history
dev to main merge - prod release
  • Loading branch information
nikhila-aot authored Sep 6, 2024
2 parents 2829f56 + c9f0a4e commit a658745
Showing 1 changed file with 75 additions and 50 deletions.
125 changes: 75 additions & 50 deletions backend/users/src/app/controllers/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,84 @@
import { Controller, Post, Body, HttpException, HttpStatus } from '@nestjs/common';
import { Resource, RoleMatchingMode, Roles, Unprotected } from 'nest-keycloak-connect';
import {
Controller,
Post,
Body,
HttpException,
HttpStatus,
} from '@nestjs/common';
import {
Resource,
RoleMatchingMode,
Roles,
Unprotected,
} from 'nest-keycloak-connect';
import { KeycloakService } from 'src/app/services/keycloak.service';
import { AddUserToGroupDto } from 'src/app/dto/addUserToGroup';

@Controller('users')
@Resource('user-service')
export class UserController {
constructor(private readonly keyCloakService: KeycloakService) {}
constructor(private readonly keyCloakService: KeycloakService) {}

/**
* Add user to a group in Keycloak.
* @param addUserToGroupDto - Object containing userId.
* @returns Object indicating success status and message.
*/
@Post('/addGroup')
@Roles({ roles: ['user-admin'], mode: RoleMatchingMode.ANY })
async addUserToGroup(@Body() addUserToGroupDto: AddUserToGroupDto): Promise<any> {
try
{
const { userId } = addUserToGroupDto;

// Get access token from Keycloak
const accessToken = await this.keyCloakService.getToken();
if (!accessToken)
{
throw new HttpException('Failed to get access token', HttpStatus.INTERNAL_SERVER_ERROR);
}

// Find group ID by name
const groupName = 'formsflow-client'; // Assuming 'formflow-client' is the group name
const groupId = await this.keyCloakService.getGroupIdByName(groupName, accessToken);
if (!groupId)
{
throw new HttpException(`Group '${groupName}' not found`, HttpStatus.NOT_FOUND);
}

// Add user to group
const result = await this.keyCloakService.addUserToGroup(userId, groupId, accessToken);
if(result.success)
{
return result;
}
}
catch (error)
{
// Handle errors
if (error.response && error.response.data && error.response.data.error)
{
// If Keycloak returns an error message, throw a Bad Request exception with the error message
throw new HttpException(error.response.data.error, HttpStatus.BAD_REQUEST);
}
else {
// If any other error occurs, throw an Internal Server Error exception
throw new HttpException('Internal server error', HttpStatus.INTERNAL_SERVER_ERROR);
}
}
/**
* Add user to a group in Keycloak.
* @param addUserToGroupDto - Object containing userId.
* @returns Object indicating success status and message.
*/
@Post('/addGroup')
@Roles({ roles: ['user-admin'], mode: RoleMatchingMode.ANY })
async addUserToGroup(
@Body() addUserToGroupDto: AddUserToGroupDto,
): Promise<any> {
try {
const { userId } = addUserToGroupDto;

// Get access token from Keycloak
const accessToken = await this.keyCloakService.getToken();
if (!accessToken) {
throw new HttpException(
'Failed to get access token',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}

// Find group ID by name
const groupName = 'formsflow-client'; // Assuming 'formflow-client' is the group name
const groupId = await this.keyCloakService.getGroupIdByName(
groupName,
accessToken,
);
if (!groupId) {
throw new HttpException(
`Group '${groupName}' not found`,
HttpStatus.NOT_FOUND,
);
}

// Add user to group
const result = await this.keyCloakService.addUserToGroup(
userId,
groupId,
accessToken,
);
if (result.success) {
return result;
}
} catch (error) {
console.log('addUserToGroup error', error);
// Handle errors
if (error.response && error.response.data && error.response.data.error) {
// If Keycloak returns an error message, throw a Bad Request exception with the error message
throw new HttpException(
error.response.data.error,
HttpStatus.BAD_REQUEST,
);
} else {
// If any other error occurs, throw an Internal Server Error exception
throw new HttpException(
'Internal server error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}
}

0 comments on commit a658745

Please sign in to comment.