model fix for caching

This commit is contained in:
Bill Burke 2014-06-30 13:12:42 -04:00
parent 3cf52b018c
commit b4f5fe9977
4 changed files with 24 additions and 13 deletions

View file

@ -130,15 +130,14 @@ public class ApplicationAdapter extends ClientAdapter implements ApplicationMode
@Override
public boolean removeRole(RoleModel roleModel) {
RoleAdapter roleAdapter = (RoleAdapter)roleModel;
if (roleModel == null) {
return false;
}
if (!roleAdapter.getContainer().equals(this)) return false;
if (!roleModel.getContainer().equals(this)) return false;
if (!roleAdapter.getRole().isApplicationRole()) return false;
RoleEntity role = RoleAdapter.toRoleEntity(roleModel, em);
if (!role.isApplicationRole()) return false;
RoleEntity role = roleAdapter.getRole();
applicationEntity.getRoles().remove(role);
applicationEntity.getDefaultRoles().remove(role);
@ -209,7 +208,8 @@ public class ApplicationAdapter extends ClientAdapter implements ApplicationMode
return;
}
}
entities.add(((RoleAdapter) role).getRole());
RoleEntity roleEntity = RoleAdapter.toRoleEntity(role, em);
entities.add(roleEntity);
em.flush();
}

View file

@ -7,6 +7,7 @@ import org.keycloak.models.RoleModel;
import org.keycloak.models.UserSessionModel;
import org.keycloak.models.jpa.entities.ClientEntity;
import org.keycloak.models.jpa.entities.ClientUserSessionAssociationEntity;
import org.keycloak.models.jpa.entities.RoleEntity;
import org.keycloak.models.jpa.entities.ScopeMappingEntity;
import javax.persistence.EntityManager;
@ -215,7 +216,8 @@ public abstract class ClientAdapter implements ClientModel {
if (hasScope(role)) return;
ScopeMappingEntity entity = new ScopeMappingEntity();
entity.setClient(getEntity());
entity.setRole(((RoleAdapter) role).getRole());
RoleEntity roleEntity = RoleAdapter.toRoleEntity(role, em);
entity.setRole(roleEntity);
em.persist(entity);
em.flush();
em.detach(entity);
@ -223,7 +225,7 @@ public abstract class ClientAdapter implements ClientModel {
@Override
public void deleteScopeMapping(RoleModel role) {
TypedQuery<ScopeMappingEntity> query = getRealmScopeMappingQuery((RoleAdapter) role);
TypedQuery<ScopeMappingEntity> query = getRealmScopeMappingQuery(role);
List<ScopeMappingEntity> results = query.getResultList();
if (results.size() == 0) return;
for (ScopeMappingEntity entity : results) {
@ -231,10 +233,11 @@ public abstract class ClientAdapter implements ClientModel {
}
}
protected TypedQuery<ScopeMappingEntity> getRealmScopeMappingQuery(RoleAdapter role) {
protected TypedQuery<ScopeMappingEntity> getRealmScopeMappingQuery(RoleModel role) {
TypedQuery<ScopeMappingEntity> query = em.createNamedQuery("hasScope", ScopeMappingEntity.class);
query.setParameter("client", getEntity());
query.setParameter("role", ((RoleAdapter) role).getRole());
RoleEntity roleEntity = RoleAdapter.toRoleEntity(role, em);
query.setParameter("role", roleEntity);
return query;
}

View file

@ -527,7 +527,8 @@ public class RealmAdapter implements RealmModel {
return;
}
}
entities.add(((RoleAdapter) role).getRole());
RoleEntity roleEntity = RoleAdapter.toRoleEntity(role, em);
entities.add(roleEntity);
em.flush();
}
@ -924,7 +925,7 @@ public class RealmAdapter implements RealmModel {
}
if (!role.getContainer().equals(this)) return false;
RoleEntity roleEntity = ((RoleAdapter) role).getRole();
RoleEntity roleEntity = RoleAdapter.toRoleEntity(role, em);
realm.getRoles().remove(role);
realm.getDefaultRoles().remove(role);

View file

@ -66,7 +66,7 @@ public class RoleAdapter implements RoleModel {
@Override
public void addCompositeRole(RoleModel role) {
RoleEntity entity = ((RoleAdapter)role).getRole();
RoleEntity entity = RoleAdapter.toRoleEntity(role, em);
for (RoleEntity composite : getRole().getCompositeRoles()) {
if (composite.equals(entity)) return;
}
@ -76,7 +76,7 @@ public class RoleAdapter implements RoleModel {
@Override
public void removeCompositeRole(RoleModel role) {
RoleEntity entity = ((RoleAdapter)role).getRole();
RoleEntity entity = RoleAdapter.toRoleEntity(role, em);
Iterator<RoleEntity> it = getRole().getCompositeRoles().iterator();
while (it.hasNext()) {
if (it.next().equals(entity)) it.remove();
@ -125,4 +125,11 @@ public class RoleAdapter implements RoleModel {
public int hashCode() {
return getId().hashCode();
}
public static RoleEntity toRoleEntity(RoleModel model, EntityManager em) {
if (model instanceof RoleAdapter) {
return ((RoleAdapter)model).getRole();
}
return em.getReference(RoleEntity.class, model.getId());
}
}