Set rights to imported users

Last modified by Anca Luca on 2026/08/05 17:15

cogSet rights to user pages that allow the user to edit their own profile page
Type
Category
Developed by

Anca Luca

Rating
0 Votes
LicenseGNU Lesser General Public License 2.1

Table of contents

Description

Copy the following piece of code in a page in syntax XWiki 2.0 (2.1 might work as well).

It will allow to set rights to all the pages containing an object of type XWiki.XWikiUsers from the current wiki, which was created after a given date. By default the given date is the beginning of the current day (0:00 AM), but it can be changed with a parameter.

It also allows to delete the users created today, if needed.

It can be used together with the Batch Import Application and the Restore All Group snippet to setup an import of users from an excel or CSV file.

{{velocity}}
## get the date, from params or today midnight if none in params
#set($fromDateString = "$!request.fromDate")
#if ($fromDateString == '') 
  #set($fromDateString = "$xwiki.formatDate($xwiki.date, 'yyyy-MM-dd') 00:00")
#end
#set($fromDate = $datetool.toDate('yyyy-MM-dd HH:mm', "$!request.fromDate"))
#if (!$fromDate)
  #set($fromDate = $datetool.toDate('yyyy-MM-dd HH:mm', "$xwiki.formatDate($xwiki.date, 'yyyy-MM-dd') 00:00"))
#end
= Adds rights to themselves to all users created after $xwiki.formatDate($fromDate, 'yyyy-MM-dd HH:mm') =

Use a fromDate parameter to change this date, in the format yyyy-MM-dd HH:mm .

Note that **this script should be executed only once**. If you execute the script and the result is a java.heap.OutOfMemory error, you can consider that the script executed just fine, **don't execute it again**.

#set($usersImportedToday = $xwiki.searchDocuments(", BaseObject obj where doc.fullName = obj.name and obj.className = 'XWiki.XWikiUsers' and doc.creationDate >= '$xwiki.formatDate($fromDate, 'yyyy-MM-dd HH:mm')'"))
#foreach($u in $usersImportedToday)
  #set($uDoc = $xwiki.getDocument($u))
  1. #if ("$!request.dosave" == 'true')Saving #end #if ("$!request.dodelete" == 'true')Deleting#end [[$uDoc>>$uDoc]]
  #if ("$!request.dodelete" == 'true')
    #set($discard = $uDoc.delete())
  #else 
    #set($uDocRights = $uDoc.newObject('XWiki.XWikiRights'))
    #set($discard = $uDocRights.set('levels', 'edit'))
    #set($discard = $uDocRights.set('allow', 1))
    #set($discard = $uDocRights.set('users', $uDoc.fullName))
    $uDocRights
    #if ("$!request.dosave" == 'true')
      #set($discard = $uDoc.save('Updated rights'))
    #end
  #end
#end

{{html}}
<form method="post" action='$doc.getURL('view')'>
  <span class="buttonwrapper">
    <input type="hidden" name="dosave" value="true" />
    <input type="hidden" name="fromDate" value="$fromDateString" />
    <input type="submit" value="Do set rights users" class="button"/>
  </span>
</form>

<form method="post" action='$doc.getURL('view')'>
  <span class="buttonwrapper">
    <input type="hidden" name="dodelete" value="true" />
    <input type="hidden" name="fromDate" value="$fromDateString" />
    <input type="submit" value="Do delete users" class="button"/>
  </span>
</form>
{{/html}}
{{/velocity}}

Get Connected