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