Add permission to connections according to Wallix configuration.

This commit is contained in:
2025-11-10 20:46:09 +01:00
parent 9465b853db
commit 5edafc44bb
2 changed files with 86 additions and 0 deletions

View File

@@ -194,6 +194,28 @@ public class WallixConfigSynchronizer implements Runnable {
for (int memberId : currentMembersIds.stream().filter(id -> ! newMembersIds.contains(id)).toList()) {
db.removeTargetGroupMember(targetGroup.getId(), memberId);
}
Set<Integer> connectionsIds = db.getTargetGroupConnectionsIds(targetGroup.getId());
HashMap<Integer, Set<Integer>> allowedEntities = db.getConnectionsAllowedEntities(connectionsIds);
for (int connectionId : connectionsIds) {
Set<Integer> allowedEntitiesOfConnection = allowedEntities.get(connectionId);
if (allowedEntitiesOfConnection == null) {
for (int entityId : newMembersIds) {
db.addConnectionPermission(connectionId, entityId);
}
} else {
for (int entityId : newMembersIds) {
if (! allowedEntitiesOfConnection.contains(entityId)) {
db.addConnectionPermission(connectionId, entityId);
}
}
for (int entityId : allowedEntitiesOfConnection) {
if (! newMembersIds.contains(entityId)) {
db.removeConnectionPermission(connectionId, entityId);
}
}
}
}
}
private void printWallixVersion() throws Exception {

View File

@@ -651,4 +651,68 @@ public class DB {
stmt.executeUpdate();
}
public Set<Integer> getTargetGroupConnectionsIds(int id) throws SQLException, GuacamoleException {
DB db = DB.getInstance();
Connection connection = db.getMySQLConnection();
PreparedStatement stmt = connection
.prepareStatement("select connection_id from guacamole_connection where parent_id=?");
Set<Integer> result = new HashSet<>();
stmt.setInt(1, id);
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
result.add(rs.getInt(1));
}
return result;
}
public void addConnectionPermission(int connectionId, int entityId) throws GuacamoleException, SQLException {
DB db = DB.getInstance();
Connection connection = db.getMySQLConnection();
PreparedStatement stmt = connection
.prepareStatement("insert into guacamole_connection_permission values (?, ?, 'READ')");
stmt.setInt(1, entityId);
stmt.setInt(2, connectionId);
stmt.executeUpdate();
}
public void removeConnectionPermission(int connectionId, int entityId) throws GuacamoleException, SQLException {
DB db = DB.getInstance();
Connection connection = db.getMySQLConnection();
PreparedStatement stmt = connection
.prepareStatement("delete from guacamole_connection_permission where entity_id=? and connection_id=?");
stmt.setInt(1, entityId);
stmt.setInt(2, connectionId);
stmt.executeUpdate();
}
public HashMap<Integer, Set<Integer>> getConnectionsAllowedEntities(Set<Integer> connectionsIds) throws GuacamoleException, SQLException {
if (connectionsIds == null || connectionsIds.isEmpty()) {
return new HashMap<>(0);
}
HashMap<Integer, Set<Integer>> result = new HashMap<>();
StringBuffer query = new StringBuffer("select entity_id, connection_id from guacamole_connection_permission where connection_id in (");
StringBuffer idList = new StringBuffer();
connectionsIds.forEach(id -> idList.append(id).append(","));
query.append(idList.substring(0, idList.length()-1)).append(")");
DB db = DB.getInstance();
Connection connection = db.getMySQLConnection();
PreparedStatement stmt = connection.prepareStatement(query.toString());
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
int connectionId = rs.getInt(2);
if (! result.containsKey(connectionId)) {
result.put(connectionId, new HashSet<Integer>());
}
result.get(connectionId).add(rs.getInt(1));
}
return result;
}
}