DefaultProfileProvider.java 7.54 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.security.user.UserProperties
 *  com.adobe.granite.security.user.UserPropertiesManager
 *  com.adobe.granite.security.user.UserPropertiesService
 *  com.day.cq.personalization.ProfileProvider
 *  com.day.cq.personalization.UserPropertiesProvider
 *  com.day.cq.security.User
 *  com.day.cq.security.profile.Profile
 *  com.day.cq.security.profile.ProfileManager
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.sling.api.SlingHttpServletRequest
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceUtil
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.personalization.impl;

import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import com.adobe.granite.security.user.UserPropertiesService;
import com.day.cq.personalization.ProfileProvider;
import com.day.cq.personalization.UserPropertiesProvider;
import com.day.cq.security.User;
import com.day.cq.security.profile.Profile;
import com.day.cq.security.profile.ProfileManager;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultProfileProvider
implements ProfileProvider,
UserPropertiesProvider {
    private static final String PARAM_AUTHORIZABLE = "authorizable";
    private static final String PROPERTY_PROFILE = "profile";
    private static final String VARIABLE_ME = "~";
    private final Logger log = LoggerFactory.getLogger(DefaultProfileProvider.class);
    private ProfileManager profileManager;
    private UserPropertiesService userPropertiesService;

    @Deprecated
    public Profile getProfile(SlingHttpServletRequest request) {
        ResourceResolver resourceResolver = request.getResourceResolver();
        Resource resource = request.getResource();
        Profile profile = (Profile)resource.adaptTo(Profile.class);
        if (profile == null) {
            this.log.debug("Resource didn't adapt to an Profile, search for authorizable ID at {}", (Object)resource.getPath());
            ValueMap properties = ResourceUtil.getValueMap((Resource)resource);
            String auth = (String)properties.get("profile", String.class);
            if (auth == null) {
                this.log.debug("Resource didn't have a authorizable property value at, search request parameter", (Object)resource.getPath());
                auth = request.getParameter("authorizable");
            }
            if (auth != null) {
                if ("~".equals(auth)) {
                    this.log.debug("replaced variable 'me' with current user", (Object)resource.getPath());
                    profile = ((User)resourceResolver.adaptTo(User.class)).getProfile();
                } else {
                    this.log.debug("resolve Profile from request to {} for User {}", (Object)resource.getPath(), (Object)auth);
                    try {
                        return this.profileManager.getProfile(auth, (Session)resourceResolver.adaptTo(Session.class));
                    }
                    catch (RepositoryException e) {
                        this.log.error("error accessing profile: ", (Throwable)e);
                    }
                }
            }
        }
        if (profile == null) {
            profile = this.getProfile(resourceResolver);
        }
        return profile;
    }

    @Deprecated
    public Profile getProfile(ResourceResolver resolver) {
        User user = (User)resolver.adaptTo(User.class);
        return null != user ? user.getProfile() : null;
    }

    public UserProperties getUserProperties(SlingHttpServletRequest request) {
        Resource resource = request.getResource();
        ResourceResolver resourceResolver = request.getResourceResolver();
        UserProperties userProperties = (UserProperties)resource.adaptTo(UserProperties.class);
        if (userProperties == null) {
            this.log.debug("resource does not represent user properties, searching via authorizable ID at {}", (Object)resource.getPath());
            ValueMap properties = ResourceUtil.getValueMap((Resource)resource);
            String auth = (String)properties.get("profile", String.class);
            if (auth == null) {
                this.log.debug("Resource didn't have a authorizable property value at, search request parameter", (Object)resource.getPath());
                auth = request.getParameter("authorizable");
            }
            if (auth != null) {
                UserPropertiesManager upm = (UserPropertiesManager)resourceResolver.adaptTo(UserPropertiesManager.class);
                if (null == upm) {
                    this.log.error("getUserProperties: UserPropertiesManager unavailable.");
                    return null;
                }
                if ("~".equals(auth)) {
                    this.log.debug("replaced variable 'me' with current user", (Object)resource.getPath());
                    try {
                        userProperties = upm.getUserProperties(((User)resourceResolver.adaptTo(User.class)).getID(), null);
                    }
                    catch (RepositoryException e) {
                        this.log.error("getUserProperties: error retrieving user properties: ", (Throwable)e);
                    }
                } else {
                    this.log.debug("resolve UserProperties from request to {} for User {}", (Object)resource.getPath(), (Object)auth);
                    try {
                        return upm.getUserProperties(auth, null);
                    }
                    catch (RepositoryException e) {
                        this.log.error("error accessing userProperties: ", (Throwable)e);
                    }
                }
            }
        }
        if (userProperties == null) {
            userProperties = this.getUserProperties(resourceResolver);
        }
        return userProperties;
    }

    public UserProperties getUserProperties(ResourceResolver resolver) {
        UserPropertiesManager upm = (UserPropertiesManager)resolver.adaptTo(UserPropertiesManager.class);
        if (null == upm) {
            this.log.error("getUserProperties: UserPropertiesManager unavailable.");
            return null;
        }
        try {
            return upm.getUserProperties(resolver.getUserID(), "profile");
        }
        catch (RepositoryException e) {
            this.log.error("getUserProperties: repository error while getting user properties: ", (Throwable)e);
            return null;
        }
    }

    protected void bindProfileManager(ProfileManager profileManager) {
        this.profileManager = profileManager;
    }

    protected void unbindProfileManager(ProfileManager profileManager) {
        if (this.profileManager == profileManager) {
            this.profileManager = null;
        }
    }

    protected void bindUserPropertiesService(UserPropertiesService userPropertiesService) {
        this.userPropertiesService = userPropertiesService;
    }

    protected void unbindUserPropertiesService(UserPropertiesService userPropertiesService) {
        if (this.userPropertiesService == userPropertiesService) {
            this.userPropertiesService = null;
        }
    }
}