1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.security.internal

File DefaultUserBridge.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

4
26
3
1
134
73
7
0.27
8.67
3
2.33

Classes

Class Line # Actions
DefaultUserBridge 50 26 0% 7 4
0.878787987.9%
 

Contributing tests

This file is covered by 15 tests. .

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 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    * Temporary implementation of the (@link UserBridge} interface to access user information.
44    *
45    * @version $Id: 36ec34309d25ea0209fd29f51ea919153c6a4acb $
46    * @since 4.0M2
47    */
48    @Component
49    @Singleton
 
50    public class DefaultUserBridge implements UserBridge
51    {
52    /** Security reference factory. */
53    @Inject
54    private SecurityReferenceFactory factory;
55   
56    /** Execution object. */
57    @Inject
58    private Execution execution;
59   
60    /**
61    * @return the current {@code XWikiContext}
62    */
 
63  151 toggle private XWikiContext getXWikiContext() {
64  151 return ((XWikiContext) execution.getContext().getProperty(XWikiContext.EXECUTIONCONTEXT_KEY));
65    }
66   
 
67  152 toggle @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    // Public users (not logged in) may not appears in any group
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    * Get all groups in a given wiki where a given user or group is a member of.
90    *
91    * @param wiki the wiki to search groups containing the user/group
92    * @param userOrGroupDocumentReference the user/group document reference
93    * @return the list of group where the user/group is a member
94    * @throws AuthorizationException when an issue arise during retrieval.
95    */
 
96  152 toggle 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    // We get the groups of the member via the group service but we make sure to not use the group service's
112    // cache by calling the method with a limit and an offset.
113    //
114    // We do not use the group service's cache because it might not have been refreshed yet (for example, it can
115    // happen when the security module is used inside a listener that reacts to the "SaveDocument" event just
116    // before the XWikiGroupService listener is called). Because of this race condition, it is not a good idea
117    // to have a cache depending on an other cache.
118    //
119    // TODO: use a proper component to retrieve the groups of a member without any cache
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    }