Search in sources :

Example 6 with XWikiUser

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

the class XWikiRightServiceImplTest method testDeniesAccessLevelForCreateIfDocumentDeniesEdit.

// This is currently a proof-of-behavior test to show that if a document prevents you from editing
// it, calling hasAccessLevel('create') on that document will also fail.
// Changing this behavior is proposed here: http://lists.xwiki.org/pipermail/devs/2013-March/053802.html
// See also: https://jira.xwiki.org/browse/XWIKI-8892
public void testDeniesAccessLevelForCreateIfDocumentDeniesEdit() throws Exception {
    getContext().setWikiId(this.user.getWikiName());
    final XWikiDocument doc = new XWikiDocument(new DocumentReference(this.user.getWikiName(), "Space", "Page"));
    // Set the creator to be the user we test against since creator should get delete rights
    BaseObject xo = new BaseObject();
    xo.setClassName("XWiki.XWikiRights");
    xo.setStringValue("levels", "edit");
    xo.setStringValue("users", user.getFullName());
    xo.setIntValue("allow", 0);
    doc.addXObject(xo);
    DocumentReference dr = new DocumentReference(this.user.getWikiName(), "XWiki", "XWikiPreferences");
    this.mockXWiki.stubs().method("getDocument").with(isA(EntityReference.class), ANYTHING).will(returnValue(new XWikiDocument(new DocumentReference(dr))));
    this.mockXWiki.stubs().method("getDocument").with(eq(doc.getPrefixedFullName()), ANYTHING).will(returnValue(doc));
    this.mockXWiki.stubs().method("getXWikiPreference").with(eq("authenticate_edit"), ANYTHING, ANYTHING).will(returnValue(""));
    this.mockXWiki.stubs().method("getXWikiPreferenceAsInt").with(eq("authenticate_edit"), ANYTHING, ANYTHING).will(returnValue(0));
    this.mockXWiki.stubs().method("getSpacePreference").with(eq("authenticate_edit"), ANYTHING, ANYTHING).will(returnValue(""));
    this.mockXWiki.stubs().method("getSpacePreferenceAsInt").with(eq("authenticate_edit"), ANYTHING, ANYTHING).will(returnValue(0));
    this.mockXWiki.stubs().method("checkAuth").with(ANYTHING).will(returnValue(new XWikiUser(this.user.getFullName())));
    this.mockXWiki.stubs().method("getRightService").will(returnValue(this.rightService));
    assertFalse("Should not have edit permission on document if it is denied at a document level", this.rightService.checkAccess("edit", doc, getContext()));
    assertFalse("Should not have create permission on document if it is denied at a document level", this.rightService.checkAccess("create", doc, getContext()));
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiUser(com.xpn.xwiki.user.api.XWikiUser) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 7 with XWikiUser

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

the class DefaultXWikiContextInitializer method authenticate.

private void authenticate(XWikiContext xcontext) throws XWikiException {
    // By default set guest as the user that is sending the request.
    xcontext.setUserReference(null);
    XWikiUser xwikiUser = xcontext.getWiki().checkAuth(xcontext);
    if (xwikiUser != null) {
        SpaceReference defaultUserSpace = new SpaceReference(XWiki.SYSTEM_SPACE, new WikiReference(xcontext.getWikiId()));
        DocumentReference userReference = this.explicitResolver.resolve(xwikiUser.getUser(), defaultUserSpace);
        xcontext.setUserReference(XWikiRightService.GUEST_USER.equals(userReference.getName()) ? null : userReference);
        this.logger.debug("Authenticated as [{}].", xwikiUser.getUser());
    }
}
Also used : XWikiUser(com.xpn.xwiki.user.api.XWikiUser) SpaceReference(org.xwiki.model.reference.SpaceReference) WikiReference(org.xwiki.model.reference.WikiReference) DocumentReference(org.xwiki.model.reference.DocumentReference)

Example 8 with XWikiUser

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

the class UserTest method testGetEmail.

public void testGetEmail() throws Exception {
    Mock mockXWiki = mock(XWiki.class);
    getContext().setWiki((XWiki) mockXWiki.proxy());
    XWikiDocument doc = new XWikiDocument(new DocumentReference("xwiki", "XWiki", "Admin"));
    BaseClass userClass = new BaseClass();
    userClass.addTextField("email", "email address", 20);
    mockXWiki.stubs().method("getXClass").will(returnValue(userClass));
    BaseObject userObj = doc.newXObject(new DocumentReference("xwiki", "XWiki", "XWikiUsers"), getContext());
    userObj.setStringValue("email", "admin@mail.com");
    mockXWiki.stubs().method("getDocument").will(returnValue(doc));
    User u = new User(null, null);
    assertNull(u.getEmail());
    XWikiUser xu = new XWikiUser("XWiki.Admin");
    u = new User(xu, getContext());
    assertEquals("admin@mail.com", u.getEmail());
}
Also used : XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) XWikiUser(com.xpn.xwiki.user.api.XWikiUser) XWikiUser(com.xpn.xwiki.user.api.XWikiUser) BaseClass(com.xpn.xwiki.objects.classes.BaseClass) Mock(org.jmock.Mock) DocumentReference(org.xwiki.model.reference.DocumentReference) BaseObject(com.xpn.xwiki.objects.BaseObject)

Example 9 with XWikiUser

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

the class XWikiContextTest method testAnonymousUser.

@Test
public void testAnonymousUser() {
    getContext().setMainXWiki("wiki");
    getContext().setWikiId("wiki");
    getContext().setUserReference(null);
    Assert.assertNull(getContext().getUserReference());
    Assert.assertEquals("XWiki.XWikiGuest", getContext().getUser());
    Assert.assertEquals("XWiki.XWikiGuest", getContext().getLocalUser());
    Assert.assertNull(getContext().getXWikiUser());
    getContext().setWikiId("wiki2");
    Assert.assertNull(getContext().getUserReference());
    Assert.assertEquals("XWiki.XWikiGuest", getContext().getUser());
    Assert.assertEquals("XWiki.XWikiGuest", getContext().getLocalUser());
    Assert.assertNull(getContext().getXWikiUser());
    getContext().setUser(XWikiRightService.GUEST_USER_FULLNAME);
    Assert.assertEquals(new XWikiUser(XWikiRightService.GUEST_USER_FULLNAME), getContext().getXWikiUser());
    Assert.assertNull(getContext().getUserReference());
    getContext().setUser(XWikiRightService.GUEST_USER);
    Assert.assertEquals(new XWikiUser(XWikiRightService.GUEST_USER), getContext().getXWikiUser());
    Assert.assertNull(getContext().getUserReference());
}
Also used : XWikiUser(com.xpn.xwiki.user.api.XWikiUser) Test(org.junit.Test)

Example 10 with XWikiUser

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

the class UserTest method testIsUserInGroupDoesNotThrowNPE.

/**
 * Checks that XWIKI-2040 remains fixed.
 */
public void testIsUserInGroupDoesNotThrowNPE() {
    User u = new User(null, null);
    assertFalse(u.isUserInGroup("XWiki.InexistentGroupName"));
    XWikiUser xu = new XWikiUser(null);
    u = new User(xu, null);
    assertFalse(u.isUserInGroup("XWiki.InexistentGroupName"));
    XWikiContext c = new XWikiContext();
    u = new User(xu, c);
    assertFalse(u.isUserInGroup("XWiki.InexistentGroupName"));
}
Also used : XWikiUser(com.xpn.xwiki.user.api.XWikiUser) XWikiUser(com.xpn.xwiki.user.api.XWikiUser) XWikiContext(com.xpn.xwiki.XWikiContext)

Aggregations

XWikiUser (com.xpn.xwiki.user.api.XWikiUser)14 XWikiException (com.xpn.xwiki.XWikiException)6 DocumentReference (org.xwiki.model.reference.DocumentReference)6 XWikiContext (com.xpn.xwiki.XWikiContext)3 WikiReference (org.xwiki.model.reference.WikiReference)3 XWiki (com.xpn.xwiki.XWiki)2 User (com.xpn.xwiki.api.User)2 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)2 BaseObject (com.xpn.xwiki.objects.BaseObject)2 IOException (java.io.IOException)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 SecurityRequestWrapper (org.securityfilter.filter.SecurityRequestWrapper)2 SpaceReference (org.xwiki.model.reference.SpaceReference)2 BaseClass (com.xpn.xwiki.objects.classes.BaseClass)1 XWikiRightNotFoundException (com.xpn.xwiki.user.api.XWikiRightNotFoundException)1 XWikiServletContext (com.xpn.xwiki.web.XWikiServletContext)1 XWikiServletRequest (com.xpn.xwiki.web.XWikiServletRequest)1 XWikiServletResponse (com.xpn.xwiki.web.XWikiServletResponse)1 Principal (java.security.Principal)1