Add User to API-Mgmt-Devportal-Users group by creation and Remove User from API-Mgmt-Devportal-Users group by deletion
MODIFY
src/app/components/admin/services/admin.service.ts
src/app/components/admin/services/admin.service.ts
@@ -58,7 +58,7 @@ export class AdminService {
return forkJoin(this.keycloak.findExistingOrCreateUser(keycloakUserToInsert)
.pipe(mergeMap((insertedKeycloakUser) =>
// 3. add devPortal role and add client role to keycloak user
- forkJoin(this.keycloak.addDevPortalRoleToUser(insertedKeycloakUser),
+ forkJoin(this.keycloak.addDevPortalGroupToUser(insertedKeycloakUser),
this.keycloak.addClientRoleToUser(insertedKeycloakUser, developerInserted.id))
)))
.pipe(map(() => developerInserted));

MODIFY
src/app/components/admin/services/keycloak-interaction.service.ts
src/app/components/admin/services/keycloak-interaction.service.ts
@@ -44,6 +44,14 @@ export class KeycloakInteractionService {
}));
}
+ /**
+ * Get API-Mgmt-Devportal-Users Group
+ */
+ private getDevPortalUserGroup() {
+ return from(this.kcAdminClient.groups.find({search: 'API-Mgmt-Devportal-Users'}))
+ .pipe(map(groups => groups.length > 0 ? groups[0] : undefined));
+ }
+
/**
* Searchs a user from keycloak
* @param username the keycloak username
@@ -158,7 +166,13 @@ export class KeycloakInteractionService {
})));
}
- removeRoleMapping(userUUID, roleUUID, roleName) {
+ /**
+ * Remove Role Mapping from user
+ * @param userUUID user UUID
+ * @param roleUUID role UUID
+ * @param roleName role name
+ */
+ private removeRoleMapping(userUUID, roleUUID, roleName) {
return from(this.kcAdminClient.users.delRealmRoleMappings({
id: userUUID,
roles: [{
@@ -168,6 +182,18 @@ export class KeycloakInteractionService {
}));
}
+ /**
+ * Remove Group Mapping from user
+ * @param userUUID user UUID
+ * @param groupUUID group UUID
+ */
+ private removeGroupMapping(userUUID, groupUUID) {
+ return from(this.kcAdminClient.users.delFromGroup({
+ id: userUUID,
+ groupId: groupUUID
+ }));
+ }
+
/**
* Remove devportal role from user
* @param user the user
@@ -177,6 +203,29 @@ export class KeycloakInteractionService {
.pipe(mergeMap(devPortalUserRole => this.removeRoleMapping(user.id, devPortalUserRole.id, devPortalUserRole.name)));
}
+ /**
+ * Add User to API-Mgmt-Devportal-Users group
+ * @param user the user
+ */
+ public addDevPortalGroupToUser(user) {
+ return this.getDevPortalUserGroup().pipe(mergeMap(devPortalUserGroup => this.kcAdminClient.users.addToGroup({
+ id: user.id,
+ groupId: devPortalUserGroup.id
+ })));
+ }
+
+ /**
+ * Remove User from API-Mgmt-Devportal-Users group
+ * @param user the user
+ */
+ public removeDevPortalGroupFromUser(user) {
+ return this.getDevPortalUserGroup()
+ .pipe(mergeMap(devPortalUserGroup => this.removeGroupMapping(user.id, devPortalUserGroup.id)))
+ // removeGroupMapping returns GroupRepresentation which is not needed
+ // and should be skipped because of typing errors of method this.deleteUser:
+ .pipe(map((groups) => {}));
+ }
+
/**
* Add client role to user
* @param user the user
@@ -243,10 +292,9 @@ export class KeycloakInteractionService {
return this.deleteUserById(userToDelete.id);
} else {
// remove only devportaluser role from user if he was not generated from dev portal
- return this.removeDevPortalRoleFromUser(userToDelete);
+ return this.removeDevPortalGroupFromUser(userToDelete);
}
}
}));
}
-
}

GW-263: Add User to API-Mgmt-Devportal-Users group by creation and Remove User from API-Mgmt-Devportal-Users group by deletion