Search in sources :

Example 26 with WikiSession

use of org.apache.wiki.WikiSession in project jspwiki by apache.

the class AuthorizationManagerTest method testAuthenticatedSession.

@Test
public void testAuthenticatedSession() throws Exception {
    // Create Alice and her roles
    Principal alice = new WikiPrincipal(Users.ALICE);
    Role it = new Role("IT");
    Role engineering = new Role("Engineering");
    Role finance = new Role("Finance");
    Principal admin = new GroupPrincipal("Admin");
    WikiSession session = WikiSessionTest.containerAuthenticatedSession(m_engine, Users.ALICE, new Principal[] { it, engineering, admin });
    // Create two groups: Alice should be part of group Bar, but not Foo
    Group fooGroup = m_groupMgr.parseGroup("Foo", "", true);
    Group barGroup = m_groupMgr.parseGroup("Bar", "", true);
    barGroup.add(alice);
    m_groupMgr.setGroup(m_session, fooGroup);
    m_groupMgr.setGroup(m_session, barGroup);
    // Test user principal posession: user principals of different
    // types should still be "the same" if their names are equal
    Assert.assertTrue("Alice has Alice", m_auth.hasRoleOrPrincipal(session, new WikiPrincipal(Users.ALICE)));
    Assert.assertTrue("Alice has Alice", m_auth.hasRoleOrPrincipal(session, new TestPrincipal(Users.ALICE)));
    Assert.assertFalse("Alice not has Bob", m_auth.hasRoleOrPrincipal(session, new WikiPrincipal(Users.BOB)));
    Assert.assertFalse("Alice not has Bob", m_auth.hasRoleOrPrincipal(session, new TestPrincipal(Users.BOB)));
    // Built-in role membership
    Assert.assertTrue("Alice in ALL", m_auth.hasRoleOrPrincipal(session, Role.ALL));
    Assert.assertFalse("Alice not in ANONYMOUS", m_auth.hasRoleOrPrincipal(session, Role.ANONYMOUS));
    Assert.assertFalse("Alice not in ASSERTED", m_auth.hasRoleOrPrincipal(session, Role.ASSERTED));
    Assert.assertTrue("Alice in AUTHENTICATED", m_auth.hasRoleOrPrincipal(session, Role.AUTHENTICATED));
    // Custom roles
    Assert.assertTrue("Alice in IT", m_auth.hasRoleOrPrincipal(session, it));
    Assert.assertTrue("Alice in Engineering", m_auth.hasRoleOrPrincipal(session, engineering));
    Assert.assertFalse("Alice not in Finance", m_auth.hasRoleOrPrincipal(session, finance));
    // Group memberships
    Assert.assertFalse("Alice not in Foo", m_auth.hasRoleOrPrincipal(session, fooGroup.getPrincipal()));
    Assert.assertTrue("Alice in Bar", m_auth.hasRoleOrPrincipal(session, barGroup.getPrincipal()));
    // Cleanup
    m_groupMgr.removeGroup("Foo");
    m_groupMgr.removeGroup("Bar");
}
Also used : Role(org.apache.wiki.auth.authorize.Role) WikiSession(org.apache.wiki.WikiSession) Group(org.apache.wiki.auth.authorize.Group) Principal(java.security.Principal) UnresolvedPrincipal(org.apache.wiki.auth.acl.UnresolvedPrincipal) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 27 with WikiSession

use of org.apache.wiki.WikiSession in project jspwiki by apache.

the class AuthorizationManagerTest method testAdminView.

@Test
public void testAdminView() throws Exception {
    m_engine.saveText("TestDefaultPage", "Foo [{ALLOW view FooBar}]");
    Principal admin = new GroupPrincipal("Admin");
    WikiSession session = WikiSessionTest.containerAuthenticatedSession(m_engine, Users.ALICE, new Principal[] { admin });
    Assert.assertTrue("Alice has AllPermission", m_auth.checkPermission(session, new AllPermission(m_engine.getApplicationName())));
    Assert.assertTrue("Alice cannot read", m_auth.checkPermission(session, new PagePermission("TestDefaultPage", "view")));
}
Also used : WikiSession(org.apache.wiki.WikiSession) AllPermission(org.apache.wiki.auth.permissions.AllPermission) Principal(java.security.Principal) UnresolvedPrincipal(org.apache.wiki.auth.acl.UnresolvedPrincipal) PagePermission(org.apache.wiki.auth.permissions.PagePermission) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 28 with WikiSession

use of org.apache.wiki.WikiSession in project jspwiki by apache.

the class AuthorizationManagerTest method testInheritedPermissions.

@Test
public void testInheritedPermissions() throws Exception {
    // Create test page & attachment
    String src = "[{ALLOW edit Alice}] ";
    m_engine.saveText("Test", src);
    File f = m_engine.makeAttachmentFile();
    Attachment att = new Attachment(m_engine, "Test", "test1.txt");
    att.setAuthor("FirstPost");
    m_engine.getAttachmentManager().storeAttachment(att, f);
    Attachment p = (Attachment) m_engine.getPage("Test/test1.txt");
    Permission view = PermissionFactory.getPagePermission(p, "view");
    Permission edit = PermissionFactory.getPagePermission(p, "edit");
    // Create authenticated session with user 'Alice', who can read & edit (in ACL)
    WikiSession session;
    session = WikiSessionTest.authenticatedSession(m_engine, Users.ALICE, Users.ALICE_PASS);
    Assert.assertTrue("Alice view Test/test1.txt", m_auth.checkPermission(session, view));
    Assert.assertTrue("Alice edit Test/test1.txt", m_auth.checkPermission(session, edit));
    // Create authenticated session with user 'Bob', who can't read or edit (not in ACL)
    session = WikiSessionTest.authenticatedSession(m_engine, Users.BOB, Users.BOB_PASS);
    Assert.assertFalse("Bob !view Test/test1.txt", m_auth.checkPermission(session, view));
    Assert.assertFalse("Bob !edit Test/test1.txt", m_auth.checkPermission(session, edit));
    // Delete test page & attachment
    m_engine.getAttachmentManager().deleteAttachment(att);
    m_engine.deletePage("Test");
}
Also used : WikiSession(org.apache.wiki.WikiSession) WikiPermission(org.apache.wiki.auth.permissions.WikiPermission) PagePermission(org.apache.wiki.auth.permissions.PagePermission) AllPermission(org.apache.wiki.auth.permissions.AllPermission) Permission(java.security.Permission) Attachment(org.apache.wiki.attachment.Attachment) File(java.io.File) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 29 with WikiSession

use of org.apache.wiki.WikiSession in project jspwiki by apache.

the class GroupManagerTest method testGroupMembership.

@Test
public void testGroupMembership() throws Exception {
    WikiSession s;
    // Anonymous; should belong to NO groups
    s = WikiSessionTest.anonymousSession(m_engine);
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test2")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test3")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("NonExistant")));
    // Alice is asserted; should belong to NO groups
    s = WikiSessionTest.assertedSession(m_engine, Users.ALICE);
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test2")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test3")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("NonExistant")));
    // Alice is authenticated; should belong to Test
    s = WikiSessionTest.authenticatedSession(m_engine, Users.ALICE, Users.ALICE_PASS);
    Assert.assertTrue(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test2")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test3")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("NonExistant")));
    // Bob is authenticated; should belong to Test & Test2
    s = WikiSessionTest.authenticatedSession(m_engine, Users.BOB, Users.BOB_PASS);
    Assert.assertTrue(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test")));
    Assert.assertTrue(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test2")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test3")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("NonExistant")));
    // Charlie is authenticated; should belong to Test
    s = WikiSessionTest.authenticatedSession(m_engine, Users.CHARLIE, Users.CHARLIE_PASS);
    Assert.assertTrue(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test2")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test3")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("NonExistant")));
    // Fred is authenticated; should belong to Test3
    s = WikiSessionTest.authenticatedSession(m_engine, Users.FRED, Users.FRED_PASS);
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test2")));
    Assert.assertTrue(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test3")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("NonExistant")));
    // Nobody loves Biff!
    s = WikiSessionTest.authenticatedSession(m_engine, Users.BIFF, Users.BIFF_PASS);
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test2")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("Test3")));
    Assert.assertFalse(m_groupMgr.isUserInRole(s, new GroupPrincipal("NonExistant")));
}
Also used : WikiSession(org.apache.wiki.WikiSession) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 30 with WikiSession

use of org.apache.wiki.WikiSession in project jspwiki by apache.

the class UserManagerTest method testSetCollidingUserProfile.

@Test
public void testSetCollidingUserProfile() throws Exception {
    // First, count the number of users in the db now.
    int oldUserCount = m_db.getWikiNames().length;
    // Create a new user with random name
    WikiSession session = m_engine.guestSession();
    String loginName = "TestUser" + String.valueOf(System.currentTimeMillis());
    UserProfile profile = m_db.newProfile();
    profile.setEmail("jspwiki.tests@mailinator.com");
    profile.setLoginName(loginName);
    profile.setFullname("FullName" + loginName);
    profile.setPassword("password");
    // Set the login name to collide with Janne's: should prohibit saving
    profile.setLoginName("janne");
    try {
        m_mgr.setUserProfile(session, profile);
        Assert.fail("UserManager allowed saving of user with login name 'janne', but it shouldn't have.");
    } catch (DuplicateUserException e) {
        // Good! That's what we expected; reset for next test
        profile.setLoginName(loginName);
    }
    // Set the login name to collide with Janne's: should prohibit saving
    profile.setFullname("Janne Jalkanen");
    try {
        m_mgr.setUserProfile(session, profile);
        Assert.fail("UserManager allowed saving of user with login name 'janne', but it shouldn't have.");
    } catch (DuplicateUserException e) {
    // Good! That's what we expected
    }
    // There shouldn't have been any users added
    Assert.assertEquals(oldUserCount, m_db.getWikiNames().length);
}
Also used : WikiSession(org.apache.wiki.WikiSession) UserProfile(org.apache.wiki.auth.user.UserProfile) DuplicateUserException(org.apache.wiki.auth.user.DuplicateUserException) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Aggregations

WikiSession (org.apache.wiki.WikiSession)40 WikiSessionTest (org.apache.wiki.WikiSessionTest)23 Test (org.junit.Test)23 Principal (java.security.Principal)15 UserProfile (org.apache.wiki.auth.user.UserProfile)9 AllPermission (org.apache.wiki.auth.permissions.AllPermission)8 PagePermission (org.apache.wiki.auth.permissions.PagePermission)8 Group (org.apache.wiki.auth.authorize.Group)7 Permission (java.security.Permission)6 UnresolvedPrincipal (org.apache.wiki.auth.acl.UnresolvedPrincipal)6 WikiPermission (org.apache.wiki.auth.permissions.WikiPermission)6 WikiPage (org.apache.wiki.WikiPage)5 Role (org.apache.wiki.auth.authorize.Role)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 Collection (java.util.Collection)3 HttpSession (javax.servlet.http.HttpSession)3 ProviderException (org.apache.wiki.api.exceptions.ProviderException)3 Attachment (org.apache.wiki.attachment.Attachment)3 GroupPrincipal (org.apache.wiki.auth.GroupPrincipal)3 File (java.io.File)2