| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
|
| 20 |
|
package org.xwiki.security.internal; |
| 21 |
|
|
| 22 |
|
import java.util.ArrayList; |
| 23 |
|
import java.util.Collection; |
| 24 |
|
import java.util.Collections; |
| 25 |
|
import java.util.HashSet; |
| 26 |
|
|
| 27 |
|
import javax.inject.Inject; |
| 28 |
|
import javax.inject.Singleton; |
| 29 |
|
|
| 30 |
|
import org.xwiki.component.annotation.Component; |
| 31 |
|
import org.xwiki.context.Execution; |
| 32 |
|
import org.xwiki.model.reference.DocumentReference; |
| 33 |
|
import org.xwiki.model.reference.WikiReference; |
| 34 |
|
import org.xwiki.security.GroupSecurityReference; |
| 35 |
|
import org.xwiki.security.SecurityReferenceFactory; |
| 36 |
|
import org.xwiki.security.UserSecurityReference; |
| 37 |
|
import org.xwiki.security.authorization.AuthorizationException; |
| 38 |
|
|
| 39 |
|
import com.xpn.xwiki.XWikiContext; |
| 40 |
|
import com.xpn.xwiki.user.api.XWikiGroupService; |
| 41 |
|
|
| 42 |
|
|
| 43 |
|
@link |
| 44 |
|
|
| 45 |
|
@version |
| 46 |
|
@since |
| 47 |
|
|
| 48 |
|
@Component |
| 49 |
|
@Singleton |
| |
|
| 87.9% |
Uncovered Elements: 4 (33) |
Complexity: 7 |
Complexity Density: 0.27 |
|
| 50 |
|
public class DefaultUserBridge implements UserBridge |
| 51 |
|
{ |
| 52 |
|
|
| 53 |
|
@Inject |
| 54 |
|
private SecurityReferenceFactory factory; |
| 55 |
|
|
| 56 |
|
|
| 57 |
|
@Inject |
| 58 |
|
private Execution execution; |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
@return |
| 62 |
|
|
| |
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 63 |
151 |
private XWikiContext getXWikiContext() {... |
| 64 |
151 |
return ((XWikiContext) execution.getContext().getProperty(XWikiContext.EXECUTIONCONTEXT_KEY)); |
| 65 |
|
} |
| 66 |
|
|
| |
|
| 81.8% |
Uncovered Elements: 2 (11) |
Complexity: 2 |
Complexity Density: 0.22 |
|
| 67 |
152 |
@Override... |
| 68 |
|
public Collection<GroupSecurityReference> getAllGroupsFor(UserSecurityReference user, WikiReference wikiReference) |
| 69 |
|
throws AuthorizationException |
| 70 |
|
{ |
| 71 |
152 |
DocumentReference userRef = user.getOriginalReference(); |
| 72 |
|
|
| 73 |
152 |
if (userRef == null) { |
| 74 |
|
|
| 75 |
0 |
return Collections.emptyList(); |
| 76 |
|
} |
| 77 |
|
|
| 78 |
152 |
Collection<DocumentReference> groupRefs = getGroupsReferencesFor(wikiReference, userRef); |
| 79 |
|
|
| 80 |
152 |
Collection<GroupSecurityReference> groups = new ArrayList<GroupSecurityReference>(groupRefs.size()); |
| 81 |
152 |
for (DocumentReference groupRef : groupRefs) { |
| 82 |
68 |
GroupSecurityReference group = factory.newGroupReference(groupRef); |
| 83 |
68 |
groups.add(group); |
| 84 |
|
} |
| 85 |
152 |
return groups; |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
|
| 89 |
|
|
| 90 |
|
|
| 91 |
|
@param |
| 92 |
|
@param |
| 93 |
|
@return |
| 94 |
|
@throws |
| 95 |
|
|
| |
|
| 88.9% |
Uncovered Elements: 2 (18) |
Complexity: 4 |
Complexity Density: 0.25 |
|
| 96 |
152 |
private Collection<DocumentReference> getGroupsReferencesFor(WikiReference wiki,... |
| 97 |
|
DocumentReference userOrGroupDocumentReference) throws AuthorizationException |
| 98 |
|
{ |
| 99 |
152 |
XWikiContext xwikiContext = getXWikiContext(); |
| 100 |
152 |
XWikiGroupService groupService; |
| 101 |
152 |
try { |
| 102 |
152 |
groupService = xwikiContext.getWiki().getGroupService(xwikiContext); |
| 103 |
|
} catch (Exception e) { |
| 104 |
0 |
throw new AuthorizationException("Failed to access the group service.", e); |
| 105 |
|
} |
| 106 |
|
|
| 107 |
152 |
String currentWiki = xwikiContext.getWikiId(); |
| 108 |
152 |
Collection<DocumentReference> groupReferences = new HashSet<>(); |
| 109 |
152 |
try { |
| 110 |
152 |
xwikiContext.setWikiId(wiki.getName()); |
| 111 |
|
|
| 112 |
|
|
| 113 |
|
|
| 114 |
|
|
| 115 |
|
|
| 116 |
|
|
| 117 |
|
|
| 118 |
|
|
| 119 |
|
|
| 120 |
152 |
final int nb = 1000; |
| 121 |
152 |
int i = 0; |
| 122 |
220 |
while (groupReferences.addAll(groupService.getAllGroupsReferencesForMember(userOrGroupDocumentReference, |
| 123 |
|
nb, i * nb, xwikiContext))) { |
| 124 |
68 |
i++; |
| 125 |
|
} |
| 126 |
152 |
return groupReferences; |
| 127 |
|
} catch (Exception e) { |
| 128 |
0 |
throw new AuthorizationException(String.format("Failed to get groups for user or group [%s] in wiki [%s]", |
| 129 |
|
userOrGroupDocumentReference, wiki), e); |
| 130 |
|
} finally { |
| 131 |
152 |
xwikiContext.setWikiId(currentWiki); |
| 132 |
|
} |
| 133 |
|
} |
| 134 |
|
} |