+ public final static Locale locale(Subject subject) {
+ Set<Locale> locales = subject.getPublicCredentials(Locale.class);
+ if (locales.isEmpty()) {
+ Locale defaultLocale = Activator.getNodeState().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(NodeConstants.ROLE_ANONYMOUS);
+ }
+
+ public CmsSession getCmsSession() {
+ Subject subject = currentSubject();
+ CmsSessionId cmsSessionId = subject.getPrivateCredentials(CmsSessionId.class).iterator().next();
+ return CmsSessionImpl.getByUuid(cmsSessionId.getUuid());
+ }
+
+ /*
+ * HELPERS
+ */
+ private static Subject currentSubject() {
+ // CmsAuthenticated cmsView = getNodeAuthenticated();
+ // if (cmsView != null)
+ // return cmsView.getSubject();
+ Subject subject = getAccessControllerSubject();
+ if (subject != null)
+ return subject;
+ throw new CmsException("Cannot find related subject");
+ }
+
+ private static Subject getAccessControllerSubject() {
+ return Subject.getSubject(AccessController.getContext());
+ }
+
+ // public static boolean isAuthenticated() {
+ // return getAccessControllerSubject() != null;
+ // }
+
+ /**
+ * The node authenticated component (typically a CMS view) related to this
+ * display, or null if none is available from this call. <b>Not API: Only for
+ * low-level access.</b>
+ */
+ // private static CmsAuthenticated getNodeAuthenticated() {
+ // return UiContext.getData(CmsAuthenticated.KEY);
+ // }
+
+ 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 = CmsSessionImpl.getByUuid(nodeSessionId.toString());
+ cmsSession.close();
+ // if (log.isDebugEnabled())
+ // log.debug("Logged out CMS session " + cmsSession.getUuid());
+ return true;
+ }
+