PageRenderTime 373ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/jspwiki-2.8.4/src/com/ecyrd/jspwiki/tags/UserProfileTag.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 295 lines | 184 code | 40 blank | 71 comment | 51 complexity | d6b5b0217994f9a4d84732ba788c81e1 MD5 | raw file
  1. /*
  2. JSPWiki - a JSP-based WikiWiki clone.
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. package com.ecyrd.jspwiki.tags;
  19. import java.io.IOException;
  20. import java.security.Principal;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.ResourceBundle;
  24. import javax.servlet.http.HttpServletRequest;
  25. import com.ecyrd.jspwiki.TextUtil;
  26. import com.ecyrd.jspwiki.WikiContext;
  27. import com.ecyrd.jspwiki.WikiEngine;
  28. import com.ecyrd.jspwiki.WikiSession;
  29. import com.ecyrd.jspwiki.auth.AuthenticationManager;
  30. import com.ecyrd.jspwiki.auth.GroupPrincipal;
  31. import com.ecyrd.jspwiki.auth.UserManager;
  32. import com.ecyrd.jspwiki.auth.WikiSecurityException;
  33. import com.ecyrd.jspwiki.auth.authorize.Role;
  34. import com.ecyrd.jspwiki.auth.user.UserProfile;
  35. import com.ecyrd.jspwiki.i18n.InternationalizationManager;
  36. /**
  37. * <p>
  38. * Returns user profile attributes, or empty strings if the user has not been
  39. * validated. This tag has a single attribute, "property."
  40. * The <code>property</code> attribute may contain one of the following
  41. * case-insensitive values:
  42. * </p>
  43. * <ul>
  44. * <li><code>created</code> - creation date</li>
  45. * <li><code>email</code> - user's e-mail address</li>
  46. * <li><code>fullname</code> - user's full name</li>
  47. * <li><code>groups</code> - a sorted list of the groups a user belongs to</li>
  48. * <li><code>loginname</code> - user's login name. If the current user does not have
  49. * a profile, the user's login principal (such as one provided by a container
  50. * login module, user cookie, or anonyous IP address), will supply the login
  51. * name property</li>
  52. * <li><code>roles</code> - a sorted list of the roles a user possesses</li>
  53. * <li><code>wikiname</code> - user's wiki name</li>
  54. * <li><code>modified</code> - last modification date</li>
  55. * <li><code>exists</code> - evaluates the body of the tag if user's profile exists
  56. * in the user database
  57. * <li><code>new</code> - evaluates the body of the tag if user's profile does not
  58. * exist in the user database
  59. * <li><code>canChangeLoginName</code> - always true if custom auth used; also true for container auth
  60. * and current UserDatabase.isSharedWithContainer() is true.</li>
  61. * <li><code>canChangePassword</code> - always true if custom auth used; also true for container auth
  62. * and current UserDatabase.isSharedWithContainer() is true.</li>
  63. * </ul>
  64. * <p>In addition, the values <code>exists</code>, <code>new</code>, <code>canChangeLoginName</code>
  65. * and <code>canChangeLoginName</code> can also be prefixed with <code>!</code> to indicate the
  66. * negative condition (for example, <code>!exists</code>).</p>
  67. * @author Andrew Jaquith
  68. * @since 2.3
  69. */
  70. public class UserProfileTag extends WikiTagBase
  71. {
  72. private static final long serialVersionUID = 3258410625431582003L;
  73. public static final String BLANK = "(not set)";
  74. private static final String CREATED = "created";
  75. private static final String EMAIL = "email";
  76. private static final String EXISTS = "exists";
  77. private static final String NOT_EXISTS= "!exists";
  78. private static final String FULLNAME = "fullname";
  79. private static final String GROUPS = "groups";
  80. private static final String LOGINNAME = "loginname";
  81. private static final String MODIFIED = "modified";
  82. private static final String NEW = "new";
  83. private static final String NOT_NEW = "!new";
  84. private static final String ROLES = "roles";
  85. private static final String WIKINAME = "wikiname";
  86. private static final String CHANGE_LOGIN_NAME = "canchangeloginname";
  87. private static final String NOT_CHANGE_LOGIN_NAME = "!canchangeloginname";
  88. private static final String CHANGE_PASSWORD = "canchangepassword";
  89. private static final String NOT_CHANGE_PASSWORD = "!canchangepassword";
  90. private String m_prop;
  91. public void initTag()
  92. {
  93. super.initTag();
  94. m_prop = null;
  95. }
  96. public final int doWikiStartTag() throws IOException, WikiSecurityException
  97. {
  98. UserManager manager = m_wikiContext.getEngine().getUserManager();
  99. UserProfile profile = manager.getUserProfile( m_wikiContext.getWikiSession() );
  100. String result = null;
  101. if ( EXISTS.equals( m_prop ) || NOT_NEW.equals( m_prop ) )
  102. {
  103. return profile.isNew() ? SKIP_BODY : EVAL_BODY_INCLUDE;
  104. }
  105. else if ( NEW.equals( m_prop ) || NOT_EXISTS.equals( m_prop ) )
  106. {
  107. return profile.isNew() ? EVAL_BODY_INCLUDE : SKIP_BODY;
  108. }
  109. else if ( CREATED.equals( m_prop ) && profile.getCreated() != null )
  110. {
  111. result = profile.getCreated().toString();
  112. }
  113. else if ( EMAIL.equals( m_prop ) )
  114. {
  115. result = profile.getEmail();
  116. }
  117. else if ( FULLNAME.equals( m_prop ) )
  118. {
  119. result = profile.getFullname();
  120. }
  121. else if ( GROUPS.equals( m_prop ) )
  122. {
  123. result = printGroups( m_wikiContext );
  124. }
  125. else if ( LOGINNAME.equals( m_prop ) )
  126. {
  127. result = profile.getLoginName();
  128. }
  129. else if ( MODIFIED.equals( m_prop ) && profile.getLastModified() != null )
  130. {
  131. result = profile.getLastModified().toString();
  132. }
  133. else if ( ROLES.equals( m_prop ) )
  134. {
  135. result = printRoles( m_wikiContext );
  136. }
  137. else if ( WIKINAME.equals( m_prop ) )
  138. {
  139. result = profile.getWikiName();
  140. if( result == null )
  141. {
  142. //
  143. // Default back to the declared user name
  144. //
  145. WikiEngine engine = this.m_wikiContext.getEngine();
  146. WikiSession wikiSession = WikiSession.getWikiSession( engine, (HttpServletRequest)pageContext.getRequest() );
  147. Principal user = wikiSession.getUserPrincipal();
  148. if( user != null )
  149. {
  150. result = user.getName();
  151. }
  152. }
  153. }
  154. else if ( CHANGE_PASSWORD.equals( m_prop ) || CHANGE_LOGIN_NAME.equals( m_prop ) )
  155. {
  156. AuthenticationManager authMgr = m_wikiContext.getEngine().getAuthenticationManager();
  157. if ( !authMgr.isContainerAuthenticated() )
  158. {
  159. return EVAL_BODY_INCLUDE;
  160. }
  161. }
  162. else if ( NOT_CHANGE_PASSWORD.equals( m_prop ) || NOT_CHANGE_LOGIN_NAME.equals( m_prop ) )
  163. {
  164. AuthenticationManager authMgr = m_wikiContext.getEngine().getAuthenticationManager();
  165. if ( authMgr.isContainerAuthenticated() )
  166. {
  167. return EVAL_BODY_INCLUDE;
  168. }
  169. }
  170. if ( result != null )
  171. {
  172. pageContext.getOut().print( TextUtil.replaceEntities(result) );
  173. }
  174. return SKIP_BODY;
  175. }
  176. public void setProperty( String property )
  177. {
  178. m_prop = property.toLowerCase().trim();
  179. }
  180. /**
  181. * Returns a sorted list of the {@link com.ecyrd.jspwiki.auth.authorize.Group} objects a user possesses
  182. * in his or her WikiSession. The result is computed by consulting
  183. * {@link com.ecyrd.jspwiki.WikiSession#getRoles()}
  184. * and extracting those that are of type Group.
  185. * @return the list of groups, sorted by name
  186. */
  187. public static String printGroups( WikiContext context )
  188. {
  189. Principal[] roles = context.getWikiSession().getRoles();
  190. List<String> tempRoles = new ArrayList<String>();
  191. ResourceBundle rb = context.getBundle(InternationalizationManager.CORE_BUNDLE);
  192. for ( Principal role : roles )
  193. {
  194. if( role instanceof GroupPrincipal )
  195. {
  196. tempRoles.add( role.getName() );
  197. }
  198. }
  199. if ( tempRoles.size() == 0 )
  200. {
  201. return rb.getString("userprofile.nogroups");
  202. }
  203. StringBuffer sb = new StringBuffer();
  204. for ( int i = 0; i < tempRoles.size(); i++ )
  205. {
  206. String name = tempRoles.get( i );
  207. sb.append( name );
  208. if ( i < ( tempRoles.size() - 1 ) )
  209. {
  210. sb.append(',');
  211. sb.append(' ');
  212. }
  213. }
  214. return sb.toString();
  215. }
  216. /**
  217. * Returns a sorted list of the {@link com.ecyrd.jspwiki.auth.authorize.Role} objects a user possesses
  218. * in his or her WikiSession. The result is computed by consulting
  219. * {@link com.ecyrd.jspwiki.WikiSession#getRoles()}
  220. * and extracting those that are of type Role.
  221. * @return the list of roles, sorted by name
  222. */
  223. public static String printRoles( WikiContext context )
  224. {
  225. Principal[] roles = context.getWikiSession().getRoles();
  226. List<String> tempRoles = new ArrayList<String>();
  227. ResourceBundle rb = context.getBundle( InternationalizationManager.CORE_BUNDLE );
  228. for ( Principal role : roles )
  229. {
  230. if ( role instanceof Role )
  231. {
  232. tempRoles.add( role.getName() );
  233. }
  234. }
  235. if ( tempRoles.size() == 0 )
  236. {
  237. return rb.getString( "userprofile.noroles" );
  238. }
  239. StringBuffer sb = new StringBuffer();
  240. for ( int i = 0; i < tempRoles.size(); i++ )
  241. {
  242. String name = tempRoles.get( i );
  243. sb.append( name );
  244. if ( i < ( tempRoles.size() - 1 ) )
  245. {
  246. sb.append(',');
  247. sb.append(' ');
  248. }
  249. }
  250. return sb.toString();
  251. }
  252. }