1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package com.xpn.xwiki.api; |
21 |
|
|
22 |
|
import java.text.MessageFormat; |
23 |
|
|
24 |
|
import org.slf4j.Logger; |
25 |
|
import org.slf4j.LoggerFactory; |
26 |
|
import org.xwiki.model.EntityType; |
27 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
28 |
|
import org.xwiki.model.reference.EntityReference; |
29 |
|
|
30 |
|
import com.xpn.xwiki.XWikiContext; |
31 |
|
import com.xpn.xwiki.XWikiException; |
32 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
33 |
|
import com.xpn.xwiki.objects.BaseObject; |
34 |
|
import com.xpn.xwiki.objects.classes.PasswordClass; |
35 |
|
import com.xpn.xwiki.user.api.XWikiUser; |
36 |
|
import com.xpn.xwiki.util.Programming; |
37 |
|
import com.xpn.xwiki.web.Utils; |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
@version |
44 |
|
@since |
45 |
|
|
|
|
| 70.2% |
Uncovered Elements: 14 (47) |
Complexity: 14 |
Complexity Density: 0.42 |
|
46 |
|
public class User extends Api |
47 |
|
{ |
48 |
|
|
49 |
|
protected static final Logger LOGGER = LoggerFactory.getLogger(User.class); |
50 |
|
|
51 |
|
|
52 |
|
private static final EntityReference USERCLASS_REFERENCE = new EntityReference("XWikiUsers", EntityType.DOCUMENT, |
53 |
|
new EntityReference("XWiki", EntityType.SPACE)); |
54 |
|
|
55 |
|
|
56 |
|
private static final DocumentReferenceResolver<String> REFERENCE_RESOLVER = Utils.getComponent( |
57 |
|
DocumentReferenceResolver.TYPE_STRING, "currentmixed"); |
58 |
|
|
59 |
|
|
60 |
|
private XWikiUser user; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
@param |
66 |
|
@param@link |
67 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
68 |
8 |
public User(XWikiUser user, XWikiContext context)... |
69 |
|
{ |
70 |
8 |
super(context); |
71 |
8 |
this.user = user; |
72 |
|
} |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@return |
78 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
79 |
0 |
@Programming... |
80 |
|
public XWikiUser getUser() |
81 |
|
{ |
82 |
0 |
if (hasProgrammingRights()) { |
83 |
0 |
return this.user; |
84 |
|
} |
85 |
0 |
return null; |
86 |
|
} |
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
@param |
93 |
|
@return |
94 |
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 3 |
Complexity Density: 0.43 |
|
95 |
4 |
public boolean isUserInGroup(String groupName)... |
96 |
|
{ |
97 |
4 |
boolean result = false; |
98 |
4 |
try { |
99 |
4 |
if (this.user == null) { |
100 |
1 |
LOGGER.warn("Invalid null user"); |
101 |
|
} else { |
102 |
3 |
result = this.user.isUserInGroup(groupName, getXWikiContext()); |
103 |
|
} |
104 |
|
} catch (Exception ex) { |
105 |
0 |
LOGGER.warn(new MessageFormat("Unhandled exception while checking if user {0}" |
106 |
|
+ " belongs to group {1}").format(new java.lang.Object[] { this.user, groupName }), ex); |
107 |
|
} |
108 |
4 |
return result; |
109 |
|
} |
110 |
|
|
111 |
|
|
112 |
|
|
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
@return |
120 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
121 |
0 |
protected boolean isMain()... |
122 |
|
{ |
123 |
0 |
return this.user.isMain(); |
124 |
|
} |
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
@return |
131 |
|
@since |
132 |
|
@since |
133 |
|
@since |
134 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
135 |
2 |
public String getEmail()... |
136 |
|
{ |
137 |
2 |
XWikiDocument userDoc; |
138 |
2 |
try { |
139 |
2 |
userDoc = getXWikiContext().getWiki().getDocument(this.user.getUser(), getXWikiContext()); |
140 |
1 |
BaseObject obj = userDoc.getObject("XWiki.XWikiUsers"); |
141 |
1 |
return obj.getStringValue("email"); |
142 |
|
} catch (Exception e) { |
143 |
|
|
144 |
|
|
145 |
|
|
146 |
1 |
return null; |
147 |
|
} |
148 |
|
} |
149 |
|
|
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
@param |
156 |
|
@return |
157 |
|
@throws |
158 |
|
|
|
|
| 72.2% |
Uncovered Elements: 5 (18) |
Complexity: 5 |
Complexity Density: 0.36 |
|
159 |
2 |
public boolean checkPassword(String password) throws XWikiException... |
160 |
|
{ |
161 |
2 |
EntityReference userReference = REFERENCE_RESOLVER.resolve(this.user.getUser()); |
162 |
2 |
EntityReference docReference = getXWikiContext().getDoc().getDocumentReference(); |
163 |
2 |
if (userReference.equals(getXWikiContext().getUserReference()) && userReference.equals(docReference)) { |
164 |
2 |
try { |
165 |
2 |
boolean result = false; |
166 |
|
|
167 |
2 |
XWikiDocument userDoc = getXWikiContext().getWiki().getDocument(userReference, getXWikiContext()); |
168 |
2 |
BaseObject obj = userDoc.getXObject(USERCLASS_REFERENCE); |
169 |
|
|
170 |
2 |
if (obj != null) { |
171 |
2 |
final String stored = obj.getStringValue("password"); |
172 |
2 |
result = new PasswordClass().getEquivalentPassword(stored, password).equals(stored); |
173 |
|
} |
174 |
|
|
175 |
2 |
return result; |
176 |
|
} catch (Throwable e) { |
177 |
0 |
LOGGER.error("Failed to check password", e); |
178 |
0 |
return false; |
179 |
|
} |
180 |
|
} else { |
181 |
0 |
throw new XWikiException(XWikiException.MODULE_XWIKI_ACCESS, XWikiException.ERROR_XWIKI_ACCESS_DENIED, |
182 |
|
"You cannot use this method for checking another user password.", null); |
183 |
|
} |
184 |
|
} |
185 |
|
|
186 |
|
} |