+ /** 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);
+ }
+ /*
+ * HELPERS
+ */
+
+ private static Subject currentSubject() {
+ NodeAuthenticated cmsView = getNodeAuthenticated();
+ if (cmsView != null)
+ return cmsView.getLoginContext().getSubject();
+ Subject subject = Subject.getSubject(AccessController.getContext());
+ if (subject != null)
+ return subject;
+ throw new CmsException("Cannot find related subject");
+ }
+
+ /**
+ * 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 NodeAuthenticated getNodeAuthenticated() {
+ return UiContext.getData(NodeAuthenticated.KEY);
+ }
+
+ private static Authorization getAuthorization(Subject subject) {
+ return subject.getPrivateCredentials(Authorization.class).iterator().next();
+ }
+