Verified Commit fd45ebb1 authored by Julien Boubechtoula's avatar Julien Boubechtoula 🤞🏼

Add User/Group into Keycloak

parent 502fa911
Pipeline #107133744 passed with stage
in 8 minutes and 8 seconds
......@@ -39,10 +39,10 @@ objects:
fieldRef:
apiVersion: v1
fieldPath: metadata.namespace
- name: KEYCLOAK_AUTH_URL
- name: KEYCLOAK_ADMIN_REALM
valueFrom:
configMapKeyRef:
key: KEYCLOAK_AUTH_URL
key: KEYCLOAK_ADMIN_REALM
name: keycloak-config
- name: KEYCLOAK_REALM
valueFrom:
......@@ -52,23 +52,33 @@ objects:
- name: KEYCLOAK_ADMIN_AUTH_URL
valueFrom:
configMapKeyRef:
key: KEYCLOAK_AUTH_URL
key: KEYCLOAK_ADMIN_AUTH_URL
name: keycloak-config
- name: KEYCLOAK_ADMIN_REALM
- name: KEYCLOAK_ADMIN_CLIENT_ID
valueFrom:
configMapKeyRef:
key: KEYCLOAK_REALM
key: KEYCLOAK_ADMIN_CLIENT_ID
name: keycloak-config
- name: KEYCLOAK_ADMIN_CLIENT_ID
- name: KEYCLOAK_ADMIN_USERNAME
valueFrom:
secretKeyRef:
key: KEYCLOAK_ADMIN_CLIENT_ID
key: KEYCLOAK_ADMIN_USERNAME
name: kathra-usermanager-secret
- name: KEYCLOAK_ADMIN_CLIENT_SECRET
- name: KEYCLOAK_ADMIN_PASSWORD
valueFrom:
secretKeyRef:
key: KEYCLOAK_ADMIN_CLIENT_SECRET
key: KEYCLOAK_ADMIN_PASSWORD
name: kathra-usermanager-secret
- name: KEYCLOAK_AUTH_URL
valueFrom:
configMapKeyRef:
key: KEYCLOAK_AUTH_URL
name: keycloak-config
- name: KEYCLOAK_REALM
valueFrom:
configMapKeyRef:
key: KEYCLOAK_REALM
name: keycloak-config
- name: KEYCLOAK_CLIENT_ID
valueFrom:
secretKeyRef:
......
......@@ -57,19 +57,19 @@ public class UserManagerController implements UserManagerService {
}
private void populateGroup(List<Group> groups, List<GroupRepresentation> subGroups) {
subGroups.forEach(subGroupRepresentation -> {
subGroups.parallelStream().forEach(subGroupRepresentation -> {
Group group = new Group()
//.id(subGroupRepresentation.getId())
.name(subGroupRepresentation.getName())
.path(subGroupRepresentation.getPath());
List<UserRepresentation> members = keycloakService.getGroupMembers(group.getId());
for (UserRepresentation member : members) {
List<UserRepresentation> members = keycloakService.getGroupMembers(subGroupRepresentation.getId());
members.parallelStream().forEach(member -> {
Assignation userAssignation = new Assignation().name(member.getUsername());
if (group.getMembers() == null || !group.getMembers().contains(userAssignation)) {
group.addMembersItem(userAssignation);
}
}
});
groups.add(group);
if (subGroupRepresentation.getSubGroups() != null)
populateGroup(groups, subGroupRepresentation.getSubGroups());
......@@ -118,7 +118,7 @@ public class UserManagerController implements UserManagerService {
* @return Group
*/
public Group getGroup(String groupPath) throws Exception {
return getGroups().stream().filter(g -> g.getPath().equals(groupPath)).findFirst().orElseThrow( () -> new KathraException("Group not found", null, KathraException.ErrorCode.NOT_FOUND ));
return getGroups().parallelStream().filter(g -> g.getPath().equals(groupPath)).findFirst().orElseThrow( () -> new KathraException("Group not found", null, KathraException.ErrorCode.NOT_FOUND ));
}
/**
......@@ -145,7 +145,11 @@ public class UserManagerController implements UserManagerService {
* @return User
*/
public User getUser(String userId) throws Exception {
return keycloakService.getUser(userId);
User user = keycloakService.getUser(userId);
if (user == null) {
throw new KathraException("User not found", null, KathraException.ErrorCode.NOT_FOUND);
}
return user;
}
/**
......
......@@ -36,6 +36,7 @@ import org.keycloak.representations.idm.UserRepresentation;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
/**
......@@ -121,17 +122,19 @@ public class KeycloakService {
}
public User getUser(String userId) {
UserRepresentation userR = kathraRealm.users().list().stream().filter(u -> u.getUsername().equals(userId)).findFirst().get();
return new User().name(userR.getUsername()).lastName(userR.getLastName()).firstName(userR.getFirstName()).email(userR.getEmail());
Optional<UserRepresentation> userR = kathraRealm.users().list().parallelStream().filter(u -> u.getUsername().equals(userId)).findFirst();
if (userR.isEmpty())
return null;
return new User().name(userR.get().getUsername()).lastName(userR.get().getLastName()).firstName(userR.get().getFirstName()).email(userR.get().getEmail());
}
private UserRepresentation getUserRepresentation(User user) {
return kathraRealm.users().list().stream().filter(u -> u.getUsername().equals(user.getName())).findFirst().get();
return kathraRealm.users().list().parallelStream().filter(u -> u.getUsername().equals(user.getName())).findFirst().get();
}
public List<User> getUsers() {
return kathraRealm.users().list().stream().map(user -> mapToUser(user)).collect(Collectors.toList());
return kathraRealm.users().list().parallelStream().map(user -> mapToUser(user)).collect(Collectors.toList());
}
private User mapToUser(UserRepresentation userR) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment