Search in sources :

Example 61 with Page

use of org.apache.wiki.api.core.Page in project jspwiki by apache.

the class WikiEngine method initReferenceManager.

/**
 *  Initializes the reference manager. Scans all existing WikiPages for
 *  internal links and adds them to the ReferenceManager object.
 *
 *  @throws WikiException If the reference manager initialization fails.
 */
public void initReferenceManager() throws WikiException {
    try {
        // Build a new manager with default key lists.
        if (getManager(ReferenceManager.class) == null) {
            final ArrayList<Page> pages = new ArrayList<>();
            pages.addAll(getManager(PageManager.class).getAllPages());
            pages.addAll(getManager(AttachmentManager.class).getAllAttachments());
            initComponent(ReferenceManager.class, this);
            getManager(ReferenceManager.class).initialize(pages);
        }
    } catch (final ProviderException e) {
        LOG.fatal("PageProvider is unable to list pages: ", e);
    } catch (final Exception e) {
        throw new WikiException("Could not instantiate ReferenceManager: " + e.getMessage(), e);
    }
}
Also used : WikiException(org.apache.wiki.api.exceptions.WikiException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) ArrayList(java.util.ArrayList) Page(org.apache.wiki.api.core.Page) WikiException(org.apache.wiki.api.exceptions.WikiException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MalformedURLException(java.net.MalformedURLException) ProviderException(org.apache.wiki.api.exceptions.ProviderException) ReferenceManager(org.apache.wiki.references.ReferenceManager)

Example 62 with Page

use of org.apache.wiki.api.core.Page in project jspwiki by apache.

the class DefaultAttachmentManager method storeAttachment.

/**
 * {@inheritDoc}
 */
@Override
public void storeAttachment(final Attachment att, final InputStream in) throws IOException, ProviderException {
    if (m_provider == null) {
        return;
    }
    // Checks if the actual, real page exists without any modifications or aliases. We cannot store an attachment to a non-existent page.
    if (!m_engine.getManager(PageManager.class).pageExists(att.getParentName())) {
        // the caller should catch the exception and use the exception text as an i18n key
        throw new ProviderException("attach.parent.not.exist");
    }
    m_provider.putAttachmentData(att, in);
    m_engine.getManager(ReferenceManager.class).updateReferences(att.getName(), new ArrayList<>());
    final Page parent = Wiki.contents().page(m_engine, att.getParentName());
    m_engine.getManager(ReferenceManager.class).updateReferences(parent);
    m_engine.getManager(SearchManager.class).reindexPage(att);
}
Also used : ProviderException(org.apache.wiki.api.exceptions.ProviderException) SearchManager(org.apache.wiki.search.SearchManager) Page(org.apache.wiki.api.core.Page) ReferenceManager(org.apache.wiki.references.ReferenceManager)

Example 63 with Page

use of org.apache.wiki.api.core.Page in project jspwiki by apache.

the class DefaultAclManager method getPermissions.

/**
 * {@inheritDoc}
 */
@Override
public Acl getPermissions(final Page page) {
    // Does the page already have cached ACLs?
    Acl acl = page.getAcl();
    log.debug("page=" + page.getName() + "\n" + acl);
    if (acl == null) {
        // If null, try the parent.
        if (page instanceof Attachment) {
            final Page parent = m_engine.getManager(PageManager.class).getPage(((Attachment) page).getParentName());
            acl = getPermissions(parent);
        } else {
            // Or, try parsing the page
            final Context ctx = Wiki.context().create(m_engine, page);
            ctx.setVariable(Context.VAR_EXECUTE_PLUGINS, Boolean.FALSE);
            m_engine.getManager(RenderingManager.class).getHTML(ctx, page);
            if (page.getAcl() == null) {
                page.setAcl(Wiki.acls().acl());
            }
            acl = page.getAcl();
        }
    }
    return acl;
}
Also used : Context(org.apache.wiki.api.core.Context) PageManager(org.apache.wiki.pages.PageManager) RenderingManager(org.apache.wiki.render.RenderingManager) Attachment(org.apache.wiki.api.core.Attachment) Page(org.apache.wiki.api.core.Page) Acl(org.apache.wiki.api.core.Acl)

Example 64 with Page

use of org.apache.wiki.api.core.Page in project jspwiki by apache.

the class KendraSearchProvider method findPages.

/**
 * {@inheritDoc}
 */
@Override
public Collection<SearchResult> findPages(final String query, final Context wikiContext) throws ProviderException, IOException {
    final QueryRequest request = new QueryRequest().withIndexId(indexId).withQueryText(query);
    final List<QueryResultItem> items;
    try {
        items = getKendra().query(request).getResultItems();
    } catch (final ThrottlingException e) {
        LOG.error("ThrottlingException. Skipping...");
        return new ArrayList<>();
    }
    final List<SearchResult> searchResults = new ArrayList<>(items.size());
    final AuthorizationManager mgr = engine.getManager(AuthorizationManager.class);
    for (final QueryResultItem item : items) {
        switch(QueryResultType.fromValue(item.getType())) {
            case DOCUMENT:
                final String documentId = item.getDocumentId();
                final String documentExcerpt = item.getDocumentExcerpt().getText();
                final String scoreConfidence = item.getScoreAttributes().getScoreConfidence();
                final Page page = this.engine.getManager(PageManager.class).getPage(documentId, PageProvider.LATEST_VERSION);
                if (page != null) {
                    final PagePermission pp = new PagePermission(page, PagePermission.VIEW_ACTION);
                    if (mgr.checkPermission(wikiContext.getWikiSession(), pp)) {
                        final SearchResult searchResult = new SearchResultImpl(page, confidence2score(scoreConfidence), new String[] { documentExcerpt });
                        searchResults.add(searchResult);
                    } else {
                        LOG.error(format("Page '%s' is not accessible", documentId));
                    }
                } else {
                    LOG.error(format("Kendra found a result page '%s' that could not be loaded, removing from index", documentId));
                    pageRemoved(Wiki.contents().page(this.engine, documentId));
                }
                break;
            default:
                LOG.error(format("Unknown query result type: %s", item.getType()));
        }
    }
    return searchResults;
}
Also used : SearchResult(org.apache.wiki.api.search.SearchResult) Page(org.apache.wiki.api.core.Page) PageManager(org.apache.wiki.pages.PageManager) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) PagePermission(org.apache.wiki.auth.permissions.PagePermission)

Example 65 with Page

use of org.apache.wiki.api.core.Page 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)

Aggregations

Page (org.apache.wiki.api.core.Page)181 PageManager (org.apache.wiki.pages.PageManager)106 Test (org.junit.jupiter.api.Test)71 Context (org.apache.wiki.api.core.Context)46 Engine (org.apache.wiki.api.core.Engine)30 Attachment (org.apache.wiki.api.core.Attachment)27 ProviderException (org.apache.wiki.api.exceptions.ProviderException)22 Date (java.util.Date)21 WikiPage (org.apache.wiki.WikiPage)18 ReferenceManager (org.apache.wiki.references.ReferenceManager)16 RenderingManager (org.apache.wiki.render.RenderingManager)15 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)14 File (java.io.File)11 ArrayList (java.util.ArrayList)10 Calendar (java.util.Calendar)10 Hashtable (java.util.Hashtable)10 IOException (java.io.IOException)9 Vector (java.util.Vector)9 TestEngine (org.apache.wiki.TestEngine)9 PagePermission (org.apache.wiki.auth.permissions.PagePermission)8