Search in sources :

Example 1 with PageManager

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

the class ListLocksPlugin method execute.

/**
 *  {@inheritDoc}
 */
@Override
public String execute(final Context context, final Map<String, String> params) throws PluginException {
    final StringBuilder result = new StringBuilder();
    final PageManager mgr = context.getEngine().getManager(PageManager.class);
    final List<PageLock> locks = mgr.getActiveLocks();
    final ResourceBundle rb = Preferences.getBundle(context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE);
    result.append("<table class=\"wikitable\">\n");
    result.append("<tr>\n");
    result.append("<th>").append(rb.getString("plugin.listlocks.page")).append("</th><th>").append(rb.getString("plugin.listlocks.locked.by")).append("</th><th>").append(rb.getString("plugin.listlocks.acquired")).append("</th><th>").append(rb.getString("plugin.listlocks.expires")).append("</th>\n");
    result.append("</tr>");
    if (locks.size() == 0) {
        result.append("<tr><td colspan=\"4\" class=\"odd\">").append(rb.getString("plugin.listlocks.no.locks.exist")).append("</td></tr>\n");
    } else {
        int rowNum = 1;
        for (final PageLock lock : locks) {
            result.append(rowNum % 2 != 0 ? "<tr class=\"odd\">" : "<tr>");
            result.append("<td>").append(lock.getPage()).append("</td>");
            result.append("<td>").append(lock.getLocker()).append("</td>");
            result.append("<td>").append(Preferences.renderDate(context, lock.getAcquisitionTime(), Preferences.TimeFormat.DATETIME)).append("</td>");
            result.append("<td>").append(Preferences.renderDate(context, lock.getExpiryTime(), Preferences.TimeFormat.DATETIME)).append("</td>");
            result.append("</tr>\n");
            rowNum++;
        }
    }
    result.append("</table>");
    return result.toString();
}
Also used : PageManager(org.apache.wiki.pages.PageManager) ResourceBundle(java.util.ResourceBundle) PageLock(org.apache.wiki.pages.PageLock)

Example 2 with PageManager

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

the class CheckLockTag method doWikiStartTag.

/**
 *  {@inheritDoc}
 */
@Override
public final int doWikiStartTag() throws IOException, ProviderException {
    final Engine engine = m_wikiContext.getEngine();
    final Page page = m_wikiContext.getPage();
    if (page != null) {
        final PageManager mgr = engine.getManager(PageManager.class);
        final PageLock lock = mgr.getCurrentLock(page);
        final HttpSession session = pageContext.getSession();
        final 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)) {
            final 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.pages.PageManager) HttpSession(javax.servlet.http.HttpSession) Page(org.apache.wiki.api.core.Page) PageLock(org.apache.wiki.pages.PageLock) Engine(org.apache.wiki.api.core.Engine)

Example 3 with PageManager

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

the class DefaultAclManager method setPermissions.

/**
 * {@inheritDoc}
 */
@Override
public void setPermissions(final Page page, final Acl acl) throws WikiSecurityException {
    final PageManager pageManager = m_engine.getManager(PageManager.class);
    // Forcibly expire any page locks
    final PageLock lock = pageManager.getCurrentLock(page);
    if (lock != null) {
        pageManager.unlockPage(lock);
    }
    // Remove all of the existing ACLs.
    final String pageText = m_engine.getManager(PageManager.class).getPureText(page);
    final Matcher matcher = DefaultAclManager.ACL_PATTERN.matcher(pageText);
    final String cleansedText = matcher.replaceAll("");
    final String newText = DefaultAclManager.printAcl(page.getAcl()) + cleansedText;
    try {
        pageManager.putPageText(page, newText);
    } catch (final 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.pages.PageManager) Matcher(java.util.regex.Matcher) ProviderException(org.apache.wiki.api.exceptions.ProviderException) PageLock(org.apache.wiki.pages.PageLock)

Example 4 with PageManager

use of org.apache.wiki.pages.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
    final int oldUserCount = m_db.getWikiNames().length;
    final GroupManager groupManager = m_engine.getManager(GroupManager.class);
    final PageManager pageManager = m_engine.getManager(PageManager.class);
    final AuthorizationManager authManager = m_engine.getManager(AuthorizationManager.class);
    final int oldGroupCount = groupManager.getRoles().length;
    final int oldPageCount = pageManager.getTotalPageCount();
    // Setup Step 1: create a new user with random name
    final Context context = Wiki.context().create(m_engine, m_engine.newHttpRequest(), "");
    final Session session = context.getWikiSession();
    final long now = System.currentTimeMillis();
    final String oldLogin = "TestLogin" + now;
    final String oldName = "Test User " + now;
    final String newLogin = "RenamedLogin" + now;
    final 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(context, profile);
    // 1a. Make sure the profile saved successfully and that we're logged in
    profile = m_mgr.getUserProfile(session);
    Assertions.assertEquals(oldLogin, profile.getLoginName());
    Assertions.assertEquals(oldName, profile.getFullname());
    Assertions.assertEquals(oldUserCount + 1, m_db.getWikiNames().length);
    Assertions.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
    Assertions.assertEquals(oldGroupCount + 1, groupManager.getRoles().length);
    Assertions.assertTrue(group.isMember(new WikiPrincipal(oldLogin)));
    Assertions.assertTrue(group.isMember(new WikiPrincipal(oldName)));
    Assertions.assertFalse(group.isMember(new WikiPrincipal(newLogin)));
    Assertions.assertFalse(group.isMember(new WikiPrincipal(newName)));
    Assertions.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.
    Page p = m_engine.getManager(PageManager.class).getPage(pageName);
    Assertions.assertEquals(oldPageCount + 1, pageManager.getTotalPageCount());
    Assertions.assertNotNull(p.getAcl().getAclEntry(new WikiPrincipal(oldLogin)));
    Assertions.assertNotNull(p.getAcl().getAclEntry(new WikiPrincipal(oldName)));
    Assertions.assertNull(p.getAcl().getAclEntry(new WikiPrincipal(newLogin)));
    Assertions.assertNull(p.getAcl().getAclEntry(new WikiPrincipal(newName)));
    Assertions.assertTrue(authManager.checkPermission(session, PermissionFactory.getPagePermission(p, "view")), "Test User view page");
    final Session bobSession = WikiSessionTest.authenticatedSession(m_engine, Users.BOB, Users.BOB_PASS);
    Assertions.assertFalse(authManager.checkPermission(bobSession, PermissionFactory.getPagePermission(p, "view")), "Bob !view page");
    // 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(context, profile);
    // Test 1: the wiki session should have the new wiki name in Subject
    Principal[] principals = session.getPrincipals();
    Assertions.assertTrue(ArrayUtils.contains(principals, new WikiPrincipal(oldLogin)));
    Assertions.assertFalse(ArrayUtils.contains(principals, new WikiPrincipal(oldName)));
    Assertions.assertFalse(ArrayUtils.contains(principals, new WikiPrincipal(newLogin)));
    Assertions.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);
    Assertions.assertFalse(group.isMember(new WikiPrincipal(oldLogin)));
    Assertions.assertFalse(group.isMember(new WikiPrincipal(oldName)));
    Assertions.assertFalse(group.isMember(new WikiPrincipal(newLogin)));
    Assertions.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.getManager(PageManager.class).getPage(pageName);
    Assertions.assertNull(p.getAcl().getAclEntry(new WikiPrincipal(oldLogin)));
    Assertions.assertNull(p.getAcl().getAclEntry(new WikiPrincipal(oldName)));
    Assertions.assertNull(p.getAcl().getAclEntry(new WikiPrincipal(newLogin)));
    Assertions.assertNotNull(p.getAcl().getAclEntry(new WikiPrincipal(newName)));
    Assertions.assertTrue(authManager.checkPermission(session, PermissionFactory.getPagePermission(p, "view")), "Test User view page");
    Assertions.assertFalse(authManager.checkPermission(bobSession, PermissionFactory.getPagePermission(p, "view")), "Bob !view page");
    // 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.getManager(PageManager.class).getText(pageName);
    Assertions.assertEquals(expectedText, actualText);
    // Remove our test page
    m_engine.getManager(PageManager.class).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.getManager(PageManager.class).getPage(pageName);
    Assertions.assertEquals(oldPageCount + 1, pageManager.getTotalPageCount());
    Assertions.assertNotNull(p.getAcl().getAclEntry(new WikiPrincipal(oldLogin)));
    Assertions.assertNotNull(p.getAcl().getAclEntry(new WikiPrincipal(oldName)));
    Assertions.assertNull(p.getAcl().getAclEntry(new WikiPrincipal(newLogin)));
    Assertions.assertNull(p.getAcl().getAclEntry(new WikiPrincipal(newName)));
    Assertions.assertTrue(authManager.checkPermission(session, PermissionFactory.getPagePermission(p, "view")), "Test User view page");
    Assertions.assertFalse(authManager.checkPermission(bobSession, PermissionFactory.getPagePermission(p, "view")), "Bob !view page");
    // 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(context, profile);
    // Test 5: the wiki session should have the new login name in Subject
    principals = session.getPrincipals();
    Assertions.assertFalse(ArrayUtils.contains(principals, new WikiPrincipal(oldLogin)));
    Assertions.assertTrue(ArrayUtils.contains(principals, new WikiPrincipal(oldName)));
    Assertions.assertTrue(ArrayUtils.contains(principals, new WikiPrincipal(newLogin)));
    Assertions.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);
    Assertions.assertFalse(group.isMember(new WikiPrincipal(oldLogin)));
    Assertions.assertTrue(group.isMember(new WikiPrincipal(oldName)));
    Assertions.assertFalse(group.isMember(new WikiPrincipal(newLogin)));
    Assertions.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.getManager(PageManager.class).getPage(pageName);
    Assertions.assertNull(p.getAcl().getAclEntry(new WikiPrincipal(oldLogin)));
    Assertions.assertNotNull(p.getAcl().getAclEntry(new WikiPrincipal(oldName)));
    Assertions.assertNull(p.getAcl().getAclEntry(new WikiPrincipal(newLogin)));
    Assertions.assertNull(p.getAcl().getAclEntry(new WikiPrincipal(newName)));
    Assertions.assertTrue(authManager.checkPermission(session, PermissionFactory.getPagePermission(p, "view")), "Test User view page");
    Assertions.assertFalse(authManager.checkPermission(bobSession, PermissionFactory.getPagePermission(p, "view")), "Bob !view page");
    // 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.getManager(PageManager.class).getText(pageName);
    Assertions.assertEquals(expectedText, actualText);
    // CLEANUP: delete the profile; user and page; should be back to old counts
    m_db.deleteByLoginName(newLogin);
    Assertions.assertEquals(oldUserCount, m_db.getWikiNames().length);
    groupManager.removeGroup(group.getName());
    Assertions.assertEquals(oldGroupCount, groupManager.getRoles().length);
    m_engine.getManager(PageManager.class).deletePage(pageName);
    Assertions.assertEquals(oldPageCount, pageManager.getTotalPageCount());
}
Also used : Context(org.apache.wiki.api.core.Context) Group(org.apache.wiki.auth.authorize.Group) UserProfile(org.apache.wiki.auth.user.UserProfile) Page(org.apache.wiki.api.core.Page) GroupManager(org.apache.wiki.auth.authorize.GroupManager) PageManager(org.apache.wiki.pages.PageManager) Principal(java.security.Principal) Session(org.apache.wiki.api.core.Session) WikiSessionTest(org.apache.wiki.WikiSessionTest) Test(org.junit.jupiter.api.Test)

Example 5 with PageManager

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

the class DefaultUserManager method initialize.

/**
 * {@inheritDoc}
 */
@Override
public void initialize(final Engine engine, final Properties props) {
    m_engine = engine;
    // Attach the PageManager as a listener
    // TODO: it would be better if we did this in PageManager directly
    addWikiEventListener(engine.getManager(PageManager.class));
    // TODO: Replace with custom annotations. See JSPWIKI-566
    WikiAjaxDispatcherServlet.registerServlet(JSON_USERS, new JSONUserModule(this), new AllPermission(null));
}
Also used : PageManager(org.apache.wiki.pages.PageManager) AllPermission(org.apache.wiki.auth.permissions.AllPermission)

Aggregations

PageManager (org.apache.wiki.pages.PageManager)8 Page (org.apache.wiki.api.core.Page)4 PageLock (org.apache.wiki.pages.PageLock)3 Test (org.junit.jupiter.api.Test)3 ProviderException (org.apache.wiki.api.exceptions.ProviderException)2 PageProvider (org.apache.wiki.api.providers.PageProvider)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 ResourceBundle (java.util.ResourceBundle)1 Matcher (java.util.regex.Matcher)1 HttpSession (javax.servlet.http.HttpSession)1 WikiPage (org.apache.wiki.WikiPage)1 WikiSessionTest (org.apache.wiki.WikiSessionTest)1 Context (org.apache.wiki.api.core.Context)1 Engine (org.apache.wiki.api.core.Engine)1 Session (org.apache.wiki.api.core.Session)1 PluginException (org.apache.wiki.api.exceptions.PluginException)1