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 ADMIN = "admin";
054    private static final String ASSERTED = "asserted";
055    private static final String AUTHENTICATED = "authenticated";
056    private static final String ANONYMOUS = "anonymous";
057    private static final String ASSERTIONS_ALLOWED = "assertionsallowed";
058    private static final String ASSERTIONS_NOT_ALLOWED = "assertionsnotallowed";
059    private static final String CONTAINER_AUTH = "containerauth";
060    private static final String CUSTOM_AUTH = "customauth";
061    private static final String KNOWN = "known";
062    private static final String NOT_AUTHENTICATED = "notauthenticated";
063
064    private String m_status;
065
066    /**
067     *  {@inheritDoc}
068     */
069    @Override
070    public void initTag() {
071        super.initTag();
072        m_status = null;
073    }
074
075    /**
076     *  Get the status as defined above.
077     *  
078     *  @return The status to be checked.
079     */
080    public String getStatus()
081    {
082        return m_status;
083    }
084
085    /**
086     *  Sets the status as defined above.
087     *  
088     *  @param status The status to be checked.
089     */
090    public void setStatus( final String status )
091    {
092        m_status = status.toLowerCase();
093    }
094
095    /**
096     * {@inheritDoc}
097     * @see org.apache.wiki.tags.WikiTagBase#doWikiStartTag()
098     */
099    @Override
100    public final int doWikiStartTag() {
101        final Session session = m_wikiContext.getWikiSession();
102        final String status = session.getStatus();
103        final AuthenticationManager mgr = m_wikiContext.getEngine().getManager( AuthenticationManager.class );
104        final boolean containerAuth = mgr.isContainerAuthenticated();
105        final boolean cookieAssertions = mgr.allowsCookieAssertions();
106
107        if( m_status != null ) {
108            switch( m_status ) {
109            case ANONYMOUS:
110                if( status.equals( Session.ANONYMOUS ) ) {
111                    return EVAL_BODY_INCLUDE;
112                }
113                break;
114            case ADMIN: 
115                if( m_wikiContext.hasAdminPermissions() ) {
116                    return EVAL_BODY_INCLUDE;
117                }
118                break;
119            case AUTHENTICATED:
120                if( status.equals( Session.AUTHENTICATED ) ) {
121                    return EVAL_BODY_INCLUDE;
122                }
123                break;
124            case ASSERTED:
125                if( status.equals( Session.ASSERTED ) ) {
126                    return EVAL_BODY_INCLUDE;
127                }
128                break;
129            case ASSERTIONS_ALLOWED:
130                if( cookieAssertions ) {
131                    return EVAL_BODY_INCLUDE;
132                }
133                return SKIP_BODY;
134            case ASSERTIONS_NOT_ALLOWED:
135                if( !cookieAssertions ) {
136                    return EVAL_BODY_INCLUDE;
137                }
138                return SKIP_BODY;
139            case CONTAINER_AUTH:
140                if( containerAuth ) {
141                    return EVAL_BODY_INCLUDE;
142                }
143                return SKIP_BODY;
144            case CUSTOM_AUTH:
145                if( !containerAuth ) {
146                    return EVAL_BODY_INCLUDE;
147                }
148                return SKIP_BODY;
149            case KNOWN:
150                if( !session.isAnonymous() ) {
151                    return EVAL_BODY_INCLUDE;
152                }
153                return SKIP_BODY;
154            case NOT_AUTHENTICATED:
155                if( !status.equals( Session.AUTHENTICATED ) ) {
156                    return EVAL_BODY_INCLUDE;
157                }
158                break;
159            }
160        }
161
162        return SKIP_BODY;
163    }
164
165}