1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.security.authorization.internal; |
21 |
|
|
22 |
|
import java.util.Arrays; |
23 |
|
import java.util.Collection; |
24 |
|
import java.util.List; |
25 |
|
import java.util.concurrent.locks.ReadWriteLock; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Provider; |
30 |
|
import javax.inject.Singleton; |
31 |
|
|
32 |
|
import org.slf4j.Logger; |
33 |
|
import org.xwiki.bridge.event.DocumentCreatedEvent; |
34 |
|
import org.xwiki.bridge.event.DocumentDeletedEvent; |
35 |
|
import org.xwiki.bridge.event.DocumentUpdatedEvent; |
36 |
|
import org.xwiki.component.annotation.Component; |
37 |
|
import org.xwiki.model.EntityType; |
38 |
|
import org.xwiki.model.reference.DocumentReference; |
39 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
40 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
41 |
|
import org.xwiki.model.reference.WikiReference; |
42 |
|
import org.xwiki.observation.EventListener; |
43 |
|
import org.xwiki.observation.event.Event; |
44 |
|
import org.xwiki.security.SecurityReferenceFactory; |
45 |
|
import org.xwiki.security.authorization.AuthorizationException; |
46 |
|
import org.xwiki.security.authorization.cache.SecurityCache; |
47 |
|
import org.xwiki.security.internal.XWikiConstants; |
48 |
|
|
49 |
|
import com.xpn.xwiki.XWikiContext; |
50 |
|
import com.xpn.xwiki.XWikiException; |
51 |
|
import com.xpn.xwiki.doc.XWikiDocument; |
52 |
|
import com.xpn.xwiki.objects.BaseObject; |
53 |
|
import com.xpn.xwiki.user.api.XWikiGroupService; |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@version |
59 |
|
@since |
60 |
|
|
61 |
|
@Component |
62 |
|
@Named(DefaultSecurityCacheRulesInvalidator.NAME) |
63 |
|
@Singleton |
|
|
| 93.2% |
Uncovered Elements: 4 (59) |
Complexity: 17 |
Complexity Density: 0.43 |
|
64 |
|
public class DefaultSecurityCacheRulesInvalidatorListener implements EventListener |
65 |
|
{ |
66 |
|
private static final List<Event> EVENTS = Arrays.<Event> asList(new DocumentCreatedEvent(), |
67 |
|
new DocumentUpdatedEvent(), new DocumentDeletedEvent()); |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
@Inject |
73 |
|
@Named(DefaultSecurityCacheRulesInvalidator.NAME) |
74 |
|
private ReadWriteLock readWriteLock; |
75 |
|
|
76 |
|
|
77 |
|
@Inject |
78 |
|
private Logger logger; |
79 |
|
|
80 |
|
|
81 |
|
@Inject |
82 |
|
private SecurityCache securityCache; |
83 |
|
|
84 |
|
|
85 |
|
@Inject |
86 |
|
private SecurityReferenceFactory securityReferenceFactory; |
87 |
|
|
88 |
|
|
89 |
|
@Inject |
90 |
|
private DocumentReferenceResolver<String> resolver; |
91 |
|
|
92 |
|
|
93 |
|
@Inject |
94 |
|
@Named("user") |
95 |
|
private DocumentReferenceResolver<String> userResolver; |
96 |
|
|
97 |
|
|
98 |
|
@Inject |
99 |
|
private EntityReferenceSerializer<String> serializer; |
100 |
|
|
101 |
|
|
102 |
|
@Inject |
103 |
|
private Provider<XWikiContext> xcontextProvider; |
104 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
105 |
518 |
@Override... |
106 |
|
public String getName() |
107 |
|
{ |
108 |
518 |
return DefaultSecurityCacheRulesInvalidator.NAME; |
109 |
|
} |
110 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
111 |
65 |
@Override... |
112 |
|
public List<Event> getEvents() |
113 |
|
{ |
114 |
65 |
return EVENTS; |
115 |
|
} |
116 |
|
|
117 |
|
|
118 |
|
@link |
119 |
|
|
120 |
|
@param |
121 |
|
@return |
122 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
123 |
3970 |
private static DocumentReference getDocumentReference(Object xwikiDocument)... |
124 |
|
{ |
125 |
3970 |
XWikiDocument doc = (XWikiDocument) xwikiDocument; |
126 |
3970 |
return doc.getDocumentReference(); |
127 |
|
} |
128 |
|
|
129 |
|
|
130 |
|
@param |
131 |
|
@return |
132 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
133 |
3970 |
private boolean isGroupDocument(Object source)... |
134 |
|
{ |
135 |
3970 |
XWikiDocument doc = (XWikiDocument) source; |
136 |
3970 |
DocumentReference docRef = doc.getDocumentReference(); |
137 |
3970 |
DocumentReference groupClass = resolver.resolve(XWikiConstants.GROUP_CLASS, docRef); |
138 |
3970 |
List<BaseObject> objects = doc.getXObjects(groupClass); |
139 |
3970 |
return objects != null && objects.size() > 0; |
140 |
|
} |
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
@param |
146 |
|
@param |
147 |
|
@throws |
148 |
|
|
|
|
| 85% |
Uncovered Elements: 3 (20) |
Complexity: 4 |
Complexity Density: 0.25 |
|
149 |
84 |
public void invalidateGroupMembers(DocumentReference group, SecurityCache securityCache)... |
150 |
|
throws AuthorizationException |
151 |
|
{ |
152 |
84 |
try { |
153 |
84 |
XWikiContext xwikiContext = this.xcontextProvider.get(); |
154 |
84 |
XWikiGroupService groupService = xwikiContext.getWiki().getGroupService(xwikiContext); |
155 |
84 |
String groupName = serializer.serialize(group); |
156 |
|
|
157 |
|
|
158 |
|
|
159 |
|
|
160 |
84 |
WikiReference wikiReference = group.getWikiReference(); |
161 |
84 |
final int nb = 100; |
162 |
84 |
int i = 0; |
163 |
84 |
Collection<String> memberNames; |
164 |
84 |
do { |
165 |
84 |
memberNames = groupService.getAllMembersNamesForGroup(groupName, nb, i * nb, xwikiContext); |
166 |
84 |
for (String member : memberNames) { |
167 |
91 |
DocumentReference memberRef = userResolver.resolve(member, wikiReference); |
168 |
|
|
169 |
|
|
170 |
|
|
171 |
91 |
if (!memberRef.equals(group)) { |
172 |
91 |
securityCache.remove(securityReferenceFactory.newUserReference(memberRef)); |
173 |
|
} |
174 |
|
} |
175 |
84 |
i++; |
176 |
84 |
} while (memberNames.size() == nb); |
177 |
|
} catch (XWikiException e) { |
178 |
0 |
throw new AuthorizationException("Failed to invalidate group member.", e); |
179 |
|
} |
180 |
|
} |
181 |
|
|
|
|
| 90% |
Uncovered Elements: 1 (10) |
Complexity: 3 |
Complexity Density: 0.38 |
|
182 |
3970 |
@Override... |
183 |
|
public void onEvent(Event event, Object source, Object data) |
184 |
|
{ |
185 |
3970 |
DocumentReference ref = getDocumentReference(source); |
186 |
3970 |
readWriteLock.writeLock().lock(); |
187 |
3970 |
try { |
188 |
3970 |
deliverUpdateEvent(ref); |
189 |
3970 |
if (isGroupDocument(source)) { |
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
84 |
invalidateGroupMembers(ref, securityCache); |
195 |
|
} |
196 |
|
} catch (AuthorizationException e) { |
197 |
0 |
this.logger.error("Failed to invalidate group members on the document: {}", ref, e); |
198 |
|
} finally { |
199 |
3970 |
readWriteLock.writeLock().unlock(); |
200 |
|
} |
201 |
|
} |
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
@param |
207 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (13) |
Complexity: 6 |
Complexity Density: 0.86 |
|
208 |
3970 |
private void deliverUpdateEvent(DocumentReference ref)... |
209 |
|
{ |
210 |
3970 |
if (XWikiConstants.WIKI_DOC_REFERENCE.equals(ref, EntityType.SPACE)) { |
211 |
|
|
212 |
141 |
securityCache.remove(securityReferenceFactory.newEntityReference(ref.getWikiReference())); |
213 |
3829 |
} else if (ref.getName().equals(XWikiConstants.SPACE_DOC)) { |
214 |
|
|
215 |
21 |
securityCache.remove(securityReferenceFactory.newEntityReference(ref.getParent())); |
216 |
|
} else { |
217 |
|
|
218 |
3808 |
securityCache.remove(securityReferenceFactory.newEntityReference(ref)); |
219 |
|
|
220 |
|
|
221 |
3808 |
if (ref.getName().startsWith(XWikiConstants.WIKI_DESCRIPTOR_PREFIX) |
222 |
|
&& XWikiConstants.XWIKI_SPACE_REFERENCE.equals(ref.getLastSpaceReference(), EntityType.SPACE) |
223 |
|
&& ref.getWikiReference().getName().equals(this.xcontextProvider.get().getMainXWiki())) { |
224 |
|
|
225 |
143 |
securityCache.remove(securityReferenceFactory.newEntityReference(new WikiReference(ref.getName() |
226 |
|
.substring(XWikiConstants.WIKI_DESCRIPTOR_PREFIX.length()).toLowerCase()))); |
227 |
|
} |
228 |
|
} |
229 |
|
} |
230 |
|
} |