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.shiro.web.tags; 020 021import javax.servlet.jsp.JspException; 022 023import org.slf4j.Logger; 024import org.slf4j.LoggerFactory; 025 026 027/** 028 * JSP tag that renders the tag body if the current user known to the system, either from a successful login attempt 029 * (not necessarily during the current session) or from 'RememberMe' services. 030 * 031 * <p><b>Note:</b> This is <em>less</em> restrictive than the <code>AuthenticatedTag</code> since it only assumes 032 * the user is who they say they are, either via a current session login <em>or</em> via Remember Me services, which 033 * makes no guarantee the user is who they say they are. The <code>AuthenticatedTag</code> however 034 * guarantees that the current user has logged in <em>during their current session</em>, proving they really are 035 * who they say they are. 036 * 037 * <p>The logically opposite tag of this one is the {@link org.apache.shiro.web.tags.GuestTag}. 038 * 039 * @since 0.9 040 */ 041public class UserTag extends SecureTag { 042 043 private static final Logger LOGGER = LoggerFactory.getLogger(UserTag.class); 044 045 public int onDoStartTag() throws JspException { 046 if (getSubject() != null && getSubject().getPrincipal() != null) { 047 if (LOGGER.isTraceEnabled()) { 048 LOGGER.trace("Subject has known identity (aka 'principal'). " 049 + "Tag body will be evaluated."); 050 } 051 return EVAL_BODY_INCLUDE; 052 } else { 053 if (LOGGER.isTraceEnabled()) { 054 LOGGER.trace("Subject does not exist or have a known identity (aka 'principal'). " 055 + "Tag body will not be evaluated."); 056 } 057 return SKIP_BODY; 058 } 059 } 060 061}