generated from bcgov/quickstart-openshift
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #723 from bcgov/dev
dev to main merge - prod release
- Loading branch information
Showing
1 changed file
with
75 additions
and
50 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
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, | ||
); | ||
} | ||
} | ||
} | ||
} |