Run Alfresco Java code as Administrator

2019-07-23 03:32发布

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

3条回答
何必那么认真
2楼-- · 2019-07-23 04:03

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 "";
      }

    });
}
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-07-23 04:18
AuthenticationUtil.runAsSystem(() -> {
            // do your work
            return null;
 });
查看更多
放荡不羁爱自由
4楼-- · 2019-07-23 04:25

Try with AuthenticationUtil.runAsSystemmethod in alfresco.

查看更多
登录 后发表回答