AuthorizableUtil.java 5.72 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.RepositoryException
 *  javax.jcr.Value
 *  org.apache.commons.lang3.StringUtils
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.security.user.util;

import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AuthorizableUtil {
    private static Logger log = LoggerFactory.getLogger(AuthorizableUtil.class);
    static final String FAMILY_NAME = "profile/familyName";
    static final String GIVEN_NAME = "profile/givenName";
    @Deprecated
    private static final String PROPERTY_NAME = "rep:fullname";
    @Deprecated
    private static final String PROPERTY_DESCRIPTION = "rep:description";
    @Deprecated
    private static final String PROPERTY_FIRST_NAME = "cq:first-name";
    @Deprecated
    private static final String PROPERTY_LAST_NAME = "cq:last-name";

    public static String getName(Authorizable authorizable) throws RepositoryException {
        return AuthorizableUtil.getName(authorizable, null);
    }

    public static String getFormattedName(ResourceResolver resolver, String userId) {
        return AuthorizableUtil.getFormattedName(resolver, null, userId, null);
    }

    public static String getFormattedName(ResourceResolver resolver, Authorizable authorizable) {
        return AuthorizableUtil.getFormattedName(resolver, authorizable, null, null);
    }

    public static String getFormattedName(ResourceResolver resolver, String userId, String nameDisplayOrder) {
        return AuthorizableUtil.getFormattedName(resolver, null, userId, nameDisplayOrder);
    }

    public static String getFormattedName(ResourceResolver resolver, Authorizable authorizable, String nameDisplayOrder) {
        return AuthorizableUtil.getFormattedName(resolver, authorizable, null, nameDisplayOrder);
    }

    private static String getFormattedName(ResourceResolver resolver, Authorizable authorizable, String userId, String nameDisplayOrder) {
        if (authorizable != null || !StringUtils.isEmpty((CharSequence)userId)) {
            try {
                UserPropertiesManager upm = (UserPropertiesManager)resolver.adaptTo(UserPropertiesManager.class);
                if (upm != null) {
                    String displayName;
                    UserProperties props;
                    UserProperties userProperties = props = authorizable != null ? upm.getUserProperties(authorizable, "profile") : upm.getUserProperties(userId, "profile");
                    if (props != null && StringUtils.isNotEmpty((CharSequence)(displayName = props.getDisplayName(nameDisplayOrder)))) {
                        return displayName;
                    }
                }
            }
            catch (RepositoryException e) {
                log.warn("Unable to get display name for " + userId, (Throwable)e);
            }
        }
        try {
            return authorizable != null ? authorizable.getID() : userId;
        }
        catch (RepositoryException e) {
            log.error("Unable to get authorizable id", (Throwable)e);
            return null;
        }
    }

    private static String getName(Authorizable authorizable, UserProperties profile) throws RepositoryException {
        String name;
        String string = name = profile != null ? profile.getDisplayName() : null;
        if (name != null && name.length() > 0) {
            return name;
        }
        name = AuthorizableUtil.getProperty(authorizable, "profile/givenName", "cq:first-name");
        if (authorizable.isGroup()) {
            if (name != null && name.length() > 0) {
                return name;
            }
            name = AuthorizableUtil.getProperty(authorizable, "rep:fullname");
            if (name != null && name.length() > 0) {
                return name;
            }
            name = AuthorizableUtil.getProperty(authorizable, "rep:description");
            if (name != null && name.length() > 0) {
                return name;
            }
        } else {
            String familyName;
            StringBuilder buf = new StringBuilder();
            if (name != null) {
                buf.append(name);
            }
            if ((familyName = AuthorizableUtil.getProperty(authorizable, "profile/familyName", "cq:last-name")) != null && familyName.length() > 0) {
                buf.append(" ").append(familyName);
            }
            if (buf.length() > 0) {
                return buf.toString();
            }
        }
        return authorizable.getID();
    }

    private static String getProperty(Authorizable authorizable, String propertyName) {
        return AuthorizableUtil.getProperty(authorizable, propertyName, null);
    }

    private static String getProperty(Authorizable authorizable, String propertyName, String fallbackName) {
        try {
            Value[] vs;
            if (authorizable.hasProperty(propertyName) && (vs = authorizable.getProperty(propertyName)) != null && vs.length > 0) {
                return vs[0].getString();
            }
            if (fallbackName != null && authorizable.hasProperty(fallbackName) && (vs = authorizable.getProperty(fallbackName)) != null && vs.length > 0) {
                return vs[0].getString();
            }
        }
        catch (RepositoryException vs) {
            // empty catch block
        }
        return null;
    }
}