Person object in a custom behaviour
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2013 05:20 AM
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
- Labels:
-
Archive
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2013 08:11 AM
Your attached files are not visible.
Please add it one more time.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-07-2013 09:49 AM
When I edited my post the files were listed. I removed them and re-added them with marginally different file names. Hopefully they are visible now unless I am doing something particularly silly.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2013 05:09 AM


- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2013 06:17 AM
Just upload that somewhere and paste a link.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-08-2013 08:21 AM
The context file looks like this
<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'> <beans> <!– Registration of new models –> <bean id="extension.dictionaryBootstrap" parent="dictionaryModelBootstrap" depends-on="dictionaryBootstrap"> <property name="models"> <list> <value>alfresco/extension/fgempModel.xml</value> </list> </property> <property name="labels"> <list> <value>alfresco/extension/fgempModelRepo</value> </list> </property> </bean> <bean id="onDischargeManagementRef" class="org.alfresco.repo.policy.registration.ClassPolicyRegistration" parent="policyRegistration"> <property name="policyName"> <value>{http://www.alfresco.org}onAddAspect</value> </property> <property name="className"> <value>{http://www.farthestgate.co.uk/emp/model/1.0}discharged</value> </property> <property name="behaviour"> <bean class="org.alfresco.repo.jscript.ScriptBehaviour" parent="scriptBehaviour"> <property name="notificationFrequency" value="FIRST_EVENT"/> <property name="location"> <bean class="org.alfresco.repo.jscript.ClasspathScriptLocation"> <constructor-arg> <value>alfresco/extension/scripts/onAddDischargedAspect.js</value> </constructor-arg> </bean> </property> </bean> </property> </bean> </beans>
and the javascript looks like this
var scriptFailed = false;// Have a look at the behaviour object that should have been passedif (behaviour == null) { logger.log("The behaviour object has not been set."); scriptFailed = true;}if (behaviour.name == null && behaviour.name != "onAddAspect") { logger.log("The behaviour name has not been set correctly."); scriptFailed = true;} else { logger.log("Behaviour name: " + behaviour.name);}// Check the argumentsif (behaviour.args == null) { logger.log("The args have not been set."); scriptFailed = true;} else { // We should have nodeType in [0] and a list of aspects in [1] if (behaviour.args.length == 2) { var docNode = behaviour.args[0]; try { docNode.properties["{http://www.farthestgate.co.uk/emp/model/1.0}whoDischarged"] = person.properties.userName; } catch (e) { logger.log("Set whoDischarged failed " + e.message); } try { docNode.properties["{http://www.farthestgate.co.uk/emp/model/1.0}whenDischarged"] = new Date(); } catch (e2) { logger.log("Set whenDischarged failed " + e2.message); } docNode.save(); } else { logger.log("The number of arguments is incorrect.it is " + behaviour.args.length + " They are " + behaviour.args[0] + " and " + behaviour.args[1]); scriptFailed = true; }}
It fails on the line
docNode.properties["{http://www.farthestgate.co.uk/emp/model/1.0}whoDischarged"] = person.properties.userName;
and logs the error "Set whoDischarged failed "person" is not defined"
Thank you for your perseverance.
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2013 02:32 AM
docNode.properties["{http://www.farthestgate.co.uk/emp/model/1.0}whoDischarged"] = person.properties.name;
Also check is this person object null or not.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-11-2013 06:52 AM
try { if(person != null) { logger.log("person != null"); docNode.properties["{http://www.farthestgate.co.uk/emp/model/1.0}whoDischarged"] = person.properties.name; } else { logger.log("person == null"); } } catch (e) { logger.log("Set whoDischarged failed person check null, use person.properties.name" + e.message); }
and got the log message
10:31:25,372 DEBUG [org.alfresco.repo.jscript.ScriptLogger] Behaviour name: onAddAspect10:31:25,379 DEBUG [org.alfresco.repo.jscript.ScriptLogger] Set whoDischarged failed person check null, use person.properties.name"person" is not defined.
Which suggest to me that the execution failed on the if person != null check as there are no other errors in the log.
So I then tried
try { if(person.properties.name != null) { logger.log("person.properties.name != null"); docNode.properties["{http://www.farthestgate.co.uk/emp/model/1.0}whoDischarged"] = person.properties.name; } else { logger.log("person.properties.name == null"); } } catch (e) { logger.log("Set whoDischarged, use person.properties.name" + e.message); }
and got the log message
10:50:17,759 DEBUG [org.alfresco.repo.jscript.ScriptLogger] Behaviour name: onAddAspect10:50:17,766 DEBUG [org.alfresco.repo.jscript.ScriptLogger] Set whoDischarged, use person.properties.name"person" is not defined.
Which seems to confirm to me that the person object is not defined in this context.
Any further thoughts will be welcome.
Thanks
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-18-2013 05:08 AM
So what I assume is it should be available in the script.
If it is then you can get AuthenticationService from it
and that service has api getCurrentUserName() which will return you currently logged in user's name.
Give it a shot.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-21-2013 08:46 PM
Thank you so much for you input on this, but I have tried
var sras = ServiceRegistry.AuthenticationService.getCurrentUserName(); var sras = serviceRegistry.AuthenticationService.getCurrentUserName(); var sras = servicesegistry.AuthenticationService.getCurrentUserName(); var sras = Serviceregistry.AuthenticationService.getCurrentUserName();
individually and all of them say ServiceRegistry (however capitalised) "is not defined".
Any other thoughts?
Thanks again
Bob
