GUACAMOLE-1123: Implement new methods for retrieving user and connection history.

This commit is contained in:
Virtually Nick
2020-08-23 14:51:32 -04:00
parent 54b80f94bf
commit 71625340f5
20 changed files with 169 additions and 34 deletions

View File

@@ -48,7 +48,7 @@ public abstract class ModeledActivityRecordSet<RecordType extends ActivityRecord
* strings within the collection will be excluded from the results.
*/
private final Set<ActivityRecordSearchTerm> requiredContents =
new HashSet<ActivityRecordSearchTerm>();
new HashSet<>();
/**
* The maximum number of history records that should be returned by a call
@@ -62,7 +62,7 @@ public abstract class ModeledActivityRecordSet<RecordType extends ActivityRecord
* properties.
*/
private final List<ActivityRecordSortPredicate> sortPredicates =
new ArrayList<ActivityRecordSortPredicate>();
new ArrayList<>();
/**
* Retrieves the history records matching the given criteria. Retrieves up

View File

@@ -27,6 +27,7 @@ import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.auth.jdbc.base.ActivityRecordSearchTerm;
import org.apache.guacamole.auth.jdbc.base.ActivityRecordSortPredicate;
import org.apache.guacamole.auth.jdbc.base.ModeledActivityRecordSet;
import org.apache.guacamole.auth.jdbc.user.ModeledAuthenticatedUser;
import org.apache.guacamole.net.auth.AuthenticatedUser;
import org.apache.guacamole.net.auth.ConnectionRecord;
@@ -43,6 +44,27 @@ public class ConnectionRecordSet extends ModeledActivityRecordSet<ConnectionReco
@Inject
private ConnectionService connectionService;
/**
* The identifier of the connection to which this record set should be
* limited, if any.
*/
private String identifier = null;
/**
* Initializes this object, associating it with the current authenticated
* user and connection identifier.
*
* @param currentUser
* The user that created or retrieved this object.
*
* @param identifier
* The connection identifier to which this record set should be limited.
*/
protected void init(ModeledAuthenticatedUser currentUser, String identifier) {
super.init(currentUser);
this.identifier = identifier;
}
@Override
protected Collection<ConnectionRecord> retrieveHistory(
AuthenticatedUser user, Set<ActivityRecordSearchTerm> requiredContents,
@@ -50,7 +72,7 @@ public class ConnectionRecordSet extends ModeledActivityRecordSet<ConnectionReco
throws GuacamoleException {
// Retrieve history from database
return connectionService.retrieveHistory(getCurrentUser(),
return connectionService.retrieveHistory(identifier, getCurrentUser(),
requiredContents, sortPredicates, limit);
}

View File

@@ -40,6 +40,7 @@ import org.apache.guacamole.form.Form;
import org.apache.guacamole.form.NumericField;
import org.apache.guacamole.form.TextField;
import org.apache.guacamole.net.GuacamoleTunnel;
import org.apache.guacamole.net.auth.ActivityRecordSet;
import org.apache.guacamole.net.auth.Connection;
import org.apache.guacamole.net.auth.ConnectionRecord;
import org.apache.guacamole.net.auth.GuacamoleProxyConfiguration;
@@ -196,6 +197,12 @@ public class ModeledConnection extends ModeledChildDirectoryObject<ConnectionMod
@Inject
private Provider<ModeledGuacamoleConfiguration> configProvider;
/**
* Provider for creating connection record sets.
*/
@Inject
private Provider<ConnectionRecordSet> connectionRecordSetProvider;
/**
* The manually-set GuacamoleConfiguration, if any.
*/
@@ -258,6 +265,14 @@ public class ModeledConnection extends ModeledChildDirectoryObject<ConnectionMod
return connectionService.retrieveHistory(getCurrentUser(), this);
}
@Override
public ActivityRecordSet<ConnectionRecord> getConnectionHistory()
throws GuacamoleException {
ConnectionRecordSet connectionRecordSet = connectionRecordSetProvider.get();
connectionRecordSet.init(getCurrentUser(), this.getIdentifier());
return connectionRecordSet;
}
@Override
public GuacamoleTunnel connect(GuacamoleClientInformation info,
Map<String, String> tokens) throws GuacamoleException {

View File

@@ -26,10 +26,12 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleUnsupportedException;
import org.apache.guacamole.auth.jdbc.sharing.connectiongroup.SharedRootConnectionGroup;
import org.apache.guacamole.auth.jdbc.tunnel.GuacamoleTunnelService;
import org.apache.guacamole.auth.jdbc.user.RemoteAuthenticatedUser;
import org.apache.guacamole.net.GuacamoleTunnel;
import org.apache.guacamole.net.auth.ActivityRecordSet;
import org.apache.guacamole.net.auth.Connection;
import org.apache.guacamole.net.auth.ConnectionRecord;
import org.apache.guacamole.protocol.GuacamoleClientInformation;
@@ -158,6 +160,12 @@ public class SharedConnection implements Connection {
return Collections.<ConnectionRecord>emptyList();
}
@Override
public ActivityRecordSet<ConnectionRecord> getConnectionHistory()
throws GuacamoleException {
throw new GuacamoleUnsupportedException("SharedConnection objects do not provide history.");
}
@Override
public Set<String> getSharingProfileIdentifiers()
throws GuacamoleException {

View File

@@ -24,8 +24,10 @@ import java.util.Date;
import java.util.List;
import java.util.Map;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleUnsupportedException;
import org.apache.guacamole.auth.jdbc.sharing.permission.SharedObjectPermissionSet;
import org.apache.guacamole.net.auth.ActivityRecord;
import org.apache.guacamole.net.auth.ActivityRecordSet;
import org.apache.guacamole.net.auth.AuthenticatedUser;
import org.apache.guacamole.net.auth.Connection;
import org.apache.guacamole.net.auth.ConnectionGroup;
@@ -107,6 +109,12 @@ public class SharedUser implements User {
}
@Override
public ActivityRecordSet<ActivityRecord> getUserHistory()
throws GuacamoleException {
throw new GuacamoleUnsupportedException("SharedUser objects do not provide login history.");
}
@Override
public String getPassword() {
return null;

View File

@@ -47,6 +47,7 @@ import org.apache.guacamole.form.TextField;
import org.apache.guacamole.form.TimeField;
import org.apache.guacamole.form.TimeZoneField;
import org.apache.guacamole.net.auth.ActivityRecord;
import org.apache.guacamole.net.auth.ActivityRecordSet;
import org.apache.guacamole.net.auth.Permissions;
import org.apache.guacamole.net.auth.RelatedObjectSet;
import org.apache.guacamole.net.auth.User;
@@ -183,6 +184,12 @@ public class ModeledUser extends ModeledPermissions<UserModel> implements User {
@Inject
private Provider<UserParentUserGroupSet> parentUserGroupSetProvider;
/**
* Provider for creating user record sets.
*/
@Inject
private Provider<UserRecordSet> userRecordSetProvider;
/**
* Whether attributes which control access restrictions should be exposed
* via getAttributes() or allowed to be set via setAttributes().
@@ -752,6 +759,14 @@ public class ModeledUser extends ModeledPermissions<UserModel> implements User {
return userService.retrieveHistory(getCurrentUser(), this);
}
@Override
public ActivityRecordSet<ActivityRecord> getUserHistory()
throws GuacamoleException {
UserRecordSet userRecordSet = userRecordSetProvider.get();
userRecordSet.init(getCurrentUser(), this.getIdentifier());
return userRecordSet;
}
@Override
public RelatedObjectSet getUserGroups() throws GuacamoleException {
UserParentUserGroupSet parentUserGroupSet = parentUserGroupSetProvider.get();

View File

@@ -44,6 +44,13 @@ public class UserRecordSet extends ModeledActivityRecordSet<ActivityRecord> {
@Inject
private UserService userService;
private String identifier = null;
protected void init(ModeledAuthenticatedUser currentUser, String identifier) {
super.init(currentUser);
this.identifier = identifier;
}
@Override
protected Collection<ActivityRecord> retrieveHistory(
AuthenticatedUser user, Set<ActivityRecordSearchTerm> requiredContents,
@@ -51,7 +58,7 @@ public class UserRecordSet extends ModeledActivityRecordSet<ActivityRecord> {
throws GuacamoleException {
// Retrieve history from database
return userService.retrieveHistory(getCurrentUser(),
return userService.retrieveHistory(identifier, getCurrentUser(),
requiredContents, sortPredicates, limit);
}

View File

@@ -26,6 +26,7 @@ import java.util.Map;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.net.auth.permission.ObjectPermissionSet;
import org.apache.guacamole.net.auth.permission.SystemPermissionSet;
import org.apache.guacamole.net.auth.simple.SimpleActivityRecordSet;
/**
* Base implementation of User which provides default implementations of
@@ -97,6 +98,12 @@ public abstract class AbstractUser extends AbstractIdentifiable
return Collections.emptyList();
}
@Override
public ActivityRecordSet<ActivityRecord> getUserHistory()
throws GuacamoleException {
return new SimpleActivityRecordSet<>();
}
/**
* {@inheritDoc}
*

View File

@@ -109,6 +109,24 @@ public interface Connection extends Identifiable, Connectable, Attributes {
*/
public List<? extends ConnectionRecord> getHistory() throws GuacamoleException;
/**
* Returns an ActivityRecordSet containing ConnectionRecords that
* represent the usage history of this Connection, including any active
* users. ConnectionRecords in this list will be sorted in descending order
* of end time (active connections are first), and then in descending order
* of start time (newer connections are first).
*
* @return
* An ActivityRecordSet containing ConnectionRecords representing the
* usage history of this Connection.
*
* @throws GuacamoleException
* If an error occurs retrieving history for this Connection, or if
* permission to retrieve the history is denied.
*/
public ActivityRecordSet<ConnectionRecord> getConnectionHistory()
throws GuacamoleException;
/**
* Returns identifiers of all readable sharing profiles that can be used to
* join this connection when it is active. The level of access granted to a

View File

@@ -140,6 +140,12 @@ public class DelegatingConnection implements Connection {
return connection.getHistory();
}
@Override
public ActivityRecordSet<ConnectionRecord> getConnectionHistory()
throws GuacamoleException {
return connection.getConnectionHistory();
}
@Override
public Set<String> getSharingProfileIdentifiers()
throws GuacamoleException {

View File

@@ -99,6 +99,12 @@ public class DelegatingUser implements User {
return user.getHistory();
}
@Override
public ActivityRecordSet<ActivityRecord> getUserHistory()
throws GuacamoleException {
return user.getUserHistory();
}
@Override
public SystemPermissionSet getSystemPermissions()
throws GuacamoleException {

View File

@@ -105,6 +105,24 @@ public interface User extends Identifiable, Attributes, Permissions {
*/
List<? extends ActivityRecord> getHistory() throws GuacamoleException;
/**
* Returns an ActivityRecordSet containing ActivityRecords representing
* the login history for this user, including any active sessions.
* ActivityRecords in this list will be sorted in descending order of end
* time (active sessions are first), and then in descending order of start
* time (newer sessions are first).
*
* @return
* An ActivityRecordSet containing ActivityRecords representing the
* login history for this user.
*
* @throws GuacamoleException
* If an error occurs retrieving this user's login history, or if
* permission to retrieve login history is denied.
*/
public ActivityRecordSet<ActivityRecord> getUserHistory()
throws GuacamoleException;
/**
* Returns a set of all readable user groups of which this user is a member.
* If permission is granted for the current user to modify the membership of

View File

@@ -33,6 +33,7 @@ import org.apache.guacamole.net.InetGuacamoleSocket;
import org.apache.guacamole.net.SSLGuacamoleSocket;
import org.apache.guacamole.net.SimpleGuacamoleTunnel;
import org.apache.guacamole.net.auth.AbstractConnection;
import org.apache.guacamole.net.auth.ActivityRecordSet;
import org.apache.guacamole.net.auth.ConnectionRecord;
import org.apache.guacamole.net.auth.GuacamoleProxyConfiguration;
import org.apache.guacamole.protocol.ConfiguredGuacamoleSocket;
@@ -289,4 +290,10 @@ public class SimpleConnection extends AbstractConnection {
return Collections.<ConnectionRecord>emptyList();
}
@Override
public ActivityRecordSet<ConnectionRecord> getConnectionHistory()
throws GuacamoleException {
return new SimpleActivityRecordSet<>();
}
}

View File

@@ -25,7 +25,9 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleUnsupportedException;
import org.apache.guacamole.net.GuacamoleTunnel;
import org.apache.guacamole.net.auth.ActivityRecordSet;
import org.apache.guacamole.net.auth.Connection;
import org.apache.guacamole.net.auth.ConnectionRecord;
import org.apache.guacamole.protocol.GuacamoleClientInformation;
@@ -123,14 +125,14 @@ public class APIConnectionWrapper implements Connection {
}
@Override
public Set<String> getSharingProfileIdentifiers() {
throw new UnsupportedOperationException("Operation not supported.");
public Set<String> getSharingProfileIdentifiers() throws GuacamoleException {
throw new GuacamoleUnsupportedException("Operation not supported.");
}
@Override
public GuacamoleTunnel connect(GuacamoleClientInformation info,
Map<String, String> tokens) throws GuacamoleException {
throw new UnsupportedOperationException("Operation not supported.");
throw new GuacamoleUnsupportedException("Operation not supported.");
}
@Override
@@ -143,4 +145,11 @@ public class APIConnectionWrapper implements Connection {
return Collections.<ConnectionRecord>emptyList();
}
@Override
public ActivityRecordSet<ConnectionRecord> getConnectionHistory()
throws GuacamoleException {
throw new GuacamoleUnsupportedException("APIConnectionWrapper does not "
+ "support retrieving connection history.");
}
}

View File

@@ -22,8 +22,6 @@ package org.apache.guacamole.rest.connection;
import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import com.google.inject.assistedinject.AssistedInject;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
@@ -33,7 +31,6 @@ import javax.ws.rs.core.MediaType;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleSecurityException;
import org.apache.guacamole.net.auth.Connection;
import org.apache.guacamole.net.auth.ConnectionRecord;
import org.apache.guacamole.net.auth.Directory;
import org.apache.guacamole.net.auth.Permissions;
import org.apache.guacamole.rest.directory.DirectoryView;
@@ -43,12 +40,12 @@ import org.apache.guacamole.net.auth.permission.ObjectPermission;
import org.apache.guacamole.net.auth.permission.ObjectPermissionSet;
import org.apache.guacamole.net.auth.permission.SystemPermission;
import org.apache.guacamole.net.auth.permission.SystemPermissionSet;
import org.apache.guacamole.rest.history.APIConnectionRecord;
import org.apache.guacamole.protocol.GuacamoleConfiguration;
import org.apache.guacamole.rest.directory.DirectoryObjectResource;
import org.apache.guacamole.rest.directory.DirectoryObjectTranslator;
import org.apache.guacamole.rest.directory.DirectoryResource;
import org.apache.guacamole.rest.directory.DirectoryResourceFactory;
import org.apache.guacamole.rest.history.ConnectionHistoryResource;
import org.apache.guacamole.rest.sharingprofile.APISharingProfile;
/**
@@ -150,18 +147,11 @@ public class ConnectionResource extends DirectoryObjectResource<Connection, APIC
* @throws GuacamoleException
* If an error occurs while retrieving the connection history.
*/
@GET
@Path("history")
public List<APIConnectionRecord> getConnectionHistory()
public ConnectionHistoryResource getConnectionHistory()
throws GuacamoleException {
// Retrieve the requested connection's history
List<APIConnectionRecord> apiRecords = new ArrayList<>();
for (ConnectionRecord record : connection.getHistory())
apiRecords.add(new APIConnectionRecord(record));
// Return the converted history
return apiRecords;
return new ConnectionHistoryResource(connection.getConnectionHistory());
}

View File

@@ -84,7 +84,7 @@ public abstract class ActivityRecordSetResource<InternalRecordType extends Activ
* A new record object containing the same data as the given internal
* record, but intended for use in interchange.
*/
protected abstract ExternalRecordType toExternalRecord(InternalRecordType record);
public abstract ExternalRecordType toExternalRecord(InternalRecordType record);
/**
* Retrieves the list of activity records stored within the underlying

View File

@@ -42,7 +42,7 @@ public class ConnectionHistoryResource extends ActivityRecordSetResource<Connect
}
@Override
protected APIConnectionRecord toExternalRecord(ConnectionRecord record) {
public APIConnectionRecord toExternalRecord(ConnectionRecord record) {
return new APIConnectionRecord(record);
}

View File

@@ -41,7 +41,7 @@ public class UserHistoryResource extends ActivityRecordSetResource<ActivityRecor
}
@Override
protected APIActivityRecord toExternalRecord(ActivityRecord record) {
public APIActivityRecord toExternalRecord(ActivityRecord record) {
return new APIActivityRecord(record);
}

View File

@@ -26,6 +26,7 @@ import java.util.Map;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleUnsupportedException;
import org.apache.guacamole.net.auth.ActivityRecord;
import org.apache.guacamole.net.auth.ActivityRecordSet;
import org.apache.guacamole.net.auth.Permissions;
import org.apache.guacamole.net.auth.RelatedObjectSet;
import org.apache.guacamole.net.auth.User;
@@ -144,4 +145,10 @@ public class APIUserWrapper implements User {
return Collections.<ActivityRecord>emptyList();
}
@Override
public ActivityRecordSet<ActivityRecord> getUserHistory()
throws GuacamoleException {
throw new GuacamoleUnsupportedException("APIUserWrapper does not provide login history.");
}
}

View File

@@ -33,7 +33,6 @@ import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import org.apache.guacamole.GuacamoleException;
import org.apache.guacamole.GuacamoleSecurityException;
import org.apache.guacamole.net.auth.ActivityRecord;
import org.apache.guacamole.net.auth.AuthenticationProvider;
import org.apache.guacamole.net.auth.Credentials;
import org.apache.guacamole.net.auth.User;
@@ -42,7 +41,7 @@ import org.apache.guacamole.net.auth.UserContext;
import org.apache.guacamole.net.auth.credentials.GuacamoleCredentialsException;
import org.apache.guacamole.rest.directory.DirectoryObjectResource;
import org.apache.guacamole.rest.directory.DirectoryObjectTranslator;
import org.apache.guacamole.rest.history.APIActivityRecord;
import org.apache.guacamole.rest.history.UserHistoryResource;
import org.apache.guacamole.rest.identifier.RelatedObjectSetResource;
import org.apache.guacamole.rest.permission.APIPermissionSet;
import org.apache.guacamole.rest.permission.PermissionSetResource;
@@ -110,18 +109,11 @@ public class UserResource
* @throws GuacamoleException
* If an error occurs while retrieving the user history.
*/
@GET
@Path("history")
public List<APIActivityRecord> getUserHistory()
public UserHistoryResource getUserHistory()
throws GuacamoleException {
// Retrieve the requested user's history
List<APIActivityRecord> apiRecords = new ArrayList<APIActivityRecord>();
for (ActivityRecord record : user.getHistory())
apiRecords.add(new APIActivityRecord(record));
// Return the converted history
return apiRecords;
return new UserHistoryResource(user.getUserHistory());
}