@Override public boolean hasRole(RoleModel role) { if (this.equals(role)) return true; if (!isComposite()) return false; Set<RoleModel> visited = new HashSet<RoleModel>(); return KeycloakModelUtils.searchFor(role, this, visited); }
@Override public void setScopeParamRequired(boolean scopeParamRequired) { getDelegateForUpdate(); updated.setScopeParamRequired(scopeParamRequired); }
@Override public int hashCode() { return getId().hashCode(); }
@Override public RoleModel getRoleById(String id, RealmModel realm) { CachedRole cached = cache.get(id, CachedRole.class); if (cached != null && !cached.getRealm().equals(realm.getId())) { cached = null; } if (cached == null) { Long loaded = cache.getCurrentRevision(id); RoleModel model = getRealmDelegate().getRoleById(id, realm); if (model == null) return null; if (invalidations.contains(id)) return model; if (model.isClientRole()) { cached = new CachedClientRole(loaded, model.getContainerId(), model, realm); } else { cached = new CachedRealmRole(loaded, model, realm); } cache.addRevisioned(cached, startupRevision); } else if (invalidations.contains(id)) { return getRealmDelegate().getRoleById(id, realm); } else if (managedRoles.containsKey(id)) { return managedRoles.get(id); } RoleAdapter adapter = new RoleAdapter(cached,this, realm); managedRoles.put(id, adapter); return adapter; }
@Override public boolean hasRole(RoleModel role) { return this.equals(role) || KeycloakModelUtils.searchFor(role, this, new HashSet<>()); }
@Override public Set<RoleModel> getComposites() { if (isUpdated()) return updated.getComposites(); if (composites == null) { composites = new HashSet<RoleModel>(); for (String id : cached.getComposites()) { RoleModel role = realm.getRoleById(id); if (role == null) { throw new IllegalStateException("Could not find composite in role " + getName() + ": " + id); } composites.add(role); } } return composites; }
@Override public String getId() { if (isUpdated()) return updated.getId(); return cached.getId(); }
private void invalidateRole(String id) { invalidations.add(id); RoleAdapter adapter = managedRoles.get(id); if (adapter != null) adapter.invalidate(); }
protected void getDelegateForUpdate() { if (updated == null) { cacheSession.registerRoleInvalidation(cached.getId(), cached.getName(), getContainerId()); updated = modelSupplier.get(); if (updated == null) throw new IllegalStateException("Not found in database"); } }
@Override public String getContainerId() { if (isClientRole()) { CachedClientRole appRole = (CachedClientRole) cached; return appRole.getClientId(); } else { return realm.getId(); } }
@Override public RoleModel getRoleById(String id, RealmModel realm) { if (!cache.isEnabled()) return getDelegate().getRoleById(id, realm); CachedRole cached = cache.getRole(id); if (cached != null && !cached.getRealm().equals(realm.getId())) { cached = null; } if (cached == null) { RoleModel model = getDelegate().getRoleById(id, realm); if (model == null) return null; if (roleInvalidations.contains(id)) return model; if (model.getContainer() instanceof ClientModel) { cached = new CachedClientRole(((ClientModel) model.getContainer()).getId(), model, realm); } else { cached = new CachedRealmRole(model, realm); } cache.addCachedRole(cached); } else if (roleInvalidations.contains(id)) { return getDelegate().getRoleById(id, realm); } else if (managedRoles.containsKey(id)) { return managedRoles.get(id); } RoleAdapter adapter = new RoleAdapter(cached, cache, this, realm); managedRoles.put(id, adapter); return adapter; }
@Override public String getName() { if (isUpdated()) return updated.getName(); return cached.getName(); }
private void roleRemovalInvalidations(String roleId, String roleName, String roleContainerId) { Set<String> newInvalidations = new HashSet<>(); cache.roleRemoval(roleId, roleName, roleContainerId, newInvalidations); invalidations.addAll(newInvalidations); // need to make sure that scope and group mapping clients and groups are invalidated for (String id : newInvalidations) { ClientModel adapter = managedApplications.get(id); if (adapter != null && adapter instanceof ClientAdapter){ ((ClientAdapter)adapter).invalidate(); continue; } GroupAdapter group = managedGroups.get(id); if (group != null) { group.invalidate(); continue; } ClientScopeAdapter clientScope = managedClientScopes.get(id); if (clientScope != null) { clientScope.invalidate(); continue; } RoleAdapter role = managedRoles.get(id); if (role != null) { role.invalidate(); continue; } } }
@Override public void setSingleAttribute(String name, String value) { getDelegateForUpdate(); updated.setSingleAttribute(name, value); }
@Override public int hashCode() { return getId().hashCode(); }
@Override public String getDescription() { if (isUpdated()) return updated.getDescription(); return cached.getDescription(); }
@Override public void addCompositeRole(RoleModel role) { getDelegateForUpdate(); updated.addCompositeRole(role); }
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || !(o instanceof RoleModel)) return false; RoleModel that = (RoleModel) o; return that.getId().equals(getId()); }
@Override public boolean isComposite() { if (isUpdated()) return updated.isComposite(); return cached.isComposite(); }
@Override public void setName(String name) { getDelegateForUpdate(); updated.setName(name); }