    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.tags;
    020
    021import org.apache.wiki.api.core.Session;
    022import org.apache.wiki.auth.AuthenticationManager;
    023
    024/**
    025 *  Includes the content if an user check validates.  This has been considerably enhanced for 2.2.  The possibilities for the
    026 *  "status"-argument are:
    027 *
    028 * <ul>
    029 * <li>"anonymous"            - the body of the tag is included if the user is completely unknown (no cookie, no password)</li>
    030 * <li>"asserted"             - the body of the tag is included if the user has either been named by a cookie, but not been authenticated.</li>
    031 * <li>"authenticated"        - the body of the tag is included if the user is validated either through the container, or by our own authentication.</li>
    032 * <li>"assertionsAllowed"    - the body of the tag is included if wiki allows identities to be asserted using cookies.</li>
    033 * <li>"assertionsNotAllowed" - the body of the tag is included if wiki does <i>not</i> allow identities to be asserted using cookies.</li>
    034 * <li>"containerAuth"        - the body of the tag is included if the user is validated through the container.</li>
    035 * <li>"customAuth"           - the body of the tag is included if the user is validated through our own authentication.</li>
    036 * <li>"known"                - if the user is not anonymous</li>
    037 * <li>"notAuthenticated"     - the body of the tag is included if the user is not yet authenticated.</li>
    038 * </ul>
    039 *
    040 *  If the old "exists" -argument is used, it corresponds as follows:
    041 *  <p>
    042 *  <tt>exists="true" ==> status="known"<br>
    043 *  <tt>exists="false" ==> status="unknown"<br>
    044 *  </p>
    045 *
    046 *  It is NOT a good idea to use BOTH of the arguments.
    047 *
    048 *  @since 2.0
    049 */
    050public class UserCheckTag extends WikiTagBase {
    051
    052    private static final long serialVersionUID = 3256438110127863858L;
    053    private static final String ASSERTED = "asserted";
    054    private static final String AUTHENTICATED = "authenticated";
    055    private static final String ANONYMOUS = "anonymous";
    056    private static final String ASSERTIONS_ALLOWED = "assertionsallowed";
    057    private static final String ASSERTIONS_NOT_ALLOWED = "assertionsnotallowed";
    058    private static final String CONTAINER_AUTH = "containerauth";
    059    private static final String CUSTOM_AUTH = "customauth";
    060    private static final String KNOWN = "known";
    061    private static final String NOT_AUTHENTICATED = "notauthenticated";
    062
    063    private String m_status;
    064
    065    /**
    066     *  {@inheritDoc}
    067     */
    068    @Override
    069    public void initTag() {
    070        super.initTag();
    071        m_status = null;
    072    }
    073
    074    /**
    075     *  Get the status as defined above.
    076     *
    077     *  @return The status to be checked.
    078     */
    079    public String getStatus()
    080    {
    081        return m_status;
    082    }
    083
    084    /**
    085     *  Sets the status as defined above.
    086     *
    087     *  @param status The status to be checked.
    088     */
    089    public void setStatus( final String status )
    090    {
    091        m_status = status.toLowerCase();
    092    }
    093
    094    /**
    095     * {@inheritDoc}
    096     * @see org.apache.wiki.tags.WikiTagBase#doWikiStartTag()
    097     */
    098    @Override
    099    public final int doWikiStartTag() {
    100        final Session session = m_wikiContext.getWikiSession();
    101        final String status = session.getStatus();
    102        final AuthenticationManager mgr = m_wikiContext.getEngine().getManager( AuthenticationManager.class );
    103        final boolean containerAuth = mgr.isContainerAuthenticated();
    104        final boolean cookieAssertions = mgr.allowsCookieAssertions();
    105
    106        if( m_status != null ) {
    107            switch( m_status ) {
    108            case ANONYMOUS:
    109                if( status.equals( Session.ANONYMOUS ) ) {
    110                    return EVAL_BODY_INCLUDE;
    111                }
    112                break;
    113            case AUTHENTICATED:
    114                if( status.equals( Session.AUTHENTICATED ) ) {
    115                    return EVAL_BODY_INCLUDE;
    116                }
    117                break;
    118            case ASSERTED:
    119                if( status.equals( Session.ASSERTED ) ) {
    120                    return EVAL_BODY_INCLUDE;
    121                }
    122                break;
    123            case ASSERTIONS_ALLOWED:
    124                if( cookieAssertions ) {
    125                    return EVAL_BODY_INCLUDE;
    126                }
    127                return SKIP_BODY;
    128            case ASSERTIONS_NOT_ALLOWED:
    129                if( !cookieAssertions ) {
    130                    return EVAL_BODY_INCLUDE;
    131                }
    132                return SKIP_BODY;
    133            case CONTAINER_AUTH:
    134                if( containerAuth ) {
    135                    return EVAL_BODY_INCLUDE;
    136                }
    137                return SKIP_BODY;
    138            case CUSTOM_AUTH:
    139                if( !containerAuth ) {
    140                    return EVAL_BODY_INCLUDE;
    141                }
    142                return SKIP_BODY;
    143            case KNOWN:
    144                if( !session.isAnonymous() ) {
    145                    return EVAL_BODY_INCLUDE;
    146                }
    147                return SKIP_BODY;
    148            case NOT_AUTHENTICATED:
    149                if( !status.equals( Session.AUTHENTICATED ) ) {
    150                    return EVAL_BODY_INCLUDE;
    151                }
    152                break;
    153            }
    154        }
    155
    156        return SKIP_BODY;
    157    }
    158
    159}