Is there any way in alfresco to get the current user name inside web script ?? I am calling a web script and want to access current user name and password by which he logged in inside it. Here is my descriptor:
<webscript>
<shortname>Save Document </shortname>
<description>Save description</description>
<url>/alfresco/save</url>
<format default="">argument</format>
<family>Active document</family>
</webscript>
My web script code :
public void execute(WebScriptRequest req, WebScriptResponse res)
throws IOException {
String nodeRefString = null;
try {
nodeRefString = req.getParameter("nodeRef");
if(nodeRefString != null && !nodeRefString.isEmpty()) {
AuthenticationUtil.runAs(new RunAsWork<String>() {
public String doWork() throws Exception {
String userName = AuthenticationUtil.getFullyAuthenticatedUser();
System.out.println("user name =" + userName);
if(personService != null) {
System.out.println("personService initialized successfully");
NodeRef personNode = personService.getPerson("mahesh");
System.out.println("password =" + nodeService.getProperty(personNode, ContentModel.PROP_PASSWORD));
} else {
System.out.println("person service is null");
}
NodeRef nodeRef = new NodeRef(nodeRefString);
setNodeRef(nodeRef);
setFileName((String) nodeService.getProperty(nodeRef,ContentModel.PROP_NAME));
return null;
}
}, AuthenticationUtil.getSystemUserName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
Do I supposed to add Authentication tag into my web script descriptor ?? I tried both AuthenticationUtil.getFullyAuthenticatedUser(); and AuthenticationUtil.getRunAsUser(). Both are returning "system" as user name.
Any help is appreciated.
Thanks
If you're using a Javascript controller or in a Freemarker template:
If you're in a Java controller:
EDIT: As per your comment, you're using the
AuthenticationUtil.runAs
family. In this case you should use the following which ignores any momentarily change to the current authentication context:You should put
AuthenticationUtil.getFullyAuthenticatedUser()
outside theRunAsWork
innerClass to retrieve the actual username, because once inside you will get 'system' instead.So your code will be like this