83 lines
3.6 KiB
Java
83 lines
3.6 KiB
Java
package eu.eudat.service.keycloak;
|
|
|
|
import gr.cite.commons.web.keycloak.api.configuration.KeycloakClientConfiguration;
|
|
import gr.cite.tools.logging.LoggerService;
|
|
import org.jetbrains.annotations.NotNull;
|
|
import org.keycloak.representations.idm.GroupRepresentation;
|
|
import org.keycloak.representations.idm.UserRepresentation;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.*;
|
|
|
|
@Service
|
|
public class KeycloakServiceImpl implements KeycloakService {
|
|
|
|
private static final LoggerService logger = new LoggerService(LoggerFactory.getLogger(KeycloakServiceImpl.class));
|
|
private final MyKeycloakAdminRestApi api;
|
|
private final KeycloakResourcesConfiguration configuration;
|
|
private final KeycloakClientConfiguration clientConfiguration;
|
|
|
|
@Autowired
|
|
public KeycloakServiceImpl(MyKeycloakAdminRestApi api, KeycloakResourcesConfiguration configuration, KeycloakClientConfiguration clientConfiguration) {
|
|
this.api = api;
|
|
this.configuration = configuration;
|
|
this.clientConfiguration = clientConfiguration;
|
|
}
|
|
|
|
@Override
|
|
public void addUserToGroup(@NotNull UUID subjectId, String groupId) {
|
|
api.users().addUserToGroup(subjectId.toString(), groupId);
|
|
}
|
|
|
|
@Override
|
|
public void removeUserFromGroup(@NotNull UUID subjectId, String groupId) {
|
|
api.users().removeUserFromGroup(subjectId.toString(), groupId);
|
|
}
|
|
|
|
@Override
|
|
public void addUserToGroup(UUID subjectId, KeycloakRole role) {
|
|
KeycloakAuthorityProperties properties = this.configuration.getProperties().getAuthorities().get(role.name());
|
|
if (properties != null)
|
|
addUserToGroup(subjectId, properties.getGroupId());
|
|
}
|
|
|
|
@Override
|
|
public void removeUserFromGroup(@NotNull UUID subjectId, KeycloakRole role) {
|
|
KeycloakAuthorityProperties properties = this.configuration.getProperties().getAuthorities().get(role.name());
|
|
if (properties != null)
|
|
removeUserFromGroup(subjectId, properties.getGroupId());
|
|
}
|
|
|
|
@Override
|
|
public void assignClientRoleToUser(UUID subjectId, String clientId, KeycloakRole role) {
|
|
if (clientId == null)
|
|
clientId = clientConfiguration.getProperties().getClientId();
|
|
UserRepresentation user = api.users().findUserById(subjectId.toString());
|
|
user.getClientRoles().computeIfAbsent(clientId, k -> new ArrayList<>());
|
|
Set<String> clientRoles = new HashSet<>(Set.copyOf(user.getClientRoles().get(clientId)));
|
|
clientRoles.add(role.name());
|
|
user.getClientRoles().get(clientId).clear();
|
|
user.getClientRoles().get(clientId).addAll(clientRoles);
|
|
api.users().updateUser(subjectId.toString(), user);
|
|
}
|
|
|
|
@Override
|
|
public void removeClientRoleFromUser(UUID subjectId, String clientId, KeycloakRole role) {
|
|
if (clientId == null) clientId = clientConfiguration.getProperties().getClientId();
|
|
UserRepresentation user = api.users().findUserById(subjectId.toString());
|
|
user.getClientRoles().computeIfAbsent(clientId, k -> new ArrayList<>());
|
|
Set<String> clientRoles = new HashSet<>(Set.copyOf(user.getClientRoles().get(clientId)));
|
|
clientRoles.remove(role.name());
|
|
user.getClientRoles().get(clientId).clear();
|
|
user.getClientRoles().get(clientId).addAll(clientRoles);
|
|
api.users().updateUser(subjectId.toString(), user);
|
|
}
|
|
|
|
public List<GroupRepresentation> getUserGroups(UUID subjectId) {
|
|
return api.users().getGroups(subjectId.toString());
|
|
}
|
|
|
|
}
|