Skip to content

Commit

Permalink
KEYCLOAK-1070 add setProtocolMappers/getProtocolMappers to ClientSession
Browse files Browse the repository at this point in the history
  • Loading branch information
mposolda committed Apr 23, 2015
1 parent dae219d commit 535023d
Show file tree
Hide file tree
Showing 27 changed files with 344 additions and 69 deletions.
Expand Up @@ -62,16 +62,26 @@
<constraints nullable="false"/>
</column>
</createTable>
<createTable tableName="CLIENT_SESSION_PROT_MAPPER">
<column name="PROTOCOL_MAPPER_ID" type="VARCHAR(36)">
<constraints nullable="false"/>
</column>
<column name="CLIENT_SESSION" type="VARCHAR(36)">
<constraints nullable="false"/>
</column>
</createTable>
<addPrimaryKey columnNames="ID" constraintName="CONSTRAINT_IDPM" tableName="IDENTITY_PROVIDER_MAPPER"/>
<addPrimaryKey columnNames="IDP_MAPPER_ID, NAME" constraintName="CONSTRAINT_IDPMConfig" tableName="IDP_MAPPER_CONFIG"/>
<addPrimaryKey columnNames="ID" constraintName="CONSTRAINT_GRNTCSNT_PM" tableName="USER_CONSENT"/>
<addPrimaryKey columnNames="USER_CONSENT_ID, ROLE_ID" constraintName="CONSTRAINT_GRNTCSNT_ROLE_PM" tableName="USER_CONSENT_ROLE"/>
<addPrimaryKey columnNames="USER_CONSENT_ID, PROTOCOL_MAPPER_ID" constraintName="CONSTRAINT_GRNTCSNT_PRM_PM" tableName="USER_CONSENT_PROT_MAPPER"/>
<addPrimaryKey columnNames="CLIENT_SESSION, PROTOCOL_MAPPER_ID" constraintName="CONSTRAINT_CS_PMP_PK" tableName="CLIENT_SESSION_PROT_MAPPER"/>
<addForeignKeyConstraint baseColumnNames="REALM_ID" baseTableName="IDENTITY_PROVIDER_MAPPER" constraintName="FK_IDPM_REALM" referencedColumnNames="ID" referencedTableName="REALM"/>
<addForeignKeyConstraint baseColumnNames="IDP_MAPPER_ID" baseTableName="IDP_MAPPER_CONFIG" constraintName="FK_IDPMConfig" referencedColumnNames="ID" referencedTableName="IDENTITY_PROVIDER_MAPPER"/>
<addForeignKeyConstraint baseColumnNames="USER_ID" baseTableName="USER_CONSENT" constraintName="FK_GRNTCSNT_USER" referencedColumnNames="ID" referencedTableName="USER_ENTITY"/>
<addForeignKeyConstraint baseColumnNames="USER_CONSENT_ID" baseTableName="USER_CONSENT_ROLE" constraintName="FK_GRNTCSNT_ROLE_GR" referencedColumnNames="ID" referencedTableName="USER_CONSENT"/>
<addForeignKeyConstraint baseColumnNames="USER_CONSENT_ID" baseTableName="USER_CONSENT_PROT_MAPPER" constraintName="FK_GRNTCSNT_PRM_GR" referencedColumnNames="ID" referencedTableName="USER_CONSENT"/>
<addForeignKeyConstraint baseColumnNames="CLIENT_SESSION" baseTableName="CLIENT_SESSION_PROT_MAPPER" constraintName="FK_33A8SGQW18I532811V7O2DK89" referencedColumnNames="ID" referencedTableName="CLIENT_SESSION"/>

<addColumn tableName="CLIENT">
<column name="CONSENT_REQUIRED" type="BOOLEAN" defaultValueBoolean="false">
Expand Down
Expand Up @@ -27,6 +27,7 @@
<!-- JpaUserSessionProvider -->
<class>org.keycloak.models.sessions.jpa.entities.ClientSessionEntity</class>
<class>org.keycloak.models.sessions.jpa.entities.ClientSessionRoleEntity</class>
<class>org.keycloak.models.sessions.jpa.entities.ClientSessionProtocolMapperEntity</class>
<class>org.keycloak.models.sessions.jpa.entities.ClientSessionNoteEntity</class>
<class>org.keycloak.models.sessions.jpa.entities.UserSessionNoteEntity</class>
<class>org.keycloak.models.sessions.jpa.entities.UserSessionEntity</class>
Expand Down
Expand Up @@ -17,6 +17,9 @@ public String getId() {

@Override
public void update(KeycloakSession session) {
deleteEntries("clientSessions");
deleteEntries("sessions");

convertApplicationsToClients();
convertOAuthClientsToClients();

Expand Down
Expand Up @@ -28,6 +28,9 @@ public interface ClientSessionModel {
public Set<String> getRoles();
public void setRoles(Set<String> roles);

public Set<String> getProtocolMappers();
public void setProtocolMappers(Set<String> protocolMappers);

/**
* Authentication request type, i.e. OAUTH, SAML 2.0, SAML 1.1, etc.
*
Expand Down
28 changes: 6 additions & 22 deletions model/api/src/main/java/org/keycloak/models/UserConsentModel.java
Expand Up @@ -8,31 +8,20 @@
*/
public class UserConsentModel {

private final RealmModel realm;
private final ClientModel client;
private Set<ProtocolMapperModel> protocolMappers = new HashSet<ProtocolMapperModel>();
private Set<RoleModel> roles = new HashSet<RoleModel>();

public UserConsentModel(RealmModel realm, String clientId) {
this.realm = realm;
this.client = realm.getClientById(clientId);

if (client == null) {
throw new ModelException("Client with id [" + clientId + "] is not available");
}
public UserConsentModel(ClientModel client) {
this.client = client;
}

public ClientModel getClient() {
return client;
}

public void addGrantedRole(String roleId) {
RoleModel role = realm.getRoleById(roleId);

// Chance that role was already deleted by other transaction and is not available anymore
if (role != null) {
roles.add(role);
}
public void addGrantedRole(RoleModel role) {
roles.add(role);
}

public Set<RoleModel> getGrantedRoles() {
Expand All @@ -46,13 +35,8 @@ public boolean isRoleGranted(RoleModel role) {
return false;
}

public void addGrantedProtocolMapper(String protocolMapperId) {
ProtocolMapperModel protocolMapper = client.getProtocolMapperById(protocolMapperId);

// Chance that protocolMapper was already deleted by other transaction and is not available anymore
if (protocolMapper != null) {
protocolMappers.add(protocolMapper);
}
public void addGrantedProtocolMapper(ProtocolMapperModel protocolMapper) {
protocolMappers.add(protocolMapper);
}

public Set<ProtocolMapperModel> getGrantedProtocolMappers() {
Expand Down
Expand Up @@ -792,23 +792,25 @@ public static UserModel createUser(KeycloakSession session, RealmModel newRealm,
throw new RuntimeException("Unable to find client consent mappings for client: " + entry.getKey());
}

UserConsentModel consentModel = new UserConsentModel(newRealm, client.getId());
UserConsentModel consentModel = new UserConsentModel(client);

UserConsentRepresentation consentRep = entry.getValue();
if (consentRep.getGrantedRoles() != null) {
for (String roleId : consentRep.getGrantedRoles()) {
if (newRealm.getRoleById(roleId) == null) {
RoleModel role = newRealm.getRoleById(roleId);
if (role == null) {
throw new RuntimeException("Unable to find realm role referenced in consent mappings of user " + user.getUsername() + ". Role ID: " + roleId);
}
consentModel.addGrantedRole(roleId);
consentModel.addGrantedRole(role);
}
}
if (consentRep.getGrantedProtocolMappers() != null) {
for (String mapperId : consentRep.getGrantedProtocolMappers()) {
if (client.getProtocolMapperById(mapperId) == null) {
ProtocolMapperModel protocolMapper = client.getProtocolMapperById(mapperId);
if (protocolMapper == null) {
throw new RuntimeException("Unable to find protocol mapper referenced in consent mappings of user " + user.getUsername() + ". Protocol mapper ID: " + mapperId);
}
consentModel.addGrantedProtocolMapper(mapperId);;
consentModel.addGrantedProtocolMapper(protocolMapper);
}
}
user.addConsent(consentModel);
Expand Down
14 changes: 11 additions & 3 deletions model/jpa/src/main/java/org/keycloak/models/jpa/UserAdapter.java
Expand Up @@ -561,19 +561,27 @@ private UserConsentModel toConsentModel(UserConsentEntity entity) {
return null;
}

UserConsentModel model = new UserConsentModel(realm, entity.getClientId());
ClientModel client = realm.getClientById(entity.getClientId());
if (client == null) {
throw new ModelException("Client with id " + entity.getClientId() + " is not available");
}
UserConsentModel model = new UserConsentModel(client);

Collection<UserConsentRoleEntity> grantedRoleEntities = entity.getGrantedRoles();
if (grantedRoleEntities != null) {
for (UserConsentRoleEntity grantedRole : grantedRoleEntities) {
model.addGrantedRole(grantedRole.getRoleId());
RoleModel grantedRoleModel = realm.getRoleById(grantedRole.getRoleId());
if (grantedRoleModel != null) {
model.addGrantedRole(grantedRoleModel);
}
}
}

Collection<UserConsentProtocolMapperEntity> grantedProtocolMapperEntities = entity.getGrantedProtocolMappers();
if (grantedProtocolMapperEntities != null) {
for (UserConsentProtocolMapperEntity grantedProtMapper : grantedProtocolMapperEntities) {
model.addGrantedProtocolMapper(grantedProtMapper.getProtocolMapperId());
ProtocolMapperModel protocolMapper = client.getProtocolMapperById(grantedProtMapper.getProtocolMapperId());
model.addGrantedProtocolMapper(protocolMapper );
}
}

Expand Down
Expand Up @@ -462,12 +462,22 @@ private MongoUserConsentEntity getConsentEntityByClientId(String clientId) {
}

private UserConsentModel toConsentModel(UserConsentEntity entity) {
UserConsentModel model = new UserConsentModel(realm, entity.getClientId());
ClientModel client = realm.getClientById(entity.getClientId());
if (client == null) {
throw new ModelException("Client with id " + entity.getClientId() + " is not available");
}
UserConsentModel model = new UserConsentModel(client);

for (String roleId : entity.getGrantedRoles()) {
model.addGrantedRole(roleId);
RoleModel roleModel = realm.getRoleById(roleId);
if (roleModel != null) {
model.addGrantedRole(roleModel);
}
}

for (String protMapperId : entity.getGrantedProtocolMappers()) {
model.addGrantedProtocolMapper(protMapperId);
ProtocolMapperModel protocolMapper = client.getProtocolMapperById(protMapperId);
model.addGrantedProtocolMapper(protocolMapper);
}
return model;
}
Expand Down
Expand Up @@ -118,6 +118,17 @@ public void setRoles(Set<String> roles) {
update();
}

@Override
public Set<String> getProtocolMappers() {
return entity.getProtocolMappers();
}

@Override
public void setProtocolMappers(Set<String> protocolMappers) {
entity.setProtocolMappers(protocolMappers);
update();
}

@Override
public String getAuthMethod() {
return entity.getAuthMethod();
Expand Down
Expand Up @@ -25,6 +25,7 @@ public class ClientSessionEntity extends SessionEntity {
private ClientSessionModel.Action action;

private Set<String> roles;
private Set<String> protocolMappers;
private Map<String, String> notes;

public String getClient() {
Expand Down Expand Up @@ -91,6 +92,14 @@ public void setRoles(Set<String> roles) {
this.roles = roles;
}

public Set<String> getProtocolMappers() {
return protocolMappers;
}

public void setProtocolMappers(Set<String> protocolMappers) {
this.protocolMappers = protocolMappers;
}

public Map<String, String> getNotes() {
return notes;
}
Expand Down
Expand Up @@ -7,6 +7,7 @@
import org.keycloak.models.UserSessionModel;
import org.keycloak.models.sessions.jpa.entities.ClientSessionEntity;
import org.keycloak.models.sessions.jpa.entities.ClientSessionNoteEntity;
import org.keycloak.models.sessions.jpa.entities.ClientSessionProtocolMapperEntity;
import org.keycloak.models.sessions.jpa.entities.ClientSessionRoleEntity;
import org.keycloak.models.sessions.jpa.entities.UserSessionEntity;

Expand Down Expand Up @@ -177,4 +178,36 @@ public Set<String> getRoles() {
}
return roles;
}

@Override
public Set<String> getProtocolMappers() {
Set<String> protMappers = new HashSet<String>();
if (entity.getProtocolMappers() != null) {
for (ClientSessionProtocolMapperEntity e : entity.getProtocolMappers()) {
protMappers.add(e.getProtocolMapperId());
}
}
return protMappers;
}

@Override
public void setProtocolMappers(Set<String> protocolMappers) {
if (protocolMappers != null) {
for (String pm : protocolMappers) {
ClientSessionProtocolMapperEntity protMapperEntity = new ClientSessionProtocolMapperEntity();
protMapperEntity.setClientSession(entity);
protMapperEntity.setProtocolMapperId(pm);
em.persist(protMapperEntity);

entity.getProtocolMappers().add(protMapperEntity);
}
} else {
if (entity.getProtocolMappers() != null) {
for (ClientSessionProtocolMapperEntity pm : entity.getProtocolMappers()) {
em.remove(pm);
}
entity.getProtocolMappers().clear();
}
}
}
}
Expand Up @@ -214,6 +214,10 @@ public void removeUserSessions(RealmModel realm, UserModel user) {
.setParameter("realmId", realm.getId())
.setParameter("userId", user.getId())
.executeUpdate();
em.createNamedQuery("removeClientSessionProtMapperByUser")
.setParameter("realmId", realm.getId())
.setParameter("userId", user.getId())
.executeUpdate();
em.createNamedQuery("removeClientSessionByUser")
.setParameter("realmId", realm.getId())
.setParameter("userId", user.getId())
Expand All @@ -238,6 +242,10 @@ public void removeExpiredUserSessions(RealmModel realm) {
.setParameter("realmId", realm.getId())
.setParameter("maxTime", dettachedClientSessionExpired)
.executeUpdate();
em.createNamedQuery("removeDetachedClientSessionProtMapperByExpired")
.setParameter("realmId", realm.getId())
.setParameter("maxTime", dettachedClientSessionExpired)
.executeUpdate();
em.createNamedQuery("removeDetachedClientSessionNoteByExpired")
.setParameter("realmId", realm.getId())
.setParameter("maxTime", dettachedClientSessionExpired)
Expand All @@ -251,6 +259,11 @@ public void removeExpiredUserSessions(RealmModel realm) {
.setParameter("maxTime", maxTime)
.setParameter("idleTime", idleTime)
.executeUpdate();
em.createNamedQuery("removeClientSessionProtMapperByExpired")
.setParameter("realmId", realm.getId())
.setParameter("maxTime", maxTime)
.setParameter("idleTime", idleTime)
.executeUpdate();
em.createNamedQuery("removeClientSessionNoteByExpired")
.setParameter("realmId", realm.getId())
.setParameter("maxTime", maxTime)
Expand All @@ -277,6 +290,7 @@ public void removeExpiredUserSessions(RealmModel realm) {
public void removeUserSessions(RealmModel realm) {
em.createNamedQuery("removeClientSessionNoteByRealm").setParameter("realmId", realm.getId()).executeUpdate();
em.createNamedQuery("removeClientSessionRoleByRealm").setParameter("realmId", realm.getId()).executeUpdate();
em.createNamedQuery("removeClientSessionProtMapperByRealm").setParameter("realmId", realm.getId()).executeUpdate();
em.createNamedQuery("removeClientSessionByRealm").setParameter("realmId", realm.getId()).executeUpdate();
em.createNamedQuery("removeUserSessionNoteByRealm").setParameter("realmId", realm.getId()).executeUpdate();
em.createNamedQuery("removeUserSessionByRealm").setParameter("realmId", realm.getId()).executeUpdate();
Expand All @@ -292,6 +306,7 @@ public void onRealmRemoved(RealmModel realm) {
public void onClientRemoved(RealmModel realm, ClientModel client) {
em.createNamedQuery("removeClientSessionNoteByClient").setParameter("realmId", realm.getId()).setParameter("clientId", client.getId()).executeUpdate();
em.createNamedQuery("removeClientSessionRoleByClient").setParameter("realmId", realm.getId()).setParameter("clientId", client.getId()).executeUpdate();
em.createNamedQuery("removeClientSessionProtMapperByClient").setParameter("realmId", realm.getId()).setParameter("clientId", client.getId()).executeUpdate();
em.createNamedQuery("removeClientSessionByClient").setParameter("realmId", realm.getId()).setParameter("clientId", client.getId()).executeUpdate();
}

Expand Down
Expand Up @@ -60,6 +60,9 @@ public class ClientSessionEntity {
@OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy="clientSession")
protected Collection<ClientSessionRoleEntity> roles = new ArrayList<ClientSessionRoleEntity>();

@OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy="clientSession")
protected Collection<ClientSessionProtocolMapperEntity> protocolMappers = new ArrayList<ClientSessionProtocolMapperEntity>();

@OneToMany(cascade = CascadeType.REMOVE, orphanRemoval = true, mappedBy="clientSession")
protected Collection<ClientSessionNoteEntity> notes = new ArrayList<ClientSessionNoteEntity>();

Expand Down Expand Up @@ -127,6 +130,14 @@ public void setRoles(Collection<ClientSessionRoleEntity> roles) {
this.roles = roles;
}

public Collection<ClientSessionProtocolMapperEntity> getProtocolMappers() {
return protocolMappers;
}

public void setProtocolMappers(Collection<ClientSessionProtocolMapperEntity> protocolMappers) {
this.protocolMappers = protocolMappers;
}

public Collection<ClientSessionNoteEntity> getNotes() {
return notes;
}
Expand Down

0 comments on commit 535023d

Please sign in to comment.