Note, that this recipe is not updated long time and could be outdated!
Got it.

XWiki registration

If any user is allowed to register in XWiki, it can be (and is) abused by spambots. This recipe shows, how to avoid it with customized XWiki.Registration class.

  1. Open XWiki.Registration class in edit mode, e.g. http://localhost:8080/xwiki/bin/edit/XWiki/Registration
  2. Uncomment sample code for sample code, which asks for favorite color (and accepts green as valid answer): 
...
  ## Uncomment this code to see an example of how you can easily add a field to the registration page
  ## NOTE: In order to save the favorite color in the "doAfterRegistration" hook, this page must be
  ## saved by an administrator and can not self sandboxing.
  #set($sandbox = false)
  #set($field =
    {'name' : 'favorite_color',
      'label' : 'What is your favorite color',
      'params' : {
        'type' : 'text',
        'size' : '60'
      },
      'validate' : {
        'mandatory' : {
          'failureMessage' : $services.localization.render('core.validation.required.message')
        },
        'regex' : {
          'pattern' : '/^green$/',
          'failureMessage' : 'You are not cool enough to register here.'
        },
        'fieldOkayMessage' : 'You are awesome.'
      },
      'doAfterRegistration' : '#saveFavoriteColor()'
    })
  #set($discard = $fields.add($field))
  ## Save the user's favorite color on their user page.
  #macro(saveFavoriteColor)
    #set($xwikiname = $request.get('xwikiname'))
    #set($userDoc = $xwiki.getDocument("$userSpace$xwikiname"))
    $userDoc.setContent("$userDoc.getContent() ${xwikiname}'s favorite color is $request.get('favorite_color')!")
    ## The user (who is not yet logged in) can't save documents so saveWithProgrammingRights
    ## will save the document as long as the user who last saved this registration page has programming rights.
    $userDoc.saveWithProgrammingRights("Saved favorite color from registration form.")
  #end
...
  1. If necessary, customize this code according to your needs and preferences.
  2. If multi lingual messages are needed, look at Writing Internationalized XWiki Applications for more info.

  
Tags XWiki English
Created by Valdis Vītoliņš on 2013-11-09 13:24
Last modified by Valdis Vītoliņš on 2021-04-13 14:30
 
Xwiki Powered
Creative Commons Attribution 3.0 Unported License