GUACAMOLE-220: Implement base API changes within database auth allowing for permission inheritance.

This commit is contained in:
Michael Jumper
2018-04-03 21:32:38 -07:00
parent 72bac09f43
commit 0a69630cbb
16 changed files with 198 additions and 99 deletions

View File

@@ -23,7 +23,6 @@ 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.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleException;
@@ -58,26 +57,23 @@ public class ActiveConnectionPermissionService
private Provider<ActiveConnectionPermissionSet> activeConnectionPermissionSetProvider; private Provider<ActiveConnectionPermissionSet> activeConnectionPermissionSetProvider;
@Override @Override
public ObjectPermission retrievePermission(ModeledAuthenticatedUser user, public boolean hasPermission(ModeledAuthenticatedUser user,
ModeledUser targetUser, ObjectPermission.Type type, ModeledUser targetUser, ObjectPermission.Type type,
String identifier) throws GuacamoleException { String identifier, boolean inherit) throws GuacamoleException {
// Retrieve permissions // Retrieve permissions
Set<ObjectPermission> permissions = retrievePermissions(user, targetUser); Set<ObjectPermission> permissions = retrievePermissions(user, targetUser, inherit);
// If retrieved permissions contains the requested permission, return it // Permission is granted if retrieved permissions contains the
// requested permission
ObjectPermission permission = new ObjectPermission(type, identifier); ObjectPermission permission = new ObjectPermission(type, identifier);
if (permissions.contains(permission)) return permissions.contains(permission);
return permission;
// Otherwise, no such permission
return null;
} }
@Override @Override
public Set<ObjectPermission> retrievePermissions(ModeledAuthenticatedUser user, public Set<ObjectPermission> retrievePermissions(ModeledAuthenticatedUser user,
ModeledUser targetUser) throws GuacamoleException { ModeledUser targetUser, boolean inherit) throws GuacamoleException {
// Retrieve permissions only if allowed // Retrieve permissions only if allowed
if (canReadPermissions(user, targetUser)) { if (canReadPermissions(user, targetUser)) {
@@ -113,9 +109,9 @@ public class ActiveConnectionPermissionService
@Override @Override
public Collection<String> retrieveAccessibleIdentifiers(ModeledAuthenticatedUser user, public Collection<String> retrieveAccessibleIdentifiers(ModeledAuthenticatedUser user,
ModeledUser targetUser, Collection<ObjectPermission.Type> permissionTypes, ModeledUser targetUser, Collection<ObjectPermission.Type> permissionTypes,
Collection<String> identifiers) throws GuacamoleException { Collection<String> identifiers, boolean inherit) throws GuacamoleException {
Set<ObjectPermission> permissions = retrievePermissions(user, targetUser); Set<ObjectPermission> permissions = retrievePermissions(user, targetUser, inherit);
Collection<String> accessibleObjects = new ArrayList<String>(permissions.size()); Collection<String> accessibleObjects = new ArrayList<String>(permissions.size());
// For each identifier/permission combination // For each identifier/permission combination
@@ -138,11 +134,11 @@ public class ActiveConnectionPermissionService
@Override @Override
public ObjectPermissionSet getPermissionSet(ModeledAuthenticatedUser user, public ObjectPermissionSet getPermissionSet(ModeledAuthenticatedUser user,
ModeledUser targetUser) throws GuacamoleException { ModeledUser targetUser, boolean inherit) throws GuacamoleException {
// Create permission set for requested user // Create permission set for requested user
ActiveConnectionPermissionSet permissionSet = activeConnectionPermissionSetProvider.get(); ActiveConnectionPermissionSet permissionSet = activeConnectionPermissionSetProvider.get();
permissionSet.init(user, targetUser); permissionSet.init(user, targetUser, inherit);
return permissionSet; return permissionSet;

View File

@@ -51,11 +51,11 @@ public class ConnectionGroupPermissionService extends ModeledObjectPermissionSer
@Override @Override
public ObjectPermissionSet getPermissionSet(ModeledAuthenticatedUser user, public ObjectPermissionSet getPermissionSet(ModeledAuthenticatedUser user,
ModeledUser targetUser) throws GuacamoleException { ModeledUser targetUser, boolean inherit) throws GuacamoleException {
// Create permission set for requested user // Create permission set for requested user
ObjectPermissionSet permissionSet = connectionGroupPermissionSetProvider.get(); ObjectPermissionSet permissionSet = connectionGroupPermissionSetProvider.get();
permissionSet.init(user, targetUser); permissionSet.init(user, targetUser, inherit);
return permissionSet; return permissionSet;

View File

@@ -51,11 +51,11 @@ public class ConnectionPermissionService extends ModeledObjectPermissionService
@Override @Override
public ObjectPermissionSet getPermissionSet(ModeledAuthenticatedUser user, public ObjectPermissionSet getPermissionSet(ModeledAuthenticatedUser user,
ModeledUser targetUser) throws GuacamoleException { ModeledUser targetUser, boolean inherit) throws GuacamoleException {
// Create permission set for requested user // Create permission set for requested user
ObjectPermissionSet permissionSet = connectionPermissionSetProvider.get(); ObjectPermissionSet permissionSet = connectionPermissionSetProvider.get();
permissionSet.init(user, targetUser); permissionSet.init(user, targetUser, inherit);
return permissionSet; return permissionSet;

View File

@@ -105,7 +105,7 @@ public abstract class ModeledObjectPermissionService
affectedIdentifiers.add(permission.getObjectIdentifier()); affectedIdentifiers.add(permission.getObjectIdentifier());
// Determine subset of affected identifiers that we have admin access to // Determine subset of affected identifiers that we have admin access to
ObjectPermissionSet affectedPermissionSet = getPermissionSet(user, user.getUser()); ObjectPermissionSet affectedPermissionSet = getPermissionSet(user, user.getUser(), true);
Collection<String> allowedSubset = affectedPermissionSet.getAccessibleObjects( Collection<String> allowedSubset = affectedPermissionSet.getAccessibleObjects(
Collections.singleton(ObjectPermission.Type.ADMINISTER), Collections.singleton(ObjectPermission.Type.ADMINISTER),
affectedIdentifiers affectedIdentifiers
@@ -154,21 +154,13 @@ public abstract class ModeledObjectPermissionService
} }
@Override @Override
public ObjectPermission retrievePermission(ModeledAuthenticatedUser user, public boolean hasPermission(ModeledAuthenticatedUser user,
ModeledUser targetUser, ObjectPermission.Type type, ModeledUser targetUser, ObjectPermission.Type type,
String identifier) throws GuacamoleException { String identifier, boolean inherit) throws GuacamoleException {
// Retrieve permissions only if allowed // Retrieve permissions only if allowed
if (canReadPermissions(user, targetUser)) { if (canReadPermissions(user, targetUser))
return getPermissionMapper().selectOne(targetUser.getModel(), type, identifier, inherit) != null;
// Read permission from database, return null if not found
ObjectPermissionModel model = getPermissionMapper().selectOne(targetUser.getModel(), type, identifier);
if (model == null)
return null;
return getPermissionInstance(model);
}
// User cannot read this user's permissions // User cannot read this user's permissions
throw new GuacamoleSecurityException("Permission denied."); throw new GuacamoleSecurityException("Permission denied.");
@@ -178,7 +170,8 @@ public abstract class ModeledObjectPermissionService
@Override @Override
public Collection<String> retrieveAccessibleIdentifiers(ModeledAuthenticatedUser user, public Collection<String> retrieveAccessibleIdentifiers(ModeledAuthenticatedUser user,
ModeledUser targetUser, Collection<ObjectPermission.Type> permissions, ModeledUser targetUser, Collection<ObjectPermission.Type> permissions,
Collection<String> identifiers) throws GuacamoleException { Collection<String> identifiers, boolean inherit)
throws GuacamoleException {
// Nothing is always accessible // Nothing is always accessible
if (identifiers.isEmpty()) if (identifiers.isEmpty())
@@ -192,7 +185,7 @@ public abstract class ModeledObjectPermissionService
return identifiers; return identifiers;
// Otherwise, return explicitly-retrievable identifiers // Otherwise, return explicitly-retrievable identifiers
return getPermissionMapper().selectAccessibleIdentifiers(targetUser.getModel(), permissions, identifiers); return getPermissionMapper().selectAccessibleIdentifiers(targetUser.getModel(), permissions, identifiers, inherit);
} }

View File

@@ -92,7 +92,7 @@ public abstract class ModeledPermissionService<PermissionSetType extends Permiss
permissions.add(getPermissionInstance(model)); permissions.add(getPermissionInstance(model));
return permissions; return permissions;
} }
/** /**
@@ -111,7 +111,7 @@ public abstract class ModeledPermissionService<PermissionSetType extends Permiss
*/ */
protected abstract ModelType getModelInstance(ModeledUser targetUser, protected abstract ModelType getModelInstance(ModeledUser targetUser,
PermissionType permission); PermissionType permission);
/** /**
* Returns a collection of model objects which are based on the given * Returns a collection of model objects which are based on the given
* permissions and target user. * permissions and target user.
@@ -129,7 +129,7 @@ public abstract class ModeledPermissionService<PermissionSetType extends Permiss
protected Collection<ModelType> getModelInstances(ModeledUser targetUser, protected Collection<ModelType> getModelInstances(ModeledUser targetUser,
Collection<PermissionType> permissions) { Collection<PermissionType> permissions) {
// Create new collection of models by manually converting each permission // Create new collection of models by manually converting each permission
Collection<ModelType> models = new ArrayList<ModelType>(permissions.size()); Collection<ModelType> models = new ArrayList<ModelType>(permissions.size());
for (PermissionType permission : permissions) for (PermissionType permission : permissions)
models.add(getModelInstance(targetUser, permission)); models.add(getModelInstance(targetUser, permission));
@@ -140,15 +140,15 @@ public abstract class ModeledPermissionService<PermissionSetType extends Permiss
@Override @Override
public Set<PermissionType> retrievePermissions(ModeledAuthenticatedUser user, public Set<PermissionType> retrievePermissions(ModeledAuthenticatedUser user,
ModeledUser targetUser) throws GuacamoleException { ModeledUser targetUser, boolean inherit) throws GuacamoleException {
// Retrieve permissions only if allowed // Retrieve permissions only if allowed
if (canReadPermissions(user, targetUser)) if (canReadPermissions(user, targetUser))
return getPermissionInstances(getPermissionMapper().select(targetUser.getModel())); return getPermissionInstances(getPermissionMapper().select(targetUser.getModel(), inherit));
// User cannot read this user's permissions // User cannot read this user's permissions
throw new GuacamoleSecurityException("Permission denied."); throw new GuacamoleSecurityException("Permission denied.");
} }
} }

View File

@@ -36,20 +36,26 @@ public interface ObjectPermissionMapper extends PermissionMapper<ObjectPermissio
* *
* @param entity * @param entity
* The entity to retrieve permissions for. * The entity to retrieve permissions for.
* *
* @param type * @param type
* The type of permission to return. * The type of permission to return.
* *
* @param identifier * @param identifier
* The identifier of the object affected by the permission to return. * The identifier of the object affected by the permission to return.
* *
* @param inherit
* Whether permissions inherited through user groups should be taken
* into account. If false, only permissions granted directly will be
* included.
*
* @return * @return
* The requested permission, or null if no such permission is granted * The requested permission, or null if no such permission is granted
* to the given entity for the given object. * to the given entity for the given object.
*/ */
ObjectPermissionModel selectOne(@Param("entity") EntityModel entity, ObjectPermissionModel selectOne(@Param("entity") EntityModel entity,
@Param("type") ObjectPermission.Type type, @Param("type") ObjectPermission.Type type,
@Param("identifier") String identifier); @Param("identifier") String identifier,
@Param("inherit") boolean inherit);
/** /**
* Retrieves the subset of the given identifiers for which the given entity * Retrieves the subset of the given identifiers for which the given entity
@@ -67,12 +73,18 @@ public interface ObjectPermissionMapper extends PermissionMapper<ObjectPermissio
* The identifiers of the objects affected by the permissions being * The identifiers of the objects affected by the permissions being
* checked. * checked.
* *
* @param inherit
* Whether permissions inherited through user groups should be taken
* into account. If false, only permissions granted directly will be
* included.
*
* @return * @return
* A collection containing the subset of identifiers for which at least * A collection containing the subset of identifiers for which at least
* one of the specified permissions is granted. * one of the specified permissions is granted.
*/ */
Collection<String> selectAccessibleIdentifiers(@Param("entity") EntityModel entity, Collection<String> selectAccessibleIdentifiers(@Param("entity") EntityModel entity,
@Param("permissions") Collection<ObjectPermission.Type> permissions, @Param("permissions") Collection<ObjectPermission.Type> permissions,
@Param("identifiers") Collection<String> identifiers); @Param("identifiers") Collection<String> identifiers,
@Param("inherit") boolean inherit);
} }

View File

@@ -35,31 +35,36 @@ public interface ObjectPermissionService
extends PermissionService<ObjectPermissionSet, ObjectPermission> { extends PermissionService<ObjectPermissionSet, ObjectPermission> {
/** /**
* Retrieves the permission of the given type associated with the given * Returns whether the permission of the given type and associated with the
* user and object, if it exists. If no such permission exists, null is * given object has been granted to the given user.
* *
* @param user * @param user
* The user retrieving the permission. * The user retrieving the permission.
* *
* @param targetUser * @param targetUser
* The user associated with the permission to be retrieved. * The user associated with the permission to be retrieved.
* *
* @param type * @param type
* The type of permission to retrieve. * The type of permission to retrieve.
* *
* @param identifier * @param identifier
* The identifier of the object affected by the permission to return. * The identifier of the object affected by the permission to return.
* *
* @param inherit
* Whether permissions inherited through user groups should be taken
* into account. If false, only permissions granted directly will be
* included.
*
* @return * @return
* The permission of the given type associated with the given user and * true if permission of the given type and associated with the given
* object, or null if no such permission exists. * object has been granted to the given user, false otherwise.
* *
* @throws GuacamoleException * @throws GuacamoleException
* If an error occurs while retrieving the requested permission. * If an error occurs while retrieving the requested permission.
*/ */
ObjectPermission retrievePermission(ModeledAuthenticatedUser user, boolean hasPermission(ModeledAuthenticatedUser user,
ModeledUser targetUser, ObjectPermission.Type type, ModeledUser targetUser, ObjectPermission.Type type,
String identifier) throws GuacamoleException; String identifier, boolean inherit) throws GuacamoleException;
/** /**
* Retrieves the subset of the given identifiers for which the given user * Retrieves the subset of the given identifiers for which the given user
@@ -80,6 +85,11 @@ public interface ObjectPermissionService
* The identifiers of the objects affected by the permissions being * The identifiers of the objects affected by the permissions being
* checked. * checked.
* *
* @param inherit
* Whether permissions inherited through user groups should be taken
* into account. If false, only permissions granted directly will be
* included.
*
* @return * @return
* A collection containing the subset of identifiers for which at least * A collection containing the subset of identifiers for which at least
* one of the specified permissions is granted. * one of the specified permissions is granted.
@@ -89,6 +99,7 @@ public interface ObjectPermissionService
*/ */
Collection<String> retrieveAccessibleIdentifiers(ModeledAuthenticatedUser user, Collection<String> retrieveAccessibleIdentifiers(ModeledAuthenticatedUser user,
ModeledUser targetUser, Collection<ObjectPermission.Type> permissions, ModeledUser targetUser, Collection<ObjectPermission.Type> permissions,
Collection<String> identifiers) throws GuacamoleException; Collection<String> identifiers, boolean inherit)
throws GuacamoleException;
} }

View File

@@ -42,6 +42,12 @@ public abstract class ObjectPermissionSet extends RestrictedObject
*/ */
private ModeledUser user; private ModeledUser user;
/**
* Whether permissions inherited through user groups should be taken into
* account. If false, only permissions granted directly will be included.
*/
boolean inherit;
/** /**
* Creates a new ObjectPermissionSet. The resulting permission set * Creates a new ObjectPermissionSet. The resulting permission set
* must still be initialized by a call to init(), or the information * must still be initialized by a call to init(), or the information
@@ -60,10 +66,17 @@ public abstract class ObjectPermissionSet extends RestrictedObject
* *
* @param user * @param user
* The user to whom the permissions in this set are granted. * The user to whom the permissions in this set are granted.
*
* @param inherit
* Whether permissions inherited through user groups should be taken
* into account. If false, only permissions granted directly will be
* included.
*/ */
public void init(ModeledAuthenticatedUser currentUser, ModeledUser user) { public void init(ModeledAuthenticatedUser currentUser, ModeledUser user,
boolean inherit) {
super.init(currentUser); super.init(currentUser);
this.user = user; this.user = user;
this.inherit = inherit;
} }
/** /**
@@ -75,16 +88,16 @@ public abstract class ObjectPermissionSet extends RestrictedObject
* permissions contained within this permission set. * permissions contained within this permission set.
*/ */
protected abstract ObjectPermissionService getObjectPermissionService(); protected abstract ObjectPermissionService getObjectPermissionService();
@Override @Override
public Set<ObjectPermission> getPermissions() throws GuacamoleException { public Set<ObjectPermission> getPermissions() throws GuacamoleException {
return getObjectPermissionService().retrievePermissions(getCurrentUser(), user); return getObjectPermissionService().retrievePermissions(getCurrentUser(), user, inherit);
} }
@Override @Override
public boolean hasPermission(ObjectPermission.Type permission, public boolean hasPermission(ObjectPermission.Type permission,
String identifier) throws GuacamoleException { String identifier) throws GuacamoleException {
return getObjectPermissionService().retrievePermission(getCurrentUser(), user, permission, identifier) != null; return getObjectPermissionService().hasPermission(getCurrentUser(), user, permission, identifier, inherit);
} }
@Override @Override
@@ -102,7 +115,7 @@ public abstract class ObjectPermissionSet extends RestrictedObject
@Override @Override
public Collection<String> getAccessibleObjects(Collection<ObjectPermission.Type> permissions, public Collection<String> getAccessibleObjects(Collection<ObjectPermission.Type> permissions,
Collection<String> identifiers) throws GuacamoleException { Collection<String> identifiers) throws GuacamoleException {
return getObjectPermissionService().retrieveAccessibleIdentifiers(getCurrentUser(), user, permissions, identifiers); return getObjectPermissionService().retrieveAccessibleIdentifiers(getCurrentUser(), user, permissions, identifiers, inherit);
} }
@Override @Override

View File

@@ -38,10 +38,16 @@ public interface PermissionMapper<PermissionType> {
* @param entity * @param entity
* The entity to retrieve permissions for. * The entity to retrieve permissions for.
* *
* @param inherit
* Whether permissions inherited through user groups should be taken
* into account. If false, only permissions granted directly will be
* included.
*
* @return * @return
* All permissions associated with the given entity. * All permissions associated with the given entity.
*/ */
Collection<PermissionType> select(@Param("entity") EntityModel entity); Collection<PermissionType> select(@Param("entity") EntityModel entity,
@Param("inherit") boolean inherit);
/** /**
* Inserts the given permissions into the database. If any permissions * Inserts the given permissions into the database. If any permissions

View File

@@ -19,16 +19,11 @@
package org.apache.guacamole.auth.jdbc.permission; package org.apache.guacamole.auth.jdbc.permission;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
import java.util.Set; import java.util.Set;
import org.apache.guacamole.auth.jdbc.user.ModeledAuthenticatedUser; import org.apache.guacamole.auth.jdbc.user.ModeledAuthenticatedUser;
import org.apache.guacamole.auth.jdbc.user.ModeledUser; import org.apache.guacamole.auth.jdbc.user.ModeledUser;
import org.apache.guacamole.GuacamoleException; import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleSecurityException;
import org.apache.guacamole.net.auth.permission.ObjectPermission;
import org.apache.guacamole.net.auth.permission.ObjectPermissionSet;
import org.apache.guacamole.net.auth.permission.Permission; import org.apache.guacamole.net.auth.permission.Permission;
import org.apache.guacamole.net.auth.permission.PermissionSet; import org.apache.guacamole.net.auth.permission.PermissionSet;
@@ -59,6 +54,11 @@ public interface PermissionService<PermissionSetType extends PermissionSet<Permi
* The user to whom the permissions in the returned permission set are * The user to whom the permissions in the returned permission set are
* granted. * granted.
* *
* @param inherit
* Whether permissions inherited through user groups should be taken
* into account. If false, only permissions granted directly will be
* included.
*
* @return * @return
* A permission set that contains all permissions associated with the * A permission set that contains all permissions associated with the
* given user, and can be used to manipulate that user's permissions. * given user, and can be used to manipulate that user's permissions.
@@ -69,7 +69,7 @@ public interface PermissionService<PermissionSetType extends PermissionSet<Permi
* user is denied. * user is denied.
*/ */
PermissionSetType getPermissionSet(ModeledAuthenticatedUser user, PermissionSetType getPermissionSet(ModeledAuthenticatedUser user,
ModeledUser targetUser) throws GuacamoleException; ModeledUser targetUser, boolean inherit) throws GuacamoleException;
/** /**
* Retrieves all permissions associated with the given user. * Retrieves all permissions associated with the given user.
@@ -80,6 +80,11 @@ public interface PermissionService<PermissionSetType extends PermissionSet<Permi
* @param targetUser * @param targetUser
* The user associated with the permissions to be retrieved. * The user associated with the permissions to be retrieved.
* *
* @param inherit
* Whether permissions inherited through user groups should be taken
* into account. If false, only permissions granted directly will be
* included.
*
* @return * @return
* The permissions associated with the given user. * The permissions associated with the given user.
* *
@@ -87,7 +92,7 @@ public interface PermissionService<PermissionSetType extends PermissionSet<Permi
* If an error occurs while retrieving the requested permissions. * If an error occurs while retrieving the requested permissions.
*/ */
Set<PermissionType> retrievePermissions(ModeledAuthenticatedUser user, Set<PermissionType> retrievePermissions(ModeledAuthenticatedUser user,
ModeledUser targetUser) throws GuacamoleException; ModeledUser targetUser, boolean inherit) throws GuacamoleException;
/** /**
* Creates the given permissions within the database. If any permissions * Creates the given permissions within the database. If any permissions

View File

@@ -51,11 +51,11 @@ public class SharingProfilePermissionService extends ModeledObjectPermissionServ
@Override @Override
public ObjectPermissionSet getPermissionSet(ModeledAuthenticatedUser user, public ObjectPermissionSet getPermissionSet(ModeledAuthenticatedUser user,
ModeledUser targetUser) throws GuacamoleException { ModeledUser targetUser, boolean inherit) throws GuacamoleException {
// Create permission set for requested user // Create permission set for requested user
ObjectPermissionSet permissionSet = sharingProfilePermissionSetProvider.get(); ObjectPermissionSet permissionSet = sharingProfilePermissionSetProvider.get();
permissionSet.init(user, targetUser); permissionSet.init(user, targetUser, inherit);
return permissionSet; return permissionSet;

View File

@@ -34,15 +34,21 @@ public interface SystemPermissionMapper extends PermissionMapper<SystemPermissio
* *
* @param entity * @param entity
* The entity to retrieve permissions for. * The entity to retrieve permissions for.
* *
* @param type * @param type
* The type of permission to return. * The type of permission to return.
* *
* @param inherit
* Whether permissions inherited through user groups should be taken
* into account. If false, only permissions granted directly will be
* included.
*
* @return * @return
* The requested permission, or null if no such permission is granted * The requested permission, or null if no such permission is granted
* to the given entity. * to the given entity.
*/ */
SystemPermissionModel selectOne(@Param("entity") EntityModel entity, SystemPermissionModel selectOne(@Param("entity") EntityModel entity,
@Param("type") SystemPermission.Type type); @Param("type") SystemPermission.Type type,
@Param("inherit") boolean inherit);
} }

View File

@@ -75,11 +75,11 @@ public class SystemPermissionService
@Override @Override
public SystemPermissionSet getPermissionSet(ModeledAuthenticatedUser user, public SystemPermissionSet getPermissionSet(ModeledAuthenticatedUser user,
ModeledUser targetUser) throws GuacamoleException { ModeledUser targetUser, boolean inherit) throws GuacamoleException {
// Create permission set for requested user // Create permission set for requested user
SystemPermissionSet permissionSet = systemPermissionSetProvider.get(); SystemPermissionSet permissionSet = systemPermissionSetProvider.get();
permissionSet.init(user, targetUser); permissionSet.init(user, targetUser, inherit);
return permissionSet; return permissionSet;
@@ -123,8 +123,9 @@ public class SystemPermissionService
} }
/** /**
* Retrieves the permission of the given type associated with the given * Retrieves whether the permission of the given type has been granted to
* user, if it exists. If no such permission exists, null is returned. * the given user. Permission inheritance through group membership is taken
* into account.
* *
* @param user * @param user
* The user retrieving the permission. * The user retrieving the permission.
@@ -135,27 +136,25 @@ public class SystemPermissionService
* @param type * @param type
* The type of permission to retrieve. * The type of permission to retrieve.
* *
* @param inherit
* Whether permissions inherited through user groups should be taken
* into account. If false, only permissions granted directly will be
* included.
*
* @return * @return
* The permission of the given type associated with the given user, or * true if permission of the given type has been granted to the given
* null if no such permission exists. * user, false otherwise.
* *
* @throws GuacamoleException * @throws GuacamoleException
* If an error occurs while retrieving the requested permission. * If an error occurs while retrieving the requested permission.
*/ */
public SystemPermission retrievePermission(ModeledAuthenticatedUser user, public boolean hasPermission(ModeledAuthenticatedUser user,
ModeledUser targetUser, SystemPermission.Type type) throws GuacamoleException { ModeledUser targetUser, SystemPermission.Type type,
boolean inherit) throws GuacamoleException {
// Retrieve permissions only if allowed // Retrieve permissions only if allowed
if (canReadPermissions(user, targetUser)) { if (canReadPermissions(user, targetUser))
return getPermissionMapper().selectOne(targetUser.getModel(), type, inherit) != null;
// Read permission from database, return null if not found
SystemPermissionModel model = getPermissionMapper().selectOne(targetUser.getModel(), type);
if (model == null)
return null;
return getPermissionInstance(model);
}
// User cannot read this user's permissions // User cannot read this user's permissions
throw new GuacamoleSecurityException("Permission denied."); throw new GuacamoleSecurityException("Permission denied.");

View File

@@ -42,6 +42,12 @@ public class SystemPermissionSet extends RestrictedObject
*/ */
private ModeledUser user; private ModeledUser user;
/**
* Whether permissions inherited through user groups should be taken into
* account. If false, only permissions granted directly will be included.
*/
private boolean inherit;
/** /**
* Service for reading and manipulating system permissions. * Service for reading and manipulating system permissions.
*/ */
@@ -66,21 +72,28 @@ public class SystemPermissionSet extends RestrictedObject
* *
* @param user * @param user
* The user to whom the permissions in this set are granted. * The user to whom the permissions in this set are granted.
*
* @param inherit
* Whether permissions inherited through user groups should be taken
* into account. If false, only permissions granted directly will be
* included.
*/ */
public void init(ModeledAuthenticatedUser currentUser, ModeledUser user) { public void init(ModeledAuthenticatedUser currentUser, ModeledUser user,
boolean inherit) {
super.init(currentUser); super.init(currentUser);
this.user = user; this.user = user;
this.inherit = inherit;
} }
@Override @Override
public Set<SystemPermission> getPermissions() throws GuacamoleException { public Set<SystemPermission> getPermissions() throws GuacamoleException {
return systemPermissionService.retrievePermissions(getCurrentUser(), user); return systemPermissionService.retrievePermissions(getCurrentUser(), user, inherit);
} }
@Override @Override
public boolean hasPermission(SystemPermission.Type permission) public boolean hasPermission(SystemPermission.Type permission)
throws GuacamoleException { throws GuacamoleException {
return systemPermissionService.retrievePermission(getCurrentUser(), user, permission) != null; return systemPermissionService.hasPermission(getCurrentUser(), user, permission, inherit);
} }
@Override @Override

View File

@@ -51,11 +51,11 @@ public class UserPermissionService extends ModeledObjectPermissionService {
@Override @Override
public ObjectPermissionSet getPermissionSet(ModeledAuthenticatedUser user, public ObjectPermissionSet getPermissionSet(ModeledAuthenticatedUser user,
ModeledUser targetUser) throws GuacamoleException { ModeledUser targetUser, boolean inherit) throws GuacamoleException {
// Create permission set for requested user // Create permission set for requested user
ObjectPermissionSet permissionSet = userPermissionSetProvider.get(); ObjectPermissionSet permissionSet = userPermissionSetProvider.get();
permissionSet.init(user, targetUser); permissionSet.init(user, targetUser, inherit);
return permissionSet; return permissionSet;

View File

@@ -350,37 +350,37 @@ public class ModeledUser extends ModeledDirectoryObject<UserModel> implements Us
@Override @Override
public SystemPermissionSet getSystemPermissions() public SystemPermissionSet getSystemPermissions()
throws GuacamoleException { throws GuacamoleException {
return systemPermissionService.getPermissionSet(getCurrentUser(), this); return systemPermissionService.getPermissionSet(getCurrentUser(), this, false);
} }
@Override @Override
public ObjectPermissionSet getConnectionPermissions() public ObjectPermissionSet getConnectionPermissions()
throws GuacamoleException { throws GuacamoleException {
return connectionPermissionService.getPermissionSet(getCurrentUser(), this); return connectionPermissionService.getPermissionSet(getCurrentUser(), this, false);
} }
@Override @Override
public ObjectPermissionSet getConnectionGroupPermissions() public ObjectPermissionSet getConnectionGroupPermissions()
throws GuacamoleException { throws GuacamoleException {
return connectionGroupPermissionService.getPermissionSet(getCurrentUser(), this); return connectionGroupPermissionService.getPermissionSet(getCurrentUser(), this, false);
} }
@Override @Override
public ObjectPermissionSet getSharingProfilePermissions() public ObjectPermissionSet getSharingProfilePermissions()
throws GuacamoleException { throws GuacamoleException {
return sharingProfilePermissionService.getPermissionSet(getCurrentUser(), this); return sharingProfilePermissionService.getPermissionSet(getCurrentUser(), this, false);
} }
@Override @Override
public ObjectPermissionSet getActiveConnectionPermissions() public ObjectPermissionSet getActiveConnectionPermissions()
throws GuacamoleException { throws GuacamoleException {
return activeConnectionPermissionService.getPermissionSet(getCurrentUser(), this); return activeConnectionPermissionService.getPermissionSet(getCurrentUser(), this, false);
} }
@Override @Override
public ObjectPermissionSet getUserPermissions() public ObjectPermissionSet getUserPermissions()
throws GuacamoleException { throws GuacamoleException {
return userPermissionService.getPermissionSet(getCurrentUser(), this); return userPermissionService.getPermissionSet(getCurrentUser(), this, false);
} }
@Override @Override
@@ -855,7 +855,52 @@ public class ModeledUser extends ModeledDirectoryObject<UserModel> implements Us
@Override @Override
public Permissions getEffectivePermissions() throws GuacamoleException { public Permissions getEffectivePermissions() throws GuacamoleException {
return this; return new Permissions() {
@Override
public ObjectPermissionSet getActiveConnectionPermissions()
throws GuacamoleException {
return activeConnectionPermissionService.getPermissionSet(getCurrentUser(), ModeledUser.this, true);
}
@Override
public ObjectPermissionSet getConnectionGroupPermissions()
throws GuacamoleException {
return connectionGroupPermissionService.getPermissionSet(getCurrentUser(), ModeledUser.this, true);
}
@Override
public ObjectPermissionSet getConnectionPermissions()
throws GuacamoleException {
return connectionPermissionService.getPermissionSet(getCurrentUser(), ModeledUser.this, true);
}
@Override
public ObjectPermissionSet getSharingProfilePermissions()
throws GuacamoleException {
return sharingProfilePermissionService.getPermissionSet(getCurrentUser(), ModeledUser.this, true);
}
@Override
public SystemPermissionSet getSystemPermissions()
throws GuacamoleException {
return systemPermissionService.getPermissionSet(getCurrentUser(), ModeledUser.this, true);
}
@Override
public ObjectPermissionSet getUserPermissions()
throws GuacamoleException {
return userPermissionService.getPermissionSet(getCurrentUser(), ModeledUser.this, true);
}
@Override
public ObjectPermissionSet getUserGroupPermissions()
throws GuacamoleException {
// FIXME: STUB
return new SimpleObjectPermissionSet();
}
};
} }
} }