我正在尝试编写一些将由'admin'用户运行的代码,该代码会将文档签出给其他用户,例如'约翰'。但是,当我在Share中查看签出文档时,它总是说文档已签出给用户'admin'。 我在Alfresco 4.0.c上运行此代码
这是我正在执行的代码
Boolean rtn = AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Boolean>() {
@Override
public Boolean doWork() throws Exception {
// executes the following code as user who had the document previously checked out
CheckOutCheckInService checkOutCheckInService = serviceRegistry.getCheckOutCheckInService();
//this line is debug code to check who the current user is according to the AuthenticationService
//AuthenticationService authService = serviceRegistry.getAuthenticationService();
//if (log.isDebugEnabled()) log.debug("Current UserName in AuthenticationService is '" + authService.getCurrentUserName() + "'.");
NodeRef checkedOutCopy = checkOutCheckInService.checkout(nodeRef);
ContentWriter writer = fileFolderService.getWriter(checkedOutCopy);
writer.putContent(workingCopyContentAndMetadata.getContentFile());
if (log.isDebugEnabled()) log.debug("Have uploaded working copy document as user '" + String.valueOf(workingCopyOwner) + "'.");
return true;
}
}, String.valueOf(workingCopyOwner));
从查看Alfresco源代码,checkOutcheckInService获取用于从AuthenticationService getCurrentUserName()方法签出文档的用户名。但是,AuthenticationUtil.runAs代码似乎不会更改AuthenticationService中的用户。
我在这里做错了什么,或者我怎么能正确地做到这一点?
提前致谢。
答案 0 :(得分:2)
您遇到的问题是注册表界面提供的服务不允许您更改系统中的当前用户。为此,您需要引入AuthenticationComponent,保留当前用户,更改当前用户,然后恢复原始用户。
这与建议的here方法相同,允许您在Alfresco中以系统用户身份运行。
举一个简短的例子:
以与引入注册表相同的方式添加身份验证组件,确保公开Spring注入的setter:
private AuthenticationComponent authComponent;
//for Spring injection
public void setAuthenticationComponent(AuthenticationComponent authComponent) {
this.authComponent = authComponent;
}
现在您可以调用该方法来设置当前用户:
authComponent.setCurrentUser(username);
现在,调用AuthenticationService.getCurrentUserName()将正确显示当前用户。不要忘记在重新启动之前需要在* -context.xml中添加对bean的引用。
在您的-context.xml中:
property name="authenticationComponent" ref="AuthenticationComponent"
祝你好运!