KEYCLOAK-1187 DB migration support for oauth/application to client

This commit is contained in:
Stian Thorgersen 2015-04-10 09:21:34 +02:00
parent 1567982f0b
commit 4ae9310f83
20 changed files with 131 additions and 153 deletions

View file

@ -1,84 +0,0 @@
package org.keycloak.connections.jpa.updater.liquibase.custom;
import liquibase.change.custom.CustomSqlChange;
import liquibase.database.Database;
import liquibase.database.jvm.JdbcConnection;
import liquibase.exception.CustomChangeException;
import liquibase.exception.SetupException;
import liquibase.exception.ValidationErrors;
import liquibase.resource.ResourceAccessor;
import liquibase.snapshot.SnapshotGeneratorFactory;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.UpdateStatement;
import liquibase.structure.core.Table;
import org.keycloak.models.utils.KeycloakModelUtils;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
/**
* @author <a href="mailto:sthorger@redhat.com">Stian Thorgersen</a>
*/
public class SetConsentRequiredOnOAuthClients implements CustomSqlChange {
private String confirmationMessage;
@Override
public SqlStatement[] generateStatements(Database database) throws CustomChangeException {
try {
StringBuilder sb = new StringBuilder();
sb.append("Set consent required for: ");
Connection connection = ((JdbcConnection) (database.getConnection())).getWrappedConnection();
ArrayList<SqlStatement> statements = new ArrayList<SqlStatement>();
String correctedTableName = database.correctObjectName("CLIENT", Table.class);
if (SnapshotGeneratorFactory.getInstance().has(new Table().setName(correctedTableName), database)) {
ResultSet resultSet = connection.createStatement().executeQuery("SELECT * FROM CLIENT");
while (resultSet.next()) {
String id = resultSet.getString(1);
UpdateStatement statement = new UpdateStatement(null, null, correctedTableName)
.addNewColumnValue("CONSENT_REQUIRED", true)
.setWhereClause("ID='" + id + "'");
statements.add(statement);
if (!resultSet.isFirst()) {
sb.append(", ");
}
sb.append(id);
}
if (!statements.isEmpty()) {
confirmationMessage = sb.toString();
}
}
return statements.toArray(new SqlStatement[statements.size()]);
} catch (Exception e) {
throw new CustomChangeException("Failed to add realm code secret", e);
}
}
@Override
public String getConfirmationMessage() {
return confirmationMessage;
}
@Override
public void setUp() throws SetupException {
}
@Override
public void setFileOpener(ResourceAccessor resourceAccessor) {
}
@Override
public ValidationErrors validate(Database database) {
return null;
}
}

View file

@ -42,6 +42,11 @@
<constraints nullable="false"/> <constraints nullable="false"/>
</column> </column>
</addColumn> </addColumn>
<update tableName="CLIENT">
<column name="CONSENT_REQUIRED" valueBoolean="true"/>
<where>DTYPE = 'OAuthClientEntity'</where>
</update>
<dropColumn tableName="CLIENT" columnName="DTYPE"/> <dropColumn tableName="CLIENT" columnName="DTYPE"/>
<renameColumn tableName="CLIENT" newColumnName="CLIENT_ID" oldColumnName="NAME"/>
</changeSet> </changeSet>
</databaseChangeLog> </databaseChangeLog>

View file

@ -10,6 +10,7 @@ import org.keycloak.connections.mongo.updater.impl.updates.Update;
import org.keycloak.connections.mongo.updater.impl.updates.Update1_0_0_Final; import org.keycloak.connections.mongo.updater.impl.updates.Update1_0_0_Final;
import org.keycloak.connections.mongo.updater.impl.updates.Update1_1_0_Beta1; import org.keycloak.connections.mongo.updater.impl.updates.Update1_1_0_Beta1;
import org.keycloak.connections.mongo.updater.impl.updates.Update1_2_0_Beta1; import org.keycloak.connections.mongo.updater.impl.updates.Update1_2_0_Beta1;
import org.keycloak.connections.mongo.updater.impl.updates.Update1_2_0_RC1;
import org.keycloak.models.KeycloakSession; import org.keycloak.models.KeycloakSession;
import java.util.Date; import java.util.Date;
@ -28,7 +29,8 @@ public class DefaultMongoUpdaterProvider implements MongoUpdaterProvider {
private Class<? extends Update>[] updates = new Class[]{ private Class<? extends Update>[] updates = new Class[]{
Update1_0_0_Final.class, Update1_0_0_Final.class,
Update1_1_0_Beta1.class, Update1_1_0_Beta1.class,
Update1_2_0_Beta1.class Update1_2_0_Beta1.class,
Update1_2_0_RC1.class
}; };
@Override @Override

View file

@ -53,15 +53,8 @@ public abstract class Update {
log.debugv("Deleted entries from {0}", collection); log.debugv("Deleted entries from {0}", collection);
} }
protected String insertApplicationRole(DBCollection roles, String roleName, String applicationId) { protected void renameCollection(String collection, String newName) {
BasicDBObject role = new BasicDBObject(); db.getCollection(collection).rename(newName);
String roleId = KeycloakModelUtils.generateId();
role.append("_id", roleId);
role.append("name", roleName);
role.append("applicationId", applicationId);
role.append("nameIndex", applicationId + "//" + roleName);
roles.insert(role);
return roleId;
} }
public void setLog(Logger log) { public void setLog(Logger log) {

View file

@ -266,4 +266,15 @@ public class Update1_2_0_Beta1 extends Update {
} }
} }
private String insertApplicationRole(DBCollection roles, String roleName, String applicationId) {
BasicDBObject role = new BasicDBObject();
String roleId = KeycloakModelUtils.generateId();
role.append("_id", roleId);
role.append("name", roleName);
role.append("applicationId", applicationId);
role.append("nameIndex", applicationId + "//" + roleName);
roles.insert(role);
return roleId;
}
} }

View file

@ -0,0 +1,51 @@
package org.keycloak.connections.mongo.updater.impl.updates;
import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import org.keycloak.models.KeycloakSession;
/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class Update1_2_0_RC1 extends Update {
@Override
public String getId() {
return "1.2.0.RC1";
}
@Override
public void update(KeycloakSession session) {
convertApplicationsToClients();
convertOAuthClientsToClients();
}
private void convertApplicationsToClients() {
DBCollection applications = db.getCollection("applications");
applications.update(new BasicDBObject(), new BasicDBObject("$set", new BasicDBObject("consentRequired", false)), false, true);
applications.update(new BasicDBObject(), new BasicDBObject("$rename", new BasicDBObject("name", "clientId")), false, true);
renameCollection("applications", "clients");
log.debugv("Converted applications to clients");
DBCollection roles = db.getCollection("roles");
roles.update(new BasicDBObject(), new BasicDBObject("$rename", new BasicDBObject("applicationId", "clientId")), false, true);
log.debugv("Renamed roles.applicationId to roles.clientId");
}
private void convertOAuthClientsToClients() {
DBCollection clients = db.getCollection("clients");
DBCollection oauthClients = db.getCollection("oauthClients");
oauthClients.update(new BasicDBObject(), new BasicDBObject("$rename", new BasicDBObject("name", "clientId")), false, true);
oauthClients.update(new BasicDBObject(), new BasicDBObject("$set", new BasicDBObject("consentRequired", true)), false, true);
DBCursor curs = oauthClients.find();
while (curs.hasNext()) {
clients.insert(curs.next());
}
oauthClients.drop();
log.debugv("Converted oauthClients to clients");
}
}

View file

@ -8,9 +8,9 @@ import java.util.Map;
/** /**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a> * @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/ */
public class ApplicationEntity extends AbstractIdentifiableEntity { public class ClientEntity extends AbstractIdentifiableEntity {
private String name; private String clientId;
private String realmId; private String realmId;
private boolean enabled; private boolean enabled;
private String secret; private String secret;
@ -41,12 +41,12 @@ public class ApplicationEntity extends AbstractIdentifiableEntity {
private List<ClientIdentityProviderMappingEntity> identityProviders = new ArrayList<ClientIdentityProviderMappingEntity>(); private List<ClientIdentityProviderMappingEntity> identityProviders = new ArrayList<ClientIdentityProviderMappingEntity>();
private List<ProtocolMapperEntity> protocolMappers = new ArrayList<ProtocolMapperEntity>(); private List<ProtocolMapperEntity> protocolMappers = new ArrayList<ProtocolMapperEntity>();
public String getName() { public String getClientId() {
return name; return clientId;
} }
public void setName(String name) { public void setClientId(String clientId) {
this.name = name; this.clientId = clientId;
} }
public boolean isEnabled() { public boolean isEnabled() {

View file

@ -33,7 +33,7 @@ import java.util.Set;
import org.keycloak.connections.file.InMemoryModel; import org.keycloak.connections.file.InMemoryModel;
import org.keycloak.models.ModelDuplicateException; import org.keycloak.models.ModelDuplicateException;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import org.keycloak.models.entities.ApplicationEntity; import org.keycloak.models.entities.ClientEntity;
import org.keycloak.models.entities.ClientIdentityProviderMappingEntity; import org.keycloak.models.entities.ClientIdentityProviderMappingEntity;
import org.keycloak.models.entities.ProtocolMapperEntity; import org.keycloak.models.entities.ProtocolMapperEntity;
import org.keycloak.models.entities.RoleEntity; import org.keycloak.models.entities.RoleEntity;
@ -48,13 +48,13 @@ public class ClientAdapter implements ClientModel {
private final RealmModel realm; private final RealmModel realm;
private KeycloakSession session; private KeycloakSession session;
private final ApplicationEntity entity; private final ClientEntity entity;
private final InMemoryModel inMemoryModel; private final InMemoryModel inMemoryModel;
private final Map<String, RoleAdapter> allRoles = new HashMap<String, RoleAdapter>(); private final Map<String, RoleAdapter> allRoles = new HashMap<String, RoleAdapter>();
private final Map<String, RoleModel> allScopeMappings = new HashMap<String, RoleModel>(); private final Map<String, RoleModel> allScopeMappings = new HashMap<String, RoleModel>();
public ClientAdapter(KeycloakSession session, RealmModel realm, ApplicationEntity entity, InMemoryModel inMemoryModel) { public ClientAdapter(KeycloakSession session, RealmModel realm, ClientEntity entity, InMemoryModel inMemoryModel) {
this.realm = realm; this.realm = realm;
this.session = session; this.session = session;
this.entity = entity; this.entity = entity;
@ -406,13 +406,13 @@ public class ClientAdapter implements ClientModel {
@Override @Override
public String getClientId() { public String getClientId() {
return entity.getName(); return entity.getClientId();
} }
@Override @Override
public void setClientId(String clientId) { public void setClientId(String clientId) {
if (appNameExists(clientId)) throw new ModelDuplicateException("Application named " + clientId + " already exists."); if (appNameExists(clientId)) throw new ModelDuplicateException("Application named " + clientId + " already exists.");
entity.setName(clientId); entity.setClientId(clientId);
} }
private boolean appNameExists(String name) { private boolean appNameExists(String name) {

View file

@ -29,7 +29,7 @@ import org.keycloak.models.RequiredCredentialModel;
import org.keycloak.models.RoleModel; import org.keycloak.models.RoleModel;
import org.keycloak.models.UserFederationProviderModel; import org.keycloak.models.UserFederationProviderModel;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import org.keycloak.models.entities.ApplicationEntity; import org.keycloak.models.entities.ClientEntity;
import org.keycloak.models.entities.IdentityProviderMapperEntity; import org.keycloak.models.entities.IdentityProviderMapperEntity;
import org.keycloak.models.entities.RealmEntity; import org.keycloak.models.entities.RealmEntity;
import org.keycloak.models.entities.RequiredCredentialEntity; import org.keycloak.models.entities.RequiredCredentialEntity;
@ -626,9 +626,9 @@ public class RealmAdapter implements RealmModel {
throw new ModelDuplicateException("Application named '" + clientId + "' already exists."); throw new ModelDuplicateException("Application named '" + clientId + "' already exists.");
} }
ApplicationEntity appEntity = new ApplicationEntity(); ClientEntity appEntity = new ClientEntity();
appEntity.setId(id); appEntity.setId(id);
appEntity.setName(clientId); appEntity.setClientId(clientId);
appEntity.setRealmId(getId()); appEntity.setRealmId(getId());
appEntity.setEnabled(true); appEntity.setEnabled(true);

View file

@ -475,12 +475,12 @@ public class ClientAdapter implements ClientModel {
@Override @Override
public String getClientId() { public String getClientId() {
return entity.getName(); return entity.getClientId();
} }
@Override @Override
public void setClientId(String clientId) { public void setClientId(String clientId) {
entity.setName(clientId); entity.setClientId(clientId);
} }
@Override @Override

View file

@ -635,7 +635,7 @@ public class RealmAdapter implements RealmModel {
public ClientModel addClient(String id, String clientId) { public ClientModel addClient(String id, String clientId) {
ClientEntity applicationData = new ClientEntity(); ClientEntity applicationData = new ClientEntity();
applicationData.setId(id); applicationData.setId(id);
applicationData.setName(clientId); applicationData.setClientId(clientId);
applicationData.setEnabled(true); applicationData.setEnabled(true);
applicationData.setRealm(realm); applicationData.setRealm(realm);
realm.getApplications().add(applicationData); realm.getApplications().add(applicationData);

View file

@ -26,14 +26,14 @@ import java.util.Set;
* @version $Revision: 1 $ * @version $Revision: 1 $
*/ */
@Entity @Entity
@Table(name="CLIENT", uniqueConstraints = {@UniqueConstraint(columnNames = {"REALM_ID", "NAME"})}) @Table(name="CLIENT", uniqueConstraints = {@UniqueConstraint(columnNames = {"REALM_ID", "CLIENT_ID"})})
public class ClientEntity { public class ClientEntity {
@Id @Id
@Column(name="ID", length = 36) @Column(name="ID", length = 36)
private String id; private String id;
@Column(name = "NAME") @Column(name = "CLIENT_ID")
private String name; private String clientId;
@Column(name="ENABLED") @Column(name="ENABLED")
private boolean enabled; private boolean enabled;
@Column(name="SECRET") @Column(name="SECRET")
@ -133,12 +133,12 @@ public class ClientEntity {
this.enabled = enabled; this.enabled = enabled;
} }
public String getName() { public String getClientId() {
return name; return clientId;
} }
public void setName(String name) { public void setClientId(String clientId) {
this.name = name; this.clientId = clientId;
} }
public Set<String> getWebOrigins() { public Set<String> getWebOrigins() {

View file

@ -11,7 +11,7 @@ import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleModel; import org.keycloak.models.RoleModel;
import org.keycloak.models.entities.ClientIdentityProviderMappingEntity; import org.keycloak.models.entities.ClientIdentityProviderMappingEntity;
import org.keycloak.models.entities.ProtocolMapperEntity; import org.keycloak.models.entities.ProtocolMapperEntity;
import org.keycloak.models.mongo.keycloak.entities.MongoApplicationEntity; import org.keycloak.models.mongo.keycloak.entities.MongoClientEntity;
import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity; import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity;
import org.keycloak.models.mongo.utils.MongoModelUtils; import org.keycloak.models.mongo.utils.MongoModelUtils;
import org.keycloak.models.utils.KeycloakModelUtils; import org.keycloak.models.utils.KeycloakModelUtils;
@ -27,13 +27,13 @@ import java.util.Set;
/** /**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a> * @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/ */
public class ClientAdapter extends AbstractMongoAdapter<MongoApplicationEntity> implements ClientModel { public class ClientAdapter extends AbstractMongoAdapter<MongoClientEntity> implements ClientModel {
protected final MongoApplicationEntity applicationEntity; protected final MongoClientEntity applicationEntity;
private final RealmModel realm; private final RealmModel realm;
protected KeycloakSession session; protected KeycloakSession session;
public ClientAdapter(KeycloakSession session, RealmModel realm, MongoApplicationEntity applicationEntity, MongoStoreInvocationContext invContext) { public ClientAdapter(KeycloakSession session, RealmModel realm, MongoClientEntity applicationEntity, MongoStoreInvocationContext invContext) {
super(invContext); super(invContext);
this.session = session; this.session = session;
this.realm = realm; this.realm = realm;
@ -41,7 +41,7 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoApplicationEntity>
} }
@Override @Override
public MongoApplicationEntity getMongoEntity() { public MongoClientEntity getMongoEntity() {
return applicationEntity; return applicationEntity;
} }
@ -58,12 +58,12 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoApplicationEntity>
@Override @Override
public String getClientId() { public String getClientId() {
return getMongoEntity().getName(); return getMongoEntity().getClientId();
} }
@Override @Override
public void setClientId(String clientId) { public void setClientId(String clientId) {
getMongoEntity().setName(clientId); getMongoEntity().setClientId(clientId);
updateMongoEntity(); updateMongoEntity();
} }
@ -507,7 +507,7 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoApplicationEntity>
public RoleAdapter getRole(String name) { public RoleAdapter getRole(String name) {
DBObject query = new QueryBuilder() DBObject query = new QueryBuilder()
.and("name").is(name) .and("name").is(name)
.and("applicationId").is(getId()) .and("clientId").is(getId())
.get(); .get();
MongoRoleEntity role = getMongoStore().loadSingleEntity(MongoRoleEntity.class, query, invocationContext); MongoRoleEntity role = getMongoStore().loadSingleEntity(MongoRoleEntity.class, query, invocationContext);
if (role == null) { if (role == null) {
@ -543,7 +543,7 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoApplicationEntity>
@Override @Override
public Set<RoleModel> getRoles() { public Set<RoleModel> getRoles() {
DBObject query = new QueryBuilder() DBObject query = new QueryBuilder()
.and("applicationId").is(getId()) .and("clientId").is(getId())
.get(); .get();
List<MongoRoleEntity> roles = getMongoStore().loadEntities(MongoRoleEntity.class, query, invocationContext); List<MongoRoleEntity> roles = getMongoStore().loadEntities(MongoRoleEntity.class, query, invocationContext);
@ -636,7 +636,7 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoApplicationEntity>
@Override @Override
public void registerNode(String nodeHost, int registrationTime) { public void registerNode(String nodeHost, int registrationTime) {
MongoApplicationEntity entity = getMongoEntity(); MongoClientEntity entity = getMongoEntity();
if (entity.getRegisteredNodes() == null) { if (entity.getRegisteredNodes() == null) {
entity.setRegisteredNodes(new HashMap<String, Integer>()); entity.setRegisteredNodes(new HashMap<String, Integer>());
} }
@ -647,7 +647,7 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoApplicationEntity>
@Override @Override
public void unregisterNode(String nodeHost) { public void unregisterNode(String nodeHost) {
MongoApplicationEntity entity = getMongoEntity(); MongoClientEntity entity = getMongoEntity();
if (entity.getRegisteredNodes() == null) return; if (entity.getRegisteredNodes() == null) return;
entity.getRegisteredNodes().remove(nodeHost); entity.getRegisteredNodes().remove(nodeHost);

View file

@ -10,7 +10,7 @@ import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel; import org.keycloak.models.RealmModel;
import org.keycloak.models.RealmProvider; import org.keycloak.models.RealmProvider;
import org.keycloak.models.RoleModel; import org.keycloak.models.RoleModel;
import org.keycloak.models.mongo.keycloak.entities.MongoApplicationEntity; import org.keycloak.models.mongo.keycloak.entities.MongoClientEntity;
import org.keycloak.models.mongo.keycloak.entities.MongoRealmEntity; import org.keycloak.models.mongo.keycloak.entities.MongoRealmEntity;
import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity; import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity;
import org.keycloak.models.utils.KeycloakModelUtils; import org.keycloak.models.utils.KeycloakModelUtils;
@ -111,7 +111,7 @@ public class MongoRealmProvider implements RealmProvider {
@Override @Override
public ClientModel getClientById(String id, RealmModel realm) { public ClientModel getClientById(String id, RealmModel realm) {
MongoApplicationEntity appData = getMongoStore().loadEntity(MongoApplicationEntity.class, id, invocationContext); MongoClientEntity appData = getMongoStore().loadEntity(MongoClientEntity.class, id, invocationContext);
// Check if application belongs to this realm // Check if application belongs to this realm
if (appData == null || !realm.getId().equals(appData.getRealmId())) { if (appData == null || !realm.getId().equals(appData.getRealmId())) {

View file

@ -18,7 +18,7 @@ import org.keycloak.models.entities.IdentityProviderEntity;
import org.keycloak.models.entities.IdentityProviderMapperEntity; import org.keycloak.models.entities.IdentityProviderMapperEntity;
import org.keycloak.models.entities.RequiredCredentialEntity; import org.keycloak.models.entities.RequiredCredentialEntity;
import org.keycloak.models.entities.UserFederationProviderEntity; import org.keycloak.models.entities.UserFederationProviderEntity;
import org.keycloak.models.mongo.keycloak.entities.MongoApplicationEntity; import org.keycloak.models.mongo.keycloak.entities.MongoClientEntity;
import org.keycloak.models.mongo.keycloak.entities.MongoRealmEntity; import org.keycloak.models.mongo.keycloak.entities.MongoRealmEntity;
import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity; import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity;
import org.keycloak.models.utils.KeycloakModelUtils; import org.keycloak.models.utils.KeycloakModelUtils;
@ -584,9 +584,9 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
public ClientModel getClientByClientId(String clientId) { public ClientModel getClientByClientId(String clientId) {
DBObject query = new QueryBuilder() DBObject query = new QueryBuilder()
.and("realmId").is(getId()) .and("realmId").is(getId())
.and("name").is(clientId) .and("clientId").is(clientId)
.get(); .get();
MongoApplicationEntity appEntity = getMongoStore().loadSingleEntity(MongoApplicationEntity.class, query, invocationContext); MongoClientEntity appEntity = getMongoStore().loadSingleEntity(MongoClientEntity.class, query, invocationContext);
return appEntity == null ? null : new ClientAdapter(session, this, appEntity, invocationContext); return appEntity == null ? null : new ClientAdapter(session, this, appEntity, invocationContext);
} }
@ -604,10 +604,10 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
DBObject query = new QueryBuilder() DBObject query = new QueryBuilder()
.and("realmId").is(getId()) .and("realmId").is(getId())
.get(); .get();
List<MongoApplicationEntity> appDatas = getMongoStore().loadEntities(MongoApplicationEntity.class, query, invocationContext); List<MongoClientEntity> appDatas = getMongoStore().loadEntities(MongoClientEntity.class, query, invocationContext);
List<ClientModel> result = new ArrayList<ClientModel>(); List<ClientModel> result = new ArrayList<ClientModel>();
for (MongoApplicationEntity appData : appDatas) { for (MongoClientEntity appData : appDatas) {
result.add(new ClientAdapter(session, this, appData, invocationContext)); result.add(new ClientAdapter(session, this, appData, invocationContext));
} }
return result; return result;
@ -620,9 +620,9 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
@Override @Override
public ClientModel addClient(String id, String clientId) { public ClientModel addClient(String id, String clientId) {
MongoApplicationEntity appData = new MongoApplicationEntity(); MongoClientEntity appData = new MongoClientEntity();
appData.setId(id); appData.setId(id);
appData.setName(clientId); appData.setClientId(clientId);
appData.setRealmId(getId()); appData.setRealmId(getId());
appData.setEnabled(true); appData.setEnabled(true);
getMongoStore().insertEntity(appData, invocationContext); getMongoStore().insertEntity(appData, invocationContext);
@ -639,7 +639,7 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
@Override @Override
public boolean removeClient(String id) { public boolean removeClient(String id) {
return getMongoStore().removeEntity(MongoApplicationEntity.class, id, invocationContext); return getMongoStore().removeEntity(MongoClientEntity.class, id, invocationContext);
} }
@Override @Override
@ -979,7 +979,7 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
@Override @Override
public ClientModel getMasterAdminApp() { public ClientModel getMasterAdminApp() {
MongoApplicationEntity appData = getMongoStore().loadEntity(MongoApplicationEntity.class, realm.getAdminAppId(), invocationContext); MongoClientEntity appData = getMongoStore().loadEntity(MongoClientEntity.class, realm.getAdminAppId(), invocationContext);
return appData != null ? new ClientAdapter(session, this, appData, invocationContext) : null; return appData != null ? new ClientAdapter(session, this, appData, invocationContext) : null;
} }

View file

@ -7,7 +7,7 @@ import org.keycloak.models.KeycloakSession;
import org.keycloak.models.RealmModel; import org.keycloak.models.RealmModel;
import org.keycloak.models.RoleContainerModel; import org.keycloak.models.RoleContainerModel;
import org.keycloak.models.RoleModel; import org.keycloak.models.RoleModel;
import org.keycloak.models.mongo.keycloak.entities.MongoApplicationEntity; import org.keycloak.models.mongo.keycloak.entities.MongoClientEntity;
import org.keycloak.models.mongo.keycloak.entities.MongoRealmEntity; import org.keycloak.models.mongo.keycloak.entities.MongoRealmEntity;
import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity; import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity;
import org.keycloak.models.utils.KeycloakModelUtils; import org.keycloak.models.utils.KeycloakModelUtils;
@ -116,13 +116,13 @@ public class RoleAdapter extends AbstractMongoAdapter<MongoRoleEntity> implement
} }
roleContainer = new RealmAdapter(session, realm, invocationContext); roleContainer = new RealmAdapter(session, realm, invocationContext);
} else if (role.getClientId() != null) { } else if (role.getClientId() != null) {
MongoApplicationEntity appEntity = getMongoStore().loadEntity(MongoApplicationEntity.class, role.getClientId(), invocationContext); MongoClientEntity appEntity = getMongoStore().loadEntity(MongoClientEntity.class, role.getClientId(), invocationContext);
if (appEntity == null) { if (appEntity == null) {
throw new IllegalStateException("Application with id: " + role.getClientId() + " doesn't exists"); throw new IllegalStateException("Application with id: " + role.getClientId() + " doesn't exists");
} }
roleContainer = new ClientAdapter(session, realm, appEntity, invocationContext); roleContainer = new ClientAdapter(session, realm, appEntity, invocationContext);
} else { } else {
throw new IllegalStateException("Both realmId and applicationId are null for role: " + this); throw new IllegalStateException("Both realmId and clientId are null for role: " + this);
} }
} }
return roleContainer; return roleContainer;

View file

@ -5,19 +5,19 @@ import com.mongodb.QueryBuilder;
import org.keycloak.connections.mongo.api.MongoCollection; import org.keycloak.connections.mongo.api.MongoCollection;
import org.keycloak.connections.mongo.api.MongoIdentifiableEntity; import org.keycloak.connections.mongo.api.MongoIdentifiableEntity;
import org.keycloak.connections.mongo.api.context.MongoStoreInvocationContext; import org.keycloak.connections.mongo.api.context.MongoStoreInvocationContext;
import org.keycloak.models.entities.ApplicationEntity; import org.keycloak.models.entities.ClientEntity;
/** /**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a> * @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/ */
@MongoCollection(collectionName = "applications") @MongoCollection(collectionName = "clients")
public class MongoApplicationEntity extends ApplicationEntity implements MongoIdentifiableEntity { public class MongoClientEntity extends ClientEntity implements MongoIdentifiableEntity {
@Override @Override
public void afterRemove(MongoStoreInvocationContext context) { public void afterRemove(MongoStoreInvocationContext context) {
// Remove all roles, which belongs to this application // Remove all roles, which belongs to this application
DBObject query = new QueryBuilder() DBObject query = new QueryBuilder()
.and("applicationId").is(getId()) .and("clientId").is(getId())
.get(); .get();
context.getMongoStore().removeEntities(MongoRoleEntity.class, query, context); context.getMongoStore().removeEntities(MongoRoleEntity.class, query, context);
} }

View file

@ -26,6 +26,6 @@ public class MongoRealmEntity extends RealmEntity implements MongoIdentifiableEn
context.getMongoStore().removeEntities(MongoRoleEntity.class, query, context); context.getMongoStore().removeEntities(MongoRoleEntity.class, query, context);
// Remove all applications of this realm // Remove all applications of this realm
context.getMongoStore().removeEntities(MongoApplicationEntity.class, query, context); context.getMongoStore().removeEntities(MongoClientEntity.class, query, context);
} }
} }

View file

@ -24,13 +24,13 @@ public class MongoRoleEntity extends RoleEntity implements MongoIdentifiableEnti
// TODO This is required as Mongo doesn't support sparse indexes with compound keys (see https://jira.mongodb.org/browse/SERVER-2193) // TODO This is required as Mongo doesn't support sparse indexes with compound keys (see https://jira.mongodb.org/browse/SERVER-2193)
public String getNameIndex() { public String getNameIndex() {
String realmId = getRealmId(); String realmId = getRealmId();
String applicationId = getClientId(); String clientId = getClientId();
String name = getName(); String name = getName();
if (realmId != null) { if (realmId != null) {
return realmId + "//" + name; return realmId + "//" + name;
} else { } else {
return applicationId + "//" + name; return clientId + "//" + name;
} }
} }
@ -75,7 +75,7 @@ public class MongoRoleEntity extends RoleEntity implements MongoIdentifiableEnti
// Remove defaultRoles from application // Remove defaultRoles from application
if (getClientId() != null) { if (getClientId() != null) {
MongoApplicationEntity appEntity = mongoStore.loadEntity(MongoApplicationEntity.class, getClientId(), invContext); MongoClientEntity appEntity = mongoStore.loadEntity(MongoClientEntity.class, getClientId(), invContext);
// Application might be already removed at this point // Application might be already removed at this point
if (appEntity != null) { if (appEntity != null) {

View file

@ -5,7 +5,7 @@ import com.mongodb.QueryBuilder;
import org.keycloak.connections.mongo.api.context.MongoStoreInvocationContext; import org.keycloak.connections.mongo.api.context.MongoStoreInvocationContext;
import org.keycloak.models.ClientModel; import org.keycloak.models.ClientModel;
import org.keycloak.models.UserModel; import org.keycloak.models.UserModel;
import org.keycloak.models.entities.ApplicationEntity; import org.keycloak.models.entities.ClientEntity;
import org.keycloak.models.mongo.keycloak.adapters.ClientAdapter; import org.keycloak.models.mongo.keycloak.adapters.ClientAdapter;
import org.keycloak.models.mongo.keycloak.adapters.UserAdapter; import org.keycloak.models.mongo.keycloak.adapters.UserAdapter;
import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity; import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity;
@ -36,7 +36,7 @@ public class MongoModelUtils {
// Get everything including both application and realm scopes // Get everything including both application and realm scopes
public static List<MongoRoleEntity> getAllScopesOfClient(ClientModel client, MongoStoreInvocationContext invContext) { public static List<MongoRoleEntity> getAllScopesOfClient(ClientModel client, MongoStoreInvocationContext invContext) {
ApplicationEntity scopedEntity = ((ClientAdapter)client).getMongoEntity(); ClientEntity scopedEntity = ((ClientAdapter)client).getMongoEntity();
List<String> scopeIds = scopedEntity.getScopeIds(); List<String> scopeIds = scopedEntity.getScopeIds();
if (scopeIds == null || scopeIds.isEmpty()) { if (scopeIds == null || scopeIds.isEmpty()) {