001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.jsp; 029 030import org.opencms.file.CmsObject; 031import org.opencms.file.CmsUser; 032import org.opencms.flex.CmsFlexController; 033import org.opencms.i18n.CmsMessageContainer; 034import org.opencms.main.CmsLog; 035 036import java.util.Arrays; 037import java.util.Iterator; 038import java.util.List; 039 040import javax.servlet.ServletRequest; 041import javax.servlet.jsp.tagext.TagSupport; 042 043import org.apache.commons.logging.Log; 044 045/** 046 * Provides access to the data of the currently logged in user.<p> 047 * 048 * @since 6.0.0 049 */ 050public class CmsJspTagUser extends TagSupport { 051 052 /** Serial version UID required for safe serialization. */ 053 private static final long serialVersionUID = 4520173761363738542L; 054 055 /** The log object for this class. */ 056 private static final Log LOG = CmsLog.getLog(CmsJspTagUser.class); 057 058 /** Static array of the possible user properties. */ 059 private static final String[] USER_PROPERTIES = { 060 "name", 061 "firstname", 062 "lastname", 063 "email", 064 "street", 065 "zip", 066 "city", 067 "description", 068 "group", 069 "currentgroup", 070 "defaultgroup", 071 "otherstuff", 072 "institution"}; 073 074 /** Array list for fast lookup. */ 075 private static final List<String> USER_PROPERTIES_LIST = Arrays.asList(USER_PROPERTIES); 076 077 /** The property name. */ 078 private String m_property; 079 080 /** 081 * Internal action method.<p> 082 * 083 * @param property the selected user property 084 * @param req the current request 085 * @return String the value of the selected user property 086 */ 087 public static String userTagAction(String property, ServletRequest req) { 088 089 CmsFlexController controller = CmsFlexController.getController(req); 090 091 CmsObject cms = controller.getCmsObject(); 092 CmsUser user = cms.getRequestContext().getCurrentUser(); 093 094 if (property == null) { 095 property = USER_PROPERTIES[0]; 096 } 097 098 String result = null; 099 switch (USER_PROPERTIES_LIST.indexOf(property)) { 100 case 0: // name 101 result = user.getName(); 102 break; 103 case 1: // firstname 104 result = user.getFirstname(); 105 break; 106 case 2: // lastname 107 result = user.getLastname(); 108 break; 109 case 3: // email 110 result = user.getEmail(); 111 break; 112 case 4: // street 113 result = user.getAddress(); 114 break; 115 case 5: // zip 116 result = user.getZipcode(); 117 break; 118 case 6: // city 119 result = user.getCity(); 120 break; 121 case 7: // description 122 result = user.getDescription(cms.getRequestContext().getLocale()); 123 break; 124 // following 3 attributes are no longer supported 125 case 8: // group 126 case 9: // currentgroup 127 case 10: // defaultgroup 128 result = ""; 129 break; 130 case 11: // otherstuff 131 Iterator<String> it = user.getAdditionalInfo().keySet().iterator(); 132 CmsMessageContainer msgContainer = Messages.get().container(Messages.GUI_TAG_USER_ADDITIONALINFO_0); 133 result = Messages.getLocalizedMessage(msgContainer, req); 134 while (it.hasNext()) { 135 Object o = it.next(); 136 result += " " + o + "=" + user.getAdditionalInfo((String)o); 137 } 138 break; 139 case 12: // institution 140 result = user.getInstitution(); 141 break; 142 default: 143 msgContainer = Messages.get().container(Messages.GUI_ERR_INVALID_USER_PROP_1, property); 144 result = Messages.getLocalizedMessage(msgContainer, req); 145 } 146 147 return result; 148 } 149 150 /** 151 * @see javax.servlet.jsp.tagext.Tag#doStartTag() 152 */ 153 @Override 154 public int doStartTag() throws javax.servlet.jsp.JspException { 155 156 javax.servlet.ServletRequest req = pageContext.getRequest(); 157 158 // This will always be true if the page is called through OpenCms 159 if (CmsFlexController.isCmsRequest(req)) { 160 161 try { 162 String result = userTagAction(m_property, req); 163 // Return value of selected property 164 pageContext.getOut().print(result); 165 } catch (Exception ex) { 166 if (LOG.isErrorEnabled()) { 167 LOG.error(Messages.get().getBundle().key(Messages.ERR_PROCESS_TAG_1, "user"), ex); 168 } 169 throw new javax.servlet.jsp.JspException(ex); 170 } 171 } 172 return SKIP_BODY; 173 } 174 175 /** 176 * Returns the property name.<p> 177 * 178 * @return String the property name 179 */ 180 public String getProperty() { 181 182 return m_property != null ? m_property : ""; 183 } 184 185 /** 186 * @see javax.servlet.jsp.tagext.Tag#release() 187 */ 188 @Override 189 public void release() { 190 191 super.release(); 192 m_property = null; 193 } 194 195 /** 196 * Sets the property name.<p> 197 * 198 * @param name the property name 199 */ 200 public void setProperty(String name) { 201 202 if (name != null) { 203 m_property = name.toLowerCase(); 204 } 205 } 206 207}