8000 Feature/organization roles by yaroslav02678 · Pull Request #48738 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
Closed
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
26 changes: 4 additions & 22 deletions .github/workflows/documentation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ on:

env:
MAVEN_ARGS: "-B -nsu -Daether.connector.http.connectionMaxTtl=25"
MAVEN_OPTS: "--enable-native-access=ALL-UNNAMED"


concurrency:
# Only cancel jobs for PR updates
Expand Down Expand Up @@ -63,8 +65,10 @@ jobs:
name: Build and verify Keycloak documentation
shell: bash
run: |
export MAVEN_OPTS="--enable-native-access=ALL-UNNAMED"
./mvnw install -Dtest=!ExternalLinksTest -am -pl docs/documentation/tests,docs/documentation/dist -e -Pdocumentation


- id: upload-keycloak-documentation
name: Upload Keycloak documentation
uses: act 8000 ions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
Expand All @@ -73,28 +77,6 @@ jobs:
path: docs/documentation/dist/target/*.zip
retention-days: 1

external-links:
name: External links check
if: ${{ needs.conditional.outputs.documentation == 'true' }}
runs-on: ubuntu-latest
needs: conditional
steps:
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0

- id: setup-java
name: Setup Java
uses: ./.github/actions/java-setup

- id: maven-cache
name: Maven cache
uses: ./.github/actions/maven-cache

- id: build-test-documentation
name: Build and verify Keycloak documentation
shell: bash
run: |
./mvnw install -Dtest=ExternalLinksTest -am -pl docs/documentation/tests -e -Pdocumentation

check:
name: Status Check - Keycloak Documentation
if: always()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,7 @@ public interface OrganizationResource {

@Path("groups")
OrganizationGroupsResource groups();

@Path("roles")
OrganizationRolesResource roles();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.keycloak.admin.client.resource;

import java.util.List;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.PUT;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import org.keycloak.representations.idm.RoleRepresentation;
import org.keycloak.representations.idm.UserRepresentation;

public interface Organizat 8000 ionRoleResource {

@GET
@Produces(MediaType.APPLICATION_JSON)
RoleRepresentation getRole();

@DELETE
void deleteRole();

@PUT
@Consumes(MediaType.APPLICATION_JSON)
Response updateRole(RoleRepresentation rep);

@GET
@Path("users")
@Produces(MediaType.APPLICATION_JSON)
List<UserRepresentation> getUsersInRole();

@PUT
@Path("users/{userId}")
void assignRoleToUser(@PathParam("userId") String userId);

@DELETE
@Path("users/{userId}")
void removeRoleFromUser(@PathParam("userId") String userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package org.keycloak.admin.client.resource;

import java.util.List;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

import org.keycloak.representations.idm.RoleRepresentation;

public interface OrganizationRolesResource {

@POST
@Consumes(MediaType.APPLICATION_JSON)
Response createRole(RoleRepresentation rep);

@GET
@Produces(MediaType.APPLICATION_JSON)
List<RoleRepresentation> getRoles(@QueryParam("search") String search,
@QueryParam("first") Integer first,
@QueryParam("max") Integer max);

@Path("{role-id}")
OrganizationRoleResource getRoleById(@PathParam("role-id") String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.keycloak.models.RealmModel;
import org.keycloak.models.RealmProvider;
import org.keycloak.models.RoleModel;
import org.keycloak.models.OrganizationModel;
import org.keycloak.models.RoleProvider;
import org.keycloak.models.UserModel;
import org.keycloak.models.cache.CacheRealmProvider;
Expand Down Expand Up @@ -903,6 +904,57 @@ public RoleModel getClientRole(ClientModel client, String name) {
return role;
}

@Override
public RoleModel addOrganizationRole(OrganizationModel organization, String id, String name) {
RoleModel role = getRoleDelegate().addOrganizationRole(organization, id, name);
addedRole(role.getId(), organization.getId(), name);
return role;
}

@Override
public RoleModel getOrganizationRole(OrganizationModel organization, String name) {
String cacheKey = getRoleByNameCacheKey(organization.getId(), name);
boolean queryDB = invalidations.contains(cacheKey) || listInvalidations.contains(organization.getId());
if (queryDB) {
return getRoleDelegate().getOrganizationRole(organization, name);
}
RoleByNameQuery query = cache.get(cacheKey, RoleByNameQuery.class);
if (query != null) {
logger.tracev("getOrganizationRole cache hit: {0}.{1}", organization.getName(), name);
}
if (query == null) {
long loaded = cache.getCurrentRevision(cacheKey);
RoleModel model = getRoleDelegate().getOrganizationRole(organization, name);
if (model == null) {
query = new RoleByNameQuery(loaded, cacheKey, organization.getRealm(), null, organization.getId());
} else {
query = new RoleByNameQuery(loaded, cacheKey, organization.getRealm(), model.getId(), organization.getId());
}
logger.tracev("adding organization role cache miss: org {0} key {1}", organization.getName(), cacheKey);
cache.addRevisioned(query, startupRevision);
}
String roleId = query.getRole();
if (roleId == null) {
return null;
}
RoleModel role = getRoleById(organization.getRealm(), roleId);
if (role == null) {
invalidations.add(cacheKey);
return getRoleDelegate().getOrganizationRole(organization, name);
}
return role;
}

@Override
public Stream<RoleModel> getOrganizationRolesStream(OrganizationModel organization, Integer first, Integer max) {
return getRoleDelegate().getOrganizationRolesStream(organization, first, max);
}

@Override
public Stream<RoleModel> searchForOrganizationRolesStream(OrganizationModel organization, String search, Integer first, Integer max) {
return getRoleDelegate().searchForOrganizationRolesStream(organization, search, first, max);
}

@Override
public boolean removeRole(RoleModel role) {
listInvalidations.add(role.getContainer().getId());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.keycloak.models.KeycloakSession;
import org.keycloak.models.OrganizationModel;
import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleModel;
import org.keycloak.models.UserModel;
import org.keycloak.models.cache.CacheRealmProvider;
import org.keycloak.models.cache.UserCache;
Expand Down Expand Up @@ -437,6 +438,46 @@ public void close() {
}
}

@Override
public RoleModel createRole(OrganizationModel organization, String name) {
return getDelegate().createRole(organization, name);
}

@Override
public Stream<RoleModel> getRoles(OrganizationModel organization, Integer first, Integer max) {
return getDelegate().getRoles(organization, first, max);
}

@Override
public Stream<RoleModel> searchRolesByName(OrganizationModel organization, String search, Integer first, Integer max) {
return getDelegate().searchRolesByName(organization, search, first, max);
}

@Override
public RoleModel getRoleById(OrganizationModel organization, String id) {
return getDelegate().getRoleById(organization, id);
}

@Override
public boolean removeRole(OrganizationModel organization, RoleModel role) {
return getDelegate().removeRole(organization, role);
}

@Override
public void grantRole(OrganizationModel organization, UserModel user, RoleModel role) {
getDelegate().grantRole(organization, user, role);
}

@Override
public void revokeRole(OrganizationModel organization, UserModel user, RoleModel role) {
getDelegate().revokeRole(organization, user, role);
}

@Override
public Stream<UserModel> getMembersWithRole(OrganizationModel organization, RoleModel role) {
return getDelegate().getMembersWithRole(organization, role);
}

void registerOrganizationInvalidation(OrganizationModel organization) {
String id = organization.getId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
import org.keycloak.models.RoleContainerModel.RoleRemovedEvent;
import org.keycloak.models.RoleModel;
import org.keycloak.models.RoleProvider;
import org.keycloak.models.OrganizationModel;
import org.keycloak.models.delegate.ClientModelLazyDelegate;
import org.keycloak.models.jpa.entities.ClientAttributeEntity;
import org.keycloak.models.jpa.entities.ClientEntity;
Expand Down Expand Up @@ -314,6 +315,49 @@ public RoleModel getClientRole(ClientModel client, String name) {
return session.roles().getRoleById(client.getRealm(), roles.get(0));
}

@Override
public RoleModel addOrganizationRole(OrganizationModel organization, String id, String name) {
if (getOrganizationRole(organization, name) != null) {
throw new ModelDuplicateException();
}
RoleEntity entity = new RoleEntity();
entity.setId(id == null ? KeycloakModelUtils.generateId() : id);
entity.setName(name);
entity.setOrganizationId(organization.getId());
entity.setRealmId(organization.getRealm().getId());

em.persist(entity);
em.flush();

return new RoleAdapter(session, organization.getRealm(), em, entity);
}

@Override
public RoleModel getOrganizationRole(OrganizationModel organization, String name) {
TypedQuery<String> query = em.createNamedQuery("getOrganizationRoleIdByName", String.class);
query.setParameter("name", name);
query.setParameter("organization", organization.getId());
List<String> roles = query.getResultList();
if (roles.isEmpty()) return null;
return session.roles().getRoleById(organization.getRealm(), roles.get(0));
}

@Override
public Stream<RoleModel> getOrganizationRolesStream(OrganizationModel organization, Integer first, Integer max) {
TypedQuery<RoleEntity> query = em.createNamedQuery("getOrganizationRoles", RoleEntity.class);
query.setParameter("organization", organization.getId());

return getRolesStream(query, organization.getRealm(), first, max);
}

@Override
public Stream<RoleModel> searchForOrganizationRolesStream(OrganizationModel organization, String search, Integer first, Integer max) {
TypedQuery<RoleEntity> query = em.createNamedQuery("searchForOrganizationRoles", RoleEntity.class);
query.setParameter("organization", organization.getId());

return searchForRoles(query, organization.getRealm(), search, first, max);
}

@Override
public Map<ClientModel, Set<String>> getAllRedirectUrisOfEnabledClients(RealmModel realm) {
TypedQuery<Map> query = em.createNamedQuery("getAllRedirectUrisOfEnabledClients", Map.class);
Expand Down
Loading
Loading
0