Search in sources :

Example 91 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class RightsManager method removeUserOrGroupFromRights.

/**
 * Remove all references to provided user or group from provided rights document.
 *
 * @param rightsDocument the document containing the rights preferences.
 * @param userOrGroupWiki the name of the wiki of the use or group.
 * @param userOrGroupSpace the name of the space of the use or group.
 * @param userOrGroupName the name of the use or group.
 * @param user indicate if it is a user or a group.
 * @param global indicate if user or group is removed from global or document rights.
 * @param context the XWiki context.
 * @return true if user or group has been found and removed.
 */
public boolean removeUserOrGroupFromRights(XWikiDocument rightsDocument, String userOrGroupWiki, String userOrGroupSpace, String userOrGroupName, boolean user, boolean global, XWikiContext context) {
    boolean needUpdate = false;
    EntityReference rightClassReference = global ? XWikiRightServiceImpl.GLOBALRIGHTCLASS_REFERENCE : XWikiRightServiceImpl.RIGHTCLASS_REFERENCE;
    List<BaseObject> rightObjects = rightsDocument.getXObjects(rightClassReference);
    if (rightObjects != null) {
        for (BaseObject bobj : rightObjects) {
            if (bobj == null) {
                continue;
            }
            needUpdate |= removeUserOrGroupFromRight(bobj, userOrGroupWiki, userOrGroupSpace, userOrGroupName, user, context);
            if (needUpdate && bobj.getStringValue(RIGHTSFIELD_USERS).trim().length() == 0 && bobj.getStringValue(RIGHTSFIELD_GROUPS).trim().length() == 0) {
                rightsDocument.removeXObject(bobj);
            }
        }
    }
    return needUpdate;
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 92 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class RightsManager method fillLevelTreeMap.

/**
 * Fill the {@link LevelTree} {@link Map}.
 *
 * @param rightsMap the {@link LevelTree} {@link Map} to fill.
 * @param preferences the document containing rights preferences.
 * @param levelsToMatch the levels names to check ("view", "edit", etc.).
 * @param global indicate it is global rights (wiki or space) or document rights.
 * @param direct if true fill the {@link LevelTree#direct} field, otherwise fill the {@link LevelTree#inherited}.
 * @param context the XWiki context.
 * @throws XWikiException error when browsing rights preferences.
 */
private void fillLevelTreeMap(Map<String, LevelTree> rightsMap, XWikiDocument preferences, List<String> levelsToMatch, boolean global, boolean direct, XWikiContext context) throws XWikiException {
    List<String> levelInherited = null;
    if (levelsToMatch == null) {
        levelInherited = new ArrayList<String>();
    }
    if (!preferences.isNew()) {
        EntityReference rightClassReference = global ? XWikiRightServiceImpl.GLOBALRIGHTCLASS_REFERENCE : XWikiRightServiceImpl.RIGHTCLASS_REFERENCE;
        List<BaseObject> rightObjects = preferences.getXObjects(rightClassReference);
        if (rightObjects != null) {
            for (BaseObject bobj : rightObjects) {
                fillLevelTreeMap(rightsMap, levelInherited, bobj, levelsToMatch, direct, context);
            }
        }
    }
    fillLevelTreeMapInherited(rightsMap, levelInherited, preferences, levelsToMatch, global, context);
}
Also used : EntityReference(org.xwiki.model.reference.EntityReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 93 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class RightsManager method removeDirectRights.

/**
 * Remove "direct" rights for wiki, space or document. This means that after that inherited right will be used.
 *
 * @param spaceOrPage the space of page where to get XWikiRights. If null get wiki rights.
 * @param levelNames the levels names to check ("view", "edit", etc.).
 * @param comment the comment to use when saving preferences document.
 * @param context the XWiki context.
 * @throws XWikiException error when browsing rights.
 */
public void removeDirectRights(String spaceOrPage, List<String> levelNames, String comment, XWikiContext context) throws XWikiException {
    XWikiDocument preferences = getXWikiPreferencesDoc(spaceOrPage, context);
    boolean global = isGlobal(preferences, spaceOrPage);
    EntityReference rightClassReference = global ? XWikiRightServiceImpl.GLOBALRIGHTCLASS_REFERENCE : XWikiRightServiceImpl.RIGHTCLASS_REFERENCE;
    List<BaseObject> rightObjects = preferences.getXObjects(rightClassReference);
    if (rightObjects != null && !rightObjects.isEmpty()) {
        preferences.removeXObjects(rightClassReference);
        context.getWiki().saveDocument(preferences, comment, context);
    }
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) EntityReference(org.xwiki.model.reference.EntityReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 94 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class XWikiAuthServiceImplTest method testLogintoVirtualXwikiWithWikiPrefixUsername.

/**
 * Test that user is authenticated with a global account when a local one with the same name exists and the username
 * contains a wiki prefix.
 */
@Test
public void testLogintoVirtualXwikiWithWikiPrefixUsername() throws Exception {
    // Setup simple user profile documents
    XWikiDocument userDocLocal = new XWikiDocument(new DocumentReference(this.oldcore.getXWikiContext().getMainXWiki(), "XWiki", "Admin"));
    BaseObject mockUserObj = userDocLocal.newXObject(new LocalDocumentReference("XWiki", "XWikiUsers"), this.oldcore.getXWikiContext());
    mockUserObj.setStringValue("password", "admin");
    // Save the user
    this.oldcore.getSpyXWiki().saveDocument(userDocLocal, this.oldcore.getXWikiContext());
    // Run the test: Using XWiki.Admin should correctly authenticate the Admin user
    Principal principalLocal = this.authService.authenticate("XWiki.Admin", "admin", this.oldcore.getXWikiContext());
    assertNotNull(principalLocal);
    assertEquals("XWiki.Admin", principalLocal.getName());
    // Set the database name to local.
    this.oldcore.getXWikiContext().setWikiId("local");
    // Finally run the test: Using xwiki:Xwiki.Admin should correctly authenticate the Admin user
    Principal principalVirtual = this.authService.authenticate("xwiki:XWiki.Admin", "admin", this.oldcore.getXWikiContext());
    assertNotNull(principalVirtual);
    assertEquals("xwiki:XWiki.Admin", principalVirtual.getName());
}
Also used : LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) LocalDocumentReference(org.xwiki.model.reference.LocalDocumentReference) Principal(java.security.Principal) BaseObject(com.xpn.xwiki.objects.BaseObject) Test(org.junit.Test)

Example 95 with BaseObject

use of com.xpn.xwiki.objects.BaseObject in project xwiki-platform by xwiki.

the class XWikiRightServiceImplTest method testHasAccessLevelWhithOnlyPageAsReference.

public void testHasAccessLevelWhithOnlyPageAsReference() throws XWikiException {
    final XWikiDocument doc = new XWikiDocument(new DocumentReference("wiki", "Space", "Page"));
    final XWikiDocument preferences = new XWikiDocument(new DocumentReference(doc.getWikiName(), doc.getSpaceName(), "WebPreferences"));
    BaseObject preferencesObject = new BaseObject();
    preferencesObject.setClassName("XWiki.XWikiGlobalRights");
    preferencesObject.setStringValue("levels", "view");
    preferencesObject.setIntValue("allow", 1);
    preferences.addXObject(preferencesObject);
    preferences.setNew(false);
    this.mockXWiki.stubs().method("getDocument").with(eq(preferences.getSpaceName()), eq(preferences.getPageName()), ANYTHING).will(returnValue(preferences));
    this.mockXWiki.stubs().method("getDocument").with(eq(XWIKIPREFERENCES_REFERENCE), ANYTHING).will(returnValue(new XWikiDocument(new DocumentReference(getContext().getWikiId(), "XWiki", "XWikiPreferences"))));
    this.mockXWiki.stubs().method("getDocument").with(eq(doc.getPrefixedFullName()), ANYTHING).will(returnValue(doc));
    getContext().setWikiId("wiki");
    getContext().setDoc(doc);
    assertFalse("Failed to check right with only page name", this.rightService.hasAccessLevel("view", this.user.getPageName(), doc.getPageName(), true, getContext()));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Aggregations

BaseObject (com.xpn.xwiki.objects.BaseObject)484 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)309 DocumentReference (org.xwiki.model.reference.DocumentReference)225 Test (org.junit.Test)137 XWikiException (com.xpn.xwiki.XWikiException)102 XWikiContext (com.xpn.xwiki.XWikiContext)99 ArrayList (java.util.ArrayList)92 LocalDocumentReference (org.xwiki.model.reference.LocalDocumentReference)69 EntityReference (org.xwiki.model.reference.EntityReference)42 XWiki (com.xpn.xwiki.XWiki)41 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)40 List (java.util.List)30 Date (java.util.Date)24 BaseProperty (com.xpn.xwiki.objects.BaseProperty)23 Document (com.xpn.xwiki.api.Document)22 HashMap (java.util.HashMap)21 QueryException (org.xwiki.query.QueryException)18 AbstractComponentTest (com.celements.common.test.AbstractComponentTest)16 IOException (java.io.IOException)16 Vector (java.util.Vector)16