diff --git a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/ConnectionDirectory.java b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/ConnectionDirectory.java index 23bb0c34b..c4410da52 100644 --- a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/ConnectionDirectory.java +++ b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/ConnectionDirectory.java @@ -330,6 +330,12 @@ public class ConnectionDirectory implements Directory{ // Verify permission to update the to connection group permissionCheckService.verifyConnectionGroupAccess(this.user_id, toConnectionGroupID, MySQLConstants.CONNECTION_GROUP_UPDATE); + + // Verify that no connection already exists with this name. + MySQLConnection previousConnection = + connectionService.retrieveConnection(mySQLConnection.getName(), user_id, parentID); + if(previousConnection != null) + throw new GuacamoleClientException("That connection name is already in use."); // Update the connection mySQLConnection.setParentID(toConnectionGroupID); diff --git a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/ConnectionGroupDirectory.java b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/ConnectionGroupDirectory.java index 95a24c95e..b92e800a6 100644 --- a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/ConnectionGroupDirectory.java +++ b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/ConnectionGroupDirectory.java @@ -274,12 +274,16 @@ public class ConnectionGroupDirectory implements Directory administerableConnectionIDs = + Set administerableConnectionIDs = Sets.newHashSet( permissionCheckService.retrieveConnectionIDs(this.user_id, - MySQLConstants.CONNECTION_ADMINISTER); - - // Get set of names corresponding to administerable connections - Map administerableConnections = - connectionService.translateNames(administerableConnectionIDs); + MySQLConstants.CONNECTION_ADMINISTER)); // Insert all given permissions for (ConnectionPermission permission : permissions) { // Get original ID - Integer connection_id = - administerableConnections.get(permission.getObjectIdentifier()); + Integer connection_id = Integer.valueOf(permission.getObjectIdentifier()); // Throw exception if permission to administer this connection // is not granted - if (connection_id == null) + if (!administerableConnectionIDs.contains(connection_id)) throw new GuacamoleSecurityException( "User #" + this.user_id + " does not have permission to administrate connection " @@ -472,24 +468,19 @@ public class UserDirectory implements Directory administerableConnectionGroupIDs = + Set administerableConnectionGroupIDs = Sets.newHashSet( permissionCheckService.retrieveConnectionGroupIDs(this.user_id, - MySQLConstants.CONNECTION_GROUP_ADMINISTER); - - // Get set of names corresponding to administerable connection groups - Map administerableConnectionGroups = - connectionGroupService.translateNames(administerableConnectionGroupIDs); + MySQLConstants.CONNECTION_GROUP_ADMINISTER)); // Insert all given permissions for (ConnectionGroupPermission permission : permissions) { // Get original ID - Integer connection_group_id = - administerableConnectionGroups.get(permission.getObjectIdentifier()); + Integer connection_group_id = Integer.valueOf(permission.getObjectIdentifier()); // Throw exception if permission to administer this connection group // is not granted - if (connection_group_id == null) + if (!administerableConnectionGroupIDs.contains(connection_group_id)) throw new GuacamoleSecurityException( "User #" + this.user_id + " does not have permission to administrate connection group" @@ -524,24 +515,19 @@ public class UserDirectory implements Directory administerableConnectionIDs = + Set administerableConnectionIDs = Sets.newHashSet( permissionCheckService.retrieveConnectionIDs(this.user_id, - MySQLConstants.CONNECTION_ADMINISTER); - - // Get set of names corresponding to administerable connections - Map administerableConnections = - connectionService.translateNames(administerableConnectionIDs); + MySQLConstants.CONNECTION_ADMINISTER)); // Delete requested permissions for (ConnectionPermission permission : permissions) { // Get original ID - Integer connection_id = - administerableConnections.get(permission.getObjectIdentifier()); + Integer connection_id = Integer.valueOf(permission.getObjectIdentifier()); // Verify that the user actually has permission to administrate // every one of these connections - if (connection_id == null) + if (!administerableConnectionIDs.contains(connection_id)) throw new GuacamoleSecurityException( "User #" + this.user_id + " does not have permission to administrate connection " @@ -576,24 +562,19 @@ public class UserDirectory implements Directory administerableConnectionGroupIDs = + Set administerableConnectionGroupIDs = Sets.newHashSet( permissionCheckService.retrieveConnectionGroupIDs(this.user_id, - MySQLConstants.CONNECTION_GROUP_ADMINISTER); - - // Get set of names corresponding to administerable connection groups - Map administerableConnectionGroups = - connectionGroupService.translateNames(administerableConnectionGroupIDs); + MySQLConstants.CONNECTION_GROUP_ADMINISTER)); // Delete requested permissions for (ConnectionGroupPermission permission : permissions) { // Get original ID - Integer connection_group_id = - administerableConnectionGroups.get(permission.getObjectIdentifier()); + Integer connection_group_id = Integer.valueOf(permission.getObjectIdentifier()); // Verify that the user actually has permission to administrate // every one of these connection groups - if (connection_group_id == null) + if (!administerableConnectionGroupIDs.contains(connection_group_id)) throw new GuacamoleSecurityException( "User #" + this.user_id + " does not have permission to administrate connection group" diff --git a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ConnectionGroupService.java b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ConnectionGroupService.java index c265af9ed..a804c49fe 100644 --- a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ConnectionGroupService.java +++ b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ConnectionGroupService.java @@ -161,87 +161,6 @@ public class ConnectionGroupService { GuacamoleClientInformation info, int userID) { throw new UnsupportedOperationException("Not yet implemented"); } - - /** - * Retrieves a map of all connection group names for the given IDs. - * - * @param ids The IDs of the connection groups to retrieve the names of. - * @return A map containing the names of all connection groups and their - * corresponding IDs. - */ - public Map retrieveNames(Collection ids) { - - // If no IDs given, just return empty map - if (ids.isEmpty()) - return Collections.EMPTY_MAP; - - // Map of all names onto their corresponding IDs. - Map names = new HashMap(); - - // Get all connection groups having the given IDs - ConnectionGroupExample example = new ConnectionGroupExample(); - example.createCriteria().andConnection_group_idIn(Lists.newArrayList(ids)); - List connectionGroups = connectionGroupDAO.selectByExample(example); - - // Produce set of names - for (ConnectionGroup connectionGroup : connectionGroups) - names.put(connectionGroup.getConnection_group_id(), - connectionGroup.getConnection_group_name()); - - return names; - - } - - /** - * Get the names of all the connection groups defined in the system. - * - * @return A Set of names of all the connection groups defined in the system. - */ - public Set getAllConnectionGroupNames() { - - // Set of all present connection group names - Set names = new HashSet(); - - // Query all connection group names - List connectionGroups = - connectionGroupDAO.selectByExample(new ConnectionGroupExample()); - for (ConnectionGroup connectionGroup : connectionGroups) - names.add(connectionGroup.getConnection_group_name()); - - return names; - - } - - /** - * Retrieves a translation map of connection group names to their - * corresponding IDs. - * - * @param ids The IDs of the connection groups to retrieve the names of. - * @return A map containing the names of all connection groups and their - * corresponding IDs. - */ - public Map translateNames(List ids) { - - // If no IDs given, just return empty map - if (ids.isEmpty()) - return Collections.EMPTY_MAP; - - // Map of all names onto their corresponding IDs. - Map names = new HashMap(); - - // Get all connections having the given IDs - ConnectionGroupExample example = new ConnectionGroupExample(); - example.createCriteria().andConnection_group_idIn(ids); - List connectionGroups = connectionGroupDAO.selectByExample(example); - - // Produce set of names - for (ConnectionGroup connectionGroup : connectionGroups) - names.put(connectionGroup.getConnection_group_name(), - connectionGroup.getConnection_group_id()); - - return names; - - } /** * Returns a list of the IDs of all connection groups with a given parent ID. @@ -272,6 +191,36 @@ public class ConnectionGroupService { return connectionGroupIDs; } + /** + * Get the identifiers of all the connection groups defined in the system + * with a certain parentID. + * + * @return A Set of identifiers of all the connection groups defined + * in the system with the given parentID. + */ + public Set getAllConnectionGroupIdentifiers(Integer parentID) { + + // Set of all present connection identifiers + Set identifiers = new HashSet(); + + // Set up Criteria + ConnectionGroupExample example = new ConnectionGroupExample(); + Criteria criteria = example.createCriteria(); + if(parentID != null) + criteria.andParent_idEqualTo(parentID); + else + criteria.andParent_idIsNull(); + + // Query connection identifiers + List connectionGroups = + connectionGroupDAO.selectByExample(example); + for (ConnectionGroup connectionGroup : connectionGroups) + identifiers.add(String.valueOf(connectionGroup.getConnection_group_id())); + + return identifiers; + + } + /** * Convert the given database-retrieved Connection into a MySQLConnection. * The parameters of the given connection will be read and added to the diff --git a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ConnectionService.java b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ConnectionService.java index 38cca7ff5..b82c20cc0 100644 --- a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ConnectionService.java +++ b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ConnectionService.java @@ -201,67 +201,6 @@ public class ConnectionService { // Otherwise, return found connection return toMySQLConnection(connection, userID); } - - /** - * Retrieves a translation map of connection names to their corresponding - * IDs. - * - * @param ids The IDs of the connections to retrieve the names of. - * @return A map containing the names of all connections and their - * corresponding IDs. - */ - public Map translateNames(List ids) { - - // If no IDs given, just return empty map - if (ids.isEmpty()) - return Collections.EMPTY_MAP; - - // Map of all names onto their corresponding IDs. - Map names = new HashMap(); - - // Get all connections having the given IDs - ConnectionExample example = new ConnectionExample(); - example.createCriteria().andConnection_idIn(ids); - List connections = connectionDAO.selectByExample(example); - - // Produce set of names - for (Connection connection : connections) - names.put(connection.getConnection_name(), - connection.getConnection_id()); - - return names; - - } - - /** - * Retrieves a map of all connection names for the given IDs. - * - * @param ids The IDs of the connections to retrieve the names of. - * @return A map containing the names of all connections and their - * corresponding IDs. - */ - public Map retrieveNames(Collection ids) { - - // If no IDs given, just return empty map - if (ids.isEmpty()) - return Collections.EMPTY_MAP; - - // Map of all names onto their corresponding IDs. - Map names = new HashMap(); - - // Get all connections having the given IDs - ConnectionExample example = new ConnectionExample(); - example.createCriteria().andConnection_idIn(Lists.newArrayList(ids)); - List connections = connectionDAO.selectByExample(example); - - // Produce set of names - for (Connection connection : connections) - names.put(connection.getConnection_id(), - connection.getConnection_name()); - - return names; - - } /** * Returns a list of the IDs of all connections with a given parent ID. @@ -483,16 +422,16 @@ public class ConnectionService { } /** - * Get the names of all the connections defined in the system + * Get the identifiers of all the connections defined in the system * with a certain parentID. * - * @return A Set of names of all the connections defined in the system + * @return A Set of identifiers of all the connections defined in the system * with the given parentID. */ - public Set getAllConnectionNames(Integer parentID) { + public Set getAllConnectionIdentifiers(Integer parentID) { - // Set of all present connection names - Set names = new HashSet(); + // Set of all present connection identifiers + Set identifiers = new HashSet(); // Set up Criteria ConnectionExample example = new ConnectionExample(); @@ -502,13 +441,13 @@ public class ConnectionService { else criteria.andParent_idIsNull(); - // Query connection names + // Query connection identifiers List connections = connectionDAO.selectByExample(example); for (Connection connection : connections) - names.add(connection.getConnection_name()); + identifiers.add(String.valueOf(connection.getConnection_id())); - return names; + return identifiers; } diff --git a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/PermissionCheckService.java b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/PermissionCheckService.java index 8269dabd0..6cba01852 100644 --- a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/PermissionCheckService.java +++ b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/PermissionCheckService.java @@ -577,7 +577,7 @@ public class PermissionCheckService { // A system administrator has access to all connections. if(checkSystemAdministratorAccess(userID)) - return connectionService.getAllConnectionNames(parentID); + return connectionService.getAllConnectionIdentifiers(parentID); // List of all connection IDs for which this user has access List connectionIDs = @@ -607,7 +607,7 @@ public class PermissionCheckService { // A system administrator has access to all connections. if(checkSystemAdministratorAccess(userID)) - return connectionService.getAllConnectionNames(parentID); + return connectionGroupService.getAllConnectionGroupIdentifiers(parentID); // List of all connection group IDs for which this user has access List connectionGroupIDs = @@ -686,22 +686,13 @@ public class PermissionCheckService { List connectionPermissions = connectionPermissionDAO.selectByExample(connectionPermissionExample); - // Get list of affected connection IDs - List connectionIDs = new ArrayList(); - for(ConnectionPermissionKey connectionPermission : connectionPermissions) - connectionIDs.add(connectionPermission.getConnection_id()); - - // Get corresponding names - Map affectedConnections = - connectionService.retrieveNames(connectionIDs); - // Add connection permissions for(ConnectionPermissionKey connectionPermission : connectionPermissions) { // Construct permission from data ConnectionPermission permission = new ConnectionPermission( ConnectionPermission.Type.valueOf(connectionPermission.getPermission()), - affectedConnections.get(connectionPermission.getConnection_id()) + String.valueOf(connectionPermission.getConnection_id()) ); // Add to set @@ -732,22 +723,13 @@ public class PermissionCheckService { List connectionGroupPermissions = connectionGroupPermissionDAO.selectByExample(connectionGroupPermissionExample); - // Get list of affected connection IDs - List connectionGroupIDs = new ArrayList(); - for(ConnectionGroupPermissionKey connectionGroupPermission : connectionGroupPermissions) - connectionGroupIDs.add(connectionGroupPermission.getConnection_group_id()); - - // Get corresponding names - Map affectedConnectionGroups = - connectionGroupService.retrieveNames(connectionGroupIDs); - // Add connection permissions for(ConnectionGroupPermissionKey connectionGroupPermission : connectionGroupPermissions) { // Construct permission from data ConnectionGroupPermission permission = new ConnectionGroupPermission( ConnectionGroupPermission.Type.valueOf(connectionGroupPermission.getPermission()), - affectedConnectionGroups.get(connectionGroupPermission.getConnection_group_id()) + String.valueOf(connectionGroupPermission.getConnection_group_id()) ); // Add to set