Alfresco Execute Code as System User

Alfresco as you know is open source Content Management system and having strong Content management features. But as we know each client has their own set of requirements where they might want to customize existing functionality or create new feature in Alfresco or integrate it with other technology. For making all of this feasible Alfresco has provided set of Services on top of alfresco repository through which we can manipulate that repository.

Now alfresco has taken care that although we can do customization using those services all critical operations like creation of contents, deletion of content etc… has to be done explicitly through System user or user with proper privileges. For implementing this security feature they has created APIs within AuthenticationUtils class.

Details of Those apis are as follow.

public static String getSystemUserName()

Get the name of the system user

Returns: system user name

public static net.sf.acegisecurity.Authentication setRunAsUserSystem()

Set the system user as the currently running user for authentication purposes.

public static <R> R runAs(AuthenticationUtil.RunAsWork<R> runAsWork, String uid)

Execute a unit of work as a given user. The thread’s authenticated user will be returned to its normal state after the call.


runAsWork – the unit of work to do

uid – the user ID


Returns the work’s return value

This is how it would look like when you implement it in your code.

private void assoicate(final NodeRef sourceRef,final NodeRef destRef,

final QName assocTypeQName) {

RunAsWork<Void> processNodesWork = new RunAsWork<Void>() {

public Void doWork() throws Exception {

// Piece of block which needs to be run as system

return null;


AuthenticationUtil.runAs(processNodesWork, AuthenticationUtil.getSystemUserName());


Keep in mind this has to be used with caution. If you try to retrieve user name within that blog it will return as system because for that particular execution user is system. One more point is that all external variables which are used in that block has to be defined as final that is the reason all parameters of that method are all final.

You can also return value from that piece of code incase require. For instance you are creating new node within that block you can always return that ndoeRef but you need to define that return type.

Looking for quality Alfresco Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , | Posted under Alfresco | RSS 2.0

Author Spotlight


I love opensource technologies working with those technologies from the time I have stepped in to the Software Industry. Alfresco CMS is my area of expertise. I have worked on various complex implementations which involved integration of Alfresco with other technologies, extensively worked with JBPM workflows and Webscripts.

Leave a Reply

Your email address will not be published. Required fields are marked *