001/* 002 * (C) Copyright 2009 Nuxeo SA (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * Anahide Tchertchian 016 */ 017package org.nuxeo.ecm.webapp.security; 018 019import javax.faces.component.UIComponent; 020import javax.faces.context.FacesContext; 021import javax.faces.convert.Converter; 022 023import org.apache.commons.lang.StringUtils; 024import org.nuxeo.ecm.core.api.DocumentModel; 025import org.nuxeo.ecm.core.api.NuxeoPrincipal; 026import org.nuxeo.ecm.platform.ui.web.directory.DirectoryFunctions; 027import org.nuxeo.ecm.platform.ui.web.tag.fn.Functions; 028import org.nuxeo.ecm.platform.ui.web.util.ComponentUtils; 029 030/** 031 * JSF Converter used for rendering, transforming a user id into the user display name. 032 * <p> 033 * Sample usage: 034 * 035 * <pre> 036 * <code> 037 * lt;h:outputText 038 * converter="#{userSuggestionActions.userConverter}"> 039 * <f:attribute name="prefixed" value="false" /> 040 * <f:attribute name="userDirectory" value="#{userManager.userDirectoryName}" /> 041 * <f:attribute name="userSchema" value="#{userManager.userSchemaName}" /> 042 * <f:attribute name="firstNameField" value="firstName" /> 043 * <f:attribute name="lastNameField" value="lastName" /> 044 * lt;/h:outputText> 045 * </code> 046 * </pre> 047 * 048 * @author Anahide Tchertchian 049 */ 050public class UserDisplayConverter implements Converter { 051 052 /** 053 * Returns given value (does not do any reverse conversion) 054 */ 055 @Override 056 public Object getAsObject(FacesContext context, UIComponent component, String value) { 057 return value; 058 } 059 060 /** 061 * Tries to build the user display name according to information passed as attribute to the component holding the 062 * converter. 063 * <p> 064 * Handled attributes are: 065 * <ul> 066 * <li>userDirectory: user directory name</li> 067 * <li>userSchema: user schema name</li> 068 * <li>firstNameField: field storing the user first name</li> 069 * <li>lastNameField: field storing the user last name</li> 070 * </ul> 071 */ 072 @Override 073 public String getAsString(FacesContext context, UIComponent component, Object value) { 074 if (value instanceof String && !StringUtils.isEmpty((String) value)) { 075 String isPrefixed = (String) ComponentUtils.getAttributeOrExpressionValue(context, component, "prefixed", 076 "false"); 077 String username; 078 if (Boolean.valueOf(isPrefixed)) { 079 username = ((String) value).substring(NuxeoPrincipal.PREFIX.length()); 080 } else { 081 username = (String) value; 082 } 083 String directory = (String) ComponentUtils.getAttributeOrExpressionValue(context, component, 084 "userDirectory", null); 085 String firstName = (String) ComponentUtils.getAttributeOrExpressionValue(context, component, 086 "firstNameField", null); 087 String lastName = (String) ComponentUtils.getAttributeOrExpressionValue(context, component, 088 "lastNameField", null); 089 String email = (String) ComponentUtils.getAttributeOrExpressionValue(context, component, "emailField", null); 090 String schema = (String) ComponentUtils.getAttributeOrExpressionValue(context, component, "userSchema", 091 null); 092 093 if (schema != null) { 094 DocumentModel doc = DirectoryFunctions.getDirectoryEntry(directory, username); 095 if (doc != null) { 096 String firstNameValue = firstName != null ? (String) doc.getProperty(schema, firstName) : null; 097 String lastNameValue = lastName != null ? (String) doc.getProperty(schema, lastName) : null; 098 String emailValue = email != null ? (String) doc.getProperty(schema, email) : null; 099 return Functions.userDisplayNameAndEmail(username, firstNameValue, lastNameValue, emailValue); 100 } 101 } else { 102 // XXX will return cached entry 103 return Functions.userFullName(username); 104 } 105 } 106 if (value != null) { 107 return value.toString(); 108 } else { 109 return null; 110 } 111 } 112 113}