summaryrefslogtreecommitdiffstats
path: root/src/main/java/de/bwlehrpool/bwlp_guac/BwlpUserContext.java
blob: 9a6e5d724865c8a52eab6d93e357d5bffc004d4d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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<Connection> 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>(connection);
		} else {
			connectionDirectory = new SimpleDirectory<Connection>();
		}
	}

	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<Connection> 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) {
						LOGGER.info("Checking connection for " + con.getIdentifier());
						if (((WrappedConnection) con).checkConnection(3)) {
							ok = true;
						}
					} else {
					}
				}
			}
		} catch (Exception e) {
			LOGGER.warn("hasValidConnection", e);
		}
		return ok;
	}

	public UserContext getOriginalContext() {
		return originalContext;
	}

}