* context.
*/
public final class CurrentUser {
- /*
- * CURRENT USER API
- */
-
/**
* Technical username of the currently authenticated user.
*
}
/** Locale of the current user */
- public final static Locale locale() {
+ public static Locale locale() {
return locale(currentSubject());
}
/** Roles of the currently logged-in user */
- public final static Set<String> roles() {
+ public static Set<String> roles() {
return roles(currentSubject());
}
}
/** Implies this {@link SystemRole} in this context. */
- public final static boolean implies(SystemRole role, String context) {
+ public static boolean implies(SystemRole role, String context) {
return role.implied(currentSubject(), context);
}
/** Implies this role name, also independently of the context. */
- public final static boolean implies(String role, String context) {
+ public static boolean implies(String role, String context) {
return SystemRole.implied(NamespaceUtils.parsePrefixedName(role), currentSubject(), context);
}
+ /** Get the primary context this user belongs to. */
+ public static boolean isUserContext(String context) {
+ // TODO have the role context as a separated credential in the Subjecto?
+ return RoleNameUtils.getContext(getUsername()).equalsIgnoreCase(context);
+ }
+
/** Executes as the current user */
- public final static <T> T doAs(PrivilegedAction<T> action) {
+ public static <T> T doAs(PrivilegedAction<T> action) {
return Subject.doAs(currentSubject(), action);
}
/** Executes as the current user */
- public final static <T> T tryAs(PrivilegedExceptionAction<T> action) throws PrivilegedActionException {
+ public static <T> T tryAs(PrivilegedExceptionAction<T> action) throws PrivilegedActionException {
return Subject.doAs(currentSubject(), action);
}
* WRAPPERS
*/
- public final static String getUsername(Subject subject) {
+ public static String getUsername(Subject subject) {
if (subject == null)
throw new IllegalArgumentException("Subject cannot be null");
if (subject.getPrincipals(X500Principal.class).size() != 1)
return principal.getName();
}
- public final static String getDisplayName(Subject subject) {
+ public static String getDisplayName(Subject subject) {
return getAuthorization(subject).toString();
}
- public final static Set<String> roles(Subject subject) {
+ public static Set<String> roles(Subject subject) {
Set<String> roles = new HashSet<String>();
roles.add(getUsername(subject));
for (Principal group : subject.getPrincipals(ImpliedByPrincipal.class)) {
return roles;
}
- public final static Locale locale(Subject subject) {
+ public static Locale locale(Subject subject) {
Set<Locale> locales = subject.getPublicCredentials(Locale.class);
if (locales.isEmpty()) {
Locale defaultLocale = CmsContextImpl.getCmsContext().getDefaultLocale();