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