Search in sources :

Example 1 with Command

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

the class DefaultCommandResolver method extractCommandFromPath.

/**
 * Extracts a Command based on the JSP path of an HTTP request. If the JSP requested matches a Command's <code>getJSP()</code>
 * value, that Command is returned.
 *
 * @param request the HTTP request
 * @return the resolved Command, or <code>null</code> if not found
 */
protected Command extractCommandFromPath(final HttpServletRequest request) {
    String jsp = request.getServletPath();
    // Take everything to right of initial / and left of # or ?
    final int hashMark = jsp.indexOf('#');
    if (hashMark != -1) {
        jsp = jsp.substring(0, hashMark);
    }
    final int questionMark = jsp.indexOf('?');
    if (questionMark != -1) {
        jsp = jsp.substring(0, questionMark);
    }
    if (jsp.startsWith("/")) {
        jsp = jsp.substring(1);
    }
    // Find special page reference?
    for (final Map.Entry<String, Command> entry : m_specialPages.entrySet()) {
        final Command specialCommand = entry.getValue();
        if (specialCommand.getJSP().equals(jsp)) {
            return specialCommand;
        }
    }
    // Still haven't found a matching command? Ok, see if we match against our standard list of JSPs
    if (!jsp.isEmpty() && JSPS.containsKey(jsp)) {
        return JSPS.get(jsp);
    }
    return null;
}
Also used : Command(org.apache.wiki.api.core.Command) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Command

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

the class PermissionTag method checkPermission.

/**
 *  Checks a single permission.
 *
 *  @param permission permission to check for
 *  @return true if granted, false if not
 */
private boolean checkPermission(final String permission) {
    final Session session = m_wikiContext.getWikiSession();
    final Page page = m_wikiContext.getPage();
    final AuthorizationManager mgr = m_wikiContext.getEngine().getManager(AuthorizationManager.class);
    boolean gotPermission = false;
    if (CREATE_GROUPS.equals(permission) || CREATE_PAGES.equals(permission) || EDIT_PREFERENCES.equals(permission) || EDIT_PROFILE.equals(permission) || LOGIN.equals(permission)) {
        gotPermission = mgr.checkPermission(session, new WikiPermission(page.getWiki(), permission));
    } else if (VIEW_GROUP.equals(permission) || EDIT_GROUP.equals(permission) || DELETE_GROUP.equals(permission)) {
        final Command command = m_wikiContext.getCommand();
        gotPermission = false;
        if (command instanceof GroupCommand && command.getTarget() != null) {
            final GroupPrincipal group = (GroupPrincipal) command.getTarget();
            final String groupName = group.getName();
            String action = "view";
            if (EDIT_GROUP.equals(permission)) {
                action = "edit";
            } else if (DELETE_GROUP.equals(permission)) {
                action = "delete";
            }
            gotPermission = mgr.checkPermission(session, new GroupPermission(groupName, action));
        }
    } else if (ALL_PERMISSION.equals(permission)) {
        gotPermission = mgr.checkPermission(session, new AllPermission(m_wikiContext.getEngine().getApplicationName()));
    } else if (page != null) {
        // 
        if (EDIT.equals(permission)) {
            final Page latest = m_wikiContext.getEngine().getManager(PageManager.class).getPage(page.getName());
            if (page.getVersion() != WikiProvider.LATEST_VERSION && latest.getVersion() != page.getVersion()) {
                return false;
            }
        }
        final Permission p = PermissionFactory.getPagePermission(page, permission);
        gotPermission = mgr.checkPermission(session, p);
    }
    return gotPermission;
}
Also used : GroupCommand(org.apache.wiki.ui.GroupCommand) GroupCommand(org.apache.wiki.ui.GroupCommand) Command(org.apache.wiki.api.core.Command) GroupPrincipal(org.apache.wiki.auth.GroupPrincipal) WikiPermission(org.apache.wiki.auth.permissions.WikiPermission) AllPermission(org.apache.wiki.auth.permissions.AllPermission) Permission(java.security.Permission) GroupPermission(org.apache.wiki.auth.permissions.GroupPermission) AllPermission(org.apache.wiki.auth.permissions.AllPermission) Page(org.apache.wiki.api.core.Page) GroupPermission(org.apache.wiki.auth.permissions.GroupPermission) AuthorizationManager(org.apache.wiki.auth.AuthorizationManager) WikiPermission(org.apache.wiki.auth.permissions.WikiPermission) Session(org.apache.wiki.api.core.Session)

Example 3 with Command

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

the class SearchResultIteratorTag method nextResult.

private int nextResult() {
    if (m_iterator != null && m_iterator.hasNext() && m_count++ < m_maxItems) {
        final SearchResult r = (SearchResult) m_iterator.next();
        // Create a wiki context for the result
        final Engine engine = m_wikiContext.getEngine();
        final HttpServletRequest request = m_wikiContext.getHttpRequest();
        final Command command = PageCommand.VIEW.targetedCommand(r.getPage());
        final Context context = Wiki.context().create(engine, request, command);
        // Stash it in the page context
        pageContext.setAttribute(Context.ATTR_CONTEXT, context, PageContext.REQUEST_SCOPE);
        pageContext.setAttribute(getId(), r);
        return EVAL_BODY_BUFFERED;
    }
    return SKIP_BODY;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Context(org.apache.wiki.api.core.Context) PageContext(javax.servlet.jsp.PageContext) PageCommand(org.apache.wiki.ui.PageCommand) Command(org.apache.wiki.api.core.Command) SearchResult(org.apache.wiki.api.search.SearchResult) Engine(org.apache.wiki.api.core.Engine)

Example 4 with Command

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

the class GroupCommandTest method testTargetedCommand.

@Test
public void testTargetedCommand() {
    // Get view command
    Command a = GroupCommand.VIEW_GROUP;
    final GroupPrincipal group = new GroupPrincipal("Test");
    // Combine with wiki group; make sure it's not equal to old command
    Command b = a.targetedCommand(group);
    Assertions.assertNotSame(a, b);
    Assertions.assertEquals(a.getRequestContext(), b.getRequestContext());
    Assertions.assertEquals(a.getJSP(), b.getJSP());
    Assertions.assertEquals(a.getURLPattern(), b.getURLPattern());
    Assertions.assertEquals(a.getContentTemplate(), b.getContentTemplate());
    Assertions.assertNotNull(b.getTarget());
    Assertions.assertNotNull(b.requiredPermission());
    Assertions.assertEquals(new GroupPermission("*:Test", "view"), b.requiredPermission());
    Assertions.assertEquals(group, b.getTarget());
    // Do the same with edit command
    a = GroupCommand.EDIT_GROUP;
    b = a.targetedCommand(group);
    Assertions.assertNotSame(a, b);
    Assertions.assertNotNull(b.getTarget());
    Assertions.assertNotNull(b.requiredPermission());
    Assertions.assertEquals(new GroupPermission("*:Test", "edit"), b.requiredPermission());
    Assertions.assertEquals(group, b.getTarget());
    // Do the same with delete command
    a = GroupCommand.DELETE_GROUP;
    b = a.targetedCommand(group);
    Assertions.assertNotSame(a, b);
    Assertions.assertNotNull(b.getTarget());
    Assertions.assertNotNull(b.requiredPermission());
    Assertions.assertEquals(new GroupPermission("*:Test", "delete"), b.requiredPermission());
    Assertions.assertEquals(group, b.getTarget());
}
Also used : Command(org.apache.wiki.api.core.Command) GroupPrincipal(org.apache.wiki.auth.GroupPrincipal) GroupPermission(org.apache.wiki.auth.permissions.GroupPermission) Test(org.junit.jupiter.api.Test)

Example 5 with Command

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

the class CommandResolverTest method testFindStaticWikiAction.

@Test
public void testFindStaticWikiAction() {
    // If we look for action with "edit" request context, we get EDIT action
    Command a = CommandResolver.findCommand(ContextEnum.PAGE_EDIT.getRequestContext());
    Assertions.assertEquals(PageCommand.EDIT, a);
    Assertions.assertEquals(ContextEnum.PAGE_EDIT.getRequestContext(), a.getRequestContext());
    // Ditto for prefs context
    a = CommandResolver.findCommand(ContextEnum.WIKI_PREFS.getRequestContext());
    Assertions.assertEquals(WikiCommand.PREFS, a);
    Assertions.assertEquals(ContextEnum.WIKI_PREFS.getRequestContext(), a.getRequestContext());
    // Ditto for group view context
    a = CommandResolver.findCommand(ContextEnum.GROUP_VIEW.getRequestContext());
    Assertions.assertEquals(GroupCommand.VIEW_GROUP, a);
    Assertions.assertEquals(ContextEnum.GROUP_VIEW.getRequestContext(), a.getRequestContext());
    // Looking for non-existent context; should result in exception
    Assertions.assertThrows(IllegalArgumentException.class, () -> CommandResolver.findCommand("nonExistentContext"));
}
Also used : Command(org.apache.wiki.api.core.Command) Test(org.junit.jupiter.api.Test)

Aggregations

Command (org.apache.wiki.api.core.Command)17 Test (org.junit.jupiter.api.Test)12 Page (org.apache.wiki.api.core.Page)4 GroupPrincipal (org.apache.wiki.auth.GroupPrincipal)4 MockHttpServletRequest (net.sourceforge.stripes.mock.MockHttpServletRequest)3 GroupPermission (org.apache.wiki.auth.permissions.GroupPermission)2 WikiPermission (org.apache.wiki.auth.permissions.WikiPermission)2 PageManager (org.apache.wiki.pages.PageManager)2 PageCommand (org.apache.wiki.ui.PageCommand)2 Permission (java.security.Permission)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 PageContext (javax.servlet.jsp.PageContext)1 TestEngine (org.apache.wiki.TestEngine)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 SearchResult (org.apache.wiki.api.search.SearchResult)1 AuthorizationManager (org.apache.wiki.auth.AuthorizationManager)1