KEYCLOAK-1187
This commit is contained in:
parent
a94fd9a098
commit
a9ed193826
5 changed files with 42 additions and 34 deletions
|
@ -58,5 +58,13 @@
|
||||||
|
|
||||||
<renameTable oldTableName="APP_NODE_REGISTRATIONS" newTableName="CLIENT_NODE_REGISTRATIONS"/>
|
<renameTable oldTableName="APP_NODE_REGISTRATIONS" newTableName="CLIENT_NODE_REGISTRATIONS"/>
|
||||||
<renameColumn tableName="CLIENT_NODE_REGISTRATIONS" newColumnName="CLIENT_ID" oldColumnName="APPLICATION_ID"/>
|
<renameColumn tableName="CLIENT_NODE_REGISTRATIONS" newColumnName="CLIENT_ID" oldColumnName="APPLICATION_ID"/>
|
||||||
|
|
||||||
|
<renameColumn tableName="KEYCLOAK_ROLE" newColumnName="CLIENT" oldColumnName="APPLICATION"/>
|
||||||
|
<renameColumn tableName="KEYCLOAK_ROLE" newColumnName="CLIENT_ROLE" oldColumnName="APPLICATION_ROLE"/>
|
||||||
|
<renameColumn tableName="KEYCLOAK_ROLE" newColumnName="CLIENT_REALM_CONSTRAINT" oldColumnName="APP_REALM_CONSTRAINT"/>
|
||||||
|
|
||||||
|
<dropUniqueConstraint tableName="KEYCLOAK_ROLE" constraintName="UK_J3RWUVD56ONTGSUHOGM184WW2"/>
|
||||||
|
<addUniqueConstraint columnNames="NAME,CLIENT_REALM_CONSTRAINT" constraintName="UK_J3RWUVD56ONTGSUHOGM184WW2-2" tableName="KEYCLOAK_ROLE"/>
|
||||||
|
|
||||||
</changeSet>
|
</changeSet>
|
||||||
</databaseChangeLog>
|
</databaseChangeLog>
|
||||||
|
|
|
@ -545,9 +545,9 @@ public class ClientAdapter implements ClientModel {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RoleModel getRole(String name) {
|
public RoleModel getRole(String name) {
|
||||||
TypedQuery<RoleEntity> query = em.createNamedQuery("getAppRoleByName", RoleEntity.class);
|
TypedQuery<RoleEntity> query = em.createNamedQuery("getClientRoleByName", RoleEntity.class);
|
||||||
query.setParameter("name", name);
|
query.setParameter("name", name);
|
||||||
query.setParameter("application", entity);
|
query.setParameter("client", entity);
|
||||||
List<RoleEntity> roles = query.getResultList();
|
List<RoleEntity> roles = query.getResultList();
|
||||||
if (roles.size() == 0) return null;
|
if (roles.size() == 0) return null;
|
||||||
return new RoleAdapter(realm, em, roles.get(0));
|
return new RoleAdapter(realm, em, roles.get(0));
|
||||||
|
@ -563,8 +563,8 @@ public class ClientAdapter implements ClientModel {
|
||||||
RoleEntity roleEntity = new RoleEntity();
|
RoleEntity roleEntity = new RoleEntity();
|
||||||
roleEntity.setId(id);
|
roleEntity.setId(id);
|
||||||
roleEntity.setName(name);
|
roleEntity.setName(name);
|
||||||
roleEntity.setApplication(entity);
|
roleEntity.setClient(entity);
|
||||||
roleEntity.setApplicationRole(true);
|
roleEntity.setClientRole(true);
|
||||||
roleEntity.setRealmId(realm.getId());
|
roleEntity.setRealmId(realm.getId());
|
||||||
em.persist(roleEntity);
|
em.persist(roleEntity);
|
||||||
entity.getRoles().add(roleEntity);
|
entity.getRoles().add(roleEntity);
|
||||||
|
@ -581,13 +581,13 @@ public class ClientAdapter implements ClientModel {
|
||||||
|
|
||||||
session.users().preRemove(getRealm(), roleModel);
|
session.users().preRemove(getRealm(), roleModel);
|
||||||
RoleEntity role = RoleAdapter.toRoleEntity(roleModel, em);
|
RoleEntity role = RoleAdapter.toRoleEntity(roleModel, em);
|
||||||
if (!role.isApplicationRole()) return false;
|
if (!role.isClientRole()) return false;
|
||||||
|
|
||||||
entity.getRoles().remove(role);
|
entity.getRoles().remove(role);
|
||||||
entity.getDefaultRoles().remove(role);
|
entity.getDefaultRoles().remove(role);
|
||||||
em.createNativeQuery("delete from COMPOSITE_ROLE where CHILD_ROLE = :role").setParameter("role", role).executeUpdate();
|
em.createNativeQuery("delete from COMPOSITE_ROLE where CHILD_ROLE = :role").setParameter("role", role).executeUpdate();
|
||||||
em.createNamedQuery("deleteScopeMappingByRole").setParameter("role", role).executeUpdate();
|
em.createNamedQuery("deleteScopeMappingByRole").setParameter("role", role).executeUpdate();
|
||||||
role.setApplication(null);
|
role.setClient(null);
|
||||||
em.flush();
|
em.flush();
|
||||||
em.remove(role);
|
em.remove(role);
|
||||||
em.flush();
|
em.flush();
|
||||||
|
|
|
@ -104,8 +104,8 @@ public class RoleAdapter implements RoleModel {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public RoleContainerModel getContainer() {
|
public RoleContainerModel getContainer() {
|
||||||
if (role.isApplicationRole()) {
|
if (role.isClientRole()) {
|
||||||
return realm.getClientById(role.getApplication().getId());
|
return realm.getClientById(role.getClient().getId());
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
return realm;
|
return realm;
|
||||||
|
|
|
@ -96,7 +96,7 @@ public class ClientEntity {
|
||||||
@Column(name="NODE_REREG_TIMEOUT")
|
@Column(name="NODE_REREG_TIMEOUT")
|
||||||
private int nodeReRegistrationTimeout;
|
private int nodeReRegistrationTimeout;
|
||||||
|
|
||||||
@OneToMany(fetch = FetchType.EAGER, cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "application")
|
@OneToMany(fetch = FetchType.EAGER, cascade ={CascadeType.REMOVE}, orphanRemoval = true, mappedBy = "client")
|
||||||
Collection<RoleEntity> roles = new ArrayList<RoleEntity>();
|
Collection<RoleEntity> roles = new ArrayList<RoleEntity>();
|
||||||
|
|
||||||
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true)
|
@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true)
|
||||||
|
|
|
@ -21,11 +21,11 @@ import java.util.Collection;
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name="KEYCLOAK_ROLE", uniqueConstraints = {
|
@Table(name="KEYCLOAK_ROLE", uniqueConstraints = {
|
||||||
@UniqueConstraint(columnNames = { "NAME", "APP_REALM_CONSTRAINT" })
|
@UniqueConstraint(columnNames = { "NAME", "CLIENT_REALM_CONSTRAINT" })
|
||||||
})
|
})
|
||||||
@NamedQueries({
|
@NamedQueries({
|
||||||
@NamedQuery(name="getAppRoleByName", query="select role from RoleEntity role where role.name = :name and role.application = :application"),
|
@NamedQuery(name="getClientRoleByName", query="select role from RoleEntity role where role.name = :name and role.client = :client"),
|
||||||
@NamedQuery(name="getRealmRoleByName", query="select role from RoleEntity role where role.applicationRole = false and role.name = :name and role.realm = :realm")
|
@NamedQuery(name="getRealmRoleByName", query="select role from RoleEntity role where role.clientRole = false and role.name = :name and role.realm = :realm")
|
||||||
})
|
})
|
||||||
|
|
||||||
public class RoleEntity {
|
public class RoleEntity {
|
||||||
|
@ -46,16 +46,16 @@ public class RoleEntity {
|
||||||
@JoinColumn(name = "REALM")
|
@JoinColumn(name = "REALM")
|
||||||
private RealmEntity realm;
|
private RealmEntity realm;
|
||||||
|
|
||||||
@Column(name="APPLICATION_ROLE")
|
@Column(name="CLIENT_ROLE")
|
||||||
private boolean applicationRole;
|
private boolean clientRole;
|
||||||
|
|
||||||
@ManyToOne(fetch = FetchType.LAZY)
|
@ManyToOne(fetch = FetchType.LAZY)
|
||||||
@JoinColumn(name = "APPLICATION")
|
@JoinColumn(name = "CLIENT")
|
||||||
private ClientEntity application;
|
private ClientEntity client;
|
||||||
|
|
||||||
// Hack to ensure that either name+application or name+realm are unique. Needed due to MS-SQL as it don't allow multiple NULL values in the column, which is part of constraint
|
// Hack to ensure that either name+client or name+realm are unique. Needed due to MS-SQL as it don't allow multiple NULL values in the column, which is part of constraint
|
||||||
@Column(name="APP_REALM_CONSTRAINT", length = 36)
|
@Column(name="CLIENT_REALM_CONSTRAINT", length = 36)
|
||||||
private String appRealmConstraint;
|
private String clientRealmConstraint;
|
||||||
|
|
||||||
@ManyToMany(fetch = FetchType.LAZY, cascade = {})
|
@ManyToMany(fetch = FetchType.LAZY, cascade = {})
|
||||||
@JoinTable(name = "COMPOSITE_ROLE", joinColumns = @JoinColumn(name = "COMPOSITE"), inverseJoinColumns = @JoinColumn(name = "CHILD_ROLE"))
|
@JoinTable(name = "COMPOSITE_ROLE", joinColumns = @JoinColumn(name = "COMPOSITE"), inverseJoinColumns = @JoinColumn(name = "CHILD_ROLE"))
|
||||||
|
@ -101,12 +101,12 @@ public class RoleEntity {
|
||||||
this.compositeRoles = compositeRoles;
|
this.compositeRoles = compositeRoles;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isApplicationRole() {
|
public boolean isClientRole() {
|
||||||
return applicationRole;
|
return clientRole;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setApplicationRole(boolean applicationRole) {
|
public void setClientRole(boolean clientRole) {
|
||||||
this.applicationRole = applicationRole;
|
this.clientRole = clientRole;
|
||||||
}
|
}
|
||||||
|
|
||||||
public RealmEntity getRealm() {
|
public RealmEntity getRealm() {
|
||||||
|
@ -115,26 +115,26 @@ public class RoleEntity {
|
||||||
|
|
||||||
public void setRealm(RealmEntity realm) {
|
public void setRealm(RealmEntity realm) {
|
||||||
this.realm = realm;
|
this.realm = realm;
|
||||||
this.appRealmConstraint = realm.getId();
|
this.clientRealmConstraint = realm.getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
public ClientEntity getApplication() {
|
public ClientEntity getClient() {
|
||||||
return application;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setApplication(ClientEntity application) {
|
public void setClient(ClientEntity client) {
|
||||||
this.application = application;
|
this.client = client;
|
||||||
if (application != null) {
|
if (client != null) {
|
||||||
this.appRealmConstraint = application.getId();
|
this.clientRealmConstraint = client.getId();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getAppRealmConstraint() {
|
public String getClientRealmConstraint() {
|
||||||
return appRealmConstraint;
|
return clientRealmConstraint;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAppRealmConstraint(String appRealmConstraint) {
|
public void setClientRealmConstraint(String clientRealmConstraint) {
|
||||||
this.appRealmConstraint = appRealmConstraint;
|
this.clientRealmConstraint = clientRealmConstraint;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue