Skeleton for invitation policies (#130)
This commit is contained in:
@@ -23,7 +23,9 @@ package io.kamax.mxisd.backend.sql;
|
||||
import io.kamax.matrix.ThreePid;
|
||||
import io.kamax.matrix._MatrixID;
|
||||
import io.kamax.matrix._ThreePid;
|
||||
import io.kamax.mxisd.UserIdType;
|
||||
import io.kamax.mxisd.config.sql.SqlConfig;
|
||||
import io.kamax.mxisd.exception.InternalServerError;
|
||||
import io.kamax.mxisd.profile.ProfileProvider;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -33,16 +35,14 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class SqlProfileProvider implements ProfileProvider {
|
||||
|
||||
private transient final Logger log = LoggerFactory.getLogger(SqlProfileProvider.class);
|
||||
private static final Logger log = LoggerFactory.getLogger(SqlProfileProvider.class);
|
||||
|
||||
private SqlConfig.Profile cfg;
|
||||
|
||||
private SqlConnectionPool pool;
|
||||
|
||||
public SqlProfileProvider(SqlConfig cfg) {
|
||||
@@ -50,6 +50,12 @@ public abstract class SqlProfileProvider implements ProfileProvider {
|
||||
this.pool = new SqlConnectionPool(cfg);
|
||||
}
|
||||
|
||||
private void setParameters(PreparedStatement stmt, String value) throws SQLException {
|
||||
for (int i = 1; i <= stmt.getParameterMetaData().getParameterCount(); i++) {
|
||||
stmt.setString(i, value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<String> getDisplayName(_MatrixID user) {
|
||||
String stmtSql = cfg.getDisplayName().getQuery();
|
||||
@@ -94,7 +100,33 @@ public abstract class SqlProfileProvider implements ProfileProvider {
|
||||
|
||||
@Override
|
||||
public List<String> getRoles(_MatrixID user) {
|
||||
return Collections.emptyList();
|
||||
log.info("Querying roles for {}", user.getId());
|
||||
|
||||
List<String> roles = new ArrayList<>();
|
||||
|
||||
String stmtSql = cfg.getRole().getQuery();
|
||||
try (Connection conn = pool.get()) {
|
||||
PreparedStatement stmt = conn.prepareStatement(stmtSql);
|
||||
if (UserIdType.Localpart.is(cfg.getRole().getType())) {
|
||||
setParameters(stmt, user.getLocalPart());
|
||||
} else if (UserIdType.MatrixID.is(cfg.getRole().getType())) {
|
||||
setParameters(stmt, user.getId());
|
||||
} else {
|
||||
throw new InternalServerError("Unsupported user type in SQL Role fetching: " + cfg.getRole().getType());
|
||||
}
|
||||
|
||||
ResultSet rSet = stmt.executeQuery();
|
||||
while (rSet.next()) {
|
||||
String role = rSet.getString(1);
|
||||
roles.add(role);
|
||||
log.debug("Found role {}", role);
|
||||
}
|
||||
|
||||
log.info("Got {} roles", roles.size());
|
||||
return roles;
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -43,6 +43,10 @@ public class SynapseQueries {
|
||||
return "SELECT medium, address FROM user_threepids WHERE user_id = ?";
|
||||
}
|
||||
|
||||
public static String getRoles() {
|
||||
return "SELECT DISTINCT(group_id) FROM group_users WHERE user_id = ?";
|
||||
}
|
||||
|
||||
public static String findByDisplayName(String type, String domain) {
|
||||
if (StringUtils.equals("sqlite", type)) {
|
||||
return "select " + getUserId(type, domain) + ", displayname from profiles p where displayname like ?";
|
||||
|
||||
@@ -26,9 +26,13 @@ import io.kamax.mxisd.config.MxisdConfig;
|
||||
import io.kamax.mxisd.directory.DirectoryProviders;
|
||||
import io.kamax.mxisd.lookup.ThreePidProviders;
|
||||
import io.kamax.mxisd.profile.ProfileProviders;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class SynapseSqlStoreSupplier implements IdentityStoreSupplier {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(SynapseSqlStoreSupplier.class);
|
||||
|
||||
@Override
|
||||
public void accept(Mxisd mxisd) {
|
||||
accept(mxisd.getConfig());
|
||||
@@ -44,6 +48,7 @@ public class SynapseSqlStoreSupplier implements IdentityStoreSupplier {
|
||||
}
|
||||
|
||||
if (cfg.getSynapseSql().getProfile().isEnabled()) {
|
||||
log.debug("Profile is enabled, registering provider");
|
||||
ProfileProviders.register(() -> new SynapseSqlProfileProvider(cfg.getSynapseSql()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user