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.
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.
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.