@@ -11,74 +11,146 @@ import {
1111 Roles ,
1212 Unprotected ,
1313} from 'nest-keycloak-connect' ;
14+ import { AddUserToGroupDto } from '../dto/addUserToGroup' ;
15+ import { Key } from 'readline' ;
16+ import { KeycloakService } from '../services/keycloak.service' ;
17+ import { ConfigService } from '@nestjs/config' ;
1418// import { KeycloakService } from 'src/app/services/keycloak.service';
1519// import { AddUserToGroupDto } from 'src/app/dto/addUserToGroup';
1620
1721@Controller ( 'users' )
1822@Resource ( 'user-service' )
1923export class UserController {
20- constructor ( ) { }
24+ constructor (
25+ private readonly keyCloakService : KeycloakService ,
26+ private readonly configService : ConfigService ,
27+ ) { }
2128
2229 /**
2330 * Add user to a group in Keycloak.
2431 * @param addUserToGroupDto - Object containing userId.
2532 * @returns Object indicating success status and message.
2633 */
27- // @Post ('/addGroup')
28- // @Roles ({ roles: ['user-admin'], mode: RoleMatchingMode.ANY })
29- // async addUserToGroup(
30- // @Body () addUserToGroupDto: AddUserToGroupDto,
31- // ): Promise<any> {
32- // try {
33- // const { userId } = addUserToGroupDto;
34+ @Post ( '/addGroup' )
35+ @Roles ( { roles : [ 'user-admin' ] , mode : RoleMatchingMode . ANY } )
36+ async addUserToGroup (
37+ @Body ( ) addUserToGroupDto : AddUserToGroupDto ,
38+ ) : Promise < any > {
39+ try {
40+ const { userId } = addUserToGroupDto ;
3441
35- // // Get access token from Keycloak
36- // const accessToken = await this.keyCloakService.getToken();
37- // if (!accessToken) {
38- // throw new HttpException(
39- // 'Failed to get access token',
40- // HttpStatus.INTERNAL_SERVER_ERROR,
41- // );
42- // }
42+ // Get access token from Keycloak
43+ const accessToken = await this . keyCloakService . getToken ( ) ;
44+ if ( ! accessToken ) {
45+ throw new HttpException (
46+ 'Failed to get access token' ,
47+ HttpStatus . INTERNAL_SERVER_ERROR ,
48+ ) ;
49+ }
4350
44- // // Find group ID by name
45- // const groupName = 'formsflow-client'; // Assuming 'formflow-client' is the group name
46- // const groupId = await this.keyCloakService.getGroupIdByName(
47- // groupName,
48- // accessToken,
49- // );
50- // if (!groupId) {
51- // throw new HttpException(
52- // `Group '${groupName}' not found`,
53- // HttpStatus.NOT_FOUND,
54- // );
55- // }
51+ // Find group ID by name
52+ const groupName = 'formsflow-client' ; // Assuming 'formflow-client' is the group name
53+ const groupId = await this . keyCloakService . getGroupIdByName (
54+ groupName ,
55+ accessToken ,
56+ ) ;
57+ if ( ! groupId ) {
58+ throw new HttpException (
59+ `Group '${ groupName } ' not found` ,
60+ HttpStatus . NOT_FOUND ,
61+ ) ;
62+ }
5663
57- // // Add user to group
58- // const result = await this.keyCloakService.addUserToGroup(
59- // userId,
60- // groupId,
61- // accessToken,
62- // );
63- // if (result.success) {
64- // return result;
65- // }
66- // } catch (error) {
67- // console.log('addUserToGroup error', error);
68- // // Handle errors
69- // if (error.response && error.response.data && error.response.data.error) {
70- // // If Keycloak returns an error message, throw a Bad Request exception with the error message
71- // throw new HttpException(
72- // error.response.data.error,
73- // HttpStatus.BAD_REQUEST,
74- // );
75- // } else {
76- // // If any other error occurs, throw an Internal Server Error exception
77- // throw new HttpException(
78- // 'Internal server error',
79- // HttpStatus.INTERNAL_SERVER_ERROR,
80- // );
81- // }
82- // }
83- // }
64+ // Add user to group
65+ const result = await this . keyCloakService . addUserToGroup (
66+ userId ,
67+ groupId ,
68+ accessToken ,
69+ ) ;
70+ if ( result . success ) {
71+ return result ;
72+ }
73+ } catch ( error ) {
74+ console . log ( 'addUserToGroup error' , error ) ;
75+ // Handle errors
76+ if ( error . response && error . response . data && error . response . data . error ) {
77+ // If Keycloak returns an error message, throw a Bad Request exception with the error message
78+ throw new HttpException (
79+ error . response . data . error ,
80+ HttpStatus . BAD_REQUEST ,
81+ ) ;
82+ } else {
83+ // If any other error occurs, throw an Internal Server Error exception
84+ throw new HttpException (
85+ 'Internal server error' ,
86+ HttpStatus . INTERNAL_SERVER_ERROR ,
87+ ) ;
88+ }
89+ }
90+ }
91+
92+ /**
93+ * Add user to a specific approving authority group in Keycloak.
94+ * @param addUserToGroupDto - Object containing userId.
95+ * @returns Object indicating success status and message.
96+ */
97+ @Post ( '/addUserToGroupForMuncipalUsers' )
98+ @Roles ( { roles : [ 'user-admin' ] , mode : RoleMatchingMode . ANY } )
99+ async addUserToGroupForMuncipalUsers (
100+ @Body ( ) addUserToGroupDto : AddUserToGroupDto ,
101+ ) : Promise < any > {
102+ try {
103+ const { userId } = addUserToGroupDto ;
104+
105+ // Get access token from Keycloak
106+ const accessToken = await this . keyCloakService . getToken ( ) ;
107+ if ( ! accessToken ) {
108+ throw new HttpException (
109+ 'Failed to get access token' ,
110+ HttpStatus . INTERNAL_SERVER_ERROR ,
111+ ) ;
112+ }
113+
114+ // Find group ID by name
115+ const groupName = this . configService . get < string > (
116+ 'LRS_APPROVING_AUTHORITY_GROUP_NAME' ,
117+ ) ; // 'lrs-approving-authority'
118+ const groupId = await this . keyCloakService . getGroupIdByName (
119+ groupName ,
120+ accessToken ,
121+ ) ;
122+ if ( ! groupId ) {
123+ throw new HttpException (
124+ `Group '${ groupName } ' not found` ,
125+ HttpStatus . NOT_FOUND ,
126+ ) ;
127+ }
128+
129+ // Add user to group
130+ const result = await this . keyCloakService . addUserToGroup (
131+ userId ,
132+ groupId ,
133+ accessToken ,
134+ ) ;
135+ if ( result . success ) {
136+ return result ;
137+ }
138+ } catch ( error ) {
139+ console . log ( 'addUserToGroupForMuncipalUsers error' , error ) ;
140+ // Handle errors
141+ if ( error . response && error . response . data && error . response . data . error ) {
142+ // If Keycloak returns an error message, throw a Bad Request exception with the error message
143+ throw new HttpException (
144+ error . response . data . error ,
145+ HttpStatus . BAD_REQUEST ,
146+ ) ;
147+ } else {
148+ // If any other error occurs, throw an Internal Server Error exception
149+ throw new HttpException (
150+ 'Internal server error' ,
151+ HttpStatus . INTERNAL_SERVER_ERROR ,
152+ ) ;
153+ }
154+ }
155+ }
84156}
0 commit comments