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

dev to main merge - prod release #723

Merged
merged 2 commits into from
Sep 6, 2024
Merged
Changes from all 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
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,
);
}
}
}
}
Loading