以管理员身份运行Alfresco Java代码 [英] Run Alfresco Java code as Administrator

查看:97
本文介绍了以管理员身份运行Alfresco Java代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

要详细了解Krutik的答案,您应该将代码包装在runAsSystem块中,如下所示:

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

    });
}

这篇关于以管理员身份运行Alfresco Java代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆