获取名为webscript的当前用户名

时间:2012-06-22 13:00:19

标签: alfresco

在露天有没有办法在网页脚本中获取当前用户名?我正在调用一个Web脚本,并希望访问其中登录的当前用户名和密码。 这是我的描述符:

<webscript>
  <shortname>Save Document </shortname>
  <description>Save description</description>
  <url>/alfresco/save</url>
  <format default="">argument</format>
  <family>Active document</family>
</webscript>

我的网页脚本代码:

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

我是否应该在我的网页脚本描述符中添加身份验证标记?我尝试了AuthenticationUtil.getFullyAuthenticatedUser();和AuthenticationUtil.getRunAsUser()。两者都返回“system”作为用户名。

感谢任何帮助。

由于

2 个答案:

答案 0 :(得分:4)

如果您使用的是Javascript控制器或Freemarker模板:

person.properties["user:username"]

如果您在Java控制器中:

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

编辑:根据您的评论,您使用的是AuthenticationUtil.runAs家庭。在这种情况下,您应该使用以下内容忽略任何暂时更改为当前身份验证上下文:

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

答案 1 :(得分:0)

您应该将AuthenticationUtil.getFullyAuthenticatedUser()放在RunAsWork innerClass之外以检索实际的用户名,因为一旦进入,您将获得'system'。

所以你的代码就像这样

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