From 41186ddf8eef2530b95fe90f03bd84ee841115d9 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 15 Apr 2020 15:40:55 +0200 Subject: First Commit --- .../de/bwlehrpool/bwlp_guac/BwlpUserContext.java | 109 +++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/main/java/de/bwlehrpool/bwlp_guac/BwlpUserContext.java (limited to 'src/main/java/de/bwlehrpool/bwlp_guac/BwlpUserContext.java') diff --git a/src/main/java/de/bwlehrpool/bwlp_guac/BwlpUserContext.java b/src/main/java/de/bwlehrpool/bwlp_guac/BwlpUserContext.java new file mode 100644 index 0000000..7f0856c --- /dev/null +++ b/src/main/java/de/bwlehrpool/bwlp_guac/BwlpUserContext.java @@ -0,0 +1,109 @@ +package de.bwlehrpool.bwlp_guac; + +import org.apache.guacamole.GuacamoleException; +import org.apache.guacamole.net.auth.AbstractUserContext; +import org.apache.guacamole.net.auth.AuthenticatedUser; +import org.apache.guacamole.net.auth.AuthenticationProvider; +import org.apache.guacamole.net.auth.Connection; +import org.apache.guacamole.net.auth.Directory; +import org.apache.guacamole.net.auth.User; +import org.apache.guacamole.net.auth.UserContext; +import org.apache.guacamole.net.auth.permission.ObjectPermissionSet; +import org.apache.guacamole.net.auth.simple.SimpleConnection; +import org.apache.guacamole.net.auth.simple.SimpleDirectory; +import org.apache.guacamole.net.auth.simple.SimpleObjectPermissionSet; +import org.apache.guacamole.net.auth.simple.SimpleUser; +import org.apache.guacamole.protocol.GuacamoleConfiguration; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class BwlpUserContext extends AbstractUserContext { + + private static final Logger LOGGER = LoggerFactory.getLogger(BwlpUserContext.class); + + private static final SimpleConnection FAKE = new SimpleConnection("FAKE", "FAKE", new GuacamoleConfiguration()); + + static { + FAKE.setParentIdentifier(DEFAULT_ROOT_CONNECTION_GROUP); + } + + private final AuthenticatedUser authUser; + private final UserContext originalContext; + + /** + * The Directory with access to all connections within the root group associated + * with this UserContext. + */ + private Directory connectionDirectory; + + public BwlpUserContext(AuthenticatedUser authenticatedUser, UserContext context) { + authUser = authenticatedUser; + originalContext = context; + // OK + addConn(); + } + + private void addConn() { + WrappedConnection connection = ConnectionManager.getForUser(authUser.getCredentials().getUsername()); + if (connection != null) { + connectionDirectory = new SimpleDirectory(connection); + } else { + connectionDirectory = new SimpleDirectory(); + } + } + + public User self() { + return new SimpleUser(authUser.getCredentials().getUsername()) { + + @Override + public ObjectPermissionSet getConnectionGroupPermissions() throws GuacamoleException { + return new SimpleObjectPermissionSet(getConnectionDirectory().getIdentifiers()); + } + + @Override + public ObjectPermissionSet getConnectionPermissions() throws GuacamoleException { + return new SimpleObjectPermissionSet(getConnectionGroupDirectory().getIdentifiers()); + } + + }; + } + + @Override + public Object getResource() throws GuacamoleException { + return null; + } + + public AuthenticationProvider getAuthenticationProvider() { + return originalContext.getAuthenticationProvider(); + } + + @Override + public Directory getConnectionDirectory() throws GuacamoleException { + return connectionDirectory; + } + + public boolean hasValidConnection() { + boolean ok = false; + try { + synchronized (this) { + for (String id : connectionDirectory.getIdentifiers()) { + Connection con = connectionDirectory.get(id); + if (con instanceof WrappedConnection) { + if (((WrappedConnection) con).checkConnection(3)) { + ok = true; + } + } else { + } + } + } + } catch (Exception e) { + LOGGER.warn("hasValidConnection", e); + } + return ok; + } + + public UserContext getOriginalContext() { + return originalContext; + } + +} -- cgit v1.2.3-55-g7522