moved implementation

This commit is contained in:
Alfredo Oliviero 2024-04-19 18:02:05 +02:00
parent 1d4dc2c27d
commit 5785c5a712
5 changed files with 82 additions and 39 deletions

View File

@ -8,9 +8,12 @@ import java.util.stream.Collectors;
import org.gcube.service.idm.keycloack.KkClientFactory;
import org.keycloak.admin.client.resource.ClientResource;
import org.keycloak.admin.client.resource.GroupResource;
import org.keycloak.admin.client.resource.RealmResource;
import org.keycloak.admin.client.resource.RoleResource;
import org.keycloak.admin.client.resource.RolesResource;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.GroupRepresentation;
import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import org.slf4j.LoggerFactory;
@ -36,12 +39,10 @@ public class KCClientsController {
return client.getId();
}
if (format.equals(REPR.client_id)) {
return client.getClientId();
}
if (format.equals(REPR.compact)) {
HashMap<String, Object> result = new HashMap<String, Object>();
result.put("id", client.getId());
@ -58,7 +59,7 @@ public class KCClientsController {
return clients.stream().map(x -> formatRepr(x, format)).filter(Objects::nonNull).collect(Collectors.toList());
}
public static List<RoleRepresentation> getRoles() {
public static List<RoleRepresentation> getRolesCurrent() {
logger.info("Searching users for context");
ClientResource client = KkClientFactory.getSingleton().getKKClient();
RolesResource roles_resource = client.roles();
@ -67,10 +68,15 @@ public class KCClientsController {
return roles;
}
public static RoleRepresentation getRoleByName(String name) {
public static RoleRepresentation getRoleByNameCurrent(String name) {
return getRoleByName(null, name);
}
public static RoleRepresentation getRoleByName(String clientId, String name) {
logger.info("Searching users for context");
ClientResource client = KkClientFactory.getSingleton().getKKClient();
RolesResource roles_resource = client.roles();
ClientResource clientResource = KkClientFactory.getSingleton().getKKClientById(clientId);
RolesResource roles_resource = clientResource.roles();
RoleResource role = roles_resource.get(name);
if (role == null) {
throw new NotFoundException("cannot retrieve role " + name);
@ -79,20 +85,58 @@ public class KCClientsController {
return role.toRepresentation();
}
public static List<UserRepresentation> getContextUsersByRole(String role_name) {
return getContextUsersByRole(role_name, null, null);
public static List<UserRepresentation> getContextUsersByRoleCurrent(String role_name) {
return getContextUsersByRoleCurrent(role_name, null, null);
}
public static List<UserRepresentation> getContextUsersByRole(String role_name, Integer firstResult,
public static List<UserRepresentation> getContextUsersByRoleCurrent(String role_name, Integer firstResult,
Integer maxResults) {
return getContextUsersByRole(null, role_name, firstResult, maxResults);
}
ClientResource client = KkClientFactory.getSingleton().getKKClient();
RolesResource roles_resource = client.roles();
public static List<UserRepresentation> getContextUsersByRole(String clientId, String role_name,
Integer firstResult,
Integer maxResults) {
ClientResource clientResource = KkClientFactory.getSingleton().getKKClientById(clientId);
RolesResource roles_resource = clientResource.roles();
RoleResource r = roles_resource.get(role_name);
List<UserRepresentation> users = r.getUserMembers(firstResult, maxResults);
return users;
}
/**
* returns the list of users of the client
* users list is a subset of members list, it's obtained from the group named as
* the context
*
* @param clientId null for current context
*/
public static List<UserRepresentation> getMemberGroupUsersCurrent() {
return getMemberGroupUsers(null, null, null);
}
/**
* returns the list of users of the client
* users list is a subset of members list, it's obtained from the group named as
* the context
*
* @param clientId null for current context
* @param firstResult
* @param maxResults
*/
public static List<UserRepresentation> getMemberGroupUsers(String clientId, Integer firstResult,
Integer maxResults) {
RealmResource realmResource = KkClientFactory.getSingleton().getKKRealm();
ClientResource clientResource = KkClientFactory.getSingleton().getKKClientById(clientId);
ClientRepresentation client = clientResource.toRepresentation();
GroupRepresentation g_repr = realmResource.getGroupByPath(client.getName());
GroupResource group = realmResource.groups().group(g_repr.getId());
List<UserRepresentation> user_members = group.members(firstResult, maxResults);
return user_members;
}
}

View File

@ -61,7 +61,7 @@ public class KCRolesController {
return roles;
}
public static RoleRepresentation getRoleByName(String name) {
public static RoleRepresentation getRoleByNameCurrent(String name) {
logger.info("Searching users for context");
ClientResource client = KkClientFactory.getSingleton().getKKClient();
RolesResource roles_resource = client.roles();

View File

@ -140,6 +140,30 @@ public class KkClientFactory {
return realm.clients().get(id);
}
/**
* select the ClientResource by name, or current client if clientId parameter is null;
*
* @param clientId
* @return
*/
public ClientResource getKKClientById(String clientId) {
if (clientId == null)
return KkClientFactory.getSingleton().getKKClient();
RealmResource realmResource = getKKRealm();
List<ClientRepresentation> clients = realmResource.clients().findByClientId(clientId);
if (clients.size() == 0) {
throw new NotFoundException();
}
String id = clients.get(0).getId();
return realmResource.clients().get(id);
}
// TODO: REMOVE
// static IsServerConfig getTestConfig() {
// String serverUrl = "https://accounts.dev.d4science.org/auth";

View File

@ -159,32 +159,7 @@ public class ClientsAPI {
@QueryParam("client_id") String clientId) {
ResponseBean responseBean = new ResponseBean();
RealmResource realmResource = KkClientFactory.getSingleton().getKKRealm();
ClientResource clientResource = null;
ClientRepresentation client = null;
// select the client by name, or current client if client_name parameter is
// null;
if (clientId == null) {
clientResource = KkClientFactory.getSingleton().getKKClient();
client = clientResource.toRepresentation();
} else {
List<ClientRepresentation> clients = realmResource.clients().findByClientId(clientId);
if (clients.size() == 0) {
throw new NotFoundException();
}
String id = clients.get(0).getId();
clientResource = realmResource.clients().get(id);
client = clientResource.toRepresentation();
}
GroupRepresentation g_repr = realmResource.getGroupByPath(client.getName());
GroupResource group = realmResource.groups().group(g_repr.getId());
List<UserRepresentation> user_members = group.members(firstResult, maxResults);
Object result = KCUserController.formatList(user_members, format_users);
responseBean.setResult(result);

View File

@ -97,7 +97,7 @@ public class RolesAPI {
ResponseBean responseBean = new ResponseBean();
try {
RoleRepresentation role = KCRolesController.getRoleByName(role_name);
RoleRepresentation role = KCRolesController.getRoleByNameCurrent(role_name);
responseBean.setResult(role);
responseBean.setSuccess(true);