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 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); } } }