Implement group members synchronization.
This commit add membership synchronization.
This commit is contained in:
@@ -9,6 +9,7 @@ import java.security.SecureRandom;
|
|||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
import java.security.cert.X509Certificate;
|
import java.security.cert.X509Certificate;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
@@ -137,11 +138,15 @@ public class Wallix {
|
|||||||
UserGroup group = new UserGroup();
|
UserGroup group = new UserGroup();
|
||||||
group.setName(node.findValue(Wallix.API.ATTRIBUTE_GROUP_NAME).asText());
|
group.setName(node.findValue(Wallix.API.ATTRIBUTE_GROUP_NAME).asText());
|
||||||
|
|
||||||
node.findValues(Wallix.API.ATTRIBUTE_GROUP_USERS).forEach(userNode -> {
|
List<JsonNode> membersList = node.findValues(Wallix.API.ATTRIBUTE_GROUP_USERS);
|
||||||
User user = new User();
|
if (! membersList.isEmpty()) {
|
||||||
user.setName(userNode.textValue());
|
JsonNode usernames = membersList.get(0);
|
||||||
group.getMembers().add(user);
|
usernames.forEach(userNode -> {
|
||||||
});
|
User user = new User();
|
||||||
|
user.setName(getNameWithoutDomain(userNode.textValue()));
|
||||||
|
group.getMembers().add(user);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
list.add(group);
|
list.add(group);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -81,7 +81,21 @@ public class WallixConfigSynchronizer implements Runnable {
|
|||||||
|
|
||||||
DB db = DB.getInstance();
|
DB db = DB.getInstance();
|
||||||
Set<UserGroup> dbGroups = db.getUserGroups();
|
Set<UserGroup> 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<UserGroup> newDbGroups = db.getUserGroups();
|
||||||
|
|
||||||
|
for (UserGroup group : newDbGroups) {
|
||||||
if (wallixGroups.contains(group)) {
|
if (wallixGroups.contains(group)) {
|
||||||
for (UserGroup wallixGroup : wallixGroups) {
|
for (UserGroup wallixGroup : wallixGroups) {
|
||||||
if (group.equals(wallixGroup)) {
|
if (group.equals(wallixGroup)) {
|
||||||
@@ -93,17 +107,6 @@ public class WallixConfigSynchronizer implements Runnable {
|
|||||||
db.deleteGroup(group);
|
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 {
|
private void printWallixVersion() throws Exception {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import java.sql.PreparedStatement;
|
|||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Statement;
|
import java.sql.Statement;
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
@@ -65,7 +64,7 @@ public class DB {
|
|||||||
DB db = DB.getInstance();
|
DB db = DB.getInstance();
|
||||||
Connection connection = db.getMySQLConnection();
|
Connection connection = db.getMySQLConnection();
|
||||||
ResultSet rs = connection
|
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();
|
.executeQuery();
|
||||||
|
|
||||||
HashSet<UserGroup> groups = new HashSet<>();
|
HashSet<UserGroup> groups = new HashSet<>();
|
||||||
@@ -73,6 +72,7 @@ public class DB {
|
|||||||
UserGroup group = new UserGroup();
|
UserGroup group = new UserGroup();
|
||||||
group.setId(rs.getInt("entity_id"));
|
group.setId(rs.getInt("entity_id"));
|
||||||
group.setName(rs.getString("name"));
|
group.setName(rs.getString("name"));
|
||||||
|
group.setGroupId(rs.getInt("user_group_id"));
|
||||||
|
|
||||||
groups.add(group);
|
groups.add(group);
|
||||||
}
|
}
|
||||||
@@ -140,55 +140,53 @@ public class DB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public UserGroup getGroup(String name) throws SQLException, GuacamoleException {
|
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();
|
DB db = DB.getInstance();
|
||||||
Connection connection = db.getMySQLConnection();
|
Connection connection = db.getMySQLConnection();
|
||||||
PreparedStatement stmt = connection
|
PreparedStatement stmt = connection
|
||||||
.prepareStatement("select entity_id,name from guacamole_entity where type=? and name=?");
|
.prepareStatement("select entity_id from guacamole_entity where type='USER_GROUP' and name=?");
|
||||||
stmt.setString(1, type == EntityType.USER ? "USER" : "USER_GROUP");
|
stmt.setString(1, name);
|
||||||
stmt.setString(2, name);
|
|
||||||
ResultSet rs = stmt.executeQuery();
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
Entity entity = type == EntityType.USER ? new User() : new UserGroup();
|
UserGroup group = new UserGroup();
|
||||||
entity.setId(rs.getInt("entity_id"));
|
group.setId(rs.getInt("entity_id"));
|
||||||
entity.setName(rs.getString("name"));
|
group.setName(name);
|
||||||
|
group.getMembers().addAll(getGroupMembers(name));
|
||||||
|
|
||||||
return entity;
|
return group;
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Entity getEntity(EntityType type, int id) throws SQLException, GuacamoleException {
|
public Set<Entity> getGroupMembers(String name) throws GuacamoleException, SQLException {
|
||||||
if (type == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
DB db = DB.getInstance();
|
DB db = DB.getInstance();
|
||||||
Connection connection = db.getMySQLConnection();
|
Connection connection = db.getMySQLConnection();
|
||||||
PreparedStatement stmt = connection
|
PreparedStatement stmt = connection
|
||||||
.prepareStatement("select entity_id,name from guacamole_entity where type=? and 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, type.toString());
|
stmt.setString(1, name);
|
||||||
stmt.setInt(2, id);
|
|
||||||
ResultSet rs = stmt.executeQuery();
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
Set<Entity> result = new HashSet<>();
|
||||||
while (rs.next()) {
|
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.setId(rs.getInt("entity_id"));
|
||||||
entity.setName(rs.getString("name"));
|
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<Entity> wallixGroupMembers) throws SQLException, GuacamoleException {
|
public void updateGroupMembers(UserGroup group, Set<Entity> wallixGroupMembers) throws SQLException, GuacamoleException {
|
||||||
@@ -197,6 +195,7 @@ public class DB {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (wallixGroupMembers == null || wallixGroupMembers.isEmpty()) {
|
if (wallixGroupMembers == null || wallixGroupMembers.isEmpty()) {
|
||||||
|
// There is no member for this group, so we delete all existing entries and return
|
||||||
DB db = DB.getInstance();
|
DB db = DB.getInstance();
|
||||||
Connection connection = db.getMySQLConnection();
|
Connection connection = db.getMySQLConnection();
|
||||||
PreparedStatement stmt = connection
|
PreparedStatement stmt = connection
|
||||||
@@ -207,25 +206,58 @@ public class DB {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Set<String> entitiesNames = new HashSet<>();
|
||||||
|
wallixGroupMembers.forEach(member -> entitiesNames.add(member.getName()));
|
||||||
|
|
||||||
DB db = DB.getInstance();
|
DB db = DB.getInstance();
|
||||||
Connection connection = db.getMySQLConnection();
|
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");
|
// We get Entity ID of each new member
|
||||||
stmt.setInt(1, group.getGroupId());
|
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();
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
|
||||||
ArrayList<Entity> toDelete = new ArrayList<>();
|
Set<Integer> newMemberIds = new HashSet<>();
|
||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
Entity member = getEntity(EntityType.valueOf(rs.getString("type")), rs.getInt("entity_id"));
|
newMemberIds.add(rs.getInt("entity_id"));
|
||||||
if (wallixGroupMembers.contains(member)) {
|
}
|
||||||
toDelete.add(member);
|
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<Integer> toAdd = new HashSet<>(newMemberIds);
|
||||||
|
Set<Integer> 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());
|
stmt = connection.prepareStatement("insert into guacamole_user_group_member values (?, ?)");
|
||||||
for (Entity member : toDelete) {
|
for (int id : toAdd) {
|
||||||
System.out.println(member.getId() + " - " + member.getName());
|
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 {
|
public void addGroup(UserGroup group) throws SQLException, GuacamoleException {
|
||||||
|
|||||||
Reference in New Issue
Block a user