From 611a6135895a1ff8dcf928460989eccd4f6adf46 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 26 Feb 2013 20:01:57 -0800 Subject: [PATCH] Ticket #269: Working connection read/write, reorganization. --- .../net/auth/mysql/ConnectionDirectory.java | 175 ++++++++++-------- .../net/auth/mysql/MySQLConnection.java | 150 ++++++--------- .../guacamole/net/auth/mysql/MySQLUser.java | 3 +- .../mysql/service/PermissionCheckService.java | 9 +- .../auth/mysql/service/ProviderService.java | 107 +++++++---- 5 files changed, 231 insertions(+), 213 deletions(-) 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 b36006ff0..894bc3499 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 @@ -1,3 +1,6 @@ + +package net.sourceforge.guacamole.net.auth.mysql; + /* ***** BEGIN LICENSE BLOCK ***** * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * @@ -33,15 +36,9 @@ * the terms of any one of the MPL, the GPL or the LGPL. * * ***** END LICENSE BLOCK ***** */ -package net.sourceforge.guacamole.net.auth.mysql; -import com.google.common.collect.Lists; import com.google.inject.Inject; -import java.util.ArrayList; -import java.util.HashMap; import java.util.HashSet; -import java.util.List; -import java.util.Map; import java.util.Set; import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.net.auth.Connection; @@ -59,7 +56,8 @@ import net.sourceforge.guacamole.protocol.GuacamoleConfiguration; import org.mybatis.guice.transactional.Transactional; /** - * + * A MySQL-based implementation of the connection directory. + * * @author James Muehlner */ public class ConnectionDirectory implements Directory{ @@ -70,18 +68,33 @@ public class ConnectionDirectory implements Directory{ */ private int user_id; + /** + * Service for checking permissions. + */ @Inject private PermissionCheckService permissionCheckService; + /** + * Service for creating and retrieving objects. + */ @Inject private ProviderService providerService; + /** + * Service for manipulating connections in the database. + */ @Inject private ConnectionMapper connectionDAO; + /** + * Service for manipulating connection permissions in the database. + */ @Inject private ConnectionPermissionMapper connectionPermissionDAO; + /** + * Service for manipulating connection parameters in the database. + */ @Inject private ConnectionParameterMapper connectionParameterDAO; @@ -115,124 +128,126 @@ public class ConnectionDirectory implements Directory{ @Transactional @Override public void add(Connection object) throws GuacamoleException { + + // Verify permission to create permissionCheckService.verifyCreateConnectionPermission(this.user_id); - MySQLConnection mySQLConnection = providerService.getNewMySQLConnection(object); - connectionDAO.insert(mySQLConnection.getConnection()); + // Create database object for insert + net.sourceforge.guacamole.net.auth.mysql.model.Connection connection = + new net.sourceforge.guacamole.net.auth.mysql.model.Connection(); - updateConfigurationValues(mySQLConnection); + connection.setConnection_name(object.getIdentifier()); + connection.setProtocol(object.getConfiguration().getProtocol()); + connectionDAO.insert(connection); - //finally, give the current user full access to the newly created connection. + // Add connection parameters + createConfigurationValues(connection.getConnection_id(), + object.getConfiguration()); + + // Finally, give the current user full access to the newly created + // connection. ConnectionPermissionKey newConnectionPermission = new ConnectionPermissionKey(); newConnectionPermission.setUser_id(this.user_id); - newConnectionPermission.setConnection_id(mySQLConnection.getConnectionID()); - newConnectionPermission.setPermission(MySQLConstants.USER_READ); + newConnectionPermission.setConnection_id(connection.getConnection_id()); + + // Read permission + newConnectionPermission.setPermission(MySQLConstants.CONNECTION_READ); connectionPermissionDAO.insert(newConnectionPermission); - newConnectionPermission.setPermission(MySQLConstants.USER_UPDATE); + + // Update permission + newConnectionPermission.setPermission(MySQLConstants.CONNECTION_UPDATE); connectionPermissionDAO.insert(newConnectionPermission); - newConnectionPermission.setPermission(MySQLConstants.USER_DELETE); + + // Delete permission + newConnectionPermission.setPermission(MySQLConstants.CONNECTION_DELETE); connectionPermissionDAO.insert(newConnectionPermission); - newConnectionPermission.setPermission(MySQLConstants.USER_ADMINISTER); + + // Administer permission + newConnectionPermission.setPermission(MySQLConstants.CONNECTION_ADMINISTER); connectionPermissionDAO.insert(newConnectionPermission); + } /** - * Saves the values of the configuration to the database - * @param connection + * Inserts all parameter values from the given configuration into the + * database, associating them with the connection having the givenID. + * + * @param connection_id The ID of the connection to associate all + * parameters with. + * @param config The GuacamoleConfiguration to read parameters from. */ - private void updateConfigurationValues(MySQLConnection mySQLConnection) { - GuacamoleConfiguration configuration = mySQLConnection.getConfiguration(); - Map existingConfiguration = new HashMap(); - ConnectionParameterExample example = new ConnectionParameterExample(); - example.createCriteria().andConnection_idEqualTo(mySQLConnection.getConnectionID()); - List connectionParameters = connectionParameterDAO.selectByExample(example); - for(ConnectionParameter parameter : connectionParameters) - existingConfiguration.put(parameter.getParameter_name(), parameter.getParameter_value()); + private void createConfigurationValues(int connection_id, + GuacamoleConfiguration config) { - List parametersToInsert = new ArrayList(); - List parametersToUpdate = new ArrayList(); + // Insert new parameters for each parameter in the config + for (String name : config.getParameterNames()) { - Set parameterNames = configuration.getParameterNames(); - - for(String parameterName : parameterNames) { - String parameterValue = configuration.getParameter(parameterName); - if(existingConfiguration.containsKey(parameterName)) { - String existingValue = existingConfiguration.get(parameterName); - // the value is different; we'll have to update this one in the database - if(!parameterValue.equals(existingValue)) { - ConnectionParameter parameterToUpdate = new ConnectionParameter(); - parameterToUpdate.setConnection_id(mySQLConnection.getConnectionID()); - parameterToUpdate.setParameter_name(parameterName); - parameterToUpdate.setParameter_value(parameterValue); - parametersToUpdate.add(parameterToUpdate); - } - } else { - // the value is new, we need to insert it - ConnectionParameter parameterToInsert = new ConnectionParameter(); - parameterToInsert.setConnection_id(mySQLConnection.getConnectionID()); - parameterToInsert.setParameter_name(parameterName); - parameterToInsert.setParameter_value(parameterValue); - parametersToInsert.add(parameterToInsert); - } - } - - // First, delete all parameters that are not in the new configuration. - example.clear(); - example.createCriteria(). - andConnection_idEqualTo(mySQLConnection.getConnectionID()). - andParameter_nameNotIn(Lists.newArrayList(existingConfiguration.keySet())); - - //Second, update all the parameters that need to be modified. - for(ConnectionParameter parameter : parametersToUpdate) { - example.clear(); - example.createCriteria(). - andConnection_idEqualTo(mySQLConnection.getConnectionID()). - andParameter_nameEqualTo(parameter.getParameter_name()); - - connectionParameterDAO.updateByExample(parameter, example); - } - - //Finally, insert any new parameters. - for(ConnectionParameter parameter : parametersToInsert) { - example.clear(); - example.createCriteria(). - andConnection_idEqualTo(mySQLConnection.getConnectionID()). - andParameter_nameEqualTo(parameter.getParameter_name()); + // Create a ConnectionParameter based on the current parameter + ConnectionParameter parameter = new ConnectionParameter(); + parameter.setConnection_id(connection_id); + parameter.setParameter_name(name); + parameter.setParameter_value(config.getParameter(name)); + // Insert connection parameter connectionParameterDAO.insert(parameter); + } + } @Transactional @Override public void update(Connection object) throws GuacamoleException { + + // Verify permission to update permissionCheckService.verifyConnectionUpdateAccess(this.user_id, object.getIdentifier()); - MySQLConnection mySQLConnection = providerService.getExistingMySQLConnection(object); - connectionDAO.updateByPrimaryKey(mySQLConnection.getConnection()); + // TODO: Rely on update() to be given MySQLConnection + MySQLConnection mySQLConnection = + providerService.getExistingMySQLConnection(object); + + // Create database object for insert + net.sourceforge.guacamole.net.auth.mysql.model.Connection connection = + new net.sourceforge.guacamole.net.auth.mysql.model.Connection(); + + connection.setConnection_id(mySQLConnection.getConnectionID()); + connection.setConnection_name(object.getIdentifier()); + connection.setProtocol(object.getConfiguration().getProtocol()); + connectionDAO.updateByPrimaryKey(connection); + + // Delete old connection parameters + ConnectionParameterExample parameterExample = new ConnectionParameterExample(); + parameterExample.createCriteria().andConnection_idEqualTo(connection.getConnection_id()); + connectionParameterDAO.deleteByExample(parameterExample); + + // Add connection parameters + createConfigurationValues(connection.getConnection_id(), + object.getConfiguration()); - updateConfigurationValues(mySQLConnection); } @Transactional @Override public void remove(String identifier) throws GuacamoleException { + + // Verify permission to delete permissionCheckService.verifyConnectionDeleteAccess(this.user_id, identifier); MySQLConnection mySQLConnection = providerService.getExistingMySQLConnection(identifier); - // delete all configuration values + // Delete all configuration values ConnectionParameterExample connectionParameterExample = new ConnectionParameterExample(); connectionParameterExample.createCriteria().andConnection_idEqualTo(mySQLConnection.getConnectionID()); connectionParameterDAO.deleteByExample(connectionParameterExample); - // delete all permissions that refer to this connection + // Delete all permissions that refer to this connection ConnectionPermissionExample connectionPermissionExample = new ConnectionPermissionExample(); connectionPermissionExample.createCriteria().andConnection_idEqualTo(mySQLConnection.getConnectionID()); connectionPermissionDAO.deleteByExample(connectionPermissionExample); - // delete the connection itself + // Delete the connection itself connectionDAO.deleteByPrimaryKey(mySQLConnection.getConnectionID()); + } } diff --git a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLConnection.java b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLConnection.java index 6368abe72..cd5ecba55 100644 --- a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLConnection.java +++ b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLConnection.java @@ -36,19 +36,16 @@ package net.sourceforge.guacamole.net.auth.mysql; import com.google.inject.Inject; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.net.GuacamoleSocket; import net.sourceforge.guacamole.net.InetGuacamoleSocket; +import net.sourceforge.guacamole.net.auth.AbstractConnection; import net.sourceforge.guacamole.net.auth.Connection; import net.sourceforge.guacamole.net.auth.ConnectionRecord; -import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionMapper; -import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionParameterMapper; -import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionExample; -import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionParameter; -import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionParameterExample; import net.sourceforge.guacamole.net.auth.mysql.properties.MySQLGuacamoleProperties; -import net.sourceforge.guacamole.net.auth.mysql.service.ConfigurationTranslationService; import net.sourceforge.guacamole.net.auth.mysql.service.ProviderService; import net.sourceforge.guacamole.properties.GuacamoleProperties; import net.sourceforge.guacamole.protocol.ConfiguredGuacamoleSocket; @@ -59,13 +56,17 @@ import net.sourceforge.guacamole.protocol.GuacamoleConfiguration; * A MySQL based implementation of the Connection object. * @author James Muehlner */ -public class MySQLConnection implements Connection { +public class MySQLConnection extends AbstractConnection { - @Inject - private ConnectionMapper connectionDAO; + /** + * The ID associated with this connection in the database. + */ + private Integer connectionID; - @Inject - private ConnectionParameterMapper connectionParameterDAO; + /** + * History of this connection. + */ + private List history = new ArrayList(); @Inject private ProviderService providerService; @@ -73,126 +74,84 @@ public class MySQLConnection implements Connection { @Inject private ActiveConnectionSet activeConnectionSet; - @Inject - private ConfigurationTranslationService configurationTranslationService; - - private net.sourceforge.guacamole.net.auth.mysql.model.Connection connection; - - private GuacamoleConfiguration configuration; - /** * Create a default, empty connection. */ public MySQLConnection() { - connection = new net.sourceforge.guacamole.net.auth.mysql.model.Connection(); - configuration = new GuacamoleConfiguration(); } /** - * Get the ID of the underlying connection record. - * @return the ID of the underlying connection + * Get the ID of the corresponding connection record. + * @return The ID of the corresponding connection, if any. */ - public int getConnectionID() { - return connection.getConnection_id(); + public Integer getConnectionID() { + return connectionID; } /** - * Get the underlying connection database record. - * @return the underlying connection record. + * Sets the ID of the corresponding connection record. + * @param connectionID The ID to assign to this connection. */ - public net.sourceforge.guacamole.net.auth.mysql.model.Connection getConnection() { - return connection; + public void setConnectionID(Integer connectionID) { + this.connectionID = connectionID; } /** - * Create a new MySQLConnection from this new connection. This is a connection that has not yet been inserted. - * @param connection + * Initialize a new MySQLConnection from this existing connection. + * + * @param connection The connection to use when populating the identifier + * and configuration of this connection. + * @throws GuacamoleException If permission to read the given connection's + * history is denied. */ - public void initNew(Connection connection) { - this.connection.setConnection_name(connection.getIdentifier()); - this.configuration = connection.getConfiguration(); + public void init(Connection connection) throws GuacamoleException { + init(null, connection.getIdentifier(), connection.getConfiguration(), connection.getHistory()); } /** - * Initializes the GuacamoleConfiguration based on the ConnectionParameter values in the database. + * Initialize from explicit values. + * + * @param connectionID The ID of the associated database record, if any. + * @param identifier The unique identifier associated with this connection. + * @param config The GuacamoleConfiguration associated with this connection. + * @param history All ConnectionRecords associated with this connection. */ - private void initConfiguration() { - ConnectionParameterExample connectionParameterExample = new ConnectionParameterExample(); - connectionParameterExample.createCriteria().andConnection_idEqualTo(connection.getConnection_id()); + public void init(Integer connectionID, String identifier, + GuacamoleConfiguration config, + List history) { - List connectionParameters = connectionParameterDAO.selectByExample(connectionParameterExample); - - configuration = configurationTranslationService.getConfiguration(connection.getProtocol(), connectionParameters); - } - - /** - * Load an existing connection by name. - * @param connectionName - */ - public void initExisting(String connectionName) throws GuacamoleException { - ConnectionExample example = new ConnectionExample(); - example.createCriteria().andConnection_nameEqualTo(connectionName); - List connections; - connections = connectionDAO.selectByExample(example); - if(connections.size() > 1) // the unique constraint should prevent this from happening - throw new GuacamoleException("Multiple connections found named '" + connectionName + "'."); - else if(connections.isEmpty()) - throw new GuacamoleException("No connection found named '" + connectionName + "'."); - - connection = connections.get(0); - - initConfiguration(); - } - - /** - * Initialize from a database record. This also initializes the configuration values. - * @param connection - */ - public void init(net.sourceforge.guacamole.net.auth.mysql.model.Connection connection) { - this.connection = connection; - initConfiguration(); - } - - @Override - public String getIdentifier() { - return connection.getConnection_name(); - } - - @Override - public void setIdentifier(String identifier) { - connection.setConnection_name(identifier); - } - - @Override - public GuacamoleConfiguration getConfiguration() { - return configuration; - } - - @Override - public void setConfiguration(GuacamoleConfiguration config) { - this.configuration = config; + this.connectionID = connectionID; + setIdentifier(identifier); + setConfiguration(config); + this.history.addAll(history); } @Override public GuacamoleSocket connect(GuacamoleClientInformation info) throws GuacamoleException { + // If the current connection is active, and multiple simultaneous connections are not allowed. if(GuacamoleProperties.getProperty(MySQLGuacamoleProperties.MYSQL_DISALLOW_SIMULTANEOUS_CONNECTIONS, false) && activeConnectionSet.contains(getConnectionID())) throw new GuacamoleException("Cannot connect. This connection is in use."); + // Get guacd connection information String host = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_HOSTNAME); int port = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_PORT); - InetGuacamoleSocket inetSocket = new InetGuacamoleSocket(host, port); - ConfiguredGuacamoleSocket configuredSocket = new ConfiguredGuacamoleSocket(inetSocket, configuration); + // Get socket + GuacamoleSocket socket = providerService.getMySQLGuacamoleSocket( + new ConfiguredGuacamoleSocket( + new InetGuacamoleSocket(host, port), + getConfiguration() + ), + getConnectionID() + ); - MySQLGuacamoleSocket mySQLSocket = providerService.getMySQLGuacamoleSocket(configuredSocket, getConnectionID()); - - // mark this connection as active + // Mark this connection as active activeConnectionSet.add(getConnectionID()); - return mySQLSocket; + return socket; } @Override @@ -216,6 +175,7 @@ public class MySQLConnection implements Connection { @Override public List getHistory() throws GuacamoleException { - return providerService.getExistingMySQLConnectionRecords(connection.getConnection_id()); + return Collections.unmodifiableList(history); } + } diff --git a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLUser.java b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLUser.java index 111a89669..e969a61b2 100644 --- a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLUser.java +++ b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/MySQLUser.java @@ -41,7 +41,6 @@ import java.util.Set; import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.net.auth.AbstractUser; import net.sourceforge.guacamole.net.auth.User; -import net.sourceforge.guacamole.net.auth.mysql.model.UserWithBLOBs; import net.sourceforge.guacamole.net.auth.permission.Permission; /** @@ -112,7 +111,7 @@ public class MySQLUser extends AbstractUser { this.userID = userID; setUsername(username); setPassword(password); - permissions.addAll(permissions); + this.permissions.addAll(permissions); } /** 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 45b2f761e..96fbda867 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 @@ -70,6 +70,7 @@ import net.sourceforge.guacamole.net.auth.permission.ConnectionPermission; import net.sourceforge.guacamole.net.auth.permission.Permission; import net.sourceforge.guacamole.net.auth.permission.UserDirectoryPermission; import net.sourceforge.guacamole.net.auth.permission.UserPermission; +import net.sourceforge.guacamole.protocol.GuacamoleConfiguration; /** * A service to retrieve information about what objects a user has permission to. @@ -696,6 +697,7 @@ public class PermissionCheckService { * @param permissionType * @return the list of all connections this user has access to */ + @Deprecated /* FIXME: Totally useless (we only ever need identifiers, and querying ALL CONNECTION DATA will take ages) */ private Set getConnections(int userID, String permissionType) { // If connections available, query them @@ -711,7 +713,12 @@ public class PermissionCheckService { Set affectedConnections = new HashSet(); for(Connection affectedConnection : connectionDBOjects) { MySQLConnection mySQLConnection = mySQLConnectionProvider.get(); - mySQLConnection.init(affectedConnection); + mySQLConnection.init( + affectedConnection.getConnection_id(), + affectedConnection.getConnection_name(), + new GuacamoleConfiguration(), + Collections.EMPTY_LIST + ); affectedConnections.add(mySQLConnection); } diff --git a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ProviderService.java b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ProviderService.java index 1ea85d7f2..c2f5b3902 100644 --- a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ProviderService.java +++ b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/ProviderService.java @@ -38,6 +38,7 @@ package net.sourceforge.guacamole.net.auth.mysql.service; import com.google.inject.Inject; import com.google.inject.Provider; import java.util.ArrayList; +import java.util.Collections; import java.util.List; import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.net.auth.Connection; @@ -48,39 +49,47 @@ import net.sourceforge.guacamole.net.auth.mysql.MySQLGuacamoleSocket; import net.sourceforge.guacamole.net.auth.mysql.MySQLUser; import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionHistoryMapper; import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionMapper; +import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionParameterMapper; import net.sourceforge.guacamole.net.auth.mysql.dao.UserMapper; import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionExample; import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionHistory; import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionHistoryExample; +import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionParameter; +import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionParameterExample; import net.sourceforge.guacamole.net.auth.mysql.model.UserExample; import net.sourceforge.guacamole.net.auth.mysql.model.UserWithBLOBs; import net.sourceforge.guacamole.protocol.ConfiguredGuacamoleSocket; +import net.sourceforge.guacamole.protocol.GuacamoleConfiguration; /** * Provides convenient provider methods for MySQL specific implementations. * @author James Muehlner */ public class ProviderService { - @Inject - UserMapper userDAO; @Inject - ConnectionMapper connectionDAO; + private UserMapper userDAO; @Inject - ConnectionHistoryMapper connectionHistoryDAO; + private ConnectionMapper connectionDAO; @Inject - Provider mySQLUserProvider; + private ConnectionParameterMapper connectionParameterDAO; @Inject - Provider mySQLConnectionProvider; + private ConnectionHistoryMapper connectionHistoryDAO; @Inject - Provider mySQLConnectionRecordProvider; + private Provider mySQLUserProvider; @Inject - Provider mySQLGuacamoleSocketProvider; + private Provider mySQLConnectionProvider; + + @Inject + private Provider mySQLConnectionRecordProvider; + + @Inject + private Provider mySQLGuacamoleSocketProvider; /** * Service for checking permissions. @@ -157,32 +166,17 @@ public class ProviderService { public MySQLUser getExistingMySQLUser(Integer id) { // Query user by ID - UserExample example = new UserExample(); - example.createCriteria().andUser_idEqualTo(id); - List users = userDAO.selectByExampleWithBLOBs(example); + UserWithBLOBs user = userDAO.selectByPrimaryKey(id); // If no user found, return null - if(users.isEmpty()) + if(user == null) return null; // Otherwise, return found user - return getExistingMySQLUser(users.get(0)); + return getExistingMySQLUser(user); } - - /** - * Create a new connection based on the provided object. - * @param connection - * @return the new Connection object. - * @throws GuacamoleException - */ - public MySQLConnection getNewMySQLConnection(Connection connection) throws GuacamoleException { - MySQLConnection mySQLConnection = mySQLConnectionProvider.get(); - mySQLConnection.initNew(connection); - return mySQLConnection; - } - /** * Get the connection based on the connection name of the provided object. * @param connection @@ -200,9 +194,20 @@ public class ProviderService { * @throws GuacamoleException */ public MySQLConnection getExistingMySQLConnection(String name) throws GuacamoleException { - MySQLConnection mySQLConnection = mySQLConnectionProvider.get(); - mySQLConnection.initExisting(name); - return mySQLConnection; + + // Query connection by ID + ConnectionExample example = new ConnectionExample(); + example.createCriteria().andConnection_nameEqualTo(name); + List connections = + connectionDAO.selectByExample(example); + + // If no connection found, return null + if(connections.isEmpty()) + return null; + + // Otherwise, return found connection + return getExistingMySQLConnection(connections.get(0)); + } /** @@ -211,9 +216,35 @@ public class ProviderService { * @return the existing MySQLConnection object. */ public MySQLConnection getExistingMySQLConnection(net.sourceforge.guacamole.net.auth.mysql.model.Connection connection) { + + // Build configuration + GuacamoleConfiguration config = new GuacamoleConfiguration(); + + // Query parameters for configuration + ConnectionParameterExample connectionParameterExample = new ConnectionParameterExample(); + connectionParameterExample.createCriteria().andConnection_idEqualTo(connection.getConnection_id()); + List connectionParameters = + connectionParameterDAO.selectByExample(connectionParameterExample); + + // Set protocol + config.setProtocol(connection.getProtocol()); + + // Set all values for all parameters + for (ConnectionParameter parameter : connectionParameters) + config.setParameter(parameter.getParameter_name(), + parameter.getParameter_value()); + + // Create new MySQLConnection from retrieved data MySQLConnection mySQLConnection = mySQLConnectionProvider.get(); - mySQLConnection.init(connection); + mySQLConnection.init( + connection.getConnection_id(), + connection.getConnection_name(), + config, + Collections.EMPTY_LIST // TODO: Read history + ); + return mySQLConnection; + } /** @@ -222,12 +253,18 @@ public class ProviderService { * @return the existing MySQLConnection object if found, null if not. */ public MySQLConnection getExistingMySQLConnection(Integer id) { - ConnectionExample example = new ConnectionExample(); - example.createCriteria().andConnection_idEqualTo(id); - List connections = connectionDAO.selectByExample(example); - if(connections.isEmpty()) + + // Query connection by ID + net.sourceforge.guacamole.net.auth.mysql.model.Connection connection = + connectionDAO.selectByPrimaryKey(id); + + // If no connection found, return null + if(connection == null) return null; - return getExistingMySQLConnection(connections.get(0)); + + // Otherwise, return found connection + return getExistingMySQLConnection(connection); + } /**