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