001/*
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.
018 */
019package org.apache.wiki.markdown.extensions.jspwikilinks.postprocessor;
020
021import org.apache.log4j.Logger;
022import org.apache.wiki.WikiContext;
023import org.apache.wiki.WikiPage;
024import org.apache.wiki.auth.WikiSecurityException;
025import org.apache.wiki.auth.acl.Acl;
026import org.apache.wiki.markdown.nodes.JSPWikiLink;
027import org.apache.wiki.render.RenderingManager;
028
029import com.vladsch.flexmark.util.NodeTracker;
030
031
032/**
033 * {@link NodePostProcessorState} which further post processes access rules links.
034 */
035public class AccessRuleLinkNodePostProcessorState implements NodePostProcessorState< JSPWikiLink > {
036
037    private static final Logger LOG = Logger.getLogger( AccessRuleLinkNodePostProcessorState.class );
038    private final WikiContext wikiContext;
039    private final boolean m_wysiwygEditorMode;
040
041    public AccessRuleLinkNodePostProcessorState( final WikiContext wikiContext ) {
042        this.wikiContext = wikiContext;
043        final Boolean wysiwygVariable = ( Boolean )wikiContext.getVariable( RenderingManager.WYSIWYG_EDITOR_MODE );
044        m_wysiwygEditorMode = wysiwygVariable != null ? wysiwygVariable.booleanValue() : false;
045    }
046
047    /**
048     * {@inheritDoc}
049     *
050     * @see NodePostProcessorState#process(NodeTracker, JSPWikiLink)
051     */
052    @Override
053    public void process( final NodeTracker state, final JSPWikiLink link ) {
054        String ruleLine = NodePostProcessorStateCommonOperations.inlineLinkTextOnWysiwyg( state, link, m_wysiwygEditorMode );
055        if( wikiContext.getEngine().getRenderingManager().getParser( wikiContext, link.getUrl().toString() ).isParseAccessRules() ) {
056            final WikiPage page = wikiContext.getRealPage();
057            if( ruleLine.startsWith( "{" ) ) {
058                ruleLine = ruleLine.substring( 1 );
059            }
060            if( ruleLine.endsWith( "}" ) ) {
061                ruleLine = ruleLine.substring( 0, ruleLine.length() - 1 );
062            }
063            LOG.debug( "page=" + page.getName() + ", ACL = " + ruleLine );
064
065            try {
066                final Acl acl = wikiContext.getEngine().getAclManager().parseAcl( page, ruleLine );
067                page.setAcl( acl );
068                link.unlink();
069                state.nodeRemoved( link );
070                LOG.debug( acl.toString() );
071            } catch( final WikiSecurityException wse ) {
072                NodePostProcessorStateCommonOperations.makeError( state, link, wse.getMessage() );
073            }
074        }
075    }
076
077}