From 6f8b0ba41e87ec20e75336e4c6bc37dbd433f87e Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sun, 1 Mar 2015 12:05:58 -0800 Subject: [PATCH] GUAC-1101: Implement connection and group duplicate checks. Fix username duplicate check. --- .../jdbc/base/DirectoryObjectService.java | 48 ++++++++++--------- .../jdbc/connection/ConnectionMapper.java | 17 +++++++ .../jdbc/connection/ConnectionService.java | 34 ++++++++----- .../ConnectionGroupMapper.java | 17 +++++++ .../ConnectionGroupService.java | 27 +++++++---- .../guacamole/auth/jdbc/user/UserMapper.java | 14 +++++- .../guacamole/auth/jdbc/user/UserService.java | 25 +++++----- .../auth/jdbc/connection/ConnectionMapper.xml | 16 +++++++ .../connectiongroup/ConnectionGroupMapper.xml | 16 +++++++ .../guacamole/auth/jdbc/user/UserMapper.xml | 18 ++++++- 10 files changed, 173 insertions(+), 59 deletions(-) diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/base/DirectoryObjectService.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/base/DirectoryObjectService.java index 1e879b904..fc2bdb331 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/base/DirectoryObjectService.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/base/DirectoryObjectService.java @@ -215,49 +215,50 @@ public abstract class DirectoryObjectService */ Set selectReadableIdentifiersWithin(@Param("user") UserModel user, @Param("parentIdentifier") String parentIdentifier); + + /** + * Selects the connection within the given parent group and having the + * given name. If no such connection exists, null is returned. + * + * @param parentIdentifier + * The identifier of the parent group to search within. + * + * @param name + * The name of the connection to find. + * + * @return + * The connection having the given name within the given parent group, + * or null if no such connection exists. + */ + ConnectionModel selectOneByName(@Param("parentIdentifier") String parentIdentifier, + @Param("name") String name); } \ No newline at end of file diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connection/ConnectionService.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connection/ConnectionService.java index fcc43208b..f7d0b5ac5 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connection/ConnectionService.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connection/ConnectionService.java @@ -147,26 +147,37 @@ public class ConnectionService extends DirectoryObjectService parameterModels = getParameterModels(object); parameterMapper.delete(object.getIdentifier()); - parameterMapper.insert(parameterModels); + if (!parameterModels.isEmpty()) + parameterMapper.insert(parameterModels); } diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connectiongroup/ConnectionGroupMapper.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connectiongroup/ConnectionGroupMapper.java index ad682cd20..a08ef7c20 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connectiongroup/ConnectionGroupMapper.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connectiongroup/ConnectionGroupMapper.java @@ -71,5 +71,22 @@ public interface ConnectionGroupMapper extends DirectoryObjectMapper selectReadableIdentifiersWithin(@Param("user") UserModel user, @Param("parentIdentifier") String parentIdentifier); + + /** + * Selects the connection group within the given parent group and having + * the given name. If no such connection group exists, null is returned. + * + * @param parentIdentifier + * The identifier of the parent group to search within. + * + * @param name + * The name of the connection group to find. + * + * @return + * The connection group having the given name within the given parent + * group, or null if no such connection group exists. + */ + ConnectionGroupModel selectOneByName(@Param("parentIdentifier") String parentIdentifier, + @Param("name") String name); } \ No newline at end of file diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connectiongroup/ConnectionGroupService.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connectiongroup/ConnectionGroupService.java index f144dcb87..cfed2edd0 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connectiongroup/ConnectionGroupService.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/connectiongroup/ConnectionGroupService.java @@ -130,26 +130,37 @@ public class ConnectionGroupService extends DirectoryObjectService { * The user having the given username and password, or null if no such * user exists. */ - UserModel selectByCredentials(@Param("username") String username, + UserModel selectOneByCredentials(@Param("username") String username, @Param("password") String password); + + /** + * Returns the user having the given username, if any. If no such user + * exists, null is returned. + * + * @param username + * The username of the user to return. + * + * @return + * The user having the given username, or null if no such user exists. + */ + UserModel selectOne(@Param("username") String username); } \ No newline at end of file diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/user/UserService.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/user/UserService.java index 0785337ee..1fd24dc54 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/user/UserService.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/glyptodon/guacamole/auth/jdbc/user/UserService.java @@ -118,38 +118,35 @@ public class UserService extends DirectoryObjectService existing = userMapper.select(Collections.singleton(object.getIdentifier())); + Collection existing = userMapper.select(Collections.singleton(model.getIdentifier())); if (!existing.isEmpty()) - throw new GuacamoleClientException("User \"" + object.getIdentifier() + "\" already exists."); + throw new GuacamoleClientException("User \"" + model.getIdentifier() + "\" already exists."); } @Override - protected void validateExistingObject(AuthenticatedUser user, - ModeledUser object) throws GuacamoleException { + protected void validateExistingModel(AuthenticatedUser user, + UserModel model) throws GuacamoleException { // Username must not be blank - if (object.getIdentifier().trim().isEmpty()) + if (model.getIdentifier().trim().isEmpty()) throw new GuacamoleClientException("The username must not be blank."); // Check whether such a user is already present - ModeledUser existing = retrieveObject(user, object.getIdentifier()); + UserModel existing = userMapper.selectOne(model.getIdentifier()); if (existing != null) { - UserModel existingModel = existing.getModel(); - UserModel updatedModel = object.getModel(); - // Do not rename to existing user - if (!existingModel.getObjectID().equals(updatedModel.getObjectID())) - throw new GuacamoleClientException("User \"" + object.getIdentifier() + "\" already exists."); + if (!existing.getObjectID().equals(model.getObjectID())) + throw new GuacamoleClientException("User \"" + model.getIdentifier() + "\" already exists."); } @@ -173,7 +170,7 @@ public class UserService extends DirectoryObjectService + + + DELETE FROM guacamole_connection diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/resources/org/glyptodon/guacamole/auth/jdbc/connectiongroup/ConnectionGroupMapper.xml b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/resources/org/glyptodon/guacamole/auth/jdbc/connectiongroup/ConnectionGroupMapper.xml index 3e3f8c156..4eb20da1c 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/resources/org/glyptodon/guacamole/auth/jdbc/connectiongroup/ConnectionGroupMapper.xml +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/resources/org/glyptodon/guacamole/auth/jdbc/connectiongroup/ConnectionGroupMapper.xml @@ -108,6 +108,22 @@ + + + DELETE FROM guacamole_connection_group diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/resources/org/glyptodon/guacamole/auth/jdbc/user/UserMapper.xml b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/resources/org/glyptodon/guacamole/auth/jdbc/user/UserMapper.xml index b3726eb8f..fb7e6ff55 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/resources/org/glyptodon/guacamole/auth/jdbc/user/UserMapper.xml +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-mysql/src/main/resources/org/glyptodon/guacamole/auth/jdbc/user/UserMapper.xml @@ -87,8 +87,8 @@ - - SELECT user_id, username, @@ -100,6 +100,20 @@ AND password_hash = UNHEX(SHA2(CONCAT(#{password,jdbcType=VARCHAR}, HEX(password_salt)), 256)) + + + DELETE FROM guacamole_user