All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adobe.cq.social.group.client.api.AbstractCommunityGroupUser Maven / Gradle / Ivy

There is a newer version: 6.5.21
Show newest version
/*************************************************************************
 *
 * ADOBE CONFIDENTIAL
 * __________________
 *
 *  Copyright 2012 Adobe Systems Incorporated
 *  All Rights Reserved.
 *
 * NOTICE:  All information contained herein is, and remains
 * the property of Adobe Systems Incorporated and its suppliers,
 * if any.  The intellectual and technical concepts contained
 * herein are proprietary to Adobe Systems Incorporated and its
 * suppliers and are protected by trade secret or copyright law.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from Adobe Systems Incorporated.
 **************************************************************************/
package com.adobe.cq.social.group.client.api;

import com.adobe.cq.social.badging.api.BadgingService;
import com.adobe.cq.social.community.api.CommunityContext;
import com.adobe.cq.social.group.api.GroupUtil;
import com.adobe.cq.social.scf.ClientUtilities;
import com.adobe.cq.social.scf.core.AbstractUser;
import com.adobe.cq.social.scoring.api.ScoringService;
import com.adobe.cq.social.serviceusers.internal.ServiceUserWrapper;
import com.adobe.granite.security.user.UserProperties;
import com.adobe.granite.security.user.UserPropertiesManager;
import org.apache.commons.lang.StringUtils;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.jcr.api.SlingRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.jcr.RepositoryException;
import javax.jcr.Session;

public class AbstractCommunityGroupUser extends AbstractUser implements CommunityGroupUser {

    private static final Logger LOG = LoggerFactory.getLogger(AbstractCommunityGroupUser.class);

    private final CommunityContext context;

    private final ServiceUserWrapper serviceUserWrapper;

    @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY, policy = ReferencePolicy.STATIC)
    private final SlingRepository repository;



    private static final String ABSRACT = "aboutMe";
    private static final String USER_ADMIN = "communities-user-admin";

    public AbstractCommunityGroupUser(final Resource resource, final ClientUtilities clientUtilities,
                                      final UserPropertiesManager upm, final ScoringService scoringService, final BadgingService badgingService,
                                      final ServiceUserWrapper serviceUserWrapper, final SlingRepository repository) {
        super(resource, clientUtilities, upm, scoringService, badgingService);
        context = clientUtils.getRequest().getResource().adaptTo(CommunityContext.class);
        this.serviceUserWrapper = serviceUserWrapper;
        this.repository = repository;

    }

    @Override
    public Boolean isAdmin() {
        // log in user admin service,
        // since checkIfUserIsGroupAdmin API needs to get Authorizable
        Session userAdminSession = null;
        try {
            userAdminSession = serviceUserWrapper.loginService(repository, USER_ADMIN);
            userAdminSession.refresh(true);
            UserManager um = ((JackrabbitSession) userAdminSession).getUserManager();
            if (um == null) {
                return false;
            }
            final String groupId = clientUtils.getRequest().getParameter("groupId");
            if (StringUtils.isEmpty(groupId)) {
                // from community context
                return context.checkIfUserIsGroupAdmin(um, this.getAuthorizableId());
            } else {
                // from groupId property
                return GroupUtil.isMember(um, groupId, this.getAuthorizableId())
                        || context.checkIfUserIsGroupAdmin(um, this.getAuthorizableId());
            }
        } catch (final RepositoryException e) {
            LOG.error("failed to check if user " + this.getAuthorizableId() + " is group admin ", e);
            return false;
        } finally {
            if (userAdminSession != null) {
                userAdminSession.logout();
            }
        }
    }

    @Override
    public boolean isCanInvite() {
        // hide operations to self
        Session userAdminSession = null;
        boolean canInviteGroupMember = false;
        try {
            userAdminSession = serviceUserWrapper.loginService(repository, USER_ADMIN);
            userAdminSession.refresh(true);
            UserManager um = ((JackrabbitSession) userAdminSession).getUserManager();
            if (this.getUserId().equals(clientUtils.getAuthorizedUserId())) {
                return false;
            }
            final ResourceResolver resolver = this.clientUtils.getRequest().getResourceResolver();
            canInviteGroupMember = GroupUtil.canInviteGroupMember(resolver, context, serviceUserWrapper, repository, um);
        } catch (RepositoryException e) {
            LOG.error("Unable to check if user can invite", e);
        } finally {
            if (userAdminSession != null) {
                userAdminSession.logout();
            }
        }
        return canInviteGroupMember;
    }

    /*
     * (non-Javadoc)
     * @see com.adobe.cq.social.group.client.api.CommunityGroupUser#getDescription()
     */
    @Override
    public String getDescription() {
        final UserProperties userProps = getUserProperties();
        try {
            if (userProps != null) {
                return userProps.getProperty(ABSRACT);
            } else {
                return null;
            }
        } catch (final RepositoryException e) {
            LOG.error("Error retrieving description for " + userProps.getAuthorizableID(), e);
            return null;
        }
    }

    @Override
    public boolean isCanPromote() {
        // hide operations to self
        Session userAdminSession = null;
        boolean canPromoteGroupMember = false;
        try {
            userAdminSession = serviceUserWrapper.loginService(repository, USER_ADMIN);
            userAdminSession.refresh(true);
            UserManager um = ((JackrabbitSession) userAdminSession).getUserManager();
            if (this.getUserId().equals(clientUtils.getAuthorizedUserId())) {
                return false;
            }
            final ResourceResolver resolver = this.clientUtils.getRequest().getResourceResolver();
            canPromoteGroupMember = GroupUtil.canPromoteGroupMember(resolver, context, serviceUserWrapper, repository, um);
        } catch (RepositoryException e) {
            LOG.error("Unable to check if user can promote", e);
        } finally {
            if (userAdminSession != null) {
                userAdminSession.logout();
            }
        }
        return canPromoteGroupMember;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy