UserAdapterFactory.java 4.84 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Properties
 *  org.apache.felix.scr.annotations.Property
 *  org.apache.felix.scr.annotations.Service
 *  org.apache.jackrabbit.api.JackrabbitSession
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.jackrabbit.api.security.user.Group
 *  org.apache.jackrabbit.api.security.user.User
 *  org.apache.jackrabbit.api.security.user.UserManager
 *  org.apache.sling.api.adapter.AdapterFactory
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.granite.security.user.internal;

import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.User;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.adapter.AdapterFactory;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Component(metatype=0)
@Service(value={AdapterFactory.class})
@Properties(value={@Property(name="service.description", value={"Adapter for user management related classes."}), @Property(name="adapter.condition", value={"If the Session is a Jackrabbit Session, the ResourceResolver wraps a Jackrabbit Session, or the Resource was obtained from a Jackrabbit Session. In the case adapting a Resource to a User, Group or Authorizable, the Resource's path must correspond to a User, Group or Authorizable."})})
public class UserAdapterFactory
implements AdapterFactory {
    private static final Class<UserManager> USER_MANAGER = UserManager.class;
    private static final Class<User> USER = User.class;
    private static final Class<Group> GROUP = Group.class;
    private static final Class<Authorizable> AUTHORIZABLE = Authorizable.class;
    @Property(name="adapters")
    public static final String[] ADAPTER_CLASSES = new String[]{USER_MANAGER.getName(), USER.getName(), GROUP.getName(), AUTHORIZABLE.getName()};
    @Property(name="adaptables")
    public static final String[] ADAPTABLE_CLASSES = new String[]{ResourceResolver.class.getName(), JackrabbitSession.class.getName(), Resource.class.getName()};
    private final Logger log = LoggerFactory.getLogger(UserAdapterFactory.class);

    public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
        if (USER_MANAGER == type) {
            JackrabbitSession session = this.getSession(adaptable);
            if (session != null) {
                try {
                    return (AdapterType)session.getUserManager();
                }
                catch (RepositoryException e) {
                    this.log.error("Error while retrieve UserManager from Session.", (Object)e.getMessage());
                }
            }
        } else if (AUTHORIZABLE == type || USER == type || GROUP == type) {
            try {
                JackrabbitSession session = this.getSession(adaptable);
                if (session != null) {
                    Authorizable auth;
                    UserManager umgr = session.getUserManager();
                    if (adaptable instanceof Resource) {
                        String path = ((Resource)adaptable).getPath();
                        auth = umgr.getAuthorizableByPath(path);
                    } else {
                        auth = umgr.getAuthorizable(session.getUserID());
                    }
                    if (auth != null && type.isAssignableFrom(auth.getClass())) {
                        return (AdapterType)auth;
                    }
                }
            }
            catch (RepositoryException e) {
                this.log.debug("Could not adapt to current User: User is not allowed to read itself or has been deleted while session is open");
            }
        }
        return null;
    }

    private JackrabbitSession getSession(Object adaptable) {
        if (adaptable instanceof ResourceResolver) {
            adaptable = ((ResourceResolver)adaptable).adaptTo(Session.class);
        }
        if (adaptable instanceof Resource) {
            adaptable = ((Resource)adaptable).getResourceResolver().adaptTo(Session.class);
        }
        if (adaptable instanceof JackrabbitSession) {
            return (JackrabbitSession)adaptable;
        }
        return null;
    }
}