get current user name inside called webscript

alfresco

Solution

If you're using a Javascript controller or in a Freemarker template:

person.properties["user:username"]

If you're in a Java controller:

/**
 * Get the user that is currently in effect for purposes of authentication.  This includes
 * any overlays introduced by {@link #setRunAsUser(String) runAs}.
 * 
 * @return              Returns the name of the user
 * @throws AuthenticationException
 */
org.alfresco.repo.security.authentication.AuthenticationUtil.getRunAsUser()

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:

/**
 * Get the fully authenticated user. 
 * It returns the name of the user that last authenticated and excludes any overlay authentication set
 * by {@link #runAs(org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork, String) runAs}.
 * 
 * @return              Returns the name of the authenticated user
 * @throws AuthenticationException
 */
org.alfresco.repo.security.authentication.AuthenticationUtil.getFullyAuthenticatedUser()

Problem

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

Original source