user_goReplace all occurences of the old user with the new user for the selected fields of existing documents inside the current wiki.
TypeXAR
CategoryApplication
Developed by

Eduard Moraru

Active Installs9
Rating
Rate!
0 Votes
LicenseGNU Lesser General Public License 2.1
Installable with the Extension Manager

Description

Replace all occurences of the old user with the new user for the selected fields of existing documents inside the current wiki.

Based on the affected fields, this will also grant the affected documents the rights of the new user, instead of the old one's:

  • contentAuthor: Gives affected document Programming or Script rights, if the new user has them, allowing existing scripts or protected API calls inside that document to be executed when the document is viewed.
  • author: Gives affected document Admin or Programming rights, if the new user has them, allowing special objects (wiki macros, wiki components, UIX, translations) to be registered at a wiki or global level. Also updates the value displayed on the document's "Last modified by" section with the new user.
  • creator: Gives delete right for the document to the new user. Also updates the value displayed on the documents "Created by" section with the new user.

Note: To list the content author users you can use the List Deleted Document Users application.

Requires Programming Rights to function.

Step1:
changeDocumentUser.png

Step2:
changeDocumentUser-confirm.png

Step3:
changeDocumentUser-result.png

Source

Below is the source to copy/paste in a new document in your XWiki instance, in case you can/will not use Extension Manager or direct XAR import to install it.

{{velocity}}
#set ($oldUser = "$!request.oldUser")
#set ($newUser = "$!request.newUser")
#set ($fields = $request.getParameterValues('field'))
#set ($fields = $fields.subList(0, $fields.size()))
##
#if ($oldUser != '' && $newUser != '' && "$!fields" != '')
  ## Build and run the query
  #set ($documentsToUpdateQuery = "SELECT doc.fullName, doc.author, doc.contentAuthor, doc.creator FROM Document doc WHERE ")
  #foreach ($field in $fields)
    #if ($foreach.count > 1)
      #set ($documentsToUpdateQuery = "${documentsToUpdateQuery} OR ")
    #end
    #set ($documentsToUpdateQuery = "${documentsToUpdateQuery} doc.${field}=:oldUser")
  #end
  #set ($documentsToUpdateQuery = "${documentsToUpdateQuery} ORDER BY doc.fullName ASC")
  #set ($documentsToUpdate = $services.query.xwql($documentsToUpdateQuery).bindValue('oldUser', $oldUser).execute())
  ##
  #macro (getMatchedFields $queryResultRow)
    #set ($matchedFields = [])
    #if ($queryResultRow[1] == $oldUser && $fields.contains('author'))
      #set ($discard = $matchedFields.add('author'))
    #end
    #if ($queryResultRow[2] == $oldUser && $fields.contains('contentAuthor'))
      #set ($discard = $matchedFields.add('contentAuthor'))
    #end
    #if ($queryResultRow[3] == $oldUser && $fields.contains('creator'))
      #set ($discard = $matchedFields.add('creator'))
    #end
  #end
  ##
  #set ($newUserReference = $services.model.resolveDocument($newUser))
  ##
  #if ($request.confirm == 'true')
    #foreach ($documentToUpdate in $documentsToUpdate)
      #getMatchedFields($documentToUpdate)
      ##
      #set ($document = $xwiki.getDocument($documentToUpdate[0]))
      #set ($protectedDocument = $document.document)
      ## Update the requested document user fields with the new user reference.
      #if ($fields.contains('author'))
        #set ($discard = $protectedDocument.setAuthorReference($newUserReference))
      #end
      #if ($fields.contains('contentAuthor'))
        #set ($discard = $protectedDocument.setContentAuthorReference($newUserReference))
      #end
      #if ($fields.contains('creator'))
        #set ($discard = $protectedDocument.setCreatorReference($newUserReference))
      #end
      ## Save the changes
      #set ($discard = $xwiki.xWiki.saveDocument($protectedDocument, "Replaced user [$oldUser] in fields $matchedFields to user [$newUser]", $xcontext.context))
      * [[$documentToUpdate[0]]] ($documentToUpdate[0]) fields $matchedFields - (/)
    #end

    Done.

    {{html}}
   <a class='button' href="$doc.getURL()">Back to main screen</a>
    {{/html}}
  #else
    Documents to update ($documentsToUpdate.size() total) from [[doc:$oldUser]] ($oldUser) to [[doc:$newUserReference]] ($newUser) for fields $fields:
    #foreach ($documentToUpdate in $documentsToUpdate)
      #getMatchedFields($documentToUpdate)
      * [[$documentToUpdate[0]]] ($documentToUpdate[0]) fields $matchedFields
    #end

    {{html}}
   <form class='xform'>
     <dl>
       <dt>
         <input type='hidden' name='oldUser' value="$escapetool.xml($oldUser)" />
         <input type='hidden' name='newUser' value="$escapetool.xml($newUser)" />
          #foreach ($field in $fields)
           <input type='hidden' name='field' value="$escapetool.xml($field)" />
          #end
         <input type='hidden' name='confirm' value='true' />
          #if ($documentsToUpdate.size() > 0)
           <input class='button' type='submit' value='Confirm' />
          #end
         <a class='button secondary' href="$doc.getURL()">Cancel</a>
       </dt>
     </dl>
   </form>
    {{/html}}
  #end
#else
  Replace all occurences of the old user with the new user for the selected fields of existing documents inside the current wiki.

  Based on the affected fields, this will also grant the affected documents the rights of the new user, instead of the old one's:
  * **contentAuthor**: Gives affected document Programming or Script rights, if the new user has them, allowing existing scripts or protected API calls inside that document to be executed when the document is viewed.
  * **author**: Gives affected document Admin or Programming rights, if the new user has them, allowing special objects (wiki macros, wiki components, UIX, translations) to be registered at a wiki or global level. Also updates the value displayed on the document's "Last modified by" section with the new user.
  * **creator**: Gives delete right for the document to the new user. Also updates the value displayed on the documents "Created by" section with the new user.

  The next step displays the list of documents that will be modified and will ask for confirmation.

  This page requires Programming Rights to function.

  {{html}}
 <form class='xform'>
   <dl>
     <dt><label>Old user to replace</label></dt>
      ## Can be existing or deleted user, so it makes sense to use a suggest widget.
     <dd><input class='suggestUsers' type='text' name='oldUser' #if ($oldUser != '')value="$escapetool.xml($oldUser)"#end /></dd>

     <dt><label>Fields to replace</label></dt>
     <dd>
       <label><input type='checkbox' name='field' value='author' #if (!$request.askConfirmation || $fields.contains('author'))checked="checked"#end />author</label><br/>
       <label><input type='checkbox' name='field' value='contentAuthor' #if (!$request.askConfirmation || $fields.contains('contentAuthor'))checked="checked"#end />contentAuthor</label><br/>
       <label><input type='checkbox' name='field' value='creator' #if ($fields.contains('creator'))checked="checked"#end />creator</label>
     </dd>

     <dt><label>New user to replace with</label></dt>
     <dd><input class='suggestUsers' type='text' name='newUser' #if ($newUser != '')value="$escapetool.xml($newUser)"#{else}value="$escapetool.xml(${xcontext.user})"#end /></dd>
   </dl>
   <div>
     <input type='submit' name='askConfirmation' class='button' value='Change...' />
   </div>
 </form>
  {{/html}}
#end
{{/velocity}}

Prerequisites & Installation Instructions

We recommend using the Extension Manager to install this extension (Make sure that the text "Installable with the Extension Manager" is displayed at the top right location on this page to know if this extension can be installed with the Extension Manager). Note that installing Extensions when being offline is currently not supported and you'd need to use some complex manual method.

You can also use the following manual method, which is useful if this extension cannot be installed with the Extension Manager or if you're using an old version of XWiki that doesn't have the Extension Manager:

  1. Log in the wiki with a user having Administration rights
  2. Go to the Administration page and select the Import category
  3. Follow the on-screen instructions to upload the downloaded XAR
  4. Click on the uploaded XAR and follow the instructions
  5. You'll also need to install all dependent Extensions that are not already installed in your wiki

Release Notes

v2.0

  • Major refactoring
    • Added fields selector and updated the query
    • Updated the confirmation page to show the fields matched on each document that will be changed
    • Renamed page

v1.2

  • Fixed yet another bug when saving the document for which the contentAuthor field was changed.

v1.1

  • Fixed a bug in the published version when the document was saved.
Tags:
Created by Eduard Moraru on 2015/10/12 16:21
    

Get Connected