Ticket #269: Working connection read/write, reorganization.

This commit is contained in:
Michael Jumper
2013-02-26 20:01:57 -08:00
parent c3d21a5e8e
commit 611a613589
5 changed files with 231 additions and 213 deletions

View File

@@ -1,3 +1,6 @@
package net.sourceforge.guacamole.net.auth.mysql;
/* ***** BEGIN LICENSE BLOCK ***** /* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1 * 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. * the terms of any one of the MPL, the GPL or the LGPL.
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
package net.sourceforge.guacamole.net.auth.mysql;
import com.google.common.collect.Lists;
import com.google.inject.Inject; import com.google.inject.Inject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.GuacamoleException;
import net.sourceforge.guacamole.net.auth.Connection; import net.sourceforge.guacamole.net.auth.Connection;
@@ -59,7 +56,8 @@ import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
import org.mybatis.guice.transactional.Transactional; import org.mybatis.guice.transactional.Transactional;
/** /**
* * A MySQL-based implementation of the connection directory.
*
* @author James Muehlner * @author James Muehlner
*/ */
public class ConnectionDirectory implements Directory<String, Connection>{ public class ConnectionDirectory implements Directory<String, Connection>{
@@ -70,18 +68,33 @@ public class ConnectionDirectory implements Directory<String, Connection>{
*/ */
private int user_id; private int user_id;
/**
* Service for checking permissions.
*/
@Inject @Inject
private PermissionCheckService permissionCheckService; private PermissionCheckService permissionCheckService;
/**
* Service for creating and retrieving objects.
*/
@Inject @Inject
private ProviderService providerService; private ProviderService providerService;
/**
* Service for manipulating connections in the database.
*/
@Inject @Inject
private ConnectionMapper connectionDAO; private ConnectionMapper connectionDAO;
/**
* Service for manipulating connection permissions in the database.
*/
@Inject @Inject
private ConnectionPermissionMapper connectionPermissionDAO; private ConnectionPermissionMapper connectionPermissionDAO;
/**
* Service for manipulating connection parameters in the database.
*/
@Inject @Inject
private ConnectionParameterMapper connectionParameterDAO; private ConnectionParameterMapper connectionParameterDAO;
@@ -115,124 +128,126 @@ public class ConnectionDirectory implements Directory<String, Connection>{
@Transactional @Transactional
@Override @Override
public void add(Connection object) throws GuacamoleException { public void add(Connection object) throws GuacamoleException {
// Verify permission to create
permissionCheckService.verifyCreateConnectionPermission(this.user_id); permissionCheckService.verifyCreateConnectionPermission(this.user_id);
MySQLConnection mySQLConnection = providerService.getNewMySQLConnection(object); // Create database object for insert
connectionDAO.insert(mySQLConnection.getConnection()); 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(); ConnectionPermissionKey newConnectionPermission = new ConnectionPermissionKey();
newConnectionPermission.setUser_id(this.user_id); newConnectionPermission.setUser_id(this.user_id);
newConnectionPermission.setConnection_id(mySQLConnection.getConnectionID()); newConnectionPermission.setConnection_id(connection.getConnection_id());
newConnectionPermission.setPermission(MySQLConstants.USER_READ);
// Read permission
newConnectionPermission.setPermission(MySQLConstants.CONNECTION_READ);
connectionPermissionDAO.insert(newConnectionPermission); connectionPermissionDAO.insert(newConnectionPermission);
newConnectionPermission.setPermission(MySQLConstants.USER_UPDATE);
// Update permission
newConnectionPermission.setPermission(MySQLConstants.CONNECTION_UPDATE);
connectionPermissionDAO.insert(newConnectionPermission); connectionPermissionDAO.insert(newConnectionPermission);
newConnectionPermission.setPermission(MySQLConstants.USER_DELETE);
// Delete permission
newConnectionPermission.setPermission(MySQLConstants.CONNECTION_DELETE);
connectionPermissionDAO.insert(newConnectionPermission); connectionPermissionDAO.insert(newConnectionPermission);
newConnectionPermission.setPermission(MySQLConstants.USER_ADMINISTER);
// Administer permission
newConnectionPermission.setPermission(MySQLConstants.CONNECTION_ADMINISTER);
connectionPermissionDAO.insert(newConnectionPermission); connectionPermissionDAO.insert(newConnectionPermission);
} }
/** /**
* Saves the values of the configuration to the database * Inserts all parameter values from the given configuration into the
* @param connection * 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) { private void createConfigurationValues(int connection_id,
GuacamoleConfiguration configuration = mySQLConnection.getConfiguration(); GuacamoleConfiguration config) {
Map<String, String> existingConfiguration = new HashMap<String, String>();
ConnectionParameterExample example = new ConnectionParameterExample();
example.createCriteria().andConnection_idEqualTo(mySQLConnection.getConnectionID());
List<ConnectionParameter> connectionParameters = connectionParameterDAO.selectByExample(example);
for(ConnectionParameter parameter : connectionParameters)
existingConfiguration.put(parameter.getParameter_name(), parameter.getParameter_value());
List<ConnectionParameter> parametersToInsert = new ArrayList<ConnectionParameter>(); // Insert new parameters for each parameter in the config
List<ConnectionParameter> parametersToUpdate = new ArrayList<ConnectionParameter>(); for (String name : config.getParameterNames()) {
Set<String> parameterNames = configuration.getParameterNames(); // Create a ConnectionParameter based on the current parameter
ConnectionParameter parameter = new ConnectionParameter();
for(String parameterName : parameterNames) { parameter.setConnection_id(connection_id);
String parameterValue = configuration.getParameter(parameterName); parameter.setParameter_name(name);
if(existingConfiguration.containsKey(parameterName)) { parameter.setParameter_value(config.getParameter(name));
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());
// Insert connection parameter
connectionParameterDAO.insert(parameter); connectionParameterDAO.insert(parameter);
} }
} }
@Transactional @Transactional
@Override @Override
public void update(Connection object) throws GuacamoleException { public void update(Connection object) throws GuacamoleException {
// Verify permission to update
permissionCheckService.verifyConnectionUpdateAccess(this.user_id, object.getIdentifier()); permissionCheckService.verifyConnectionUpdateAccess(this.user_id, object.getIdentifier());
MySQLConnection mySQLConnection = providerService.getExistingMySQLConnection(object); // TODO: Rely on update() to be given MySQLConnection
connectionDAO.updateByPrimaryKey(mySQLConnection.getConnection()); 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 @Transactional
@Override @Override
public void remove(String identifier) throws GuacamoleException { public void remove(String identifier) throws GuacamoleException {
// Verify permission to delete
permissionCheckService.verifyConnectionDeleteAccess(this.user_id, identifier); permissionCheckService.verifyConnectionDeleteAccess(this.user_id, identifier);
MySQLConnection mySQLConnection = providerService.getExistingMySQLConnection(identifier); MySQLConnection mySQLConnection = providerService.getExistingMySQLConnection(identifier);
// delete all configuration values // Delete all configuration values
ConnectionParameterExample connectionParameterExample = new ConnectionParameterExample(); ConnectionParameterExample connectionParameterExample = new ConnectionParameterExample();
connectionParameterExample.createCriteria().andConnection_idEqualTo(mySQLConnection.getConnectionID()); connectionParameterExample.createCriteria().andConnection_idEqualTo(mySQLConnection.getConnectionID());
connectionParameterDAO.deleteByExample(connectionParameterExample); connectionParameterDAO.deleteByExample(connectionParameterExample);
// delete all permissions that refer to this connection // Delete all permissions that refer to this connection
ConnectionPermissionExample connectionPermissionExample = new ConnectionPermissionExample(); ConnectionPermissionExample connectionPermissionExample = new ConnectionPermissionExample();
connectionPermissionExample.createCriteria().andConnection_idEqualTo(mySQLConnection.getConnectionID()); connectionPermissionExample.createCriteria().andConnection_idEqualTo(mySQLConnection.getConnectionID());
connectionPermissionDAO.deleteByExample(connectionPermissionExample); connectionPermissionDAO.deleteByExample(connectionPermissionExample);
// delete the connection itself // Delete the connection itself
connectionDAO.deleteByPrimaryKey(mySQLConnection.getConnectionID()); connectionDAO.deleteByPrimaryKey(mySQLConnection.getConnectionID());
} }
} }

View File

@@ -36,19 +36,16 @@
package net.sourceforge.guacamole.net.auth.mysql; package net.sourceforge.guacamole.net.auth.mysql;
import com.google.inject.Inject; import com.google.inject.Inject;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.GuacamoleException;
import net.sourceforge.guacamole.net.GuacamoleSocket; import net.sourceforge.guacamole.net.GuacamoleSocket;
import net.sourceforge.guacamole.net.InetGuacamoleSocket; 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.Connection;
import net.sourceforge.guacamole.net.auth.ConnectionRecord; 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.properties.MySQLGuacamoleProperties;
import net.sourceforge.guacamole.net.auth.mysql.service.ConfigurationTranslationService;
import net.sourceforge.guacamole.net.auth.mysql.service.ProviderService; import net.sourceforge.guacamole.net.auth.mysql.service.ProviderService;
import net.sourceforge.guacamole.properties.GuacamoleProperties; import net.sourceforge.guacamole.properties.GuacamoleProperties;
import net.sourceforge.guacamole.protocol.ConfiguredGuacamoleSocket; import net.sourceforge.guacamole.protocol.ConfiguredGuacamoleSocket;
@@ -59,13 +56,17 @@ import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
* A MySQL based implementation of the Connection object. * A MySQL based implementation of the Connection object.
* @author James Muehlner * @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<ConnectionRecord> history = new ArrayList<ConnectionRecord>();
@Inject @Inject
private ProviderService providerService; private ProviderService providerService;
@@ -73,126 +74,84 @@ public class MySQLConnection implements Connection {
@Inject @Inject
private ActiveConnectionSet activeConnectionSet; 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. * Create a default, empty connection.
*/ */
public MySQLConnection() { public MySQLConnection() {
connection = new net.sourceforge.guacamole.net.auth.mysql.model.Connection();
configuration = new GuacamoleConfiguration();
} }
/** /**
* Get the ID of the underlying connection record. * Get the ID of the corresponding connection record.
* @return the ID of the underlying connection * @return The ID of the corresponding connection, if any.
*/ */
public int getConnectionID() { public Integer getConnectionID() {
return connection.getConnection_id(); return connectionID;
} }
/** /**
* Get the underlying connection database record. * Sets the ID of the corresponding connection record.
* @return the underlying connection record. * @param connectionID The ID to assign to this connection.
*/ */
public net.sourceforge.guacamole.net.auth.mysql.model.Connection getConnection() { public void setConnectionID(Integer connectionID) {
return connection; this.connectionID = connectionID;
} }
/** /**
* Create a new MySQLConnection from this new connection. This is a connection that has not yet been inserted. * Initialize a new MySQLConnection from this existing connection.
* @param 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) { public void init(Connection connection) throws GuacamoleException {
this.connection.setConnection_name(connection.getIdentifier()); init(null, connection.getIdentifier(), connection.getConfiguration(), connection.getHistory());
this.configuration = connection.getConfiguration();
} }
/** /**
* 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() { public void init(Integer connectionID, String identifier,
ConnectionParameterExample connectionParameterExample = new ConnectionParameterExample(); GuacamoleConfiguration config,
connectionParameterExample.createCriteria().andConnection_idEqualTo(connection.getConnection_id()); List<? extends ConnectionRecord> history) {
List<ConnectionParameter> connectionParameters = connectionParameterDAO.selectByExample(connectionParameterExample); this.connectionID = connectionID;
setIdentifier(identifier);
configuration = configurationTranslationService.getConfiguration(connection.getProtocol(), connectionParameters); setConfiguration(config);
} this.history.addAll(history);
/**
* 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<net.sourceforge.guacamole.net.auth.mysql.model.Connection> 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;
} }
@Override @Override
public GuacamoleSocket connect(GuacamoleClientInformation info) throws GuacamoleException { public GuacamoleSocket connect(GuacamoleClientInformation info) throws GuacamoleException {
// If the current connection is active, and multiple simultaneous connections are not allowed. // If the current connection is active, and multiple simultaneous connections are not allowed.
if(GuacamoleProperties.getProperty(MySQLGuacamoleProperties.MYSQL_DISALLOW_SIMULTANEOUS_CONNECTIONS, false) if(GuacamoleProperties.getProperty(MySQLGuacamoleProperties.MYSQL_DISALLOW_SIMULTANEOUS_CONNECTIONS, false)
&& activeConnectionSet.contains(getConnectionID())) && activeConnectionSet.contains(getConnectionID()))
throw new GuacamoleException("Cannot connect. This connection is in use."); throw new GuacamoleException("Cannot connect. This connection is in use.");
// Get guacd connection information
String host = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_HOSTNAME); String host = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_HOSTNAME);
int port = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_PORT); int port = GuacamoleProperties.getProperty(GuacamoleProperties.GUACD_PORT);
InetGuacamoleSocket inetSocket = new InetGuacamoleSocket(host, port); // Get socket
ConfiguredGuacamoleSocket configuredSocket = new ConfiguredGuacamoleSocket(inetSocket, configuration); 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()); activeConnectionSet.add(getConnectionID());
return mySQLSocket; return socket;
} }
@Override @Override
@@ -216,6 +175,7 @@ public class MySQLConnection implements Connection {
@Override @Override
public List<? extends ConnectionRecord> getHistory() throws GuacamoleException { public List<? extends ConnectionRecord> getHistory() throws GuacamoleException {
return providerService.getExistingMySQLConnectionRecords(connection.getConnection_id()); return Collections.unmodifiableList(history);
} }
} }

View File

@@ -41,7 +41,6 @@ import java.util.Set;
import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.GuacamoleException;
import net.sourceforge.guacamole.net.auth.AbstractUser; import net.sourceforge.guacamole.net.auth.AbstractUser;
import net.sourceforge.guacamole.net.auth.User; import net.sourceforge.guacamole.net.auth.User;
import net.sourceforge.guacamole.net.auth.mysql.model.UserWithBLOBs;
import net.sourceforge.guacamole.net.auth.permission.Permission; import net.sourceforge.guacamole.net.auth.permission.Permission;
/** /**
@@ -112,7 +111,7 @@ public class MySQLUser extends AbstractUser {
this.userID = userID; this.userID = userID;
setUsername(username); setUsername(username);
setPassword(password); setPassword(password);
permissions.addAll(permissions); this.permissions.addAll(permissions);
} }
/** /**

View File

@@ -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.Permission;
import net.sourceforge.guacamole.net.auth.permission.UserDirectoryPermission; import net.sourceforge.guacamole.net.auth.permission.UserDirectoryPermission;
import net.sourceforge.guacamole.net.auth.permission.UserPermission; 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. * A service to retrieve information about what objects a user has permission to.
@@ -696,6 +697,7 @@ public class PermissionCheckService {
* @param permissionType * @param permissionType
* @return the list of all connections this user has access to * @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<MySQLConnection> getConnections(int userID, String permissionType) { private Set<MySQLConnection> getConnections(int userID, String permissionType) {
// If connections available, query them // If connections available, query them
@@ -711,7 +713,12 @@ public class PermissionCheckService {
Set<MySQLConnection> affectedConnections = new HashSet<MySQLConnection>(); Set<MySQLConnection> affectedConnections = new HashSet<MySQLConnection>();
for(Connection affectedConnection : connectionDBOjects) { for(Connection affectedConnection : connectionDBOjects) {
MySQLConnection mySQLConnection = mySQLConnectionProvider.get(); MySQLConnection mySQLConnection = mySQLConnectionProvider.get();
mySQLConnection.init(affectedConnection); mySQLConnection.init(
affectedConnection.getConnection_id(),
affectedConnection.getConnection_name(),
new GuacamoleConfiguration(),
Collections.EMPTY_LIST
);
affectedConnections.add(mySQLConnection); affectedConnections.add(mySQLConnection);
} }

View File

@@ -38,6 +38,7 @@ package net.sourceforge.guacamole.net.auth.mysql.service;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Provider; import com.google.inject.Provider;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import net.sourceforge.guacamole.GuacamoleException; import net.sourceforge.guacamole.GuacamoleException;
import net.sourceforge.guacamole.net.auth.Connection; 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.MySQLUser;
import net.sourceforge.guacamole.net.auth.mysql.dao.ConnectionHistoryMapper; 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.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.dao.UserMapper;
import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionExample; 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.ConnectionHistory;
import net.sourceforge.guacamole.net.auth.mysql.model.ConnectionHistoryExample; 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.UserExample;
import net.sourceforge.guacamole.net.auth.mysql.model.UserWithBLOBs; import net.sourceforge.guacamole.net.auth.mysql.model.UserWithBLOBs;
import net.sourceforge.guacamole.protocol.ConfiguredGuacamoleSocket; import net.sourceforge.guacamole.protocol.ConfiguredGuacamoleSocket;
import net.sourceforge.guacamole.protocol.GuacamoleConfiguration;
/** /**
* Provides convenient provider methods for MySQL specific implementations. * Provides convenient provider methods for MySQL specific implementations.
* @author James Muehlner * @author James Muehlner
*/ */
public class ProviderService { public class ProviderService {
@Inject
UserMapper userDAO;
@Inject @Inject
ConnectionMapper connectionDAO; private UserMapper userDAO;
@Inject @Inject
ConnectionHistoryMapper connectionHistoryDAO; private ConnectionMapper connectionDAO;
@Inject @Inject
Provider<MySQLUser> mySQLUserProvider; private ConnectionParameterMapper connectionParameterDAO;
@Inject @Inject
Provider<MySQLConnection> mySQLConnectionProvider; private ConnectionHistoryMapper connectionHistoryDAO;
@Inject @Inject
Provider<MySQLConnectionRecord> mySQLConnectionRecordProvider; private Provider<MySQLUser> mySQLUserProvider;
@Inject @Inject
Provider<MySQLGuacamoleSocket> mySQLGuacamoleSocketProvider; private Provider<MySQLConnection> mySQLConnectionProvider;
@Inject
private Provider<MySQLConnectionRecord> mySQLConnectionRecordProvider;
@Inject
private Provider<MySQLGuacamoleSocket> mySQLGuacamoleSocketProvider;
/** /**
* Service for checking permissions. * Service for checking permissions.
@@ -157,32 +166,17 @@ public class ProviderService {
public MySQLUser getExistingMySQLUser(Integer id) { public MySQLUser getExistingMySQLUser(Integer id) {
// Query user by ID // Query user by ID
UserExample example = new UserExample(); UserWithBLOBs user = userDAO.selectByPrimaryKey(id);
example.createCriteria().andUser_idEqualTo(id);
List<UserWithBLOBs> users = userDAO.selectByExampleWithBLOBs(example);
// If no user found, return null // If no user found, return null
if(users.isEmpty()) if(user == null)
return null; return null;
// Otherwise, return found user // 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. * Get the connection based on the connection name of the provided object.
* @param connection * @param connection
@@ -200,9 +194,20 @@ public class ProviderService {
* @throws GuacamoleException * @throws GuacamoleException
*/ */
public MySQLConnection getExistingMySQLConnection(String name) throws GuacamoleException { public MySQLConnection getExistingMySQLConnection(String name) throws GuacamoleException {
MySQLConnection mySQLConnection = mySQLConnectionProvider.get();
mySQLConnection.initExisting(name); // Query connection by ID
return mySQLConnection; ConnectionExample example = new ConnectionExample();
example.createCriteria().andConnection_nameEqualTo(name);
List<net.sourceforge.guacamole.net.auth.mysql.model.Connection> 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. * @return the existing MySQLConnection object.
*/ */
public MySQLConnection getExistingMySQLConnection(net.sourceforge.guacamole.net.auth.mysql.model.Connection connection) { 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<ConnectionParameter> 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 mySQLConnection = mySQLConnectionProvider.get();
mySQLConnection.init(connection); mySQLConnection.init(
connection.getConnection_id(),
connection.getConnection_name(),
config,
Collections.EMPTY_LIST // TODO: Read history
);
return mySQLConnection; return mySQLConnection;
} }
/** /**
@@ -222,12 +253,18 @@ public class ProviderService {
* @return the existing MySQLConnection object if found, null if not. * @return the existing MySQLConnection object if found, null if not.
*/ */
public MySQLConnection getExistingMySQLConnection(Integer id) { public MySQLConnection getExistingMySQLConnection(Integer id) {
ConnectionExample example = new ConnectionExample();
example.createCriteria().andConnection_idEqualTo(id); // Query connection by ID
List<net.sourceforge.guacamole.net.auth.mysql.model.Connection> connections = connectionDAO.selectByExample(example); net.sourceforge.guacamole.net.auth.mysql.model.Connection connection =
if(connections.isEmpty()) connectionDAO.selectByPrimaryKey(id);
// If no connection found, return null
if(connection == null)
return null; return null;
return getExistingMySQLConnection(connections.get(0));
// Otherwise, return found connection
return getExistingMySQLConnection(connection);
} }
/** /**