Search code examples
javaadminalfrescobackendrunas

Run Alfresco Java code as Administrator


I'm trying to implement an action in which I will add permissions to all parent nodes. However, I need to run as admin to manage the permissions. Currently my code looks like this:

        permissionService = serviceRegistry.getPermissionService();
        //Read the username of the current user
        final String loggedInUser = authenticationService.getCurrentUserName();

        ChildAssociationRef childAssociationRef = nodeService.getPrimaryParent(actionedUponNodeRef);

        //Get the parent NodeRef
        NodeRef parent = childAssociationRef.getParentRef();
        String fileName = (String) nodeService.getProperty(parent, ContentModel.PROP_NAME);

        //Iterate till you get to document library
        while(!fileName.contains("documentLibrary")){
            ChildAssociationRef childAssociationRef2 = nodeService.getPrimaryParent(parent);
            parent = childAssociationRef2.getParentRef();
            //Have to declare a final variable in order to access it in the RunAsWork
            final NodeRef ref = parent;

            fileName = (String) nodeService.getProperty(parent, ContentModel.PROP_NAME);

            RunAsWork<?> raw = new RunAsWork<Object>() {
             public Object doWork() throws Exception {
                 //Set permission to this folder for the logged in user
                 permissionService.setPermission(ref, loggedInUser, PermissionService.CONTRIBUTOR, true);
                 return null;
                }   
            };
            //Run as admin
            AuthenticationUtil.runAs(raw, "admin");
        }

The exception that I get is pretty obvious: SEVERE: 04210027 Access Denied. You do not have the appropriate permissions to perform this operation.

Any suggestions? Thanks


Solution

  • To detail the answer from Krutik, you should wrap code in a runAsSystem block like this:

    final permissionService = serviceRegistry.getPermissionService();
    //Read the username of the current user
    final String loggedInUser = authenticationService.getCurrentUserName();
    
    ChildAssociationRef childAssociationRef = nodeService.getPrimaryParent(actionedUponNodeRef);
    
    //Get the parent NodeRef
    NodeRef parent = childAssociationRef.getParentRef();
    String fileName = (String) nodeService.getProperty(parent, ContentModel.PROP_NAME);
    
    //Iterate till you get to document library
    while(!fileName.contains("documentLibrary")){
        ChildAssociationRef childAssociationRef2 = nodeService.getPrimaryParent(parent);
        parent = childAssociationRef2.getParentRef();
        //Have to declare a final variable in order to access it in the RunAsWork
        final NodeRef ref = parent;
    
        fileName = (String) nodeService.getProperty(parent, ContentModel.PROP_NAME);
    
        AuthenticationUtil.runAsSystem(new AuthenticationUtil.RunAsWork<Object>() {
          public Object doWork() throws Exception {
           permissionService.setPermission(ref, loggedInUser, PermissionService.CONTRIBUTOR, true);
           return "";
          }
    
        });
    }