From c1ef4bfdd2c544502f4409542fef97d300b5b584 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Sat, 14 Feb 2015 00:14:26 -0800 Subject: [PATCH] GUAC-1101: Fix user creation/deletion. Fix system permission modification. --- .../guacamole/net/auth/mysql/MySQLUser.java | 22 +++++-- .../net/auth/mysql/UserDirectory.java | 3 +- .../mysql/service/DirectoryObjectService.java | 48 ++++++++++---- .../service/SystemPermissionService.java | 65 +++++-------------- .../net/auth/mysql/service/UserService.java | 19 +++++- .../auth/mysql/dao/SystemPermissionMapper.xml | 3 +- .../net/auth/mysql/dao/UserMapper.xml | 12 ++-- 7 files changed, 92 insertions(+), 80 deletions(-) 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 fd8d8fa49..2a0cdf377 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 @@ -133,14 +133,22 @@ public class MySQLUser implements User, DirectoryObject { // Store plaintext password internally this.password = password; - - // Generate new salt and hash given password using newly-generated salt - byte[] salt = saltService.generateSalt(); - byte[] hash = encryptionService.createPasswordHash(password, salt); - // Set stored salt and hash - userModel.setPasswordSalt(salt); - userModel.setPasswordHash(hash); + // If no password provided, clear password salt and hash + if (password == null) { + userModel.setPasswordSalt(null); + userModel.setPasswordHash(null); + } + + // Otherwise generate new salt and hash given password using newly-generated salt + else { + byte[] salt = saltService.generateSalt(); + byte[] hash = encryptionService.createPasswordHash(password, salt); + + // Set stored salt and hash + userModel.setPasswordSalt(salt); + userModel.setPasswordHash(hash); + } } diff --git a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/UserDirectory.java b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/UserDirectory.java index 3b3e30882..542f5e8d8 100644 --- a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/UserDirectory.java +++ b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/UserDirectory.java @@ -92,8 +92,7 @@ public class UserDirectory implements Directory { @Override @Transactional public void add(User object) throws GuacamoleException { - MySQLUser user = (MySQLUser) object; - userService.createObject(currentUser, user); + userService.createObject(currentUser, object); } @Override diff --git a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/DirectoryObjectService.java b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/DirectoryObjectService.java index 016adb54a..8bbc6b360 100644 --- a/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/DirectoryObjectService.java +++ b/extensions/guacamole-auth-mysql/src/main/java/net/sourceforge/guacamole/net/auth/mysql/service/DirectoryObjectService.java @@ -40,14 +40,20 @@ import org.glyptodon.guacamole.net.auth.permission.ObjectPermissionSet; * permissions of the current user. * * @author Michael Jumper - * @param - * The type of object this service provides access to. + * @param + * The specific internal implementation of the type of object this service + * provides access to. + * + * @param + * The external interface or implementation of the type of object this + * service provides access to, as defined by the guacamole-ext API. * * @param - * The underlying model object used to represent ObjectType in the + * The underlying model object used to represent InternalType in the * database. */ -public abstract class DirectoryObjectService, ModelType> { +public abstract class DirectoryObjectService, + ExternalType, ModelType> { /** * Returns an instance of a mapper for the type of object used by this @@ -72,9 +78,25 @@ public abstract class DirectoryObjectService getObjectInstances(AuthenticatedUser currentUser, + protected Collection getObjectInstances(AuthenticatedUser currentUser, Collection models) { // Create new collection of objects by manually converting each model - Collection objects = new ArrayList(models.size()); + Collection objects = new ArrayList(models.size()); for (ModelType model : models) objects.add(getObjectInstance(currentUser, model)); @@ -154,11 +176,11 @@ public abstract class DirectoryObjectService objects = retrieveObjects(user, Collections.singleton(identifier)); + Collection objects = retrieveObjects(user, Collections.singleton(identifier)); // If no such object, return null if (objects.isEmpty()) @@ -189,7 +211,7 @@ public abstract class DirectoryObjectService retrieveObjects(AuthenticatedUser user, + public Collection retrieveObjects(AuthenticatedUser user, Collection identifiers) throws GuacamoleException { // Do not query if no identifiers given @@ -226,12 +248,12 @@ public abstract class DirectoryObjectService { +public class UserService extends DirectoryObjectService { /** * Mapper for accessing users. @@ -68,6 +69,22 @@ public class UserService extends DirectoryObjectService { return user; } + @Override + protected UserModel getModelInstance(AuthenticatedUser currentUser, + final User object) { + + // Create new MySQLUser backed by blank model + UserModel model = new UserModel(); + MySQLUser user = getObjectInstance(currentUser, model); + + // Set model contents through MySQLUser, copying the provided user + user.setIdentifier(object.getIdentifier()); + user.setPassword(object.getPassword()); + + return model; + + } + @Override protected boolean hasCreatePermission(AuthenticatedUser user) throws GuacamoleException { diff --git a/extensions/guacamole-auth-mysql/src/main/resources/net/sourceforge/guacamole/net/auth/mysql/dao/SystemPermissionMapper.xml b/extensions/guacamole-auth-mysql/src/main/resources/net/sourceforge/guacamole/net/auth/mysql/dao/SystemPermissionMapper.xml index ae80c70be..8b8e0f8eb 100644 --- a/extensions/guacamole-auth-mysql/src/main/resources/net/sourceforge/guacamole/net/auth/mysql/dao/SystemPermissionMapper.xml +++ b/extensions/guacamole-auth-mysql/src/main/resources/net/sourceforge/guacamole/net/auth/mysql/dao/SystemPermissionMapper.xml @@ -83,8 +83,7 @@ permission ) VALUES - + (#{permission.userID,jdbcType=INTEGER}, #{permission.type,jdbcType=VARCHAR}) diff --git a/extensions/guacamole-auth-mysql/src/main/resources/net/sourceforge/guacamole/net/auth/mysql/dao/UserMapper.xml b/extensions/guacamole-auth-mysql/src/main/resources/net/sourceforge/guacamole/net/auth/mysql/dao/UserMapper.xml index 32fa591fc..fe149f1ac 100644 --- a/extensions/guacamole-auth-mysql/src/main/resources/net/sourceforge/guacamole/net/auth/mysql/dao/UserMapper.xml +++ b/extensions/guacamole-auth-mysql/src/main/resources/net/sourceforge/guacamole/net/auth/mysql/dao/UserMapper.xml @@ -115,9 +115,9 @@ password_salt ) VALUES ( - #{username,jdbcType=VARCHAR}, - #{passwordHash,jdbcType=BINARY}, - #{passwordSalt,jdbcType=BINARY} + #{object.username,jdbcType=VARCHAR}, + #{object.passwordHash,jdbcType=BINARY}, + #{object.passwordSalt,jdbcType=BINARY} ) @@ -129,9 +129,9 @@ UPDATE guacamole_user - SET password_hash = #{passwordHash,jdbcType=BINARY}, - password_salt = #{passwordSalt,jdbcType=BINARY} - WHERE user_id = #{userID,jdbcType=VARCHAR} + SET password_hash = #{object.passwordHash,jdbcType=BINARY}, + password_salt = #{object.passwordSalt,jdbcType=BINARY} + WHERE user_id = #{object.userID,jdbcType=VARCHAR} \ No newline at end of file