cancel
Showing results for 
Search instead for 
Did you mean: 

Person object in a custom behaviour

rjohnson
Star Contributor
Star Contributor
I am currently working in 4.0a

I have written a custom behaviour that executes when an aspect of a given type is added to a node. I wanted to record in the aspect who did the adding (just the users full name, not the full user object).

Having written several repo tier web scripts I used person.properties.userName only to be told that "person" is not defined.

On the basis that a behaviour runs in a security aware context, Alfresco must "know" who is running it, but I am at a loss as to how to get that information in the absence of the "person" object.

For further information I have attached the data model context file in which the behaviour is defined and the javascript that gets executed.

If anyone could assist, I would be most grateful.

Bob Johnson
10 REPLIES 10

matthewpatin
Champ in-the-making
Champ in-the-making
Hi Bob,

I've been having the same problem and believe I know the cause/have a workaround.

The ScriptBehaviour class gets the serviceRegistry injected in Spring in the context. However, when the model is created and passed to the script execution, the serviceRegistry is left out, which leaves us just with the behaviour in the model:

<java>
private Object invokeScript(Method method, Object[] args)
{
   // Build the model
   Map<String, Object> model = new HashMap<String, Object>(1);
   model.put("behaviour", new org.alfresco.repo.jscript.Behaviour(this.behaviour.serviceRegistry, method.getName(), args));
   
   // Execute the script
   return this.behaviour.serviceRegistry.getScriptService().executeScript(this.behaviour.location, model);
}
</java>
This would be fine, as the registry is a member of the class, however, it's set to private… so we can't access it in javascript like this (i've tried):


behaviour.serviceRegistry


However, if we want to modify the alfresco class, you can get the property from the model, by adding this line to the invokescript method:

<java>
model.put("serviceRegistry", this.behaviour.serviceRegistry);
</java>

After including this modification, I was able to access the authenticationService:


serviceRegistry.getAuthenticationService().getCurrentUserName();


Just make sure you're injecting the registry to the behaviour like so:

<bean id="onEditErrorNode" class="org.alfresco.repo.policy.registration.ClassPolicyRegistration" parent="policyRegistration">
       <property name="policyName">
           <value>{http://www.alfresco.org}onUpdateNode</value>
       </property>
       <property name="className">
           <value>{http://mynamespace}MY_ASPECT</value>
       </property>
       <property name="behaviour">
           <bean class="org.alfresco.repo.jscript.ScriptBehaviour" parent="scriptBehaviour">
               <property name="location">
                   <bean class="org.alfresco.repo.jscript.ClasspathScriptLocation">
                       <constructor-arg>
                           <value>alfresco/module/error/scripts/fixerror.js</value>
                       </constructor-arg>
                   </bean>
               </property>
               <property name="notificationFrequency">
               <value>TRANSACTION_COMMIT</value>
            </property>
            <property name="serviceRegistry" ref="ServiceRegistry"/>
           </bean>
       </property>
   </bean>

   
Hope this helps! If you can think of a cleaner way to work around this problem, I would love to hear it. If not, maybe we should raise an enhancement request for it so they can add the registry to the model?
Best regards,
Matthew