001/* 002 * (C) Copyright 2007-2015 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * 020 */ 021 022package org.nuxeo.ecm.platform.ui.web.tag.fn; 023 024import org.apache.commons.logging.Log; 025import org.apache.commons.logging.LogFactory; 026import org.nuxeo.ecm.core.api.DocumentModel; 027import org.nuxeo.ecm.core.api.NuxeoPrincipal; 028import org.nuxeo.ecm.core.cache.Cache; 029import org.nuxeo.ecm.core.cache.CacheService; 030import org.nuxeo.ecm.directory.Session; 031import org.nuxeo.ecm.directory.api.DirectoryService; 032import org.nuxeo.ecm.platform.usermanager.UserConfig; 033import org.nuxeo.ecm.platform.usermanager.UserManager; 034import org.nuxeo.runtime.api.Framework; 035import org.nuxeo.runtime.services.event.Event; 036import org.nuxeo.runtime.services.event.EventListener; 037import org.nuxeo.runtime.services.event.EventService; 038 039/** 040 * Helper class to encapsulate userName => DisplayName resolution. Does direct access to the underlying directories to 041 * avoid performance issues. 042 * 043 * @author tiry 044 * @since 7.2 045 */ 046public class UserNameResolverHelper implements EventListener { 047 048 protected static final Log log = LogFactory.getLog(UserNameResolverHelper.class); 049 050 public static final String USERNAMECACHE = "userDisplayName"; 051 052 protected volatile Cache cache; 053 054 public String getUserFullName(String login) { 055 056 String displayName = null; 057 058 Cache cache = getCache(); 059 if (cache != null) { 060 displayName = (String) cache.get(login); 061 } 062 063 if (displayName == null) { 064 displayName = computeUserFullName(login); 065 if (cache != null) { 066 cache.put(login, displayName); 067 } 068 } 069 070 if (displayName == null) { 071 displayName = login; 072 } 073 return displayName; 074 } 075 076 protected Cache getCache() { 077 Cache result = cache; 078 if (result == null) { 079 synchronized (this) { 080 result = cache; 081 if (result == null) { 082 CacheService cs = Framework.getService(CacheService.class); 083 if (cs != null) { 084 result = cs.getCache(USERNAMECACHE); 085 if (result != null) { 086 EventService es = Framework.getService(EventService.class); 087 es.addListener("usermanager", this); 088 } 089 cache = result; 090 } 091 } 092 } 093 } 094 return result; 095 } 096 097 protected String computeUserFullName(String login) { 098 099 UserManager um = Framework.getService(UserManager.class); 100 String dname = um.getUserDirectoryName(); 101 102 try (Session dirSession = Framework.getService(DirectoryService.class).open(dname)) { 103 DocumentModel entry = dirSession.getEntry(login, false); 104 if (entry == null) { 105 // virtual user ? 106 NuxeoPrincipal principal = um.getPrincipal(login); 107 if (principal != null) { 108 return computeUserFullName(principal); 109 } else { 110 return login; 111 } 112 } else { 113 return computeUserFullName(entry, um.getUserSchemaName()); 114 } 115 } 116 } 117 118 protected String computeUserFullName(DocumentModel entry, String schema) { 119 String first = (String) entry.getProperty(schema, UserConfig.DEFAULT.firstNameKey); 120 String last = (String) entry.getProperty(schema, UserConfig.DEFAULT.lastNameKey); 121 String username = (String) entry.getProperty(schema, UserConfig.DEFAULT.nameKey); 122 return Functions.userDisplayName(username, first, last); 123 } 124 125 protected String computeUserFullName(NuxeoPrincipal principal) { 126 String first = principal.getFirstName(); 127 String last = principal.getLastName(); 128 return Functions.userDisplayName(principal.getName(), first, last); 129 } 130 131 @Override 132 public boolean aboutToHandleEvent(Event arg0) { 133 return true; 134 } 135 136 @Override 137 public void handleEvent(Event event) { 138 if ("user_changed".equals(event.getId())) { 139 String userName = (String) event.getData(); 140 Cache cache = getCache(); 141 if (cache != null) { 142 cache.invalidate(userName); 143 } 144 } 145 } 146 147}