1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.web

File RegisterAction.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
66% of files have more coverage

Code metrics

16
39
3
1
141
82
13
0.33
13
3
4.33

Classes

Class Line # Actions
RegisterAction 41 39 0% 13 18
0.689655269%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.web;
21   
22    import javax.script.ScriptContext;
23   
24    import org.apache.commons.lang3.exception.ExceptionUtils;
25    import org.slf4j.Logger;
26    import org.slf4j.LoggerFactory;
27    import org.xwiki.captcha.Captcha;
28    import org.xwiki.captcha.CaptchaConfiguration;
29    import org.xwiki.model.reference.DocumentReference;
30   
31    import com.xpn.xwiki.XWiki;
32    import com.xpn.xwiki.XWikiContext;
33    import com.xpn.xwiki.XWikiException;
34    import com.xpn.xwiki.doc.XWikiDocument;
35   
36    /**
37    * Register xwiki action.
38    *
39    * @version $Id: 7939327d30903d8369796ba03d497166c4d86020 $
40    */
 
41    public class RegisterAction extends XWikiAction
42    {
43    /** Name of the corresponding template and URL parameter. */
44    private static final String REGISTER = "register";
45   
46    /** Logger. */
47    private static final Logger LOGGER = LoggerFactory.getLogger(RegisterAction.class);
48   
49    /** Space where the registration config and class are stored. */
50    private static final String WIKI_SPACE = "XWiki";
51   
 
52  512 toggle @Override
53    public boolean action(XWikiContext context) throws XWikiException
54    {
55  512 XWiki xwiki = context.getWiki();
56  512 XWikiRequest request = context.getRequest();
57  512 XWikiResponse response = context.getResponse();
58   
59  512 String register = request.getParameter(REGISTER);
60  512 if (register != null && register.equals("1")) {
61    // CSRF prevention
62  77 if (!csrfTokenCheck(context)) {
63  1 return false;
64    }
65    // Let's verify that the user submitted the right CAPTCHA (if required).
66  76 if (!verifyCaptcha(context, xwiki)) {
67  0 return false;
68    }
69   
70  76 int useemail = xwiki.getXWikiPreferenceAsInt("use_email_verification", 0, context);
71  76 int result;
72  76 if (useemail == 1) {
73  0 result = xwiki.createUser(true, "edit", context);
74    } else {
75  76 result = xwiki.createUser(context);
76    }
77  76 getCurrentScriptContext().setAttribute("reg", Integer.valueOf(result), ScriptContext.ENGINE_SCOPE);
78   
79    // Redirect if a redirection parameter is passed.
80  76 String redirect = Utils.getRedirect(request, null);
81  76 if (redirect == null) {
82  42 return true;
83    } else {
84  34 sendRedirect(response, redirect);
85  34 return false;
86    }
87    }
88   
89  435 return true;
90    }
91   
 
92  477 toggle @Override
93    public String render(XWikiContext context) throws XWikiException
94    {
95  477 return REGISTER;
96    }
97   
98    /**
99    * Verifies the user CAPTCHA answer (if required).
100    *
101    * @param context Current context
102    * @param xwiki Current wiki
103    * @return true If the user submitted the correct answer or if no CAPTCHA is required
104    * @throws XWikiException exception
105    */
 
106  76 toggle private boolean verifyCaptcha(XWikiContext context, XWiki xwiki) throws XWikiException
107    {
108    // No verification if the current user has programming rights.
109  76 if (xwiki.getRightService().hasProgrammingRights(context)) {
110  0 return true;
111    }
112   
113    // The document where the "requirecaptcha" parameter is stored.
114  76 DocumentReference configRef = new DocumentReference(context.getWikiId(), WIKI_SPACE, "RegistrationConfig");
115  76 DocumentReference classReference = new DocumentReference(context.getWikiId(), WIKI_SPACE, "Registration");
116  76 XWikiDocument configDoc = xwiki.getDocument(configRef, context);
117    // Retrieve the captcha configuration.
118  76 int requireCaptcha = configDoc.getIntValue(classReference, "requireCaptcha");
119   
120  76 if (requireCaptcha == 1) {
121  0 CaptchaConfiguration captchaConfiguration =
122    Utils.getComponent(org.xwiki.captcha.CaptchaConfiguration.class);
123  0 String defaultCaptchaName = captchaConfiguration.getDefaultName();
124  0 try {
125    // Use the currently configured default CAPTCHA implementation.
126  0 Captcha captcha = Utils.getComponent(org.xwiki.captcha.Captcha.class, defaultCaptchaName);
127   
128  0 if (!captcha.isValid()) {
129  0 LOGGER.warn("Incorrect CAPTCHA answer");
130  0 return false;
131    }
132    } catch (Exception e) {
133  0 LOGGER.warn("Cannot verify answer for CAPTCHA of type [{}]: {}", defaultCaptchaName,
134    ExceptionUtils.getRootCauseMessage(e));
135  0 return false;
136    }
137    }
138   
139  76 return true;
140    }
141    }