08-06-2019 06:10 AM
There is any way to set admin user from java and through this i can access node and all child node .
in my case if i have login with any user and i pass nodeId to fetch all child node of node. so i am thinking i will set admin user in java code and using admin i can fetch all child node for any user .Is it possible?
08-06-2019 08:01 AM
In your custom logic you can use - AuthenticationUtil.runAsUser(..)
08-06-2019 09:02 AM
Instead of setting user as admin, better to use system user which doesn't affect security context.
Here is a sample example for what you want to get:
final List<ChildAssociationRef> childNodes = nodeService.getChildAssocs(parentNodeRef);
for (final ChildAssociationRef eachChildAssocRef : childNodes) {
final NodeRef childRef = eachChildAssocRef.getChildRef();
//TODO:: DO SOMETHING...
}
/**
* Gets the child nodes.
*
* @param parentNodeRef the parent node ref
* @return the child nodes
*/
private List<ChildAssociationRef> getChildNodes(final NodeRef parentNodeRef) {
final List<ChildAssociationRef> childAssocs = AuthenticationUtil.runAs(
new AuthenticationUtil.RunAsWork<List<ChildAssociationRef>>() {
public List<ChildAssociationRef> doWork() throws Exception {
return nodeService.getChildAssocs(parentNodeRef);
}
}, AuthenticationUtil.getSystemUserName());
return childAssocs;
}
Or simply run your whole operation by setting the authentication as system user based on your use case if you want to do many such operations like getting child-nodes...
Here is a sample example:
AuthenticationUtil.setRunAsUserSystem();
final List<ChildAssociationRef> childNodes = nodeService.getChildAssocs(parentNodeRef);
for (final ChildAssociationRef eachChildAssocRef : childNodes) {
final NodeRef childRef = eachChildAssocRef.getChildRef();
//TODO:: DO SOMETHING...
final Map<QName, Serializable> properties = nodeService.getProperties(childRef);
//TODO:: DO SOMETHING...
}
// More operations which bypasses current user permissions and run as system user
//TODO:: DO SOMETHING...
Explore our Alfresco products with the links below. Use labels to filter content by product module.