get current user name inside called webscript

蹲街弑〆低调 提交于 2019-12-10 10:22:54

问题


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


回答1:


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()



回答2:


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();
    }
}


来源:https://stackoverflow.com/questions/11156671/get-current-user-name-inside-called-webscript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!