运行 Alfresco Java 代码为管理员

Run Alfresco Java code as Administrator

我正在尝试执行一项操作,在该操作中我将向所有父节点添加权限。但是,我需要 运行 as admin 来管理权限。目前我的代码如下所示:

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

我得到的异常非常明显: 严重:04210027 访问被拒绝。您没有执行此操作的适当权限。

有什么建议吗? 谢谢

在露天尝试AuthenticationUtil.runAsSystem方法。

要详细说明 Krutik 的答案,您应该像这样将代码包装在 runAsSystem 块中:

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

    });
}
AuthenticationUtil.runAsSystem(() -> {
            // do your work
            return null;
 });