cancel
Showing results for 
Search instead for 
Did you mean: 

Behaviour question

davidez
Champ in-the-making
Champ in-the-making
I have a question on behaviours. I want that only some users can delete folders on my site.
If I use behaviour,I suppose I can use "beforeDeleteNode" method. But how can I identify the user that is deleting the node?
6 REPLIES 6

openpj
Elite Collaborator
Elite Collaborator
You can invoke the getCurrentUserName method from the AuthenticationService that you can inject using setter injection:

String currentUsername = authenticationService.getCurrentUserName();
Hope this helps  :wink:

davidez
Champ in-the-making
Champ in-the-making
Thank you for your reply!
I'm using javascript, and I tried with:
 var currentUsername = person.properties.userName; 

But this instruction isn't executed.

(My script has only this instruction and some "logger.log").

Any suggests?

davidez
Champ in-the-making
Champ in-the-making
I have added this in custom-model-context.xml

    <property name="authenticationService">
           <ref bean="authenticationService"/>
      </property>

but I have  this error in log:

Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'authenticationService' of bean class [org.alfresco.repo.jscript.ScriptBehaviour]: Bean property 'authenticationService' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1361)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1086)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
   at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:270)
   … 40 more
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'authenticationService' of bean class [org.alfresco.repo.jscript.ScriptBehaviour]: Bean property 'authenticationService' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
   at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1024)
   at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:900)
   at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
   at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
   at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1358)
   … 44 more

mrogers
Star Contributor
Star Contributor
There's some muddle in the thread above between a Java Bahaviour and a Java Script Behaviour.

The Java Script Behaviour injects the the serviceRegistry if you need to access something like the authenticationService.

However if you do want to inject your own property you will need to extend Script Behaviour and add your property to the model.

davidez
Champ in-the-making
Champ in-the-making
Sorry but I'm a little bit confused.

What I want to do is to identify the user that raises an event: the event is the folder deleting.
So I used a custom behaviour called "beforeDeleteNode".  On this event a javascript file is executed and, into this javascript file, I need to check the username that is trying to delete a folder.
How can I do this?
Can you show me an example?

davidez
Champ in-the-making
Champ in-the-making
In my custom-model-context.xml I have added:

<bean id="onBeforeDeleteMyFolder" class="org.alfresco.repo.policy.registration.ClassPolicyRegistration" parent="policyRegistration">
      <property name="policyName">
   <value>{http://www.alfresco.org}beforeDeleteNode</value>
      </property>
      <property name="className">
   <value>{my.test.com}MyDocument</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/extension/scripts/onBeforeDeleteNode.js</value>
         </constructor-arg>
       </bean>
     </property>
   </bean>
      </property>

    </bean>

The script onBeforeDeleteNode.js is:

try {
var currentUsername = person.properties.userName;
}
catch (e)
{
   logger.log ("error: " + e.message);
}
logger.log("currentUsername: " + currentUsername);

When my script is triggered I have this message in the log:
DEBUG [repo.jscript.ScriptLogger] error: "person" is not defined.

I hope that someone can help me… Please!