+ public final static Set<String> roles(Subject subject) {
+ Set<String> roles = new HashSet<String>();
+ roles.add(getUsername(subject));
+ for (Principal group : subject.getPrincipals(ImpliedByPrincipal.class)) {
+ roles.add(group.getName());
+ }
+ return roles;
+ }
+
+ 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());
+ }
+
+ /*
+ * HELPERS
+ */
+ private static Subject currentSubject() {
+ Subject subject = getAccessControllerSubject();
+ if (subject != null)
+ return subject;
+ throw new IllegalStateException("Cannot find related subject");
+ }
+
+ private static Subject getAccessControllerSubject() {
+ return Subject.getSubject(AccessController.getContext());
+ }
+