diff --git a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/WallixConfigSynchronizer.java b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/WallixConfigSynchronizer.java index a75da3d..efd834d 100644 --- a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/WallixConfigSynchronizer.java +++ b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/WallixConfigSynchronizer.java @@ -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 connectionsIds = db.getTargetGroupConnectionsIds(targetGroup.getId()); + HashMap> allowedEntities = db.getConnectionsAllowedEntities(connectionsIds); + + for (int connectionId : connectionsIds) { + Set 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 { diff --git a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/DB.java b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/DB.java index 584cdbe..461ca58 100644 --- a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/DB.java +++ b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/db/DB.java @@ -651,4 +651,68 @@ public class DB { stmt.executeUpdate(); } + public Set 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 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> getConnectionsAllowedEntities(Set connectionsIds) throws GuacamoleException, SQLException { + if (connectionsIds == null || connectionsIds.isEmpty()) { + return new HashMap<>(0); + } + + HashMap> 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()); + } + result.get(connectionId).add(rs.getInt(1)); + } + + return result; + } + }