AnsweredAssumed Answered

Trigger script when accessing a folder

Question asked by abder on Oct 19, 2017

I am currently developing a connector for a CRM-system called Odoo. I have successfully created my directory-structure based on the customers in the CRM. Now the problem is that Odoo has its own security-rules for accessing a record. (for example a salesperson can only view his/her own customers.)

 

Those record-rules are setup based on specific filters (f.e [('current_logged_user','=',sales_person)]). The sales_person in this example is a field on my customer-form. 

 

It could be that salesPersonA has initially access to a record. But when the salesperson on the form is changed, then he doesn't have access anymore. So the rules have a dynamic nature. To synchronize every field that could change, is a bit of an overkill.

 

A possible solution may be to run a script, when opening a folder, and check in Odoo if the current user has access to the particular record. If he/she has acces, show the children files/directories. If he/she doesn't have access, show an error message. On each folder created from the CRM, a custom aspect is attached with the record ID and record model.

 

 

 

 

I tried to create a custom onContentRead-behavior and put my custom logic in here. But this method gets triggered a couple of times, so I don't think this is the best place for my custom code.

 

I find it a bit strange I can't trigger a custom script/action when opening a folder. Any ideas ?

public class Odoo implements BeforeDeleteNodePolicy, OnUpdateNodePolicy, OnContentReadPolicy {

// Dependencies
private NodeService nodeService;
private PolicyComponent policyComponent;

// Behaviours
private Behaviour beforeDeleteNode;
private Behaviour onUpdateNode;
private Behaviour onContentRead;
private Logger logger = Logger.getLogger(Odoo.class);

public void init() {

    this.beforeDeleteNode = new JavaBehaviour(this, "beforeDeleteNode", NotificationFrequency.EVERY_EVENT);
    this.onUpdateNode = new JavaBehaviour(this, "onUpdateNode", NotificationFrequency.EVERY_EVENT);
    this.onContentRead = new JavaBehaviour(this, "onContentRead", NotificationFrequency.EVERY_EVENT);
    this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "beforeDeleteNode"), "cm:content",this.beforeDeleteNode);
    this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onUpdateNode"), "cm:content",this.onUpdateNode);
    this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onContentRead"), OdooModel.ASPECT_INFO, this.onContentRead);

}

@Override
public void beforeDeleteNode(NodeRef nodeRef) {
    logger.debug("Node : " + nodeRef.toString());
    String logged_user = AuthenticationUtil.getFullyAuthenticatedUser();
    String creator = nodeService.getProperty(nodeRef, ContentModel.PROP_CREATOR).toString();
    if(creator.equals("odoo") && (!logged_user.equals("odoo") && !logged_user.equals("admin"))){
        throw new AlfrescoRuntimeException("Dit is een odoo-folder. Deze mag niet verwijderd worden. " + nodeRef);
    }
}

@Override
public void onUpdateNode(NodeRef nodeRef)
{
    if (nodeService.exists(nodeRef)){
       try {
         logger.debug("Node : " + nodeRef.toString());
       }
       catch (Exception e) {
          String msg = "onUpdateNode for property "+"" +" failed.";
          logger.fatal(msg);
          throw new AlfrescoRuntimeException(msg, e);
      }
    }
}

@Override
public void onContentRead(NodeRef nodeRef)
{
    if (nodeService.exists(nodeRef)){
        try {
            logger.debug("Node : " + nodeRef.toString());
       }
       catch (Exception e) {
            String msg = "onUpdateNode for property "+"" +" failed.";
            logger.fatal(msg);
            throw new AlfrescoRuntimeException(msg, e);
      }
    }
}

public NodeService getNodeService() {
    return nodeService;
}

public void setNodeService(NodeService nodeService) {
    this.nodeService = nodeService;
}

public PolicyComponent getPolicyComponent() {
    return policyComponent;
}

public void setPolicyComponent(PolicyComponent policyComponent) {
    this.policyComponent = policyComponent;
}
}

Outcomes