From 555933379babe340ef8dde051abb953e27695380 Mon Sep 17 00:00:00 2001 From: Abba Soungui YOUNOUSS Date: Wed, 5 Nov 2025 16:17:55 +0100 Subject: [PATCH] Implement group members synchronization. This commit add membership synchronization. --- .../guacamole/ext/wallix/sync/Wallix.java | 15 ++- .../wallix/sync/WallixConfigSynchronizer.java | 27 +++-- .../guacamole/ext/wallix/sync/db/DB.java | 112 +++++++++++------- 3 files changed, 97 insertions(+), 57 deletions(-) diff --git a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/Wallix.java b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/Wallix.java index e7e648c..c41fdc8 100644 --- a/src/main/java/cm/soungui/guacamole/ext/wallix/sync/Wallix.java +++ b/src/main/java/cm/soungui/guacamole/ext/wallix/sync/Wallix.java @@ -9,6 +9,7 @@ import java.security.SecureRandom; import java.security.cert.CertificateException; import java.security.cert.X509Certificate; import java.util.HashSet; +import java.util.List; import java.util.Set; import javax.net.ssl.SSLContext; @@ -137,11 +138,15 @@ public class Wallix { UserGroup group = new UserGroup(); group.setName(node.findValue(Wallix.API.ATTRIBUTE_GROUP_NAME).asText()); - node.findValues(Wallix.API.ATTRIBUTE_GROUP_USERS).forEach(userNode -> { - User user = new User(); - user.setName(userNode.textValue()); - group.getMembers().add(user); - }); + List membersList = node.findValues(Wallix.API.ATTRIBUTE_GROUP_USERS); + if (! membersList.isEmpty()) { + JsonNode usernames = membersList.get(0); + usernames.forEach(userNode -> { + User user = new User(); + user.setName(getNameWithoutDomain(userNode.textValue())); + group.getMembers().add(user); + }); + } list.add(group); }); 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 92292a2..ca10885 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 @@ -81,7 +81,21 @@ public class WallixConfigSynchronizer implements Runnable { DB db = DB.getInstance(); Set dbGroups = db.getUserGroups(); - for (UserGroup group : dbGroups) { + + wallixGroups.forEach(group -> { + try { + if (! dbGroups.contains(group)) { + System.out.println("Adding group " + group.getName()); + db.addGroup(group); + } + } catch (SQLException | GuacamoleException e) { + System.err.println(e.getMessage()); + } + }); + + Set newDbGroups = db.getUserGroups(); + + for (UserGroup group : newDbGroups) { if (wallixGroups.contains(group)) { for (UserGroup wallixGroup : wallixGroups) { if (group.equals(wallixGroup)) { @@ -93,17 +107,6 @@ public class WallixConfigSynchronizer implements Runnable { db.deleteGroup(group); } } - - wallixGroups.forEach(group -> { - try { - if (! dbGroups.contains(group)) { - System.out.println("Adding group " + group.getName()); - db.addGroup(group); - } - } catch (SQLException | GuacamoleException e) { - System.err.println(e.getMessage()); - } - }); } 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 c465c81..0239bc3 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 @@ -6,7 +6,6 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; -import java.util.ArrayList; import java.util.HashSet; import java.util.Set; @@ -65,7 +64,7 @@ public class DB { DB db = DB.getInstance(); Connection connection = db.getMySQLConnection(); ResultSet rs = connection - .prepareStatement("select entity_id,name from guacamole_entity where type='USER_GROUP'") + .prepareStatement("select e.entity_id,e.name,u.user_group_id from guacamole_entity e,guacamole_user_group u where type='USER_GROUP' and e.entity_id=u.entity_id") .executeQuery(); HashSet groups = new HashSet<>(); @@ -73,6 +72,7 @@ public class DB { UserGroup group = new UserGroup(); group.setId(rs.getInt("entity_id")); group.setName(rs.getString("name")); + group.setGroupId(rs.getInt("user_group_id")); groups.add(group); } @@ -140,55 +140,53 @@ public class DB { } public UserGroup getGroup(String name) throws SQLException, GuacamoleException { - return (UserGroup) getEntity(Entity.EntityType.GROUP, name); - } - - private Entity getEntity(EntityType type, String name) throws SQLException, GuacamoleException { - if (type == null || name == null) { - return null; - } - DB db = DB.getInstance(); Connection connection = db.getMySQLConnection(); PreparedStatement stmt = connection - .prepareStatement("select entity_id,name from guacamole_entity where type=? and name=?"); - stmt.setString(1, type == EntityType.USER ? "USER" : "USER_GROUP"); - stmt.setString(2, name); + .prepareStatement("select entity_id from guacamole_entity where type='USER_GROUP' and name=?"); + stmt.setString(1, name); ResultSet rs = stmt.executeQuery(); while (rs.next()) { - Entity entity = type == EntityType.USER ? new User() : new UserGroup(); - entity.setId(rs.getInt("entity_id")); - entity.setName(rs.getString("name")); + UserGroup group = new UserGroup(); + group.setId(rs.getInt("entity_id")); + group.setName(name); + group.getMembers().addAll(getGroupMembers(name)); - return entity; + return group; } return null; } - - private Entity getEntity(EntityType type, int id) throws SQLException, GuacamoleException { - if (type == null) { - return null; - } - + + public Set getGroupMembers(String name) throws GuacamoleException, SQLException { DB db = DB.getInstance(); Connection connection = db.getMySQLConnection(); PreparedStatement stmt = connection - .prepareStatement("select entity_id,name from guacamole_entity where type=? and id=?"); - stmt.setString(1, type.toString()); - stmt.setInt(2, id); + .prepareStatement("select entity_id,name,type from guacamole_entity where entity_id in (select member_entity_id from guacamole_user_group_member where user_group_id in (select user_group_id from guacamole_user_group where entity_id=(select entity_id from guacamole_entity where name=?)))"); + stmt.setString(1, name); ResultSet rs = stmt.executeQuery(); - + Set result = new HashSet<>(); while (rs.next()) { - Entity entity = new Entity(); + String type = rs.getString("type"); + Entity entity; + if ("USER".equals(type)) { + entity = new User(); + } else { + entity = new UserGroup(); + } entity.setId(rs.getInt("entity_id")); entity.setName(rs.getString("name")); - return entity; + if (entity.getType()==EntityType.GROUP) { + UserGroup group = (UserGroup) entity; + group.getMembers().addAll(getGroupMembers(entity.getName())); + } + + result.add(entity); } - return null; + return result; } public void updateGroupMembers(UserGroup group, Set wallixGroupMembers) throws SQLException, GuacamoleException { @@ -197,6 +195,7 @@ public class DB { } if (wallixGroupMembers == null || wallixGroupMembers.isEmpty()) { + // There is no member for this group, so we delete all existing entries and return DB db = DB.getInstance(); Connection connection = db.getMySQLConnection(); PreparedStatement stmt = connection @@ -207,25 +206,58 @@ public class DB { return; } + Set entitiesNames = new HashSet<>(); + wallixGroupMembers.forEach(member -> entitiesNames.add(member.getName())); + DB db = DB.getInstance(); Connection connection = db.getMySQLConnection(); - PreparedStatement stmt = connection - .prepareStatement("select user_group_id,member_entity_id,type from guacamole_user_group_member,guacamole_entity where user_group_id=? and member_entity_id=entity_id"); - stmt.setInt(1, group.getGroupId()); + + // We get Entity ID of each new member + StringBuffer sqlQuery = new StringBuffer("select entity_id from guacamole_entity where name in ('").append(String.join("','", entitiesNames)).append("')"); + + PreparedStatement stmt = connection.prepareStatement(sqlQuery.toString()); ResultSet rs = stmt.executeQuery(); - ArrayList toDelete = new ArrayList<>(); + Set newMemberIds = new HashSet<>(); while (rs.next()) { - Entity member = getEntity(EntityType.valueOf(rs.getString("type")), rs.getInt("entity_id")); - if (wallixGroupMembers.contains(member)) { - toDelete.add(member); + newMemberIds.add(rs.getInt("entity_id")); + } + rs.close(); + stmt.close(); + + // We get current members of the group + stmt = connection.prepareStatement("select member_entity_id from guacamole_user_group_member,guacamole_entity where user_group_id=? and member_entity_id=entity_id"); + stmt.setInt(1, group.getGroupId()); + rs = stmt.executeQuery(); + + Set toAdd = new HashSet<>(newMemberIds); + Set toDelete = new HashSet<>(); + while (rs.next()) { + int id = rs.getInt("member_entity_id"); + if (newMemberIds.contains(id)) { + toAdd.remove(id); + } else { + toDelete.add(id); } } + rs.close(); + stmt.close(); - System.out.println("Member to delete from group " + group.getName()); - for (Entity member : toDelete) { - System.out.println(member.getId() + " - " + member.getName()); + stmt = connection.prepareStatement("insert into guacamole_user_group_member values (?, ?)"); + for (int id : toAdd) { + stmt.setInt(1, group.getGroupId()); + stmt.setInt(2, id); + stmt.executeUpdate(); } + stmt.close(); + + stmt = connection.prepareStatement("delete from guacamole_user_group_member values (?, ?)"); + for (int id : toDelete) { + stmt.setInt(1, group.getGroupId()); + stmt.setInt(2, id); + stmt.executeUpdate(); + } + stmt.close(); } public void addGroup(UserGroup group) throws SQLException, GuacamoleException {