[英]Run Alfresco Java code as Administrator
我正在嘗試執行一項操作,在該操作中,我將向所有父節點添加權限。 但是,我需要以管理員身份運行以管理權限。 目前,我的代碼如下所示:
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訪問被拒絕。 您沒有適當的權限來執行此操作。
有什么建議么? 謝謝
要詳細說明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
方法。
AuthenticationUtil.runAsSystem(() -> {
// do your work
return null;
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.