KEYCLOAK-17749 Remove need for iterating by all clients

This commit is contained in:
Hynek Mlnarik 2021-04-13 13:31:43 +02:00 committed by Hynek Mlnařík
parent b8a7750000
commit c2e2cbe180

View file

@ -25,15 +25,12 @@ import java.util.Set;
import org.keycloak.authorization.AuthorizationProvider; import org.keycloak.authorization.AuthorizationProvider;
import org.keycloak.authorization.model.PermissionTicket; import org.keycloak.authorization.model.PermissionTicket;
import org.keycloak.authorization.model.Policy; import org.keycloak.authorization.model.Policy;
import org.keycloak.authorization.model.ResourceServer;
import org.keycloak.authorization.policy.provider.PolicyProviderFactory; import org.keycloak.authorization.policy.provider.PolicyProviderFactory;
import org.keycloak.authorization.store.PermissionTicketStore; import org.keycloak.authorization.store.PermissionTicketStore;
import org.keycloak.authorization.store.PolicyStore; import org.keycloak.authorization.store.PolicyStore;
import org.keycloak.authorization.store.ResourceServerStore;
import org.keycloak.authorization.store.ResourceStore; import org.keycloak.authorization.store.ResourceStore;
import org.keycloak.authorization.store.StoreFactory; import org.keycloak.authorization.store.StoreFactory;
import org.keycloak.models.KeycloakSessionFactory; import org.keycloak.models.KeycloakSessionFactory;
import org.keycloak.models.RealmModel;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import org.keycloak.models.UserModel.UserRemovedEvent; import org.keycloak.models.UserModel.UserRemovedEvent;
import org.keycloak.provider.ProviderFactory; import org.keycloak.provider.ProviderFactory;
@ -85,17 +82,11 @@ public class UserSynchronizer implements Synchronizer<UserRemovedEvent> {
StoreFactory storeFactory = authorizationProvider.getStoreFactory(); StoreFactory storeFactory = authorizationProvider.getStoreFactory();
PolicyStore policyStore = storeFactory.getPolicyStore(); PolicyStore policyStore = storeFactory.getPolicyStore();
ResourceStore resourceStore = storeFactory.getResourceStore(); ResourceStore resourceStore = storeFactory.getResourceStore();
ResourceServerStore resourceServerStore = storeFactory.getResourceServerStore();
RealmModel realm = event.getRealm();
UserModel userModel = event.getUser(); UserModel userModel = event.getUser();
realm.getClientsStream().forEach(clientModel -> { resourceStore.findByOwner(userModel.getId(), null, resource -> {
ResourceServer resourceServer = resourceServerStore.findById(clientModel.getId());
if (resourceServer != null) {
resourceStore.findByOwner(userModel.getId(), resourceServer.getId()).forEach(resource -> {
String resourceId = resource.getId(); String resourceId = resource.getId();
policyStore.findByResource(resourceId, resourceServer.getId()).forEach(policy -> { policyStore.findByResource(resourceId, resource.getResourceServer()).forEach(policy -> {
if (policy.getResources().size() == 1) { if (policy.getResources().size() == 1) {
policyStore.delete(policy.getId()); policyStore.delete(policy.getId());
} else { } else {
@ -105,8 +96,6 @@ public class UserSynchronizer implements Synchronizer<UserRemovedEvent> {
resourceStore.delete(resourceId); resourceStore.delete(resourceId);
}); });
} }
});
}
private void removeFromUserPermissionTickets(UserRemovedEvent event, AuthorizationProvider authorizationProvider) { private void removeFromUserPermissionTickets(UserRemovedEvent event, AuthorizationProvider authorizationProvider) {
StoreFactory storeFactory = authorizationProvider.getStoreFactory(); StoreFactory storeFactory = authorizationProvider.getStoreFactory();