Skip to content

Commit

Permalink
ui: Groups with link count resource
Browse files Browse the repository at this point in the history
  • Loading branch information
JBWatenbergScality committed Apr 4, 2024
1 parent 863798b commit 3d8dfc9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
@NamedQuery(name = "findByGroupId", query = "select t from GroupFederationLinkEntity t where t.groupId = :groupId"),
@NamedQuery(name = "findGroupsByFederationLinkAndName", query = "select g from GroupFederationLinkEntity gl, GroupEntity g where gl.groupId = g.id and g.name like concat('%',:name,'%') and gl.federationLink = :federationLink"),
@NamedQuery(name = "findGroupsByFederationLink", query = "select g from GroupFederationLinkEntity gl, GroupEntity g where gl.groupId = g.id and gl.federationLink = :federationLink"),
@NamedQuery(name = "countGroupsByFederationLinkAndName", query = "select count(g) from GroupFederationLinkEntity gl, GroupEntity g where gl.groupId = g.id and g.name like concat('%',:name,'%') and gl.federationLink = :federationLink"),
@NamedQuery(name = "countGroupsByFederationLink", query = "select count(g) from GroupFederationLinkEntity gl, GroupEntity g where gl.groupId = g.id and gl.federationLink = :federationLink"),
})
public class GroupFederationLinkEntity {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import static org.keycloak.models.jpa.PaginationUtils.paginateQuery;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Stream;

Expand All @@ -27,6 +29,7 @@
import jakarta.persistence.TypedQuery;
import jakarta.ws.rs.DefaultValue;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
Expand Down Expand Up @@ -70,7 +73,7 @@ public Stream<GroupWithLinkRepresentation> getGroupsWithLink(@QueryParam("search
logger.info("getGroupsWithLink");

if (Objects.isNull(federationLink) || federationLink.isEmpty()) {
Stream<GroupRepresentation> groups = groupsResource.getGroups(search, search, exact, firstResult,
Stream<GroupRepresentation> groups = groupsResource.getGroups(search, null, exact, firstResult,
maxResults, true, true);

return groups.map(group -> {
Expand Down Expand Up @@ -125,4 +128,49 @@ public Stream<GroupWithLinkRepresentation> getGroupsWithLink(@QueryParam("search
}
}

@GET
@Path("count")
@NoCache
@Produces(MediaType.APPLICATION_JSON)
@Tag(name = KeycloakOpenAPI.Admin.Tags.GROUPS)
@Operation(summary = "Get groups")
public Map<String, Long> countGroupsWithLink(@QueryParam("search") String search,
@QueryParam("link") String federationLink,
@QueryParam("exact") @DefaultValue("false") Boolean exact,
@QueryParam("first") Integer firstResult,
@QueryParam("max") Integer maxResults) {

GroupPermissionEvaluator groupsEvaluator = auth.groups();
groupsEvaluator.requireList();
logger.info("getGroupsWithLink");

if (Objects.isNull(federationLink) || federationLink.isEmpty()) {
Map<String, Long> count = groupsResource.getGroupCount(search, false);

return count;
}

try {
TypedQuery<Long> query = getEntityManager()
.createNamedQuery("countGroupsByFederationLinkAndName", Long.class)
.setParameter("federationLink", federationLink)
.setParameter("name", search);
if (Objects.isNull(search) || search.isEmpty()) {
query = getEntityManager()
.createNamedQuery("findGroupsByFederationLink", Long.class)
.setParameter("federationLink", federationLink);
}

Long count = query.getSingleResult();
Map<String, Long> result = new HashMap<>();
result.put("count", count);
return result;
} catch (NoResultException e) {
logger.trace("No group found for federation link " + federationLink, e);
Map<String, Long> result = new HashMap<>();
result.put("count", 0L);
return result;
}
}

}
38 changes: 38 additions & 0 deletions src/test/java/com/scality/keycloak/GroupWithLinkTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.Stream;

import org.apache.commons.io.IOUtils;
Expand All @@ -19,6 +21,7 @@
import org.testcontainers.containers.output.Slf4jLogConsumer;
import org.testcontainers.utility.MountableFile;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.scality.keycloak.groupFederationLink.GroupWithLinkRepresentation;

Expand Down Expand Up @@ -262,6 +265,37 @@ private void syncLdapGroups(KeycloakContainer keycloak, ProviderAndMapper provid
}
}

private Long countGroupsWithLink(KeycloakContainer keycloak, String federationLink) throws IOException {
URL url = new URL(
keycloak.getAuthServerUrl() + "/admin/realms/master/groups-with-link/count?link="
+ federationLink);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Authorization", "Bearer " + tokenProvider.getToken(keycloak));

int responseCode = conn.getResponseCode();

if (responseCode != 200) {
System.out.println("responseCode = " + responseCode);
InputStream errorStream = conn.getErrorStream();
if (errorStream != null) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = errorStream.read(buffer)) != -1) {
System.out.write(buffer, 0, bytesRead);
}
}
return 0L;
}

String responsePayload = IOUtils.toString(conn.getInputStream(), "UTF-8");
ObjectMapper objectMapper = new ObjectMapper();
TypeReference<HashMap<String, Long>> typeRef = new TypeReference<HashMap<String, Long>>() {
};
Map<String, Long> result = objectMapper.readValue(responsePayload, typeRef);
return result.get("count");
}

private Stream<GroupWithLinkRepresentation> getGroupsWithLink(KeycloakContainer keycloak, String federationLink,
String search)
throws IOException {
Expand Down Expand Up @@ -358,6 +392,10 @@ public void groups_with_links_should_be_returned_when_listing_groups()
groupsWithLink = getGroupsWithLink(keycloak, providerAndMapper.providerID(), "myGroup1");
System.out.println(groupsWithLink);
assertEquals(0, groupsWithLink.count());

Long count = countGroupsWithLink(keycloak, providerAndMapper.providerID());
Long expectedLong = 1L;
assertEquals(expectedLong, count);
}
}

Expand Down

0 comments on commit 3d8dfc9

Please sign in to comment.