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;
}
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);
}
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);
}
}
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());
}
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()));
}
Aggregations