1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.web; |
21 |
|
|
22 |
|
import javax.script.ScriptContext; |
23 |
|
|
24 |
|
import org.slf4j.Logger; |
25 |
|
import org.slf4j.LoggerFactory; |
26 |
|
import org.xwiki.captcha.CaptchaVerifier; |
27 |
|
import org.xwiki.model.reference.DocumentReference; |
28 |
|
|
29 |
|
import com.xpn.xwiki.XWiki; |
30 |
|
import com.xpn.xwiki.XWikiContext; |
31 |
|
import com.xpn.xwiki.XWikiException; |
32 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
@version |
38 |
|
|
|
|
| 73.2% |
Uncovered Elements: 15 (56) |
Complexity: 13 |
Complexity Density: 0.35 |
|
39 |
|
public class RegisterAction extends XWikiAction |
40 |
|
{ |
41 |
|
|
42 |
|
private static final String REGISTER = "register"; |
43 |
|
|
44 |
|
|
45 |
|
private static final Logger LOGGER = LoggerFactory.getLogger(RegisterAction.class); |
46 |
|
|
47 |
|
|
48 |
|
private static final String WIKI_SPACE = "XWiki"; |
49 |
|
|
50 |
|
|
51 |
|
private static CaptchaVerifier verifier = Utils.getComponent(CaptchaVerifier.class, "image"); |
52 |
|
|
|
|
| 87.1% |
Uncovered Elements: 4 (31) |
Complexity: 7 |
Complexity Density: 0.33 |
|
53 |
163 |
@Override... |
54 |
|
public boolean action(XWikiContext context) throws XWikiException |
55 |
|
{ |
56 |
163 |
XWiki xwiki = context.getWiki(); |
57 |
163 |
XWikiRequest request = context.getRequest(); |
58 |
163 |
XWikiResponse response = context.getResponse(); |
59 |
|
|
60 |
163 |
String register = request.getParameter(REGISTER); |
61 |
163 |
if (register != null && register.equals("1")) { |
62 |
|
|
63 |
30 |
if (!csrfTokenCheck(context)) { |
64 |
1 |
return false; |
65 |
|
} |
66 |
|
|
67 |
29 |
if (!verifyCaptcha(context, xwiki)) { |
68 |
0 |
return false; |
69 |
|
} |
70 |
|
|
71 |
29 |
int useemail = xwiki.getXWikiPreferenceAsInt("use_email_verification", 0, context); |
72 |
29 |
int result; |
73 |
29 |
if (useemail == 1) { |
74 |
0 |
result = xwiki.createUser(true, "edit", context); |
75 |
|
} else { |
76 |
29 |
result = xwiki.createUser(context); |
77 |
|
} |
78 |
29 |
getCurrentScriptContext().setAttribute("reg", Integer.valueOf(result), ScriptContext.ENGINE_SCOPE); |
79 |
|
|
80 |
|
|
81 |
29 |
String redirect = Utils.getRedirect(request, null); |
82 |
29 |
if (redirect == null) { |
83 |
6 |
return true; |
84 |
|
} else { |
85 |
23 |
sendRedirect(response, redirect); |
86 |
23 |
return false; |
87 |
|
} |
88 |
|
} |
89 |
|
|
90 |
133 |
return true; |
91 |
|
} |
92 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
93 |
139 |
@Override... |
94 |
|
public String render(XWikiContext context) throws XWikiException |
95 |
|
{ |
96 |
139 |
return REGISTER; |
97 |
|
} |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
@param |
103 |
|
@param |
104 |
|
@return |
105 |
|
@throws |
106 |
|
|
|
|
| 47.6% |
Uncovered Elements: 11 (21) |
Complexity: 5 |
Complexity Density: 0.33 |
|
107 |
29 |
private boolean verifyCaptcha(XWikiContext context, XWiki xwiki) throws XWikiException... |
108 |
|
{ |
109 |
|
|
110 |
29 |
if (xwiki.getRightService().hasProgrammingRights(context)) { |
111 |
0 |
return true; |
112 |
|
} |
113 |
29 |
XWikiRequest request = context.getRequest(); |
114 |
|
|
115 |
29 |
DocumentReference configRef = new DocumentReference(context.getWikiId(), WIKI_SPACE, "RegistrationConfig"); |
116 |
29 |
DocumentReference classReference = new DocumentReference(context.getWikiId(), WIKI_SPACE, "Registration"); |
117 |
29 |
XWikiDocument configDoc = xwiki.getDocument(configRef, context); |
118 |
|
|
119 |
29 |
int captcha = configDoc.getIntValue(classReference, "requireCaptcha"); |
120 |
|
|
121 |
29 |
if (captcha == 1) { |
122 |
0 |
try { |
123 |
0 |
if (!verifier.isAnswerCorrect(verifier.getUserId(request), request.get("captcha_answer"))) { |
124 |
0 |
LOGGER.warn("Incorrect captcha answer"); |
125 |
0 |
return false; |
126 |
|
} |
127 |
|
} catch (Exception e) { |
128 |
0 |
LOGGER.warn("Cannot verify captcha answer: {}", e.getMessage()); |
129 |
0 |
return false; |
130 |
|
} |
131 |
|
} |
132 |
29 |
return true; |
133 |
|
} |
134 |
|
} |