package org.openslx.dozmod.thrift; 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.thrifthelper.ThriftManager; public class Session { private static final Logger LOGGER = Logger.getLogger(Session.class); private static WhoamiInfo data = null; private static String satelliteToken = null; private static String masterToken = null; private static String satelliteAddress = null; public static void initialize(WhoamiInfo whoami, String satAddress, String satToken, String masToken) { 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; } /** * @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; } private static SatelliteConfig satConf = null; /** * 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; } } } }