From d95e05961275a773aa689adac6dae7d204426201 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 3 Apr 2018 11:17:31 -0700 Subject: [PATCH] GUACAMOLE-220: Refactor user-related model objects and services to leverage the base "entity" model. --- .../guacamole/auth/jdbc/base/EntityModel.java | 113 ++++++++++++++++++ .../guacamole/auth/jdbc/base/EntityType.java | 38 ++++++ .../base/ModeledDirectoryObjectService.java | 3 +- .../ModeledObjectPermissionService.java | 3 +- .../permission/ObjectPermissionMapper.java | 20 ++-- .../jdbc/permission/PermissionMapper.java | 13 +- .../auth/jdbc/permission/PermissionModel.java | 50 ++------ .../permission/SystemPermissionMapper.java | 12 +- .../permission/SystemPermissionService.java | 3 +- .../guacamole/auth/jdbc/user/UserModel.java | 6 +- .../guacamole/auth/jdbc/user/UserService.java | 5 +- 11 files changed, 195 insertions(+), 71 deletions(-) create mode 100644 extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/EntityModel.java create mode 100644 extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/EntityType.java diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/EntityModel.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/EntityModel.java new file mode 100644 index 000000000..c42db16b9 --- /dev/null +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/EntityModel.java @@ -0,0 +1,113 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.guacamole.auth.jdbc.base; + +/** + * Base representation of a Guacamole object that can be granted permissions + * (an "entity"), such as a user or user group, as represented in the database. + * Each entity has three base properties: + * + * 1. The "entityID", which points to the common entry in the + * guacamole_entity table and is common to any type of entity. + * + * 2. The "objectID", which points to the type-specific entry for the object + * in question (ie: an entry in guacamole_user or guacamole_user_group). + * + * 3. The "identifier", which contains the unique "name" value defined for + * the entity within the guacamole_entity table. + */ +public abstract class EntityModel extends ObjectModel { + + /** + * The ID of the entity entry which corresponds to this object in the + * database, if any. Note that this is distinct from the objectID, + * inherited from ObjectModel, which is specific to the actual type of + * object represented by the entity. + */ + private Integer entityID; + + /** + * The type of object represented by the entity (user or user group). + */ + private EntityType type; + + /** + * Creates a new, empty entity. + */ + public EntityModel() { + } + + /** + * Creates a new entity of the given type which is otherwise empty. + * + * @param type + * The type to assign to the new entity. + */ + public EntityModel(EntityType type) { + this.type = type; + } + + /** + * Returns the ID of the entity entry which corresponds to this object in + * the database, if it exists. Note that this is distinct from the objectID, + * inherited from ObjectModel, which is specific to the actual type of + * object represented by the entity. + * + * @return + * The ID of this entity in the database, or null if this entity was + * not retrieved from the database. + */ + public Integer getEntityID() { + return entityID; + } + + /** + * Sets the ID of this entity to the given value. + * + * @param entityID + * The ID to assign to this entity. + */ + public void setEntityID(Integer entityID) { + this.entityID = entityID; + } + + /** + * Returns the type of object represented by the entity. Each entity may be + * either a user or a user group. + * + * @return + * The type of object represented by the entity. + */ + public EntityType getEntityType() { + return type; + } + + /** + * Sets the type of object represented by the entity. Each entity may be + * either a user or a user group. + * + * @param type + * The type of object represented by the entity. + */ + public void setEntityType(EntityType type) { + this.type = type; + } + +} diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/EntityType.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/EntityType.java new file mode 100644 index 000000000..9b1f1edc9 --- /dev/null +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/EntityType.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.guacamole.auth.jdbc.base; + +/** + * The type of object represented by an entity. Each entity may represent + * either a user or a user group. + */ +public enum EntityType { + + /** + * An individual user. + */ + USER, + + /** + * A group of users and/or other groups. + */ + USER_GROUP + +} diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/ModeledDirectoryObjectService.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/ModeledDirectoryObjectService.java index 21508c471..3e3e707d5 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/ModeledDirectoryObjectService.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/base/ModeledDirectoryObjectService.java @@ -432,8 +432,7 @@ public abstract class ModeledDirectoryObjectService selectAccessibleIdentifiers(@Param("user") UserModel user, + Collection selectAccessibleIdentifiers(@Param("entity") EntityModel entity, @Param("permissions") Collection permissions, @Param("identifiers") Collection identifiers); diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/permission/PermissionMapper.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/permission/PermissionMapper.java index d49dc30c3..7b476b362 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/permission/PermissionMapper.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/permission/PermissionMapper.java @@ -20,7 +20,7 @@ package org.apache.guacamole.auth.jdbc.permission; import java.util.Collection; -import org.apache.guacamole.auth.jdbc.user.UserModel; +import org.apache.guacamole.auth.jdbc.base.EntityModel; import org.apache.ibatis.annotations.Param; /** @@ -32,15 +32,16 @@ import org.apache.ibatis.annotations.Param; public interface PermissionMapper { /** - * Retrieves all permissions associated with the given user. + * Retrieves all permissions associated with the given entity (user or user + * group). * - * @param user - * The user to retrieve permissions for. + * @param entity + * The entity to retrieve permissions for. * * @return - * All permissions associated with the given user. + * All permissions associated with the given entity. */ - Collection select(@Param("user") UserModel user); + Collection select(@Param("entity") EntityModel entity); /** * Inserts the given permissions into the database. If any permissions diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/permission/PermissionModel.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/permission/PermissionModel.java index fbc3e8de4..da1ec2d28 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/permission/PermissionModel.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/permission/PermissionModel.java @@ -21,7 +21,7 @@ package org.apache.guacamole.auth.jdbc.permission; /** * Generic base permission model which grants a permission of a particular type - * to a specific user. + * to a specific entity (user or user group). * * @param * The type of permissions allowed within this model. @@ -29,14 +29,9 @@ package org.apache.guacamole.auth.jdbc.permission; public abstract class PermissionModel { /** - * The database ID of the user to whom this permission is granted. + * The database ID of the entity to whom this permission is granted. */ - private Integer userID; - - /** - * The username of the user to whom this permission is granted. - */ - private String username; + private Integer entityID; /** * The type of action granted by this permission. @@ -44,43 +39,24 @@ public abstract class PermissionModel { private PermissionType type; /** - * Returns the database ID of the user to whom this permission is granted. + * Returns the database ID of the entity to whom this permission is + * granted. * * @return - * The database ID of the user to whom this permission is granted. + * The database ID of the entity to whom this permission is granted. */ - public Integer getUserID() { - return userID; + public Integer getEntityID() { + return entityID; } /** - * Sets the database ID of the user to whom this permission is granted. + * Sets the database ID of the entity to whom this permission is granted. * - * @param userID - * The database ID of the user to whom this permission is granted. + * @param entityID + * The database ID of the entity to whom this permission is granted. */ - public void setUserID(Integer userID) { - this.userID = userID; - } - - /** - * Returns the username of the user to whom this permission is granted. - * - * @return - * The username of the user to whom this permission is granted. - */ - public String getUsername() { - return username; - } - - /** - * Sets the username of the user to whom this permission is granted. - * - * @param username - * The username of the user to whom this permission is granted. - */ - public void setUsername(String username) { - this.username = username; + public void setEntityID(Integer entityID) { + this.entityID = entityID; } /** diff --git a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/permission/SystemPermissionMapper.java b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/permission/SystemPermissionMapper.java index 929d6e9aa..738062c2a 100644 --- a/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/permission/SystemPermissionMapper.java +++ b/extensions/guacamole-auth-jdbc/modules/guacamole-auth-jdbc-base/src/main/java/org/apache/guacamole/auth/jdbc/permission/SystemPermissionMapper.java @@ -19,7 +19,7 @@ package org.apache.guacamole.auth.jdbc.permission; -import org.apache.guacamole.auth.jdbc.user.UserModel; +import org.apache.guacamole.auth.jdbc.base.EntityModel; import org.apache.ibatis.annotations.Param; import org.apache.guacamole.net.auth.permission.SystemPermission; @@ -30,19 +30,19 @@ public interface SystemPermissionMapper extends PermissionMapper