4

在露天有什么办法可以在网络脚本中获取当前用户名?我正在调用一个网络脚本,并想访问他在其中登录的当前用户名和密码。这是我的描述符:

<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()。两者都返回“系统”作为用户名。

任何帮助表示赞赏。

谢谢

4

2 回答 2

5

如果您使用的是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()
于 2012-06-22T13:50:27.360 回答
0

您应该放在innerClassAuthenticationUtil.getFullyAuthenticatedUser()之外以检索实际的用户名,因为一旦进入您将获得“系统”。RunAsWork

所以你的代码会是这样的

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();
    }
}
于 2013-05-31T14:06:45.950 回答