Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public Policy create(ResourceServer resourceServer, AbstractPolicyRepresentation

if (resources != null && !resources.isEmpty()) {
representation.setResources(resources.stream().map(id -> {
Resource resource = AdminPermissionsSchema.SCHEMA.getOrCreateResource(keycloakSession, resourceServer, representation.getType(), representation.getResourceType(), id);
Resource resource = AdminPermissionsSchema.SCHEMA.getOrCreateResource(keycloakSession, resourceServer, representation.getResourceType(), id);

if (resource == null) {
resource = storeFactory.getResourceStore().findById(resourceServer, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private AdminPermissionsSchema() {
));
}

public Resource getOrCreateResource(KeycloakSession session, ResourceServer resourceServer, String policyType, String resourceType, String id) {
public Resource getOrCreateResource(KeycloakSession session, ResourceServer resourceServer, String resourceType, String id) {
if (!supportsAuthorizationSchema(session, resourceServer)) {
return null;
}
Expand All @@ -139,10 +139,14 @@ public Resource getOrCreateResource(KeycloakSession session, ResourceServer reso
String name;

switch (resourceType) {
case CLIENTS_RESOURCE_TYPE -> name = resolveClient(session, id).map(ClientModel::getId).orElse(resourceType);
case GROUPS_RESOURCE_TYPE -> name = resolveGroup(session, id).map(GroupModel::getId).orElse(resourceType);
case ROLES_RESOURCE_TYPE -> name = resolveRole(session, id).map(RoleModel::getId).orElse(resourceType);
case USERS_RESOURCE_TYPE -> name = resolveUser(session, id).map(UserModel::getId).orElse(resourceType);
case CLIENTS_RESOURCE_TYPE -> name = resolveClient(session, id).map(ClientModel::getId)
.orElseThrow(() -> new ModelValidationException("Resource [" + id + "] does not exist for type [" + resourceType + "]"));
case GROUPS_RESOURCE_TYPE -> name = resolveGroup(session, id).map(GroupModel::getId)
.orElseThrow(() -> new ModelValidationException("Resource [" + id + "] does not exist for type [" + resourceType + "]"));
case ROLES_RESOURCE_TYPE -> name = resolveRole(session, id).map(RoleModel::getId)
.orElseThrow(() -> new ModelValidationException("Resource [" + id + "] does not exist for type [" + resourceType + "]"));
case USERS_RESOURCE_TYPE -> name = resolveUser(session, id).map(UserModel::getId)
.orElseThrow(() -> new ModelValidationException("Resource [" + id + "] does not exist for type [" + resourceType + "]"));

default -> throw new IllegalStateException("Resource type [" + resourceType + "] not found.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,7 @@ private static void updateResources(AbstractPolicyRepresentation representation,
}

resourceIds = resourceIds.stream().map(id -> {
Resource resource = AdminPermissionsSchema.SCHEMA.getOrCreateResource(session, resourceServer, policy.getType(), policy.getResourceType(), id);
Resource resource = AdminPermissionsSchema.SCHEMA.getOrCreateResource(session, resourceServer, policy.getResourceType(), id);

if (resource == null) {
return id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,31 @@ public void permissionsTest() {
.resourceType(AdminPermissionsSchema.USERS.getType())
.scopes(Set.of("edit", "write", "token-exchange"))
.build(), Response.Status.BAD_REQUEST);

// valid resourceType, valid scopes, non-existent resource ID
createPermission(client, PermissionBuilder.create()
.resourceType(AdminPermissionsSchema.USERS.getType())
.resources(Set.of("non-existent-id"))
.scopes(AdminPermissionsSchema.USERS.getScopes())
.build(), Response.Status.BAD_REQUEST);

createPermission(client, PermissionBuilder.create()
.resourceType(AdminPermissionsSchema.GROUPS.getType())
.resources(Set.of("non-existent-id"))
.scopes(AdminPermissionsSchema.GROUPS.getScopes())
.build(), Response.Status.BAD_REQUEST);

createPermission(client, PermissionBuilder.create()
.resourceType(AdminPermissionsSchema.CLIENTS.getType())
.resources(Set.of("non-existent-id"))
.scopes(AdminPermissionsSchema.CLIENTS.getScopes())
.build(), Response.Status.BAD_REQUEST);

createPermission(client, PermissionBuilder.create()
.resourceType(AdminPermissionsSchema.ROLES.getType())
.resources(Set.of("non-existent-id"))
.scopes(AdminPermissionsSchema.ROLES.getScopes())
.build(), Response.Status.BAD_REQUEST);
}

@Test
Expand Down
Loading