Blog Post

...

Alfresco Extension Module evaluators

Since Alfresco 4 the extension modules were presented which allow to change/extend default Share components. This is very useful when we need to perform some minor changes to the graphical user interface within Alfresco. In this tutorial I will present how to implement such kind of module having as an example Share’s Site Members Dashlet that we’ll hide or show depending on user role. Evaluator logic will be implemented in Java.

First of all we need to define an extension module config. A quick guide how to do this can be found under Alfresco official page. This config can be placed to <TOMCAT>/shared/classes/alfresco/web-extension/site-data/extensions/some-extension.xml or <TOMCAT>/share/WEB-INF/lib/custom.jar/alfresco/site-data/extensions/some-extension.xml. I personally prefer the latter option because the extension modules are always related to Share application and it’s convenient to keep all customization files packed in a single jar. Some-extension.xml will look as following:

<extension>
  <modules>
    <module>
        <id>Conditionally Hide Site Members Dashlet</id>
        <auto-deploy>true</auto-deploy>
        <components>
            <component>
                <region-id>component-1-1</region-id>
                <source-id>site/{site}/dashboard</source-id>
                <scope>page</scope>
                <sub-components>
                    <sub-component id="default">
                        <evaluations>
                            <evaluation>
                                <evaluators>
                                    <evaluator type="evaluator.module.IsUserInRole">
                                        <params>
                                            <role>CustomConsumer</role>
                                        </params>
                                    </evaluator>
                                </evaluators>
                                <render>false</render>
                            </evaluation>                           
                        </evaluations>
                    </sub-component>
                </sub-components>
            </component>
        </components>
    </module>       
  </modules>
</extension>

 

In this configuration should be clarified:

  • source-id is the page id which current module belongs to. This id can be found by means of Alfresco Surfbug. In our case it is the Share Dashboard that contains all user dashlets.
  • region-id is the id of the component within parent page. This id can also be found with the Surfbug.
  • evaluator.type is the id of a bean that extends Alfresco Evaluator class. A snippet of Spring context is shown below.
  • params tag is a parameter wrapper that is passed to our evaluator. Role is a custom tag, text element of which will be taken by Evaluator to get value.
  • render false: hide current dashlet for every user in role CustomConsumer depending on evaluation result.

The snippet of spring context that defines our evaluator may be put to <TOMCAT>/share/WEB-INF/lib/custom.jar/alfresco/web-extension/share-custom-context.xml. It looks so:

   <bean id="evaluator.module.IsUserInRole" class="alfresco.extension.de.evaluators.IsUserInRole">
      <property name="slingshotEvaluatorUtil" ref="slingshot.evaluator.utility" />
   </bean>

 

For this bean we also inject EvalutorUtility bean which in turn defined in Alfresco default context. The java class of our evaluator:

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.alfresco.web.extensibility.SlingshotEvaluatorUtil;
import org.springframework.extensions.surf.RequestContext;
import org.springframework.extensions.surf.extensibility.impl.DefaultSubComponentEvaluator;
import org.springframework.extensions.surf.support.ThreadLocalRequestContext;

public class IsUserInRole extends DefaultSubComponentEvaluator {
    
    protected SlingshotEvaluatorUtil util = null;

    public void setSlingshotEvaluatorUtil(SlingshotEvaluatorUtil slingshotExtensibilityUtil) {
        this.util = slingshotExtensibilityUtil;
    }

    public boolean evaluate(RequestContext context, Map<String, String> params) {
        final RequestContext rc = ThreadLocalRequestContext.getRequestContext();
        List<String> groups = new ArrayList<String>();
        String roles = params.get("role");
        for (String role : roles.split(",")) {
            groups.add(role);
        }
        boolean hasMembership = this.util.isMemberOfGroups(rc, groups, false);
        return hasMembership;
    }
}

As a result we get an evaluator which checks whether logged in user is in CustomConsumer role and if so when hide Site Members Dashlet for this user or show otherwise.

Comments (1)

Tags: alfresco


Comments:

...

Hiten Rastogi Feb 08, 2016 at 09:37 #

Hi Stanislav, I am trying to create a module evaluator which hides a certain module based on the space where we are, like hiding the country folder module when we are state space but I am not able to get it working. Can you please help me with this ?? Copying the link to alfresco forum for my question. https://forums.alfresco.com/forum/end-user-discussions/alfresco-share/custom-module-evaluator-not-working-properly-01212016-0844

Leave a Comment