Search in sources :

Example 1 with PageManager

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

the class CheckLockTag method doWikiStartTag.

/**
 *  {@inheritDoc}
 */
@Override
public final int doWikiStartTag() throws IOException, ProviderException {
    WikiEngine engine = m_wikiContext.getEngine();
    WikiPage page = m_wikiContext.getPage();
    if (page != null) {
        PageManager mgr = engine.getPageManager();
        PageLock lock = mgr.getCurrentLock(page);
        HttpSession session = pageContext.getSession();
        PageLock userLock = (PageLock) session.getAttribute("lock-" + page.getName());
        if ((lock != null && m_mode == LockState.LOCKED && lock != userLock) || (lock != null && m_mode == LockState.OWNED && lock == userLock) || (lock == null && m_mode == LockState.NOTLOCKED)) {
            String tid = getId();
            if (tid != null && lock != null) {
                pageContext.setAttribute(tid, lock);
            }
            return EVAL_BODY_INCLUDE;
        }
    }
    return SKIP_BODY;
}
Also used : PageManager(org.apache.wiki.PageManager) HttpSession(javax.servlet.http.HttpSession) WikiPage(org.apache.wiki.WikiPage) PageLock(org.apache.wiki.PageLock) WikiEngine(org.apache.wiki.WikiEngine)

Example 2 with PageManager

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

the class DefaultAclManager method setPermissions.

/**
 * Sets the access control list for the page and persists it by prepending
 * it to the wiki page markup and saving the page. When this method is
 * called, all other ACL markup in the page is removed. This method will forcibly
 * expire locks on the wiki page if they exist. Any ProviderExceptions will be
 * re-thrown as WikiSecurityExceptions.
 *
 * @param page the wiki page
 * @param acl  the access control list
 * @throws WikiSecurityException of the Acl cannot be set
 * @since 2.5
 */
public void setPermissions(WikiPage page, Acl acl) throws WikiSecurityException {
    PageManager pageManager = m_engine.getPageManager();
    // Forcibly expire any page locks
    PageLock lock = pageManager.getCurrentLock(page);
    if (lock != null) {
        pageManager.unlockPage(lock);
    }
    // Remove all of the existing ACLs.
    String pageText = m_engine.getPureText(page);
    Matcher matcher = DefaultAclManager.ACL_PATTERN.matcher(pageText);
    String cleansedText = matcher.replaceAll("");
    String newText = DefaultAclManager.printAcl(page.getAcl()) + cleansedText;
    try {
        pageManager.putPageText(page, newText);
    } catch (ProviderException e) {
        throw new WikiSecurityException("Could not set Acl. Reason: ProviderExcpetion " + e.getMessage(), e);
    }
}
Also used : WikiSecurityException(org.apache.wiki.auth.WikiSecurityException) PageManager(org.apache.wiki.PageManager) Matcher(java.util.regex.Matcher) ProviderException(org.apache.wiki.api.exceptions.ProviderException) PageLock(org.apache.wiki.PageLock)

Example 3 with PageManager

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

the class UserManagerTest method testSetRenamedUserProfile.

@Test
public void testSetRenamedUserProfile() throws Exception {
    // First, count the number of users, groups, and pages
    int oldUserCount = m_db.getWikiNames().length;
    GroupManager groupManager = m_engine.getGroupManager();
    PageManager pageManager = m_engine.getPageManager();
    AuthorizationManager authManager = m_engine.getAuthorizationManager();
    int oldGroupCount = groupManager.getRoles().length;
    int oldPageCount = pageManager.getTotalPageCount();
    // Setup Step 1: create a new user with random name
    WikiSession session = m_engine.guestSession();
    long now = System.currentTimeMillis();
    String oldLogin = "TestLogin" + now;
    String oldName = "Test User " + now;
    String newLogin = "RenamedLogin" + now;
    String newName = "Renamed User " + now;
    UserProfile profile = m_db.newProfile();
    profile.setEmail("jspwiki.tests@mailinator.com");
    profile.setLoginName(oldLogin);
    profile.setFullname(oldName);
    profile.setPassword("password");
    m_mgr.setUserProfile(session, profile);
    // 1a. Make sure the profile saved successfully and that we're logged in
    profile = m_mgr.getUserProfile(session);
    Assert.assertEquals(oldLogin, profile.getLoginName());
    Assert.assertEquals(oldName, profile.getFullname());
    Assert.assertEquals(oldUserCount + 1, m_db.getWikiNames().length);
    Assert.assertTrue(session.isAuthenticated());
    // Setup Step 2: create a new group with our test user in it
    Group group = groupManager.parseGroup(m_groupName, "Alice \n Bob \n Charlie \n " + oldLogin + "\n" + oldName, true);
    groupManager.setGroup(session, group);
    // 2a. Make sure the group is created with the user in it, and the role is added to the Subject
    Assert.assertEquals(oldGroupCount + 1, groupManager.getRoles().length);
    Assert.assertTrue(group.isMember(new WikiPrincipal(oldLogin)));
    Assert.assertTrue(group.isMember(new WikiPrincipal(oldName)));
    Assert.assertFalse(group.isMember(new WikiPrincipal(newLogin)));
    Assert.assertFalse(group.isMember(new WikiPrincipal(newName)));
    Assert.assertTrue(groupManager.isUserInRole(session, group.getPrincipal()));
    // Setup Step 3: create a new page with our test user in the ACL
    String pageName = "TestPage" + now;
    m_engine.saveText(pageName, "Test text. [{ALLOW view " + oldName + ", " + oldLogin + ", Alice}] More text.");
    // 3a. Make sure the page got saved, and that ONLY our test user has permission to read it.
    WikiPage p = m_engine.getPage(pageName);
    Assert.assertEquals(oldPageCount + 1, pageManager.getTotalPageCount());
    Assert.assertNotNull(p.getAcl().getEntry(new WikiPrincipal(oldLogin)));
    Assert.assertNotNull(p.getAcl().getEntry(new WikiPrincipal(oldName)));
    Assert.assertNull(p.getAcl().getEntry(new WikiPrincipal(newLogin)));
    Assert.assertNull(p.getAcl().getEntry(new WikiPrincipal(newName)));
    Assert.assertTrue("Test User view page", authManager.checkPermission(session, PermissionFactory.getPagePermission(p, "view")));
    WikiSession bobSession = WikiSessionTest.authenticatedSession(m_engine, Users.BOB, Users.BOB_PASS);
    Assert.assertFalse("Bob !view page", authManager.checkPermission(bobSession, PermissionFactory.getPagePermission(p, "view")));
    // Setup Step 4: change the user name in the profile and see what happens
    profile = m_db.newProfile();
    profile.setEmail("jspwiki.tests@mailinator.com");
    profile.setLoginName(oldLogin);
    profile.setFullname(newName);
    profile.setPassword("password");
    m_mgr.setUserProfile(session, profile);
    // Test 1: the wiki session should have the new wiki name in Subject
    Principal[] principals = session.getPrincipals();
    Assert.assertTrue(ArrayUtils.contains(principals, new WikiPrincipal(oldLogin)));
    Assert.assertFalse(ArrayUtils.contains(principals, new WikiPrincipal(oldName)));
    Assert.assertFalse(ArrayUtils.contains(principals, new WikiPrincipal(newLogin)));
    Assert.assertTrue(ArrayUtils.contains(principals, new WikiPrincipal(newName)));
    // Test 2: our group should not contain the old name OR login name any more
    // (the full name is always used)
    group = groupManager.getGroup(m_groupName);
    Assert.assertFalse(group.isMember(new WikiPrincipal(oldLogin)));
    Assert.assertFalse(group.isMember(new WikiPrincipal(oldName)));
    Assert.assertFalse(group.isMember(new WikiPrincipal(newLogin)));
    Assert.assertTrue(group.isMember(new WikiPrincipal(newName)));
    // Test 3: our page should not contain the old wiki name OR login name
    // in the ACL any more (the full name is always used)
    p = m_engine.getPage(pageName);
    Assert.assertNull(p.getAcl().getEntry(new WikiPrincipal(oldLogin)));
    Assert.assertNull(p.getAcl().getEntry(new WikiPrincipal(oldName)));
    Assert.assertNull(p.getAcl().getEntry(new WikiPrincipal(newLogin)));
    Assert.assertNotNull(p.getAcl().getEntry(new WikiPrincipal(newName)));
    Assert.assertTrue("Test User view page", authManager.checkPermission(session, PermissionFactory.getPagePermission(p, "view")));
    Assert.assertFalse("Bob !view page", authManager.checkPermission(bobSession, PermissionFactory.getPagePermission(p, "view")));
    // Test 4: our page text should have been re-written
    // (The new full name should be in the ACL, but the login name should have been removed)
    String expectedText = "[{ALLOW view Alice," + newName + "}]\nTest text.  More text.\r\n";
    String actualText = m_engine.getText(pageName);
    Assert.assertEquals(expectedText, actualText);
    // Remove our test page
    m_engine.deletePage(pageName);
    // Setup Step 6: re-create the group with our old test user names in it
    group = groupManager.parseGroup(m_groupName, "Alice \n Bob \n Charlie \n " + oldLogin + "\n" + oldName, true);
    groupManager.setGroup(session, group);
    // Setup Step 7: Save a new page with the old login/wiki names in the ACL again
    // The test user should still be able to see the page (because the login name matches...)
    pageName = "TestPage2" + now;
    m_engine.saveText(pageName, "More test text. [{ALLOW view " + oldName + ", " + oldLogin + ", Alice}] More text.");
    p = m_engine.getPage(pageName);
    Assert.assertEquals(oldPageCount + 1, pageManager.getTotalPageCount());
    Assert.assertNotNull(p.getAcl().getEntry(new WikiPrincipal(oldLogin)));
    Assert.assertNotNull(p.getAcl().getEntry(new WikiPrincipal(oldName)));
    Assert.assertNull(p.getAcl().getEntry(new WikiPrincipal(newLogin)));
    Assert.assertNull(p.getAcl().getEntry(new WikiPrincipal(newName)));
    Assert.assertTrue("Test User view page", authManager.checkPermission(session, PermissionFactory.getPagePermission(p, "view")));
    Assert.assertFalse("Bob !view page", authManager.checkPermission(bobSession, PermissionFactory.getPagePermission(p, "view")));
    // Setup Step 8: re-save the profile with the new login name
    profile = m_db.newProfile();
    profile.setEmail("jspwiki.tests@mailinator.com");
    profile.setLoginName(newLogin);
    profile.setFullname(oldName);
    profile.setPassword("password");
    m_mgr.setUserProfile(session, profile);
    // Test 5: the wiki session should have the new login name in Subject
    principals = session.getPrincipals();
    Assert.assertFalse(ArrayUtils.contains(principals, new WikiPrincipal(oldLogin)));
    Assert.assertTrue(ArrayUtils.contains(principals, new WikiPrincipal(oldName)));
    Assert.assertTrue(ArrayUtils.contains(principals, new WikiPrincipal(newLogin)));
    Assert.assertFalse(ArrayUtils.contains(principals, new WikiPrincipal(newName)));
    // Test 6: our group should not contain the old name OR login name any more
    // (the full name is always used)
    group = groupManager.getGroup(m_groupName);
    Assert.assertFalse(group.isMember(new WikiPrincipal(oldLogin)));
    Assert.assertTrue(group.isMember(new WikiPrincipal(oldName)));
    Assert.assertFalse(group.isMember(new WikiPrincipal(newLogin)));
    Assert.assertFalse(group.isMember(new WikiPrincipal(newName)));
    // Test 7: our page should not contain the old wiki name OR login name
    // in the ACL any more (the full name is always used)
    p = m_engine.getPage(pageName);
    Assert.assertNull(p.getAcl().getEntry(new WikiPrincipal(oldLogin)));
    Assert.assertNotNull(p.getAcl().getEntry(new WikiPrincipal(oldName)));
    Assert.assertNull(p.getAcl().getEntry(new WikiPrincipal(newLogin)));
    Assert.assertNull(p.getAcl().getEntry(new WikiPrincipal(newName)));
    Assert.assertTrue("Test User view page", authManager.checkPermission(session, PermissionFactory.getPagePermission(p, "view")));
    Assert.assertFalse("Bob !view page", authManager.checkPermission(bobSession, PermissionFactory.getPagePermission(p, "view")));
    // Test 8: our page text should have been re-written
    // (The new full name should be in the ACL, but the login name should have been removed)
    expectedText = "[{ALLOW view Alice," + oldName + "}]\nMore test text.  More text.\r\n";
    actualText = m_engine.getText(pageName);
    Assert.assertEquals(expectedText, actualText);
    // CLEANUP: delete the profile; user and page; should be back to old counts
    m_db.deleteByLoginName(newLogin);
    Assert.assertEquals(oldUserCount, m_db.getWikiNames().length);
    groupManager.removeGroup(group.getName());
    Assert.assertEquals(oldGroupCount, groupManager.getRoles().length);
    m_engine.deletePage(pageName);
    Assert.assertEquals(oldPageCount, pageManager.getTotalPageCount());
}
Also used : Group(org.apache.wiki.auth.authorize.Group) UserProfile(org.apache.wiki.auth.user.UserProfile) WikiPage(org.apache.wiki.WikiPage) GroupManager(org.apache.wiki.auth.authorize.GroupManager) WikiSession(org.apache.wiki.WikiSession) PageManager(org.apache.wiki.PageManager) Principal(java.security.Principal) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.Test)

Example 4 with PageManager

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

the class VersioningFileProviderTest method testDeleteVersion.

@Test
public void testDeleteVersion() throws Exception {
    engine.saveText(NAME1, "v1\r\n");
    engine.saveText(NAME1, "v2\r\n");
    engine.saveText(NAME1, "v3\r\n");
    PageManager mgr = engine.getPageManager();
    WikiPageProvider provider = mgr.getProvider();
    List l = provider.getVersionHistory(NAME1);
    Assert.assertEquals("wrong # of versions", 3, l.size());
    provider.deleteVersion(NAME1, 2);
    l = provider.getVersionHistory(NAME1);
    Assert.assertEquals("wrong # of versions", 2, l.size());
    Assert.assertEquals("v1", "v1\r\n", provider.getPageText(NAME1, 1));
    Assert.assertEquals("v3", "v3\r\n", provider.getPageText(NAME1, 3));
    try {
        provider.getPageText(NAME1, 2);
        Assert.fail("v2");
    } catch (NoSuchVersionException e) {
    // This is expected
    }
}
Also used : PageManager(org.apache.wiki.PageManager) List(java.util.List) Test(org.junit.Test)

Example 5 with PageManager

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

the class VersioningFileProviderTest method testDelete.

@Test
public void testDelete() throws Exception {
    engine.saveText(NAME1, "v1");
    engine.saveText(NAME1, "v2");
    engine.saveText(NAME1, "v3");
    PageManager mgr = engine.getPageManager();
    WikiPageProvider provider = mgr.getProvider();
    provider.deletePage(NAME1);
    File f = new File(files, NAME1 + AbstractFileProvider.FILE_EXT);
    Assert.assertFalse("file exists", f.exists());
}
Also used : PageManager(org.apache.wiki.PageManager) File(java.io.File) Test(org.junit.Test)

Aggregations

PageManager (org.apache.wiki.PageManager)7 PageLock (org.apache.wiki.PageLock)3 WikiPage (org.apache.wiki.WikiPage)3 Test (org.junit.Test)3 ProviderException (org.apache.wiki.api.exceptions.ProviderException)2 File (java.io.File)1 Principal (java.security.Principal)1 ParseException (java.text.ParseException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Iterator (java.util.Iterator)1 List (java.util.List)1 ResourceBundle (java.util.ResourceBundle)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 HttpSession (javax.servlet.http.HttpSession)1 WikiEngine (org.apache.wiki.WikiEngine)1 WikiSession (org.apache.wiki.WikiSession)1 WikiSessionTest (org.apache.wiki.WikiSessionTest)1 PluginException (org.apache.wiki.api.exceptions.PluginException)1