cancel
Showing results for 
Search instead for 
Did you mean: 

How to check out a document to a different user

kbrady
Champ in-the-making
Champ in-the-making
I'm trying to write some code that will be run by the 'admin' user that will check out a document to a different user e.g. 'John'. However, when I view the checked out document in Share it always says that the document is checked out to the user 'admin'.
I'm running this code on Alfresco 4.0.c

Here is the code that I am executing

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

From looking at the Alfresco source code, the checkOutcheckInService gets the username to check-out the document to from the AuthenticationService getCurrentUserName() method. However, it appears that the AuthenticationUtil.runAs code does not change the user in the AuthenticationService.

Am I doing something wrong here or how can I do this correctly?

Thanks in advance.
4 REPLIES 4

mitpatoliya
Star Collaborator
Star Collaborator
Yes, there is problem in code.
Whatever the piece of code you write in that block will be executed as the admin user only.
And in that code you are checking out the document by default it is taking the current user as the checkout person.
why you need to write that code under that block.
try it without block you will be fine.

kbrady
Champ in-the-making
Champ in-the-making
Thanks for your reply.

The code that I'm executing will always be run by the user 'admin' so if I try and check out a document it will be checked out to the user 'admin' instead of the actual user who owns the document e.g. 'John'. Also the document will be locked to the user 'admin' instead of 'John'.
I was trying to use the AuthenticationUtil.RunAs method to be able to execute code as another user e.g. 'John'

I'm trying to import documents into Alfresco and have some of the documents automatically checked out to some users. This code needs to iterate over a number of different documents and potentially check out documents to different users. Hence it needs to be able to impersonate a user to check out the document to them. The code itself can not be run by each of the individual users.

I wont have access to the users passwords so I cannot use the AuthenticationService.authenticate() method.

mitpatoliya
Star Collaborator
Star Collaborator
you can use AuthenticationService.runasUser("userid");

kbrady
Champ in-the-making
Champ in-the-making
I have got it working now.
Previously I had failed to call AuthenticationUtil.setFullyAuthenticatedUser(user)

Here is my working code


final String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();
NodeRef checkedOutCopy = null;
AuthenticationUtil.setFullyAuthenticatedUser(String.valueOf(workingCopyOwner));
if (log.isDebugEnabled()) log.debug("set fully authenticated user to '" + String.valueOf(workingCopyOwner) + "'");

final CheckOutCheckInService cociService = serviceRegistry.getCheckOutCheckInService();

try
{
   checkedOutCopy = AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<NodeRef>() {
      public NodeRef doWork() throws Exception {
         return cociService.checkout(nodeRef);
      }
   }, String.valueOf(workingCopyOwner));
   
   ContentWriter writer = fileFolderService.getWriter(checkedOutCopy);
   writer.putContent(workingCopyContentAndMetadata.getContentFile());
   if (log.isDebugEnabled()) log.debug("uploaded working copy content ");
}
finally
{
   if (log.isDebugEnabled()) log.debug("set fully authenticated user back to '" + currentUser + "'");
   AuthenticationUtil.setFullyAuthenticatedUser(currentUser);
}