KEYCLOAK-1187 DB migration support for oauth/application to client
This commit is contained in:
parent
1567982f0b
commit
4ae9310f83
20 changed files with 131 additions and 153 deletions
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -42,6 +42,11 @@
|
|||
<constraints nullable="false"/>
|
||||
</column>
|
||||
</addColumn>
|
||||
<update tableName="CLIENT">
|
||||
<column name="CONSENT_REQUIRED" valueBoolean="true"/>
|
||||
<where>DTYPE = 'OAuthClientEntity'</where>
|
||||
</update>
|
||||
<dropColumn tableName="CLIENT" columnName="DTYPE"/>
|
||||
<renameColumn tableName="CLIENT" newColumnName="CLIENT_ID" oldColumnName="NAME"/>
|
||||
</changeSet>
|
||||
</databaseChangeLog>
|
||||
|
|
|
@ -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_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_RC1;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
|
||||
import java.util.Date;
|
||||
|
@ -28,7 +29,8 @@ public class DefaultMongoUpdaterProvider implements MongoUpdaterProvider {
|
|||
private Class<? extends Update>[] updates = new Class[]{
|
||||
Update1_0_0_Final.class,
|
||||
Update1_1_0_Beta1.class,
|
||||
Update1_2_0_Beta1.class
|
||||
Update1_2_0_Beta1.class,
|
||||
Update1_2_0_RC1.class
|
||||
};
|
||||
|
||||
@Override
|
||||
|
|
|
@ -53,15 +53,8 @@ public abstract class Update {
|
|||
log.debugv("Deleted entries from {0}", collection);
|
||||
}
|
||||
|
||||
protected 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;
|
||||
protected void renameCollection(String collection, String newName) {
|
||||
db.getCollection(collection).rename(newName);
|
||||
}
|
||||
|
||||
public void setLog(Logger log) {
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
|
||||
}
|
|
@ -8,9 +8,9 @@ import java.util.Map;
|
|||
/**
|
||||
* @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 boolean enabled;
|
||||
private String secret;
|
||||
|
@ -41,12 +41,12 @@ public class ApplicationEntity extends AbstractIdentifiableEntity {
|
|||
private List<ClientIdentityProviderMappingEntity> identityProviders = new ArrayList<ClientIdentityProviderMappingEntity>();
|
||||
private List<ProtocolMapperEntity> protocolMappers = new ArrayList<ProtocolMapperEntity>();
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
public String getClientId() {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
public void setClientId(String clientId) {
|
||||
this.clientId = clientId;
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
|
@ -33,7 +33,7 @@ import java.util.Set;
|
|||
import org.keycloak.connections.file.InMemoryModel;
|
||||
import org.keycloak.models.ModelDuplicateException;
|
||||
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.ProtocolMapperEntity;
|
||||
import org.keycloak.models.entities.RoleEntity;
|
||||
|
@ -48,13 +48,13 @@ public class ClientAdapter implements ClientModel {
|
|||
|
||||
private final RealmModel realm;
|
||||
private KeycloakSession session;
|
||||
private final ApplicationEntity entity;
|
||||
private final ClientEntity entity;
|
||||
private final InMemoryModel inMemoryModel;
|
||||
|
||||
private final Map<String, RoleAdapter> allRoles = new HashMap<String, RoleAdapter>();
|
||||
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.session = session;
|
||||
this.entity = entity;
|
||||
|
@ -406,13 +406,13 @@ public class ClientAdapter implements ClientModel {
|
|||
|
||||
@Override
|
||||
public String getClientId() {
|
||||
return entity.getName();
|
||||
return entity.getClientId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setClientId(String clientId) {
|
||||
if (appNameExists(clientId)) throw new ModelDuplicateException("Application named " + clientId + " already exists.");
|
||||
entity.setName(clientId);
|
||||
entity.setClientId(clientId);
|
||||
}
|
||||
|
||||
private boolean appNameExists(String name) {
|
||||
|
|
|
@ -29,7 +29,7 @@ import org.keycloak.models.RequiredCredentialModel;
|
|||
import org.keycloak.models.RoleModel;
|
||||
import org.keycloak.models.UserFederationProviderModel;
|
||||
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.RealmEntity;
|
||||
import org.keycloak.models.entities.RequiredCredentialEntity;
|
||||
|
@ -626,9 +626,9 @@ public class RealmAdapter implements RealmModel {
|
|||
throw new ModelDuplicateException("Application named '" + clientId + "' already exists.");
|
||||
}
|
||||
|
||||
ApplicationEntity appEntity = new ApplicationEntity();
|
||||
ClientEntity appEntity = new ClientEntity();
|
||||
appEntity.setId(id);
|
||||
appEntity.setName(clientId);
|
||||
appEntity.setClientId(clientId);
|
||||
appEntity.setRealmId(getId());
|
||||
appEntity.setEnabled(true);
|
||||
|
||||
|
|
|
@ -475,12 +475,12 @@ public class ClientAdapter implements ClientModel {
|
|||
|
||||
@Override
|
||||
public String getClientId() {
|
||||
return entity.getName();
|
||||
return entity.getClientId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setClientId(String clientId) {
|
||||
entity.setName(clientId);
|
||||
entity.setClientId(clientId);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -635,7 +635,7 @@ public class RealmAdapter implements RealmModel {
|
|||
public ClientModel addClient(String id, String clientId) {
|
||||
ClientEntity applicationData = new ClientEntity();
|
||||
applicationData.setId(id);
|
||||
applicationData.setName(clientId);
|
||||
applicationData.setClientId(clientId);
|
||||
applicationData.setEnabled(true);
|
||||
applicationData.setRealm(realm);
|
||||
realm.getApplications().add(applicationData);
|
||||
|
|
|
@ -26,14 +26,14 @@ import java.util.Set;
|
|||
* @version $Revision: 1 $
|
||||
*/
|
||||
@Entity
|
||||
@Table(name="CLIENT", uniqueConstraints = {@UniqueConstraint(columnNames = {"REALM_ID", "NAME"})})
|
||||
@Table(name="CLIENT", uniqueConstraints = {@UniqueConstraint(columnNames = {"REALM_ID", "CLIENT_ID"})})
|
||||
public class ClientEntity {
|
||||
|
||||
@Id
|
||||
@Column(name="ID", length = 36)
|
||||
private String id;
|
||||
@Column(name = "NAME")
|
||||
private String name;
|
||||
@Column(name = "CLIENT_ID")
|
||||
private String clientId;
|
||||
@Column(name="ENABLED")
|
||||
private boolean enabled;
|
||||
@Column(name="SECRET")
|
||||
|
@ -133,12 +133,12 @@ public class ClientEntity {
|
|||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
public String getClientId() {
|
||||
return clientId;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
public void setClientId(String clientId) {
|
||||
this.clientId = clientId;
|
||||
}
|
||||
|
||||
public Set<String> getWebOrigins() {
|
||||
|
|
|
@ -11,7 +11,7 @@ import org.keycloak.models.RealmModel;
|
|||
import org.keycloak.models.RoleModel;
|
||||
import org.keycloak.models.entities.ClientIdentityProviderMappingEntity;
|
||||
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.utils.MongoModelUtils;
|
||||
import org.keycloak.models.utils.KeycloakModelUtils;
|
||||
|
@ -27,13 +27,13 @@ import java.util.Set;
|
|||
/**
|
||||
* @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;
|
||||
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);
|
||||
this.session = session;
|
||||
this.realm = realm;
|
||||
|
@ -41,7 +41,7 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoApplicationEntity>
|
|||
}
|
||||
|
||||
@Override
|
||||
public MongoApplicationEntity getMongoEntity() {
|
||||
public MongoClientEntity getMongoEntity() {
|
||||
return applicationEntity;
|
||||
}
|
||||
|
||||
|
@ -58,12 +58,12 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoApplicationEntity>
|
|||
|
||||
@Override
|
||||
public String getClientId() {
|
||||
return getMongoEntity().getName();
|
||||
return getMongoEntity().getClientId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setClientId(String clientId) {
|
||||
getMongoEntity().setName(clientId);
|
||||
getMongoEntity().setClientId(clientId);
|
||||
updateMongoEntity();
|
||||
}
|
||||
|
||||
|
@ -507,7 +507,7 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoApplicationEntity>
|
|||
public RoleAdapter getRole(String name) {
|
||||
DBObject query = new QueryBuilder()
|
||||
.and("name").is(name)
|
||||
.and("applicationId").is(getId())
|
||||
.and("clientId").is(getId())
|
||||
.get();
|
||||
MongoRoleEntity role = getMongoStore().loadSingleEntity(MongoRoleEntity.class, query, invocationContext);
|
||||
if (role == null) {
|
||||
|
@ -543,7 +543,7 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoApplicationEntity>
|
|||
@Override
|
||||
public Set<RoleModel> getRoles() {
|
||||
DBObject query = new QueryBuilder()
|
||||
.and("applicationId").is(getId())
|
||||
.and("clientId").is(getId())
|
||||
.get();
|
||||
List<MongoRoleEntity> roles = getMongoStore().loadEntities(MongoRoleEntity.class, query, invocationContext);
|
||||
|
||||
|
@ -636,7 +636,7 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoApplicationEntity>
|
|||
|
||||
@Override
|
||||
public void registerNode(String nodeHost, int registrationTime) {
|
||||
MongoApplicationEntity entity = getMongoEntity();
|
||||
MongoClientEntity entity = getMongoEntity();
|
||||
if (entity.getRegisteredNodes() == null) {
|
||||
entity.setRegisteredNodes(new HashMap<String, Integer>());
|
||||
}
|
||||
|
@ -647,7 +647,7 @@ public class ClientAdapter extends AbstractMongoAdapter<MongoApplicationEntity>
|
|||
|
||||
@Override
|
||||
public void unregisterNode(String nodeHost) {
|
||||
MongoApplicationEntity entity = getMongoEntity();
|
||||
MongoClientEntity entity = getMongoEntity();
|
||||
if (entity.getRegisteredNodes() == null) return;
|
||||
|
||||
entity.getRegisteredNodes().remove(nodeHost);
|
||||
|
|
|
@ -10,7 +10,7 @@ import org.keycloak.models.KeycloakSession;
|
|||
import org.keycloak.models.RealmModel;
|
||||
import org.keycloak.models.RealmProvider;
|
||||
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.MongoRoleEntity;
|
||||
import org.keycloak.models.utils.KeycloakModelUtils;
|
||||
|
@ -111,7 +111,7 @@ public class MongoRealmProvider implements RealmProvider {
|
|||
|
||||
@Override
|
||||
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
|
||||
if (appData == null || !realm.getId().equals(appData.getRealmId())) {
|
||||
|
|
|
@ -18,7 +18,7 @@ import org.keycloak.models.entities.IdentityProviderEntity;
|
|||
import org.keycloak.models.entities.IdentityProviderMapperEntity;
|
||||
import org.keycloak.models.entities.RequiredCredentialEntity;
|
||||
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.MongoRoleEntity;
|
||||
import org.keycloak.models.utils.KeycloakModelUtils;
|
||||
|
@ -584,9 +584,9 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
|
|||
public ClientModel getClientByClientId(String clientId) {
|
||||
DBObject query = new QueryBuilder()
|
||||
.and("realmId").is(getId())
|
||||
.and("name").is(clientId)
|
||||
.and("clientId").is(clientId)
|
||||
.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);
|
||||
}
|
||||
|
||||
|
@ -604,10 +604,10 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
|
|||
DBObject query = new QueryBuilder()
|
||||
.and("realmId").is(getId())
|
||||
.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>();
|
||||
for (MongoApplicationEntity appData : appDatas) {
|
||||
for (MongoClientEntity appData : appDatas) {
|
||||
result.add(new ClientAdapter(session, this, appData, invocationContext));
|
||||
}
|
||||
return result;
|
||||
|
@ -620,9 +620,9 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
|
|||
|
||||
@Override
|
||||
public ClientModel addClient(String id, String clientId) {
|
||||
MongoApplicationEntity appData = new MongoApplicationEntity();
|
||||
MongoClientEntity appData = new MongoClientEntity();
|
||||
appData.setId(id);
|
||||
appData.setName(clientId);
|
||||
appData.setClientId(clientId);
|
||||
appData.setRealmId(getId());
|
||||
appData.setEnabled(true);
|
||||
getMongoStore().insertEntity(appData, invocationContext);
|
||||
|
@ -639,7 +639,7 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
|
|||
|
||||
@Override
|
||||
public boolean removeClient(String id) {
|
||||
return getMongoStore().removeEntity(MongoApplicationEntity.class, id, invocationContext);
|
||||
return getMongoStore().removeEntity(MongoClientEntity.class, id, invocationContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -979,7 +979,7 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
|
|||
|
||||
@Override
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -7,7 +7,7 @@ import org.keycloak.models.KeycloakSession;
|
|||
import org.keycloak.models.RealmModel;
|
||||
import org.keycloak.models.RoleContainerModel;
|
||||
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.MongoRoleEntity;
|
||||
import org.keycloak.models.utils.KeycloakModelUtils;
|
||||
|
@ -116,13 +116,13 @@ public class RoleAdapter extends AbstractMongoAdapter<MongoRoleEntity> implement
|
|||
}
|
||||
roleContainer = new RealmAdapter(session, realm, invocationContext);
|
||||
} 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) {
|
||||
throw new IllegalStateException("Application with id: " + role.getClientId() + " doesn't exists");
|
||||
}
|
||||
roleContainer = new ClientAdapter(session, realm, appEntity, invocationContext);
|
||||
} 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;
|
||||
|
|
|
@ -5,19 +5,19 @@ import com.mongodb.QueryBuilder;
|
|||
import org.keycloak.connections.mongo.api.MongoCollection;
|
||||
import org.keycloak.connections.mongo.api.MongoIdentifiableEntity;
|
||||
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>
|
||||
*/
|
||||
@MongoCollection(collectionName = "applications")
|
||||
public class MongoApplicationEntity extends ApplicationEntity implements MongoIdentifiableEntity {
|
||||
@MongoCollection(collectionName = "clients")
|
||||
public class MongoClientEntity extends ClientEntity implements MongoIdentifiableEntity {
|
||||
|
||||
@Override
|
||||
public void afterRemove(MongoStoreInvocationContext context) {
|
||||
// Remove all roles, which belongs to this application
|
||||
DBObject query = new QueryBuilder()
|
||||
.and("applicationId").is(getId())
|
||||
.and("clientId").is(getId())
|
||||
.get();
|
||||
context.getMongoStore().removeEntities(MongoRoleEntity.class, query, context);
|
||||
}
|
|
@ -26,6 +26,6 @@ public class MongoRealmEntity extends RealmEntity implements MongoIdentifiableEn
|
|||
context.getMongoStore().removeEntities(MongoRoleEntity.class, query, context);
|
||||
|
||||
// Remove all applications of this realm
|
||||
context.getMongoStore().removeEntities(MongoApplicationEntity.class, query, context);
|
||||
context.getMongoStore().removeEntities(MongoClientEntity.class, query, context);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
public String getNameIndex() {
|
||||
String realmId = getRealmId();
|
||||
String applicationId = getClientId();
|
||||
String clientId = getClientId();
|
||||
String name = getName();
|
||||
|
||||
if (realmId != null) {
|
||||
return realmId + "//" + name;
|
||||
} else {
|
||||
return applicationId + "//" + name;
|
||||
return clientId + "//" + name;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -75,7 +75,7 @@ public class MongoRoleEntity extends RoleEntity implements MongoIdentifiableEnti
|
|||
|
||||
// Remove defaultRoles from application
|
||||
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
|
||||
if (appEntity != null) {
|
||||
|
|
|
@ -5,7 +5,7 @@ import com.mongodb.QueryBuilder;
|
|||
import org.keycloak.connections.mongo.api.context.MongoStoreInvocationContext;
|
||||
import org.keycloak.models.ClientModel;
|
||||
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.UserAdapter;
|
||||
import org.keycloak.models.mongo.keycloak.entities.MongoRoleEntity;
|
||||
|
@ -36,7 +36,7 @@ public class MongoModelUtils {
|
|||
|
||||
// Get everything including both application and realm scopes
|
||||
public static List<MongoRoleEntity> getAllScopesOfClient(ClientModel client, MongoStoreInvocationContext invContext) {
|
||||
ApplicationEntity scopedEntity = ((ClientAdapter)client).getMongoEntity();
|
||||
ClientEntity scopedEntity = ((ClientAdapter)client).getMongoEntity();
|
||||
List<String> scopeIds = scopedEntity.getScopeIds();
|
||||
|
||||
if (scopeIds == null || scopeIds.isEmpty()) {
|
||||
|
|
Loading…
Reference in a new issue