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.DataModel; 027import org.nuxeo.ecm.core.api.DocumentModel; 028import org.nuxeo.ecm.core.api.NuxeoPrincipal; 029import org.nuxeo.ecm.core.cache.Cache; 030import org.nuxeo.ecm.core.cache.CacheService; 031import org.nuxeo.ecm.directory.Session; 032import org.nuxeo.ecm.directory.api.DirectoryService; 033import org.nuxeo.ecm.platform.usermanager.UserConfig; 034import org.nuxeo.ecm.platform.usermanager.UserManager; 035import org.nuxeo.runtime.api.Framework; 036import org.nuxeo.runtime.services.event.Event; 037import org.nuxeo.runtime.services.event.EventListener; 038import org.nuxeo.runtime.services.event.EventService; 039 040/** 041 * Helper class to encapsulate userName => DisplayName resolution. Does direct access to the underlying directories to 042 * avoid performance issues. 043 * 044 * @author tiry 045 * @since 7.2 046 */ 047public class UserNameResolverHelper implements EventListener { 048 049 protected static final Log log = LogFactory.getLog(UserNameResolverHelper.class); 050 051 public static final String USERNAMECACHE = "userDisplayName"; 052 053 protected volatile Cache cache; 054 055 public String getUserFullName(String login) { 056 057 String displayName = null; 058 059 Cache cache = getCache(); 060 if (cache != null) { 061 displayName = (String) cache.get(login); 062 } 063 064 if (displayName == null) { 065 displayName = computeUserFullName(login); 066 if (cache != null) { 067 cache.put(login, displayName); 068 } 069 } 070 071 if (displayName == null) { 072 displayName = login; 073 } 074 return displayName; 075 } 076 077 protected Cache getCache() { 078 Cache result = cache; 079 if (result == null) { 080 synchronized (this) { 081 result = cache; 082 if (result == null) { 083 CacheService cs = Framework.getService(CacheService.class); 084 if (cs != null) { 085 result = cs.getCache(USERNAMECACHE); 086 if (result != null) { 087 EventService es = Framework.getService(EventService.class); 088 es.addListener("usermanager", this); 089 } 090 cache = result; 091 } 092 } 093 } 094 } 095 return result; 096 } 097 098 protected String computeUserFullName(String login) { 099 100 UserManager um = Framework.getService(UserManager.class); 101 String dname = um.getUserDirectoryName(); 102 103 try (Session dirSession = Framework.getService(DirectoryService.class).open(dname)) { 104 DocumentModel entry = dirSession.getEntry(login, false); 105 if (entry == null) { 106 // virtual user ? 107 NuxeoPrincipal principal = um.getPrincipal(login); 108 if (principal != null) { 109 return computeUserFullName(principal); 110 } else { 111 return login; 112 } 113 } else { 114 DataModel model = entry.getDataModel(um.getUserSchemaName()); 115 return computeUserFullName(model); 116 } 117 } 118 } 119 120 protected String computeUserFullName(DataModel model) { 121 String first = (String) model.getData(UserConfig.DEFAULT.firstNameKey); 122 String last = (String) model.getData(UserConfig.DEFAULT.lastNameKey); 123 String username = (String) model.getData(UserConfig.DEFAULT.nameKey); 124 return Functions.userDisplayName(username, first, last); 125 } 126 127 protected String computeUserFullName(NuxeoPrincipal principal) { 128 String first = principal.getFirstName(); 129 String last = principal.getLastName(); 130 return Functions.userDisplayName(principal.getName(), first, last); 131 } 132 133 @Override 134 public boolean aboutToHandleEvent(Event arg0) { 135 return true; 136 } 137 138 @Override 139 public void handleEvent(Event event) { 140 if ("user_changed".equals(event.getId())) { 141 String userName = (String) event.getData(); 142 Cache cache = getCache(); 143 if (cache != null) { 144 cache.invalidate(userName); 145 } 146 } 147 } 148 149}