Search in sources :

Example 1 with XWikiGroupService

use of com.xpn.xwiki.user.api.XWikiGroupService in project xwiki-platform by xwiki.

the class XWikiRightServiceImpl method addMemberGroups.

private void addMemberGroups(String wiki, String prefixedFullName, DocumentReference userOrGroupDocumentReference, Collection<String> grouplist, XWikiContext context) throws XWikiException {
    XWikiGroupService groupService = context.getWiki().getGroupService(context);
    Map<String, Collection<String>> grouplistcache = (Map<String, Collection<String>>) context.get("grouplist");
    if (grouplistcache == null) {
        grouplistcache = new HashMap<String, Collection<String>>();
        context.put("grouplist", grouplistcache);
    }
    // the key is for the entity <code>prefixedFullName</code> in current wiki
    String key = wiki + ":" + prefixedFullName;
    Collection<String> tmpGroupList = grouplistcache.get(key);
    if (tmpGroupList == null) {
        String currentWiki = context.getWikiId();
        try {
            context.setWikiId(wiki);
            Collection<DocumentReference> groupReferences = groupService.getAllGroupsReferencesForMember(userOrGroupDocumentReference, 0, 0, context);
            tmpGroupList = new ArrayList<String>(groupReferences.size());
            for (DocumentReference groupReference : groupReferences) {
                tmpGroupList.add(this.entityReferenceSerializer.serialize(groupReference));
            }
        } catch (Exception e) {
            LOGGER.error("Failed to get groups for user or group [{}] in wiki [{}]", prefixedFullName, wiki, e);
            tmpGroupList = Collections.emptyList();
        } finally {
            context.setWikiId(currentWiki);
        }
        grouplistcache.put(key, tmpGroupList);
    }
    grouplist.addAll(tmpGroupList);
}
Also used : Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) XWikiRightNotFoundException(com.xpn.xwiki.user.api.XWikiRightNotFoundException) XWikiGroupService(com.xpn.xwiki.user.api.XWikiGroupService)

Example 2 with XWikiGroupService

use of com.xpn.xwiki.user.api.XWikiGroupService in project xwiki-platform by xwiki.

the class UsersClassTest method getMap.

@Test
public void getMap() throws Exception {
    XWikiContext context = mock(XWikiContext.class);
    com.xpn.xwiki.XWiki xwiki = mock(com.xpn.xwiki.XWiki.class);
    when(context.getWiki()).thenReturn(xwiki);
    XWikiGroupService groupService = mock(XWikiGroupService.class);
    when(xwiki.getGroupService(any())).thenReturn(groupService);
    when(groupService.getAllMatchedUsers(any(), anyBoolean(), anyInt(), anyInt(), any(), any())).thenReturn((List) Arrays.asList("XWiki.Admin"));
    when(xwiki.getUserName(eq("XWiki.Admin"), any(), anyBoolean(), any())).thenReturn("Administrator");
    Utils.setComponentManager(this.componentManager);
    this.componentManager.registerMockComponent(EntityReferenceSerializer.TYPE_STRING, "local");
    this.componentManager.registerMockComponent(EntityReferenceResolver.TYPE_STRING, "relative");
    this.componentManager.registerMockComponent(DocumentReferenceResolver.TYPE_REFERENCE, "current");
    UsersClass usersClass = new UsersClass();
    Map<String, ListItem> results = usersClass.getMap(context);
    assertEquals(1, results.size());
    assertEquals("XWiki.Admin", results.get("XWiki.Admin").getId());
    assertEquals("Administrator", results.get("XWiki.Admin").getValue());
}
Also used : XWikiContext(com.xpn.xwiki.XWikiContext) XWikiGroupService(com.xpn.xwiki.user.api.XWikiGroupService) Test(org.junit.Test)

Example 3 with XWikiGroupService

use of com.xpn.xwiki.user.api.XWikiGroupService in project xwiki-platform by xwiki.

the class DefaultWikiMacroTest method setUp.

@Override
@Before
public void setUp() throws Exception {
    super.setUp();
    getMockery().checking(new Expectations() {

        {
            allowing(mockWikiDescriptorManager).getCurrentWikiId();
            will(new CustomAction("WikiDescriptorManager#getCurrentWikiId") {

                @Override
                public Object invoke(Invocation invocation) throws Throwable {
                    return getContext().getWikiId();
                }
            });
        }
    });
    final ContextualAuthorizationManager mockCam = getContextualAuthorizationManager();
    final XWiki mockXWiki = getMockery().mock(XWiki.class);
    final XWikiGroupService mockXWikiGroupService = getMockery().mock(XWikiGroupService.class);
    getContext().setWiki(mockXWiki);
    this.xwiki20Parser = getComponentManager().getInstance(Parser.class, "xwiki/2.0");
    this.wikiMacroDocumentReference = new DocumentReference(getContext().getWikiId(), "space", "macroPage");
    this.wikiMacroManager = getComponentManager().getInstance(WikiMacroManager.class);
    this.wikiMacroDocument = new XWikiDocument(wikiMacroDocumentReference);
    final XWikiRightService rightService = new XWikiRightServiceImpl();
    this.user = new XWikiDocument(new DocumentReference(getContext().getWikiId(), "XWiki", "user"));
    this.user.setNew(false);
    BaseObject userObject = new BaseObject();
    userObject.setXClassReference(new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiusers"));
    this.user.addXObject(userObject);
    this.wikiMacroDocument.setCreatorReference(this.user.getAuthorReference());
    this.wikiMacroDocument.setAuthorReference(this.user.getAuthorReference());
    this.wikiMacroDocument.setContentAuthorReference(this.user.getAuthorReference());
    // Setup an XWikiPreferences document granting programming rights to user
    final XWikiDocument prefs = new XWikiDocument(new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiPreferences"));
    final BaseObject mockGlobalRightObj = getMockery().mock(BaseObject.class);
    getMockery().checking(new Expectations() {

        {
            allowing(mockCam).hasAccess(Right.PROGRAM);
            will(returnValue(true));
            allowing(mockXWiki).getDocument(with(equal(wikiMacroDocumentReference)), with(any(XWikiContext.class)));
            will(returnValue(wikiMacroDocument));
            allowing(mockXWiki).isReadOnly();
            will(returnValue(false));
            allowing(mockXWiki).getLanguagePreference(with(any(XWikiContext.class)));
            will(returnValue(null));
            allowing(mockXWiki).getRightService();
            will(returnValue(rightService));
            allowing(mockXWiki).getGroupService(with(any(XWikiContext.class)));
            will(returnValue(mockXWikiGroupService));
            allowing(mockXWikiGroupService).getAllGroupsReferencesForMember(with(any(DocumentReference.class)), with(any(int.class)), with(any(int.class)), with(any(XWikiContext.class)));
            will(returnValue(Collections.EMPTY_LIST));
            allowing(mockXWiki).getDocument(with(equal(XWIKIPREFERENCES_REFERENCE)), with(any(XWikiContext.class)));
            will(returnValue(prefs));
            allowing(mockGlobalRightObj).getStringValue("levels");
            will(returnValue("programming"));
            allowing(mockGlobalRightObj).getStringValue("users");
            will(returnValue(user.getFullName()));
            allowing(mockGlobalRightObj).getIntValue("allow");
            will(returnValue(1));
            allowing(mockGlobalRightObj).setNumber(with(any(int.class)));
            allowing(mockGlobalRightObj).setDocumentReference(with(any(DocumentReference.class)));
            allowing(mockGlobalRightObj).setOwnerDocument(with(any(XWikiDocument.class)));
        }
    });
    prefs.addObject("XWiki.XWikiGlobalRights", mockGlobalRightObj);
    getContext().setUserReference(this.user.getDocumentReference());
}
Also used : Expectations(org.jmock.Expectations) ContextualAuthorizationManager(org.xwiki.security.authorization.ContextualAuthorizationManager) Invocation(org.jmock.api.Invocation) CustomAction(org.jmock.lib.action.CustomAction) XWiki(com.xpn.xwiki.XWiki) XWikiContext(com.xpn.xwiki.XWikiContext) WikiMacroManager(org.xwiki.rendering.macro.wikibridge.WikiMacroManager) Parser(org.xwiki.rendering.parser.Parser) BaseObject(com.xpn.xwiki.objects.BaseObject) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiRightService(com.xpn.xwiki.user.api.XWikiRightService) XWikiRightServiceImpl(com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiGroupService(com.xpn.xwiki.user.api.XWikiGroupService) Before(org.junit.Before)

Example 4 with XWikiGroupService

use of com.xpn.xwiki.user.api.XWikiGroupService in project xwiki-platform by xwiki.

the class DefaultUserBridge method getGroupsReferencesFor.

/**
 * Get all groups in a given wiki where a given user or group is a member of.
 *
 * @param wiki the wiki to search groups containing the user/group
 * @param userOrGroupDocumentReference the user/group document reference
 * @return the list of group where the user/group is a member
 * @throws AuthorizationException when an issue arise during retrieval.
 */
private Collection<DocumentReference> getGroupsReferencesFor(WikiReference wiki, DocumentReference userOrGroupDocumentReference) throws AuthorizationException {
    XWikiContext xwikiContext = getXWikiContext();
    XWikiGroupService groupService;
    try {
        groupService = xwikiContext.getWiki().getGroupService(xwikiContext);
    } catch (Exception e) {
        throw new AuthorizationException("Failed to access the group service.", e);
    }
    String currentWiki = xwikiContext.getWikiId();
    Collection<DocumentReference> groupReferences = new HashSet<>();
    try {
        xwikiContext.setWikiId(wiki.getName());
        // We get the groups of the member via the group service but we make sure to not use the group service's
        // cache by calling the method with a limit and an offset.
        // 
        // We do not use the group service's cache because it might not have been refreshed yet (for example, it can
        // happen when the security module is used inside a listener that reacts to the "SaveDocument" event just
        // before the XWikiGroupService listener is called). Because of this race condition, it is not a good idea
        // to have a cache depending on an other cache.
        // 
        // TODO: use a proper component to retrieve the groups of a member without any cache
        final int nb = 1000;
        int i = 0;
        while (groupReferences.addAll(groupService.getAllGroupsReferencesForMember(userOrGroupDocumentReference, nb, i * nb, xwikiContext))) {
            i++;
        }
        return groupReferences;
    } catch (Exception e) {
        throw new AuthorizationException(String.format("Failed to get groups for user or group [%s] in wiki [%s]", userOrGroupDocumentReference, wiki), e);
    } finally {
        xwikiContext.setWikiId(currentWiki);
    }
}
Also used : AuthorizationException(org.xwiki.security.authorization.AuthorizationException) XWikiContext(com.xpn.xwiki.XWikiContext) AuthorizationException(org.xwiki.security.authorization.AuthorizationException) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiGroupService(com.xpn.xwiki.user.api.XWikiGroupService) HashSet(java.util.HashSet)

Example 5 with XWikiGroupService

use of com.xpn.xwiki.user.api.XWikiGroupService in project xwiki-platform by xwiki.

the class DefaultSecurityCacheRulesInvalidatorListener method invalidateGroupMembers.

/**
 * Drop from the cache all members of a given group.
 *
 * @param group The group.
 * @param securityCache Right cache instance to invalidate.
 * @throws org.xwiki.security.authorization.AuthorizationException on error.
 */
public void invalidateGroupMembers(DocumentReference group, SecurityCache securityCache) throws AuthorizationException {
    try {
        XWikiContext xwikiContext = this.xcontextProvider.get();
        XWikiGroupService groupService = xwikiContext.getWiki().getGroupService(xwikiContext);
        String groupName = serializer.serialize(group);
        // The group members inherit the wiki from the group
        // itself, unless the wiki name is explicitly given.
        WikiReference wikiReference = group.getWikiReference();
        final int nb = 100;
        int i = 0;
        Collection<String> memberNames;
        do {
            memberNames = groupService.getAllMembersNamesForGroup(groupName, nb, i * nb, xwikiContext);
            for (String member : memberNames) {
                DocumentReference memberRef = userResolver.resolve(member, wikiReference);
                if (!memberRef.equals(group)) {
                    securityCache.remove(securityReferenceFactory.newUserReference(memberRef));
                }
            }
            i++;
        } while (memberNames.size() == nb);
    } catch (XWikiException e) {
        throw new AuthorizationException("Failed to invalidate group member.", e);
    }
}
Also used : AuthorizationException(org.xwiki.security.authorization.AuthorizationException) XWikiContext(com.xpn.xwiki.XWikiContext) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference) XWikiException(com.xpn.xwiki.XWikiException) XWikiGroupService(com.xpn.xwiki.user.api.XWikiGroupService)

Aggregations

XWikiGroupService (com.xpn.xwiki.user.api.XWikiGroupService)5 XWikiContext (com.xpn.xwiki.XWikiContext)4 DocumentReference (org.xwiki.model.reference.DocumentReference)4 XWikiException (com.xpn.xwiki.XWikiException)2 AuthorizationException (org.xwiki.security.authorization.AuthorizationException)2 XWiki (com.xpn.xwiki.XWiki)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 BaseObject (com.xpn.xwiki.objects.BaseObject)1 XWikiRightNotFoundException (com.xpn.xwiki.user.api.XWikiRightNotFoundException)1 XWikiRightService (com.xpn.xwiki.user.api.XWikiRightService)1 XWikiRightServiceImpl (com.xpn.xwiki.user.impl.xwiki.XWikiRightServiceImpl)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Expectations (org.jmock.Expectations)1 Invocation (org.jmock.api.Invocation)1 CustomAction (org.jmock.lib.action.CustomAction)1 Before (org.junit.Before)1 Test (org.junit.Test)1