Wiki source code of Registration

Version 2.2 by John Stroy on 2012/05/27 09:58

Show last authors
1 {{velocity}}
2 ## These are defined in other places around XWiki, changing them here will result in undefined behavior.
3 #set($redirectParam = 'xredirect')
4 #set($userSpace = 'XWiki.')
5 #set($loginPage = 'XWiki.XWikiLogin')
6 #set($loginAction = 'loginsubmit')
7 ##
8 #set($documentName = 'XWiki.Registration')
9 ##
10 ## Security measure:
11 ## If this document is changed such that it must have programming permission in order to run, change this to false.
12 #set($sandbox = true)
13 ##
14 ## Load the configuration from a seperate document.
15 #loadConfig('XWiki.RegistrationConfig')
16 ##
17 ## Defines what server generated error messages should look like
18 ## The error message when a field is entered incorrectly
19 #set($failureMessageParams = { 'class' : 'LV_validation_message LV_invalid'})
20 ## 'LV_validation_message LV_invalid' depends on this:
21 $xwiki.get('ssfx').use('uicomponents/widgets/validation/livevalidation.css')
22 ##
23 ## The * next to the fields to denote they are mandatory.
24 #set($fieldMandatoryStar = { 'class' : 'xRequired'})
25 ##
26 #*
27 * You may include this document in other documents using {{include document="XWiki.Registration"/}}
28 * To specify that the user is invited and should be allowed to register even if Guest does not have permission to
29 * register, set $invited to true. NOTE: The including script must have programming permission to do this.
30 *
31 * To specify some code which should run after registration is successfully completed, set
32 * $doAfterRegistration to a define block of velocity code like so:
33 * #define($doAfterRegistration)
34 * some code
35 * #end
36 * Output from running this code will not be printed.
37 *
38 * The fields which will be seen on the registration page are defined here.
39 * $fields is an array and each field is a Map. The names shown below are Map keys.
40 *
41 * Each field must have:
42 * name - this is the name of the field, it will be the value for "name" and "id"
43 *
44 * Each field may have:
45 * label - this String will be written above the field.
46 *
47 * tag - the HTML tag which will be created, default is <input>, may also be a non form tag such as <img>
48 *
49 * params - a Map, each key value pair will be in the html tag. eg: {"size" : "30"} becomes <input size=30...
50 *
51 * validate a Map describing how to validate the field, validation is done in javascript then redone in velocity
52 * | for security and because not everyone has javascript.
53 * |
54 * +-mandatory (Optional) - Will fail if the field is not filled in.
55 * | +-failureMessage (Required) - The message to display if the field is not filled in.
56 * | +-noscript (Optional) - will not be checked by javascript
57 * |
58 * +-regex (Optional) - Will validate the field using a regular expression.
59 * | | because of character escaping, you must provide a different expression for the
60 * | | javascript validation and the server side validation. Both javascript and server side
61 * | | validation are optional, but if you provide neither, then your field will not be validated.
62 * | |
63 * | +-failureMessage (Optional) - The message to display if the regex evaluation returns false.
64 * | +-jsFailureMessage (Optional) - The message for Javascript to display if regex fails.
65 * | | If jsFailureMessage is not defined Javascript uses failureMessage.
66 * | | NOTE: Javascript injects the failure message using createTextNode so &lt; will
67 * | | be displayed as &lt;
68 * | |
69 * | +-pattern (Optional) - The regular expression to test the input at the server side, it's important to use
70 * | | this if you need to validate the field for security reasons, also it is good because not
71 * | | all browsers use javascript or have it enabled.
72 * | |
73 * | +-jsPattern (Optional) - The regular expression to use for client side, you can use escaped characters to avoid
74 * | | them being parsed as HTML or javascript. To get javascript to unescape characters use:
75 * | | {"jsPattern" : "'+unescape('%5E%5B%24')+'"}
76 * | | NOTE: If no jsPattern is specified, the jsValidator will try to validate
77 * | | using the server pattern.
78 * | |
79 * | +-noscript (Optional) - will not be checked by javascript
80 * |
81 * +-mustMatch (Optional) - Will fail if the entry into the field is not the same as the entry in another field.
82 * | | Good for password confirmation.
83 * | |
84 * | +-failureMessage (Required) - The message to display if the field doesn't match the named field.
85 * | +-name (Required) - The name of the field which this field must match.
86 * | +-noscript (Optional) - will not be checked by javascript
87 * |
88 * +-programmaticValidation (Optional) - This form of validation executes a piece of code which you give it and
89 * | | if the code returns the word "failed" then it gives the error message.
90 * | | Remember to put the code in singel quotes ('') because you want the value
91 * | | of 'code' to equal the literal code, not the output from running it.
92 * | |
93 * | +-code (Required) - The code which will be executed to test whether the field is filled in correctly.
94 * | +-failureMessage (Required) - The message which will be displayed if evaluating the code returns "false"
95 * |
96 * +-fieldOkayMessage (Optional) - The message which is displayed by LiveValidation when a field is validated as okay.
97 * If not specified, will be $defaultFieldOkayMessage
98 *
99 * noReturn - If this is specified, the field will not be filled in if there is an error and the user has to fix their
100 * registration information. If you don't want a password to be passed back in html then set this true
101 * for the password fields. Used for the captcha because it makes no sense to pass back a captcha answer.
102 *
103 * doAfterRegistration - Some Velocity code which will be executed after a successfull registration.
104 * This is used in the favorite color example.
105 * Remember to put the code in singel quotes ('') because you want the 'code' entry to equal the literal
106 * code, not the output from running it.
107 *
108 * Each field may not have: (reserved names)
109 * error - This is used to pass back any error message from the server side code.
110 *
111 * NOTE: This template uses a registration method which requires:
112 * * register_first_name
113 * * register_last_name
114 * * xwikiname
115 * * register_password
116 * * register2_password
117 * * register_email
118 * * template
119 * Removing or renaming any of these fields will result in undefined behavior.
120 *
121 *###
122 #set($fields = [])
123 ##
124 ## The first name field, no checking.
125 #set($field =
126 {'name' : 'register_first_name',
127 'label' : $msg.get('core.register.firstName'),
128 'params' : {
129 'type' : 'text',
130 'size' : '60'
131 }
132 })
133 #set($discard = $fields.add($field))
134 ##
135 ## The last name field, no checking.
136 #set($field =
137 {'name' : 'register_last_name',
138 'label' : $msg.get('core.register.lastName'),
139 'params' : {
140 'type' : 'text',
141 'size' : '60'
142 }
143 })
144 #set($discard = $fields.add($field))
145 ##
146 ## The user name field, mandatory and programmatically checked to make sure the username doesn't exist.
147 #set($field =
148 {'name' : 'xwikiname',
149 'label' : $msg.get('core.register.username'),
150 'params' : {
151 'type' : 'text',
152 'onfocus' : 'prepareName(document.forms.register);',
153 'size' : '60'
154 },
155 'validate' : {
156 'mandatory' : {
157 'failureMessage' : $msg.get('core.validation.required.message')
158 },
159 'programmaticValidation' : {
160 'code' : '#nameAvailable($request.get("xwikiname"))',
161 'failureMessage' : $msg.get('core.register.userAlreadyExists')
162 }
163 }
164 })
165 #set($discard = $fields.add($field))
166 ## Make sure the chosen user name is not already taken
167 ## This macro is called by programmaticValidation for xwikiname (above)
168 #macro(nameAvailable, $name)
169 #if($xwiki.exists("$userSpace$name"))
170 failed
171 #end
172 #end
173 ##
174 ##The password field, mandatory and must be at least 6 characters long.
175 #set($field =
176 {'name' : 'register_password',
177 'label' : $msg.get('core.register.password'),
178 'params' : {
179 'type' : 'password',
180 'size' : '60'
181 },
182 'validate' : {
183 'mandatory' : {
184 'failureMessage' : $msg.get('core.validation.required.message')
185 },
186 'regex' : {
187 'pattern' : '/.{6,}/',
188 'failureMessage' : $msg.get('xe.admin.registration.passwordTooShort')
189 }
190 }
191 })
192 #set($discard = $fields.add($field))
193 ##
194 ##The confirm password field, mandatory, must match password field, and must also be 6+ characters long.
195 #set($field =
196 {'name' : 'register2_password',
197 'label' : $msg.get('core.register.passwordRepeat'),
198 'params' : {
199 'type' : 'password',
200 'size' : '60'
201 },
202 'validate' : {
203 'mandatory' : {
204 'failureMessage' : $msg.get('core.validation.required.message')
205 },
206 'mustMatch' : {
207 'name' : 'register_password',
208 'failureMessage' : $msg.get('xe.admin.registration.passwordMismatch')
209 },
210 'regex' : {
211 'pattern' : '/.{6,}/',
212 'failureMessage' : $msg.get('xe.admin.registration.passwordTooShort')
213 }
214 }
215 })
216 #set($discard = $fields.add($field))
217 ##
218 ## The email address field, regex checked with an email pattern.
219 #set($field =
220 {'name' : 'register_email',
221 'label' : $msg.get('core.register.email'),
222 'params' : {
223 'type' : 'text',
224 'size' : '60'
225 },
226 'validate' : {
227 'regex' : {
228 'pattern' : '/^([^@\s]+)@((?:[-a-zA-Z0-9]+\.)+[a-zA-Z]{2,})$/',
229 'failureMessage' : $msg.get('xe.admin.registration.invalidEmail')
230 }
231 }
232 })
233 #set($discard = $fields.add($field))
234 ##
235 #* ## Uncomment this code to see an example of how you can easily add a field to the registration page
236 ## Note: The user's favorite color is not saved anywhere, see above for information on how to save it.
237 #set($field =
238 {'name' : 'favorite_color',
239 'label' : 'What is your favorite color',
240 'params' : {
241 'type' : 'text',
242 'size' : '60'
243 },
244 'validate' : {
245 'mandatory' : {
246 'failureMessage' : $msg.get('core.validation.required.message')
247 },
248 'regex' : {
249 'pattern' : '/^green$/i',
250 'failureMessage' : 'You are not cool enough to register here.'
251 },
252 'fieldOkayMessage' : 'You are awesome.'
253 },
254 'doAfterRegistration' : '#saveFavoriteColor()'
255 })
256 #set($discard = $fields.add($field))
257 ## Save the user's favorite color on their user page.
258 #macro(saveFavoriteColor)
259 #set($xwikiname = $request.get('xwikiname'))
260 #set($userDoc = $xwiki.getDocument("$userSpace$xwikiname"))
261 $userDoc.setContent("$userDoc.getContent() ${xwikiname}'s favorite color is $request.get('favorite_color')!")
262 ## The user (who is not yet logged in) can't save documents so saveWithProgrammingRights
263 ## will save the document as long as the user who last saved this registration page has programming rights.
264 $userDoc.saveWithProgrammingRights("Saved favorite color from registration form.")
265 #end
266 ## *###
267 note##
268 ## To disable the captcha on this page, comment out the next two entries.
269 ## The captcha image, not an input field but still defined the same way.
270 #if($captchaservice
271 && !$invited
272 && $xcontext.getUser() == "XWiki.XWikiGuest"
273 && $requireCaptcha)
274 ## Empty label field used for padding.
275 ## Empty 'name' field overriddes name="captcha_image" with "" so name is not specified at all.
276 #set($field =
277 {'name' : 'captcha_image',
278 'label' : "<span class='hidden'>$msg.get('core.captcha.image.label')</span>",
279 'tag' : 'img',
280 'params' : {
281 'src' : $doc.getURL('imagecaptcha'),
282 'alt' : $msg.get('core.captcha.image.alternateText', [$msg.get('core.register.submit')]),
283 'name' : ''
284 }
285 })
286 #set($discard = $fields.add($field))
287 ## The captcha field, mandatory, programmatically checked to make sure the captcha is right
288 ## Not checked by javascript because javascript can't check the captcha and the Ok message because it passes the
289 ## mandatory test is misleading.
290 ## and not filled back in if there is an error ('noReturn')
291 #set($field =
292 {'name' : 'captcha_answer',
293 'label' : $msg.get('core.captcha.image.instruction'),
294 'params' : {
295 'type' : 'text',
296 'size' : '60'
297 },
298 'validate' : {
299 'mandatory' : {
300 'failureMessage' : $msg.get('core.captcha.captchaAnswerIsWrong'),
301 'noscript' : true
302 },
303 'programmaticValidation' : {
304 'code' : '#checkCaptcha($request, $request.get("captcha_answer"))',
305 'failureMessage' : $msg.get('core.captcha.captchaAnswerIsWrong')
306 }
307 },
308 'noReturn' : true
309 })
310 #set($discard = $fields.add($field))
311 #end
312 ##
313 ## Checks the captcha answer; used by programmaticValidation above.
314 #macro(checkCaptcha, $request, $answer)
315 #set($cv = $captchaservice.getCaptchaVerifier('image'))
316 #if(!$cv.isAnswerCorrect($cv.getUserId($request), $answer))
317 failed
318 #end
319 #end
320 ##
321 ## Pass the name of the template to $xwiki.createUser so any contained information will be passed in.
322 #set($field =
323 {'name' : 'template',
324 'params' : {
325 'type' : 'hidden',
326 'value' : 'XWiki.XWikiUserTemplate'
327 }
328 })
329 #set($discard = $fields.add($field))
330 ##
331 ## Pass the redirect parameter on so that the login page may redirect to the right place.
332 ## Not necessary in Firefox 3.0.10 or Opera 9.64, I don't know about IE or Safari.
333 #set($field =
334 {'name' : $redirectParam,
335 'params' : {
336 'type' : 'hidden'
337 }
338 })
339 #set($discard = $fields.add($field))
340 ##
341 #######################################################################
342 ## The Code.
343 #######################################################################
344 ##
345 #if($useLiveValidation)
346 $xwiki.get('jsfx').use('uicomponents/widgets/validation/livevalidation_prototype.js')
347 $xwiki.get('ssfx').use('uicomponents/widgets/validation/livevalidation.css')
348 #end
349 ## This application's HTML is dynamically generated and editing in WYSIWYG would not work
350 #if($xcontext.getAction() == 'edit')
351 $response.sendRedirect("$xwiki.getURL($doc.getFullName(), 'edit')?editor=wiki")
352 #end
353 ##
354 ## If this document has PR and is not included from another document then it's author should be set to Guest
355 ## for the duration of it's execution in order to improve security.
356 ## Note we compare document ids because
357 #if($sandbox
358 && $xcontext.hasProgrammingRights()
359 && $xcontext.getDoc().getDocumentReference().equals($xwiki.getDocument($documentName).getDocumentReference()))
360 ##
361 $xcontext.dropPermissions()##
362 #end
363 ##
364 ## Access level to register must be explicitly checked because it is only checked in XWiki.prepareDocuments
365 ## and this page is accessible through view action.
366 #if(!$xcontext.hasAccessLevel('register', 'XWiki.XWikiPreferences'))
367 ## Make an exception if another document with programming permission (Invitation app) has included this
368 ## document and set $invited to true.
369 #if(!$invited || !$xcontext.hasProgrammingRights())
370 $response.sendRedirect("$xwiki.getURL($doc.getFullName(), 'login')")
371 #end
372 #end
373 ## If this is true, then assume the registration page is being viewed inside of a lightbox
374 #if($request.get('xpage'))
375 #set($assumeLightbox = true)
376 #end
377 ##
378 ## Display the heading
379 $heading
380 ## If the submit button has been pressed, then we test the input and maybe create the user.
381 #if($request.getParameter('xwikiname'))
382 ## Do server side validation of input fields.
383 #set($discard = "#validateFields($fields, $request)")
384 ## If server side validation was successfull, create the user
385 #if(!$registrationFailed)
386 #createUser($fields, $request, $response, $doAfterRegistration)
387 #end
388 #end
389 ## If the registration was not successful or if the user hasn't submitted the info yet
390 ## Then we display the registration form.
391 #if(!$registrationDone)
392 $welcomeMessage
393
394 {{html clean=false wiki=false}}
395 <form id="register" action="" method="post" class="xform half">
396 <div>
397 <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}" />
398 #generateHtml($fields, $fieldMandatoryStar, $failureMessageParams)
399 <div class="wikimodel-emptyline"></div>
400 <span class="buttonwrapper">
401 #if($assumeLightbox)
402 ## LightBox detected...
403 <script type="text/javascript">
404 ## Make the X button not reload the page. (overriding LbClose)
405 window.lb.lbClose = function() {
406 this.lbHide();
407 this.lbClearData();
408 ##return false;
409 }
410 ## Post the form entry to the page and load the result. (we override lbSaveForm)
411 window.lb.lbSaveForm = function() {
412 var formParams = Form.serialize(this.form);
413 Form.disable(this.form);
414 var ajaxRequest = new Ajax.Request(this.saveUrl, {
415 parameters: formParams,
416 asynchronous: false
417 });
418 window.lb.lbFormDataLoaded(ajaxRequest.transport);
419 }
420 </script>
421 ## It doesn't really matter where these are, the scripts will be relocated to the head.
422 <!-- com.xpn.xwiki.plugin.skinx.CssSkinFileExtensionPlugin -->
423 <!-- com.xpn.xwiki.plugin.skinx.JsSkinFileExtensionPlugin -->
424 ##
425 <input class="button" type="submit" value="$msg.get('save')" onclick="window.lb.lbSaveForm();"/>
426 </span>#* End ButtonWrapper then start another...*#<span class="buttonwrapper">
427 <input class="button secondary" type="submit" value="$msg.get("cancel")" onclick="Form.disable(window.lb.form); window.lb.lbClose();"/>
428 #else
429 ## Not using the LightBox
430 <input type="submit" value="$msg.get('core.register.submit')" class="button"/>
431 #end
432 </span>## ButtonWrapper
433 </div>
434 </form>
435 #if($useLiveValidation)
436 #generateJavascript($fields)
437 #end
438 {{/html}}
439
440 ##
441 ## Allow permitted users to configure this application.
442 #if($xcontext.getUser() != 'XWiki.XWikiGuest' && $xcontext.hasAccessLevel("edit", $documentName))
443 [[$msg.get('xe.admin.registration.youCanConfigureRegistrationHere')>>XWiki.XWikiPreferences?section=Registration&editor=globaladmin#HCustomizeXWikiRegistration]]
444 {{html}}<a href="$xwiki.getURL($documentName, 'edit', 'editor=wiki')">$msg.get('xe.admin.registration.youCanConfigureRegistrationFieldsHere')</a>{{/html}}
445 #end
446 ## If the registration is done (successful) and we detect the Lightbox simply send the user back to the original page.
447 #elseif($assumeLightbox)
448 {{html clean=false wiki=false}}
449 <script type="text/javascript">
450 var url = window.lb.redirectUrl;
451 window.lb.lbClose;
452 if (url != undefined) {
453 if(window.location.pathname + window.location.search == url) {
454 ## Under certain circumstances (bug) Opera will not load a page if the location is the same as the current page.
455 ## In these cases, location.reload() doesn't work either, the only solution (I could find) was to change the URL.
456 window.location.href = url + "&";
457 } else {
458 window.location.href = url;
459 }
460 }
461 </script>
462 {{/html}}
463 #end
464 ##
465 ####### The Macros (nothing below this point is run directly) #########
466 #*
467 * Server side validation, this is necessary for security and because not everyone has Javascript
468 *
469 * @param $fields The array of fields to validate.
470 * @param $request An XWikiRequest object which made the register request, used to get parameters.
471 *###
472 #macro(validateFields, $fields, $request)
473 #foreach($field in $fields)
474 #if($field.get('validate') && $field.get('name'))
475 #set($fieldName = $field.get('name'))
476 #set($validate = $field.get('validate'))
477 #set($error = '')
478 #set($value = $request.get($fieldName))
479 #if($value && $value != '')
480 ##
481 ## mustMatch validation
482 #if($error == '' && $validate.get('mustMatch'))
483 #set($mustMatch = $validate.get('mustMatch'))
484 #if($mustMatch.get('name') && $mustMatch.get('failureMessage'))
485 #if($request.get($fieldName) != $request.get($mustMatch.get('name')))
486 #set($error = $mustMatch.get('failureMessage'))
487 #end
488 #else
489 ERROR: In field: ${fieldName}: mustMatch validation required both name
490 (of field which this field must match) and failureMessage.
491 #end
492 #end
493 ##
494 ## Regex validation
495 ## We won't bother with regex validation if there is no entry, that would defeat the purpose of 'mandatory'
496 #if($error == '' && $validate.get('regex') && $value && $value != '')
497 #set($regex = $validate.get('regex'))
498 #if($regex.get('pattern') && $regex.get('failureMessage'))
499 ## Make Java regexes more compatible with Perl/js style regexes by removing leading and trailing /
500 #if($regex.get('pattern').length() > 1)
501 #set($pattern = $regex.get('pattern').substring(1, $mathtool.add($regex.get('pattern').length(), -1)))
502 #else
503 ## I don't expect this but want to maintain compatibility.
504 #set($pattern = $regex.get('pattern'))
505 #end
506 #if($regextool.find($value, $pattern).isEmpty())
507 #set($error = $regex.get('failureMessage'))
508 #end
509 #elseif($regex.get('pattern'))
510 ERROR: In field: ${fieldName}: regex validation must include failureMessage.
511 #end
512 #end
513 ##
514 ## If regex and mustMatch validation passed, try programmatic validation
515 #if($error == '' && $validate.get('programmaticValidation'))
516 #set($pv = $validate.get('programmaticValidation'))
517 #if($pv.get('code') && $pv.get('failureMessage'))
518 #set($pvReturn = "#evaluate($pv.get('code'))")
519 #if($pvReturn.indexOf('failed') != -1)
520 #set($error = $pv.get('failureMessage'))
521 #end
522 #else
523 ERROR: In field: ${fieldName}: programmaticValidation requires code and failureMessage
524 #end
525 #end
526 #else
527 ##
528 ## If no content, check if content is mandatory
529 #if($validate.get('mandatory'))
530 #set($mandatory = $validate.get('mandatory'))
531 #if($mandatory.get('failureMessage'))
532 #set($error = $mandatory.get('failureMessage'))
533 #else
534 ERROR: In field: ${fieldName}: mandatory validation requires a failureMessage
535 #end
536 #end
537 #end
538 #if($error != '')
539 #set($discard = $field.put('error', $error))
540 #set($registrationFailed = true)
541 #end
542 #elseif(!$field.get('name'))
543 ERROR: Field with no name.
544 #end##if(validate)
545 #end##loop
546 #end##macro
547 #*
548 * Create the user.
549 * Calls $xwiki.createUser to create a new user.
550 *
551 * @param $request An XWikiRequest object which made the register request.
552 * @param $response The XWikiResponse object to send any redirects to.
553 * @param $doAfterRegistration code block to run after registration completes successfully.
554 *###
555 #macro(createUser, $fields, $request, $response, $doAfterRegistration)
556 ## CSRF check
557 #if(${services.csrf.isTokenValid("$!{request.getParameter('form_token')}")})
558 ## See if email verification is required and register the user.
559 #if($xwiki.getXWikiPreferenceAsInt('use_email_verification', 0) == 1)
560 #set($reg = $xwiki.createUser(true))
561 #else
562 #set($reg = $xwiki.createUser(false))
563 #end
564 #else
565 $response.sendRedirect("$!{services.csrf.getResubmissionURL()}")
566 #end
567 ##
568 ## Handle output from the registration.
569 #if($reg && $reg <= 0)
570 {{error}}
571 #if($reg == -2)
572 $msg.get('core.register.passwordMismatch')
573 ## -3 means username taken, -8 means username is superadmin name
574 #elseif($reg == -3 || $reg == -8)
575 $msg.get('core.register.userAlreadyExists')
576 #elseif($reg == -4)
577 $msg.get('core.register.invalidUsername')
578 #else
579 $msg.get('core.register.registerFailed', [$reg])
580 #end
581 {{/error}}
582 #elseif($reg)
583 ## Registration was successful
584 #set($registrationDone = true)
585 ##
586 ## If there is any thing to "doAfterRegistration" then do it.
587 #foreach($field in $fields)
588 #if($field.get('doAfterRegistration'))
589 #evaluate($field.get('doAfterRegistration'))
590 #end
591 #end
592 ## If there is a "global" doAfterRegistration, do that as well.
593 #if($doAfterRegistration)
594 #set($discard = $doAfterRegistration.toString())
595 #end
596 ## Define some strings which may be used by autoLogin or loginButton
597 #set($userName = $!request.get('xwikiname'))
598 #set($password = $!request.get('register_password'))
599 #set($loginURL = $xwiki.getURL($loginPage, $loginAction))
600 #if("$!request.getParameter($redirectParam)" != '')
601 #set($redirect = $request.getParameter($redirectParam))
602 #else
603 #set($redirect = $defaultRedirect)
604 #end
605 ## Display a "registration successful" message
606
607 #evaluate($registrationSuccessMessage)
608
609 ## Empty line prevents message from being forced into a <p> block.
610
611 ## Give the user a login button which posts their username and password to loginsubmit
612 #if($loginButton)
613
614 {{html clean=false wiki=false}}
615 <form id="loginForm" action="$loginURL" method="post">
616 <div>
617 <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}" />
618 <input id="j_username" name="j_username" type="hidden" value="$escapetool.xml($!userName)" />
619 <input id="j_password" name="j_password" type="hidden" value="$escapetool.xml($!password)" />
620 <input id="$redirectParam" name="$redirectParam" type="hidden" value="$escapetool.xml($redirect)" />
621 <span class="buttonwrapper" style="margin-left:47%;">
622 <input type="submit" value="$msg.get('login')" class="button"/>
623 </span>
624 </div>
625 </form>
626 ## We don't want autoLogin if we are administrators adding users...
627 #if($autoLogin && !$assumeLightbox)
628 <script type='text/javascript'>
629 document.observe('dom:loaded', function() {
630 document.forms['loginForm'].submit();
631 });
632 </script>
633 #end
634 {{/html}}
635
636 #end
637 #end
638 ##
639 #end## createUser Macro
640 #*
641 * Generate HTML form, this is the only place where HTML is written.
642 *
643 * @param $fields The array of fields to use for generating html code.
644 * @param $fieldMandatoryStar The tag parameters for a * indicating a mandatory field.
645 * @param $failureMessageParams The tag parameters for a failure message.
646 *###
647 #macro(generateHtml, $fields, $fieldMandatoryStar, $failureMessageParams)
648 ## Put the same values back into the fields.
649 #getParams($fields)
650 ##
651 <dl>
652 #foreach($field in $fields)
653 #if($field.get('name'))
654 #set($fieldName = $field.get('name'))
655 #if($field.get('label'))
656 #set($label = $field.get('label'))
657 <dt><label for="$fieldName">$label
658 #if($field.get('validate').get('mandatory'))
659 <span ##
660 #foreach($entry in $fieldMandatoryStar.entrySet())
661 $entry.getKey()="$entry.getValue()" ##
662 #end
663 >$msg.get('core.validation.required')</span>
664 #end
665 </label>
666 </dt>
667 #end
668 ## If no tag then default tag is <input>
669 #if($field.get('tag'))
670 #set($tag = $field.get('tag'))
671 #else
672 #set($tag = 'input')
673 #end
674 <dd><$tag id="$fieldName" ##
675 #set($params = $field.get('params'))
676 ## If no name parameter is spacified, then we use the field name
677 #if(!$params.get('name'))
678 #set($discard = $params.put('name', $fieldName))
679 #end
680 #foreach($entry in $params.entrySet())
681 ## If a parameter is specified as '' then we don't include it.
682 #if($entry.getValue() != '')
683 $entry.getKey()="$escapetool.xml($entry.getValue())" ##
684 #end
685 #end
686 ></$tag>
687 #if($field.get('error'))
688 <span ##
689 #foreach($entry in $failureMessageParams.entrySet())
690 $entry.getKey()="$entry.getValue()" ##
691 #end
692 >$field.get('error')</span>
693 #end
694 </dd>
695 #else
696 ERROR: Field with no name.
697 #end##if fieldName exists
698 #end
699 </dl>
700 #end
701 #*
702 * Generate the Javascript for interacting with LiveValidation.
703 *
704 * @param $fields The array of fields which to validate.
705 *###
706 #macro(generateJavascript, $fields)
707 <script type='text/javascript'>
708 /* <![CDATA[ */
709 document.observe('dom:loaded', function() {
710 ##
711 #foreach($field in $fields)
712 #if($field.get('validate') && $field.get('name'))
713 #set($validate = $field.get('validate'))
714 #if(($validate.get('mandatory') && !$validate.get('mandatory').get('noscript'))
715 || ($validate.get('regex') && !$validate.get('regex').get('noscript'))
716 || ($validate.get('mustMatch') && !$validate.get('mustMatch').get('noscript')))
717 #set($fieldName = $field.get('name'))
718 #if($validate.get('fieldOkayMessage'))
719 #set($okayMessage = $validate.get('fieldOkayMessage'))
720 #else
721 #set($okayMessage = $defaultFieldOkayMessage)
722 #end
723 var ${fieldName}Validator = new LiveValidation("$fieldName", { validMessage: "$okayMessage", wait: 500} );
724 ##
725 #if($validate.get('mandatory'))
726 #set($mandatory = $validate.get('mandatory'))
727 #if($mandatory.get('failureMessage') && !$mandatory.get('noscript'))
728 ${fieldName}Validator.add( Validate.Presence, { failureMessage: "$!mandatory.get('failureMessage')"} );
729 #end
730 #end
731 ##
732 #if($validate.get('mustMatch'))
733 #set($mustMatch = $validate.get('mustMatch'))
734 #if($mustMatch.get('name') && $mustMatch.get('failureMessage') && !$mustMatch.get('noscript'))
735 ${fieldName}Validator.add( Validate.Confirmation, { match: $$("input[name=$!mustMatch.get('name')]")[0], failureMessage: "$!mustMatch.get('failureMessage')"} );
736 #end
737 #end
738 ##
739 #if($validate.get('regex'))
740 #set($regex = $validate.get('regex'))
741 #set($pattern = "")
742 #if($regex.get('jsPattern'))
743 #set($pattern = $regex.get('jsPattern'))
744 #elseif($regex.get('pattern'))
745 #set($pattern = $regex.get('pattern'))
746 #end
747 #set($failMessage = "")
748 #if($regex.get('jsFailureMessage'))
749 #set($failMessage = $regex.get('jsFailureMessage'))
750 #elseif($regex.get('failureMessage'))
751 #set($failMessage = $regex.get('failureMessage'))
752 #end
753 #if($pattern != '' && $failMessage != '' && !$regex.get('noscript'))
754 ${fieldName}Validator.add( Validate.Format, { pattern: $pattern, failureMessage: "$failMessage"} );
755 #end
756 #end##if regex
757 #end##if contains js validateable fields.
758 #end##if validate
759 #end##loop
760 });// ]]>
761 </script>
762 #end##macro
763 #*
764 * Get parameters from request so that values will be filled in if there is a mistake
765 * in one of the entries. Entries will be returned to fields[n].params.value
766 * Fields will not be returned if they have either noReturn or error specified.
767 *
768 * @param $fields The array of fields to get parameters for.
769 *###
770 #macro(getParams $fields)
771 #foreach($field in $fields)
772 #if($field.get('name') && $request.get($field.get('name')))
773 #if(!$field.get('noReturn') && !$field.get('error'))
774 #if(!$field.get('params'))
775 #set($params = {})
776 #set($discard = $field.put('params', $params))
777 #else
778 #set($params = $field.get('params'))
779 #end
780 #set($discard = $params.put('value', $request.get($field.get('name'))))
781 #end
782 #end
783 #end
784 #end
785 #*
786 * Get the configuration from the configuration object.
787 *
788 * @param $configDocumentName The name of the document to get the configuration from.
789 *###
790 #macro(loadConfig, $configDocumentName)
791 #set($configDocument = $xwiki.getDocument($configDocumentName))
792 #if(!$configDocument || !$configDocument.getObject($documentName))
793 ## No config document, load defaults.
794 #set($heading = "$msg.get('core.register.title')")
795 #set($welcomeMessage = "$msg.get('core.register.welcome')")
796 #set($useLiveValidation = true)
797 #set($defaultFieldOkayMessage = "$msg.get('core.validation.valid.message')")
798 #set($loginButton = true)
799 #set($defaultRedirect = "$xwiki.getURL('Main.WebHome')")
800 #set($userFullName = "$request.get('register_first_name') $request.get('register_last_name')")
801 #set($registrationSuccessMessage = '{{info}}$msg.get("core.register.successful", ["[[$fullName>>$userSpace$userName]]", $userName]){{/info}}')
802 #else
803 #set($configObject = $configDocument.getObject($documentName))
804 #if ($context.action == 'register')
805 #set ($heading = "(% id='document-title'%)((( = #evaluate($configObject.getProperty('heading').getValue()) = )))(%%)")
806 #else
807 #set ($heading = "= #evaluate($configObject.getProperty('heading').getValue()) =")
808 #end
809 #set($welcomeMessage = "#evaluate($configObject.getProperty('welcomeMessage').getValue())")
810 #if($configObject.getProperty('liveValidation_enabled').getValue() == 1)
811 #set($useLiveValidation = true)
812 #end
813 #set($defaultFieldOkayMessage = "#evaluate($configObject.getProperty('liveValidation_defaultFieldOkMessage').getValue())")
814 #if($configObject.getProperty('loginButton_enabled').getValue() == 1)
815 #set($loginButton = true)
816 #end
817 #if($configObject.getProperty('loginButton_autoLogin_enabled').getValue() == 1)
818 #set($autoLogin = true)
819 #end
820 #set($defaultRedirect = "#evaluate($configObject.getProperty('defaultRedirect').getValue())")
821 #set($registrationSuccessMessage = "$configObject.getProperty('registrationSuccessMessage').getValue()")
822 #if($configObject.getProperty('requireCaptcha').getValue() == 1)
823 #set($requireCaptcha = true)
824 #end
825 #end
826 #end
827 {{/velocity}}