4

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

mahesh
  • 597
  • 11
  • 23

2 Answers2

5

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()
skuro
  • 13,414
  • 1
  • 48
  • 67
  • Thanks for reply. I am using this from java controller. The above solution is returning "system" as user name. I want the actual user name by which he logs in. – mahesh Jun 22 '12 at 15:58
  • could you please post your web script descriptor XML? Also, are you using anything like `AuthenticationUtils.runAs` and the like? – skuro Jun 22 '12 at 16:00
  • Yes I am using AuthneticationUtils.runs. Here is my descriptor Save Document Save description /alfresco/save argument Active document – mahesh Jun 25 '12 at 07:22
  • skuro, I tried your solution, but no luck. Still getting "System" as user name. I modified my question and added web script code. Am I missing anything ??? – mahesh Jun 25 '12 at 16:41
  • That might be enough to file a but at Alfresco, which version are you useing? Anyway, what about having a `final String userName = AuthenticationUtil. getFullyAuthenticatedUser();` as the first line of the web script body? You can then reuse it within the callback code. – skuro Jun 25 '12 at 17:32
  • I am using alfresco community edition 4. AuthenticationUtil. getFullyAuthenticatedUser() as first line in web script, always returns null. Also, Can we get password for user using person service api ?? – mahesh Jun 26 '12 at 08:11
0

You should put AuthenticationUtil.getFullyAuthenticatedUser() outside the RunAsWork innerClass to retrieve the actual username, because once inside you will get 'system' instead.

So your code will be like this

public void execute(WebScriptRequest req, WebScriptResponse res)
        throws IOException {
    String nodeRefString = null;
    try {
        nodeRefString = req.getParameter("nodeRef");
        final String userName = AuthenticationUtil.getFullyAuthenticatedUser();

        if(nodeRefString != null && !nodeRefString.isEmpty()) {
            AuthenticationUtil.runAs(new RunAsWork<String>() {
                public String doWork() throws Exception {

                    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();
    }
}
alaeddine.nasri
  • 182
  • 1
  • 2
  • 11
  • A tad late for responding but my response is left around for future generations; https://github.com/Alfresco/community-edition/blob/2c1eff9953d3105e738f7b06ba9ba8a079ca4c24/projects/data-model/source/java/org/alfresco/repo/security/authentication/AuthenticationUtil.java#L410 states that it gets the user name of the LAST authenticated user perhaps what you want is: https://github.com/Alfresco/community-edition/blob/2c1eff9953d3105e738f7b06ba9ba8a079ca4c24/projects/data-model/source/java/org/alfresco/repo/security/authentication/AuthenticationUtil.java#L372 – Dark Star1 Dec 13 '16 at 12:36