Alfresco, how to notify when a user leaves a Site

Recently a colleague asked me if the sysadmin could be notified every time a user leaves a Site in Alfresco. The short answer is no, as Alfresco does not have this feature by default. However, by using a classical development skill named Behaviour, the feature can be ready in about half an hour.

How a member relation to a Site is stored in Alfresco repository

Alfresco is only a tree of nodes, so every piece of data can be found as a node and its relationships.

For a site member, a child association of type cm:member is created for the user belonging the site. Every role of a site (Manager, Collaborator, Contributor, Consumer) is stored as a node of type cm:authorityContainer, so the role Manager for the Site swsdp has an authority GROUP named site_swdsp_SiteManager including all the members for this role as children (admin and abeecher in the picture below).

Creating a behaviour to react when a member is removed from a role group

Alfresco provides different kind of behaviours, which allows a developer to include custom code when a condition is matched.

In our case, is required to react when an association of type cm:member is removed from an cm:authorityContainer . This can be done with a few lines of Java.

public class MemberAssociationBehaviour
    implements NodeServicePolicies.OnDeleteChildAssociationPolicy {

    // Bind your code to the behaviour
    public void init() {
        policyComponent.bindAssociationBehaviour(
            NodeServicePolicies.OnDeleteChildAssociationPolicy.QNAME,
            ContentModel.TYPE_AUTHORITY_CONTAINER,
            ContentModel.ASSOC_MEMBER,
            new JavaBehaviour(this, "onDeleteChildAssociation",
                NotificationFrequency.TRANSACTION_COMMIT));
    }

    @Override
    public void onDeleteChildAssociation(ChildAssociationRef childAssocRef) {
        // Do some magic!
    }
}

Once declared this behaviour as a Spring bean, any notification mechanism can be developed in the “do some magic” section. Since then, every time a user leaves a site our custom code will be fired.

Final words

A sample implementation for this blog post has been provided at GitHub, however it should be easy to adapt the notification part to your requirements.

No modification has been done to Alfresco product nor configuration, just only a standard extension mechanism has been used to provide this new feature.

Note also that this sample includes i18n messages to provide a consistent log trace for the sysadm.

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s