summaryrefslogtreecommitdiffstats
path: root/src/main/java/de/bwlehrpool/bwlp_guac/BwlpUserContext.java
blob: 88208b9be6eb5dbe2ea84d557a792c12a034cce3 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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.credentials.GuacamoleCredentialsException;
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;
	private final Integer groupid;
	private final String resolution;

	/**
	 * 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, int groupid, String resolution)
			throws GuacamoleCredentialsException {
		authUser = authenticatedUser;
		originalContext = context;
		this.groupid = groupid;
		this.resolution = resolution;
		// OK
		addConn();
	}

	public BwlpUserContext(AuthenticatedUser authenticatedUser, UserContext context, WrappedConnection exConn) {
		authUser = authenticatedUser;
		originalContext = context;
		this.groupid = -1;
		this.resolution = "";
		connectionDirectory = new SimpleDirectory<Connection>(exConn);
	}

	private void addConn() throws GuacamoleCredentialsException {
		WrappedConnection connection = ConnectionManager.getForUser(authUser.getCredentials().getUsername(), groupid);
		if (connection != null) {
			connection.remoteLogin(authUser.getCredentials(), this.resolution);
			connection.setContext(this);
			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 void clearConnections() {
		connectionDirectory = new SimpleDirectory<Connection>();
	}

	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);
		}
		if (!ok) connectionDirectory = new SimpleDirectory<Connection>();
		return ok;
	}

	public UserContext getOriginalContext() {
		return originalContext;
	}

}