以管理员身份运行Alfresco Java代码

以管理员身份运行Alfresco Java代码,java,admin,alfresco,backend,runas,Java,Admin,Alfresco,Backend,Runas,我正在尝试实现一个操作,在该操作中,我将向所有父节点添加权限。但是,我需要以管理员身份运行来管理权限。当前我的代码如下所示: permissionService = serviceRegistry.getPermissionService(); //Read the username of the current user final String loggedInUser = authenticationService.getCurrentUse

我正在尝试实现一个操作,在该操作中,我将向所有父节点添加权限。但是,我需要以管理员身份运行来管理权限。当前我的代码如下所示:

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

    });
}
permissionService=serviceRegistry.getPermissionService();
//读取当前用户的用户名
最终字符串loggedInUser=authenticationService.getCurrentUserName();
ChildAssociationRef ChildAssociationRef=nodeService.getPrimaryParent(actionedUponNodeRef);
//获取父节点ref
NodeRef parent=childAssociationRef.getParentRef();
字符串文件名=(字符串)nodeService.getProperty(父级,ContentModel.PROP_NAME);
//迭代直到到达文档库
而(!fileName.contains(“documentLibrary”)){
ChildAssociationRef childAssociationRef2=nodeService.getPrimaryParent(父级);
parent=childAssociationRef2.getParentRef();
//必须声明最终变量才能在RunAsWork中访问它
最终节点ref=父节点;
fileName=(字符串)nodeService.getProperty(父级,ContentModel.PROP_NAME);
RunAsWork raw=新RunAsWork(){
公共对象doWork()引发异常{
//为登录用户设置此文件夹的权限
permissionService.setPermission(ref,loggedInUser,permissionService.CONTRIBUTOR,true);
返回null;
}   
};
//以管理员身份运行
AuthenticationUtil.runAs(原始,“admin”);
}
我得到的例外非常明显: 严重:04210027访问被拒绝。您没有执行此操作的适当权限。

有什么建议吗?
谢谢

在alfresco尝试使用
AuthenticationUtil.runAsSystem
方法。

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

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

    });
}
final permissionService=serviceRegistry.getPermissionService();
//读取当前用户的用户名
最终字符串loggedInUser=authenticationService.getCurrentUserName();
ChildAssociationRef ChildAssociationRef=nodeService.getPrimaryParent(actionedUponNodeRef);
//获取父节点ref
NodeRef parent=childAssociationRef.getParentRef();
字符串文件名=(字符串)nodeService.getProperty(父级,ContentModel.PROP_NAME);
//迭代直到到达文档库
而(!fileName.contains(“documentLibrary”)){
ChildAssociationRef childAssociationRef2=nodeService.getPrimaryParent(父级);
parent=childAssociationRef2.getParentRef();
//必须声明最终变量才能在RunAsWork中访问它
最终节点ref=父节点;
fileName=(字符串)nodeService.getProperty(父级,ContentModel.PROP_NAME);
AuthenticationUtil.runAsSystem(新的AuthenticationUtil.RunAsWork(){
公共对象doWork()引发异常{
permissionService.setPermission(ref,loggedInUser,permissionService.CONTRIBUTOR,true);
返回“”;
}
});
}