Skip to content

Commit

Permalink
KEYCLOAK-1187
Browse files Browse the repository at this point in the history
  • Loading branch information
stianst committed Apr 14, 2015
1 parent a94fd9a commit a9ed193
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 34 deletions.
Expand Up @@ -58,5 +58,13 @@

<renameTable oldTableName="APP_NODE_REGISTRATIONS" newTableName="CLIENT_NODE_REGISTRATIONS"/>
<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>
</databaseChangeLog>
Expand Up @@ -545,9 +545,9 @@ public void setDirectGrantsOnly(boolean flag) {

@Override
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("application", entity);
query.setParameter("client", entity);
List<RoleEntity> roles = query.getResultList();
if (roles.size() == 0) return null;
return new RoleAdapter(realm, em, roles.get(0));
Expand All @@ -563,8 +563,8 @@ public RoleModel addRole(String id, String name) {
RoleEntity roleEntity = new RoleEntity();
roleEntity.setId(id);
roleEntity.setName(name);
roleEntity.setApplication(entity);
roleEntity.setApplicationRole(true);
roleEntity.setClient(entity);
roleEntity.setClientRole(true);
roleEntity.setRealmId(realm.getId());
em.persist(roleEntity);
entity.getRoles().add(roleEntity);
Expand All @@ -581,13 +581,13 @@ public boolean removeRole(RoleModel roleModel) {

session.users().preRemove(getRealm(), roleModel);
RoleEntity role = RoleAdapter.toRoleEntity(roleModel, em);
if (!role.isApplicationRole()) return false;
if (!role.isClientRole()) return false;

entity.getRoles().remove(role);
entity.getDefaultRoles().remove(role);
em.createNativeQuery("delete from COMPOSITE_ROLE where CHILD_ROLE = :role").setParameter("role", role).executeUpdate();
em.createNamedQuery("deleteScopeMappingByRole").setParameter("role", role).executeUpdate();
role.setApplication(null);
role.setClient(null);
em.flush();
em.remove(role);
em.flush();
Expand Down
Expand Up @@ -104,8 +104,8 @@ public boolean hasRole(RoleModel role) {

@Override
public RoleContainerModel getContainer() {
if (role.isApplicationRole()) {
return realm.getClientById(role.getApplication().getId());
if (role.isClientRole()) {
return realm.getClientById(role.getClient().getId());

} else {
return realm;
Expand Down
Expand Up @@ -96,7 +96,7 @@ public class ClientEntity {
@Column(name="NODE_REREG_TIMEOUT")
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>();

@OneToMany(fetch = FetchType.LAZY, cascade ={CascadeType.REMOVE}, orphanRemoval = true)
Expand Down
Expand Up @@ -21,11 +21,11 @@
*/
@Entity
@Table(name="KEYCLOAK_ROLE", uniqueConstraints = {
@UniqueConstraint(columnNames = { "NAME", "APP_REALM_CONSTRAINT" })
@UniqueConstraint(columnNames = { "NAME", "CLIENT_REALM_CONSTRAINT" })
})
@NamedQueries({
@NamedQuery(name="getAppRoleByName", query="select role from RoleEntity role where role.name = :name and role.application = :application"),
@NamedQuery(name="getRealmRoleByName", query="select role from RoleEntity role where role.applicationRole = false and role.name = :name and role.realm = :realm")
@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.clientRole = false and role.name = :name and role.realm = :realm")
})

public class RoleEntity {
Expand All @@ -46,16 +46,16 @@ public class RoleEntity {
@JoinColumn(name = "REALM")
private RealmEntity realm;

@Column(name="APPLICATION_ROLE")
private boolean applicationRole;
@Column(name="CLIENT_ROLE")
private boolean clientRole;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "APPLICATION")
private ClientEntity application;
@JoinColumn(name = "CLIENT")
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
@Column(name="APP_REALM_CONSTRAINT", length = 36)
private String appRealmConstraint;
// 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="CLIENT_REALM_CONSTRAINT", length = 36)
private String clientRealmConstraint;

@ManyToMany(fetch = FetchType.LAZY, cascade = {})
@JoinTable(name = "COMPOSITE_ROLE", joinColumns = @JoinColumn(name = "COMPOSITE"), inverseJoinColumns = @JoinColumn(name = "CHILD_ROLE"))
Expand Down Expand Up @@ -101,12 +101,12 @@ public void setCompositeRoles(Collection<RoleEntity> compositeRoles) {
this.compositeRoles = compositeRoles;
}

public boolean isApplicationRole() {
return applicationRole;
public boolean isClientRole() {
return clientRole;
}

public void setApplicationRole(boolean applicationRole) {
this.applicationRole = applicationRole;
public void setClientRole(boolean clientRole) {
this.clientRole = clientRole;
}

public RealmEntity getRealm() {
Expand All @@ -115,26 +115,26 @@ public RealmEntity getRealm() {

public void setRealm(RealmEntity realm) {
this.realm = realm;
this.appRealmConstraint = realm.getId();
this.clientRealmConstraint = realm.getId();
}

public ClientEntity getApplication() {
return application;
public ClientEntity getClient() {
return client;
}

public void setApplication(ClientEntity application) {
this.application = application;
if (application != null) {
this.appRealmConstraint = application.getId();
public void setClient(ClientEntity client) {
this.client = client;
if (client != null) {
this.clientRealmConstraint = client.getId();
}
}

public String getAppRealmConstraint() {
return appRealmConstraint;
public String getClientRealmConstraint() {
return clientRealmConstraint;
}

public void setAppRealmConstraint(String appRealmConstraint) {
this.appRealmConstraint = appRealmConstraint;
public void setClientRealmConstraint(String clientRealmConstraint) {
this.clientRealmConstraint = clientRealmConstraint;
}

@Override
Expand Down

0 comments on commit a9ed193

Please sign in to comment.