summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/thrift/Session.java
blob: aa43601eacf4d2d8fd20fbb088c33d37ff260b7a (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package org.openslx.dozmod.thrift;

import java.util.HashSet;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.openslx.bwlp.thrift.iface.ImagePermissions;
import org.openslx.bwlp.thrift.iface.LecturePermissions;
import org.openslx.bwlp.thrift.iface.SatelliteConfig;
import org.openslx.bwlp.thrift.iface.WhoamiInfo;
import org.openslx.sat.thrift.version.Feature;
import org.openslx.thrifthelper.ThriftManager;

public class Session {

	private static final Logger LOGGER = Logger.getLogger(Session.class);

	private static WhoamiInfo data = null;

	private static String satelliteAddress = null;

	private static String satelliteToken = null;

	private static String masterToken = null;

	private static long satelliteApiVersion = -1;

	private static SatelliteConfig satConf = null;
	
	private static Set<Feature> features = null;

	public static synchronized void initialize(WhoamiInfo whoami, String satAddress, String satToken, String masToken,
			long satApiVersion) {
		if (whoami == null || whoami.user == null || whoami.user.userId == null) {
			throw new IllegalArgumentException(
					"Cannot initialize session: whoami-Information from satellite incomplete");
		}
		if (data != null && !data.user.userId.equals(whoami.user.userId))
			throw new IllegalArgumentException("Cannot set new session data with different user id");
		if (satelliteAddress != null && !satelliteAddress.equals(satAddress))
			throw new IllegalArgumentException("Cannot set new session data with different satellite address");
		data = whoami;
		masterToken = masToken;
		satelliteToken = satToken;
		satelliteAddress = satAddress;
		satelliteApiVersion = satApiVersion;
	}

	/**
	 * @return the first name
	 */
	public static String getFirstName() {
		if (data == null)
			return null;
		return data.user.firstName;
	}

	/**
	 * @return the last name
	 */
	public static String getLastName() {
		if (data == null)
			return null;
		return data.user.lastName;
	}

	/**
	 * @return the eMail
	 */
	public static String getEMail() {
		if (data == null)
			return null;
		return data.user.eMail;
	}

	/**
	 * @return the user id
	 */
	public static String getUserId() {
		if (data == null)
			return null;
		return data.user.userId;
	}

	/**
	 * @return the organization id
	 */
	public static String getOrganizationId() {
		if (data == null)
			return null;
		return data.user.organizationId;
	}

	/**
	 * @return whether user is superuser (has all permissions on everything, can
	 *         declare an image a template)
	 */
	public static boolean isSuperUser() {
		if (data == null)
			return false;
		return data.isSuperUser;
	}

	/**
	 * @return whether user can see list of images. students will only be able
	 *         to list lectures
	 */
	public static boolean canListImages() {
		if (data == null)
			return false;
		return data.canListImages;
	}

	/**
	 * @return the satellite token
	 */
	public static String getSatelliteToken() {
		return satelliteToken;
	}

	/**
	 * @return the master token
	 */
	public static String getMasterToken() {
		return masterToken;
	}

	/**
	 * @return the satelliteAddress
	 */
	public static String getSatelliteAddress() {
		return satelliteAddress;
	}

	/**
	 * @return the satellite server's API version
	 */
	public static long getSatApiVersion() {
		return satelliteApiVersion;
	}

	/**
	 * @return true if the satellite's API version supports publications of
	 *         images (version 3 and above), false otherwise
	 */
	public static boolean isImagePublishSupported() {
		return satelliteApiVersion >= 3;
	}

	/**
	 * @return true if the satellite's API version supports usb, netrules, internet
	 *         blocking, exam mode restrictions (version 4 and above), false otherwise
	 */
	public static boolean isLectureRestrictionsSupported() {
		return satelliteApiVersion >= 4;
	}

	/**
	 * Query the satellite server for its configuration and default values.
	 * 
	 * @return
	 */
	public static SatelliteConfig getSatelliteConfig() {
		synchronized (SatelliteConfig.class) {
			if (satConf != null)
				return satConf;
			try {
				satConf = ThriftManager.getSatClient().getConfiguration();
				return satConf;
			} catch (TException e) {
				LOGGER.warn("Could not get satellite configuration, falling back to builtin defaults", e);
				SatelliteConfig satConfig = new SatelliteConfig();
				satConfig.setDefaultImagePermissions(new ImagePermissions(true, true, false, false));
				satConfig.setDefaultLecturePermissions(new LecturePermissions(false, false));
				satConfig.setMaxImageValidityDays(200);
				satConfig.setMaxLectureValidityDays(100);
				satConfig.setPageSize(200);
				return satConfig;
			}
		}
	}

	public static boolean canExtendImageExpiry() {
		return isSuperUser() && satelliteApiVersion >= 3;
	}

	public static boolean hasFeature(Feature feature) {
		synchronized (Feature.class) {
			if (features == null) {
				String ret;
				try {
					ret = ThriftManager.getSatClient().getSupportedFeatures();
				} catch (TException e) {
					return false;
				}
				if (ret == null)
					return false;
				features = new HashSet<>();
				for (String f : ret.split(" ")) {
					try {
						features.add(Feature.valueOf(f));
					} catch (Throwable t) {}
				}
			}
			return features.contains(feature);
		}
	}

}