AEM 6520 - Sites Impersonation Component

Goal


Create a component for impersonating users on Site's pages in author and publish

Demo | Package Install | Github


Configure Impersonating Users

                              here user nalabotu can impersonate as user cavery




Configure User Group in Dialog

                              here user nalabotu is part of administrators group and can see the textbox to enter userid for impersonation



Impersonation



Revert to Self



Solution


1) Configure the project sling models package in bundle/pom.xml

<Sling-Model-Packages>
       apps.experienceaem.sites
</Sling-Model-Packages>

2) Create a sling model to support Impersonation component. This checks for the existence of sling.sudo cookie and if logged user can be shown the impersonation feature (user is part of configured group)

package apps.experienceaem.sites;

import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.models.annotations.Model;

import org.apache.commons.lang3.StringUtils;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceUtil;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.models.annotations.Model;
import org.apache.sling.models.annotations.Required;
import org.apache.sling.models.annotations.injectorspecific.Self;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.PostConstruct;
import java.util.*;

@Model(adaptables = SlingHttpServletRequest.class)
public class EAEMImpersonationModel {
    private static final Logger log = LoggerFactory.getLogger(EAEMImpersonationModel.class);

    private static final String PROP_IMPERSONATION_GROUP = "impersonatorsGroup";

    @Self
    @Required
    private SlingHttpServletRequest request;

    private boolean showImpersonation;

    @PostConstruct
    private void init() {
        showImpersonation = false;

        try {
            if(request.getCookie("sling.sudo") != null){
                showImpersonation = true;
                return;
            }

            ResourceResolver resolver = request.getResourceResolver();

            ValueMap resourceProps = ResourceUtil.getValueMap(request.getResource());
            String impersonationGroup = resourceProps.get(PROP_IMPERSONATION_GROUP, "");

            if(StringUtils.isEmpty(impersonationGroup)){
                return;
            }

            Authorizable auth = resolver.adaptTo(Authorizable.class);
            Iterator<Group> groups = auth.memberOf();

            while(groups.hasNext()){
                if(groups.next().getID().equalsIgnoreCase(impersonationGroup)){
                    showImpersonation = true;
                    return;
                }
            }
        } catch (Exception e) {
            log.error("Error getting impersonation model", e);
        }
    }

    public boolean getShowImpersonation() {
        return showImpersonation;
    }
}

3) Create component /apps/eaem-impersonation-component/user-impersonation and add the following code in user-impersonation.html

<div class="ui form"
     data-sly-use.impModel="apps.experienceaem.sites.EAEMImpersonationModel"
     data-sly-test="${impModel.showImpersonation}">
    <div id="eaem-impersonate">
        <div style="margin: 0 0 15px 0">Impersonate as (enter user id)</div>
        <input type="text" style="width: 100%" onchange="EAEM_IMPERSONATION.impersonateAsUser(this.value)"/>
    </div>

    <div id="eaem-impersonate-revert" style="display:none">
        <div>
            Impersonating as "<span id="eaem-impersonate-user"></span>"
        </div>
        <div style="margin: 10px 0 0 0; cursor: pointer;"
             onclick="EAEM_IMPERSONATION.revertToSelf()">
            Revert to Self
        </div>
    </div>
</div>

<div data-sly-test="${!impModel.showImpersonation && wcmmode.edit}">
    Impersonators group not configured or logged in user not a member of the group
</div>

<sly data-sly-use.clientLib="/libs/granite/sightly/templates/clientlib.html"
     data-sly-call="${clientlib.all @ categories='eaem.user.impersonation'}"/>

4) Create client library /apps/eaem-impersonation-component/user-impersonation/clientlib with categories eaem.user.impersonation, add the following code in user-impersonation.js

(function () {
    window.EAEM_IMPERSONATION = {
        impersonateAsUser: function (userId) {
            document.cookie = "sling.sudo=" + userId + "; path=/";
            location.reload();
        },

        revertToSelf: function () {
            document.cookie = "sling.sudo=; path=/;";
            location.reload();
        },

        checkImpersonated: function () {
            var cookies = document.cookie;

            if (cookies && (cookies.indexOf("sling.sudo") != -1)) {
                var user = cookies.match('(^|;) ?' + 'sling.sudo' + '=([^;]*)(;|$)');

                $("#eaem-impersonate").hide();

                $("#eaem-impersonate-user").html(user ? user[2] : "");
                $("#eaem-impersonate-revert").show();
            }
        }
    };

    EAEM_IMPERSONATION.checkImpersonated();
}());

No comments:

Post a Comment