+ public final static Locale locale(Subject subject) {
+ Set<Locale> locales = subject.getPublicCredentials(Locale.class);
+ if (locales.isEmpty()) {
+ Locale defaultLocale = CmsContextImpl.getCmsContext().getDefaultLocale();
+ return defaultLocale;
+ } else
+ return locales.iterator().next();
+ }
+
+ /** Whether this user is currently authenticated. */
+ public static boolean isAnonymous(Subject subject) {
+ if (subject == null)
+ return true;
+ String username = getUsername(subject);
+ return username == null || username.equalsIgnoreCase(CmsConstants.ROLE_ANONYMOUS);
+ }
+
+ public static CmsSession getCmsSession() {
+ Subject subject = currentSubject();
+ Iterator<CmsSessionId> it = subject.getPrivateCredentials(CmsSessionId.class).iterator();
+ if (!it.hasNext())
+ throw new IllegalStateException("No CMS session id available for " + subject);
+ CmsSessionId cmsSessionId = it.next();
+ if (it.hasNext())
+ throw new IllegalStateException("More than one CMS session id available for " + subject);
+ return CmsContextImpl.getCmsContext().getCmsSessionByUuid(cmsSessionId.getUuid());
+ }
+
+ public static boolean isAvailable() {
+ return CurrentSubject.current() != null;
+ }
+
+ /*
+ * HELPERS
+ */
+ private static Subject currentSubject() {
+ Subject subject = CurrentSubject.current();
+ if (subject == null)
+ throw new IllegalStateException("Cannot find related subject");
+ return subject;
+ }
+
+ private static Authorization getAuthorization(Subject subject) {
+ return subject.getPrivateCredentials(Authorization.class).iterator().next();
+ }
+
+ public static boolean logoutCmsSession(Subject subject) {
+ UUID nodeSessionId;
+ if (subject.getPrivateCredentials(CmsSessionId.class).size() == 1)
+ nodeSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next().getUuid();
+ else
+ return false;
+ CmsSessionImpl cmsSession = CmsContextImpl.getCmsContext().getCmsSessionByUuid(nodeSessionId);
+
+ // FIXME logout all views
+ // TODO check why it is sometimes null
+ if (cmsSession != null)
+ cmsSession.close();
+ // if (log.isDebugEnabled())
+ // log.debug("Logged out CMS session " + cmsSession.getUuid());
+ return true;
+ }
+
+ /** singleton */