cancel
Showing results for 
Search instead for 
Did you mean: 

Set Creator & Modifier properties

rdifrango
Champ in-the-making
Champ in-the-making
All, I have a web script as follows:


script:
{
   var currentUser = null;
   var debug = (args.debug != null) ? args.debug : "false";
   var recursive = (args.recursive != null) ? args.recursive : "false";
   var impersonateUser = args.impersonate;
   
   if (impersonateUser != null) {
      currentUser = impersonation.impersonate(impersonateUser);
   }
   
   var fullpath = url.extension;
   var share = null;
   var filename = null;
   var content = null;
   var noderef = null;
   var title = "";
   var description = "";

   // locate file attributes
   for each (field in formdata.fields)
   {
      if (field.name == "share")
      {
         share = field.value;
      }
      else if (field.name == "noderef")
      {
         noderef = field.value;
      }
      else if (field.name == "title")
      {
         title = field.value;
      }
      else if (field.name == "desc")
      {
         description = field.value;
      }
      else if (field.name == "file" && field.isFile)
      {
         filename = field.filename;
         content = field.content;
      }
   }

   // ensure mandatory file attributes have been located
   if (filename == undefined || content == undefined)
   {
      status.code = 400;
      status.message = "Uploaded file cannot be located in request";
      status.redirect = true;
      break script;
   }
   
   // Check via the noderef
   var node = null;
   if (noderef != undefined && noderef != "")
   {
      node = search.findNode(noderef);
   }
   // Check share
   else if (share != undefined && share != "")
   {
      // Get the share site
      var site = siteService.getSite(share);
      if (site == undefined)
      {
         status.code = 400;
         status.message = "Site cannot be located in request";
         status.redirect = true;
         break script;
      }
      // Now get the folder
      if (fullpath != undefined && fullpath != "")
      {
         node = site.getContainer("documentLibrary/" + fullpath);
      }
      else
      {
         node = site.getContainer("documentLibrary");
      }
   }
   else
   {
      node = roothome.childByNamePath("Company Home/" + fullpath);
   }
   
    if (node == undefined)
    {
      status.code = 404;
      status.message = "Path " + fullpath + ".";
      status.redirect = true;
      break script;
    }
   
    if (!node.hasPermission("Read") || !node.hasPermission("Write"))
    {
       status.code = 404;
      status.message = "Access denied to " + fullpath + ".";
      status.redirect = true;
      break script;
    }
   
    var upload = null;
    if (noderef != undefined && noderef != "")
   {
       upload = node;
   }
    else
    {
      upload = node.childByNamePath(filename);   
      if (upload != null)
      {
         if (!upload.hasPermission("Read") || !upload.hasPermission("Write"))
         {
            status.code = 404;
            status.message = "Access denied to " + fullpath + ".";
            status.redirect = true;
            break script;
         }
      }
      else
      {
         if (!node.hasPermission("CreateChildren"))
         {
            status.code = 404;
            status.message = "Access denied to create files under " + fullpath + ".";
            status.redirect = true;
            break script;
         }
         
         upload = node.createFile(filename);
      }
    }
   
   // Set the content
   upload.properties.content.write(content);
   upload.properties.content.guessMimetype(filename);
   upload.properties.content.encoding = "UTF-8";
   upload.properties.title = title;
   upload.properties.description = description;
   upload.save();
   
   // Set the owner to the user who uploaded the file
   if (impersonateUser != null) {
      upload.setOwner(impersonateUser);
   }

   // setup model for response template
   model.upload = upload;
   model.debug = debug;
   model.recursive = recursive;
   
   if (currentUser != null) {
      impersonation.impersonate(currentUser);
   }
}

With this code the owner is set correctly, but the creator and modifier fields are not being set to the username we are impersonating.

Our impersonation code is from another example and looks like:


// String currentUser = AuthenticationUtil.getCurrentUserName();
        String currentUser = AuthenticationUtil.getFullyAuthenticatedUser();

      if (currentUser == null || !currentUser.equals(username)) {
         // AuthenticationUtil.setCurrentUser(username);
            AuthenticationUtil.setRunAsUser(username);
      }

      return currentUser;

The interesting thing is that the permission checks (via hasPermission) are behaving correctly in that we are authenticating for the impersonated user.
1 REPLY 1

rdifrango
Champ in-the-making
Champ in-the-making
I found that if do the following:

   AuthenticationUtil.setRunAsUser(username);

to

   AuthenticationUtil.setFullyAuthenticatedUser(username);