获取被称为网页脚本的当前用户名 [英] get current user name inside called webscript

查看:108
本文介绍了获取被称为网页脚本的当前用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在户外是否有任何方法可以在Web脚本中获取当前用户名?我正在调用Web脚本,并希望访问他在其中登录时所使用的当前用户名和密码。
这是我的描述符:

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>

我的网络脚本代码:

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

我是否应该在Web脚本中添加身份验证标签描述符??我都尝试了AuthenticationUtil.getFullyAuthenticatedUser();和AuthenticationUtil.getRunAsUser()。两者都返回系统作为用户名。

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.

感谢您的帮助。

谢谢

推荐答案

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

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

person.properties["user:username"]

如果您在Java控制器中:

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






编辑:根据您的评论,您重新使用 AuthenticationUtil.runAs 系列。在这种情况下,您应该使用以下命令忽略当前身份验证上下文的任何暂时更改:


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

这篇关于获取被称为网页脚本的当前用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆