NotificationHelper.java 8.18 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.security.user.UserProperties
 *  com.adobe.granite.security.user.UserPropertiesManager
 *  javax.jcr.Credentials
 *  javax.jcr.PathNotFoundException
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.SimpleCredentials
 *  org.apache.jackrabbit.api.security.user.Authorizable
 *  org.apache.jackrabbit.api.security.user.Group
 *  org.apache.jackrabbit.api.security.user.UserManager
 *  org.apache.sling.api.resource.LoginException
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ResourceResolverFactory
 *  org.apache.sling.jcr.api.SlingRepository
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.workflow.impl.email;

import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.jcr.Credentials;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.SimpleCredentials;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.LoginException;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NotificationHelper {
    private static final Logger LOGGER = LoggerFactory.getLogger(NotificationHelper.class);
    private static final String WORKFLOW_SERVICE = "workflow";

    private NotificationHelper() {
    }

    public static Authorizable getAuthorizable(UserManager manager, String userId) {
        if (null != manager && null != userId) {
            if ("system".equals(userId)) {
                userId = "admin";
            }
            try {
                return manager.getAuthorizable(userId);
            }
            catch (RepositoryException e) {
                LOGGER.warn("User Manager was unable to find the User: {}.", (Object)userId, (Object)e);
            }
        } else {
            LOGGER.warn("user manager or user id unavailable: {} - {}", (Object)manager, (Object)userId);
        }
        return null;
    }

    public static Set<Authorizable> getParticipants(String userGroupId, ResourceResolver resolver) {
        UserManager um;
        HashSet<Authorizable> auths = new HashSet<Authorizable>();
        if (userGroupId != null && userGroupId.length() > 0 && (um = (UserManager)resolver.adaptTo(UserManager.class)) != null) {
            Authorizable participant = NotificationHelper.getAuthorizable(um, userGroupId);
            auths.addAll(NotificationHelper.getParticipants(participant, resolver));
        }
        return auths;
    }

    private static Set<Authorizable> getParticipants(Authorizable userGroup, ResourceResolver resolver) {
        HashSet<Authorizable> participants;
        block9 : {
            participants = new HashSet<Authorizable>();
            if (userGroup != null) {
                if (!userGroup.isGroup()) {
                    try {
                        String userID = userGroup.getID();
                        String email = NotificationHelper.getEmailAddress(userGroup.getID(), resolver);
                        if (email != null) {
                            LOGGER.debug("extracted user {} with email {} from workflow step.", (Object)userID, (Object)email);
                            participants.add(userGroup);
                            break block9;
                        }
                        LOGGER.debug("extracted user {} which has no email, not sending notification to this user.", (Object)userID);
                    }
                    catch (RepositoryException e) {
                        LOGGER.error("Unable to look up user's ID.", (Throwable)e);
                    }
                } else {
                    Group group = (Group)userGroup;
                    Iterator members = null;
                    try {
                        members = group.getMembers();
                    }
                    catch (RepositoryException e) {
                        LOGGER.error("Unable to query for members of group.", (Throwable)e);
                    }
                    while (members != null && members.hasNext()) {
                        Authorizable member = (Authorizable)members.next();
                        participants.addAll(NotificationHelper.getParticipants(member, resolver));
                    }
                }
            }
        }
        return participants;
    }

    public static String getEmailAddress(String userID, ResourceResolver resolver) throws RepositoryException {
        UserPropertiesManager userPropertiesManager = (UserPropertiesManager)resolver.adaptTo(UserPropertiesManager.class);
        UserProperties props = userPropertiesManager.getUserProperties(userID, "profile");
        String mail = props.getProperty("email");
        if (mail != null && mail.length() == 0) {
            mail = null;
        }
        return mail;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static String getUserLanguage(SlingRepository repository, ResourceResolverFactory resolverFactory, String userId) {
        block16 : {
            if ("system".equals(userId)) {
                return null;
            }
            Session userSession = null;
            ResourceResolver resolver = null;
            if (null != repository) {
                SimpleCredentials credentials = new SimpleCredentials(userId, new char[0]);
                try {
                    userSession = repository.impersonateFromService("workflow", (Credentials)credentials, null);
                    resolver = resolverFactory.getResourceResolver(Collections.singletonMap("user.jcr.session", userSession));
                    UserPropertiesManager userPropertiesManager = (UserPropertiesManager)resolver.adaptTo(UserPropertiesManager.class);
                    UserProperties userProperties = userPropertiesManager.getUserProperties(userId, "preferences");
                    try {
                        String language;
                        String string = language = null != userProperties ? userProperties.getProperty("language") : null;
                        return string;
                    }
                    catch (PathNotFoundException e) {
                        try {
                            LOGGER.error("Could not get resource bundle for {} path does not exist: ", (Object)userId, (Object)e);
                        }
                        catch (RepositoryException e) {
                            LOGGER.error("Could not get resource bundle for user {}, impersonation failed: ", (Object)userId, (Object)e);
                        }
                        catch (LoginException e) {
                            LOGGER.error("Could not get resource bundle for user {}, impersonation failed: ", (Object)userId, (Object)e);
                        }
                        catch (Throwable var10_13) {
                            throw var10_13;
                        }
                        if (userSession != null && userSession.isLive()) {
                            userSession.logout();
                        }
                        if (resolver != null && resolver.isLive()) {
                            resolver.close();
                        }
                        break block16;
                    }
                }
                finally {
                    if (userSession != null && userSession.isLive()) {
                        userSession.logout();
                    }
                    if (resolver != null && resolver.isLive()) {
                        resolver.close();
                    }
                }
            }
            LOGGER.error("could not get resource bundle for {}, repository unavailable.", (Object)userId);
        }
        return null;
    }
}