001/* 002 * Copyright 2015 Anyware Services 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 */ 016package org.ametys.plugins.core.ui.user; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.parameters.Parameters; 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.cocoon.acting.ServiceableAction; 027import org.apache.cocoon.environment.ObjectModelHelper; 028import org.apache.cocoon.environment.Redirector; 029import org.apache.cocoon.environment.Request; 030import org.apache.cocoon.environment.SourceResolver; 031import org.apache.commons.lang3.StringUtils; 032 033import org.ametys.core.cocoon.JSonReader; 034import org.ametys.core.user.CurrentUserProvider; 035import org.ametys.core.user.UserIdentity; 036import org.ametys.core.util.ServerCommHelper; 037import org.ametys.plugins.core.ui.user.DefaultProfileImageProvider.ProfileImageSource; 038 039/** 040 * Retrieves the available user profile images 041 */ 042public class GetUserProfileImagesAction extends ServiceableAction 043{ 044 /** Servercomm helper */ 045 protected ServerCommHelper _serverCommHelper; 046 047 /** Current user provider */ 048 protected CurrentUserProvider _currentUserProvider; 049 050 /** User profile image provider */ 051 protected DefaultProfileImageProvider _profileImageProvider; 052 053 @Override 054 public void service(ServiceManager sm) throws ServiceException 055 { 056 super.service(sm); 057 _serverCommHelper = (ServerCommHelper) sm.lookup(ServerCommHelper.ROLE); 058 _currentUserProvider = (CurrentUserProvider) sm.lookup(CurrentUserProvider.ROLE); 059 } 060 061 @Override 062 public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception 063 { 064 // Delayed initialized to ensure safe mode do not fail to load 065 if (_profileImageProvider == null) 066 { 067 try 068 { 069 _profileImageProvider = (DefaultProfileImageProvider) manager.lookup(ProfileImageProvider.ROLE); 070 } 071 catch (ServiceException e) 072 { 073 throw new RuntimeException("Lazy initialization failed.", e); 074 } 075 } 076 077 Map<String, Object> jsParameters = _serverCommHelper.getJsParameters(); 078 079 UserIdentity user; 080 String login = (String) jsParameters.get("login"); 081 String populationId = (String) jsParameters.get("populationId"); 082 if (StringUtils.isEmpty(login) || StringUtils.isEmpty(populationId)) 083 { 084 user = _currentUserProvider.getUser(); 085 } 086 else 087 { 088 user = new UserIdentity(login, populationId); 089 } 090 091 Map<String, Object> result = new HashMap<>(); 092 093 List<Map<String, Object>> images = new ArrayList<>(); 094 result.put("images", images); 095 096 // Stored image (from upload) 097 _addStoredImage(images, user); 098 099 // Gravatar 100 _addGravatarImage(images, user); 101 102 // Initials 103 _addInitialsImage(images, user); 104 105 // Local images 106 _addLocalImages(images, login); 107 108 // Default 109 _addDefaultImage(images); 110 111 Request request = ObjectModelHelper.getRequest(objectModel); 112 request.setAttribute(JSonReader.OBJECT_TO_READ, result); 113 114 return EMPTY_MAP; 115 } 116 117 /** 118 * Add the stored image from the userpref (uploaded image are stored in the userpref) 119 * @param images The image list accumulator 120 * @param user The user 121 */ 122 protected void _addStoredImage(List<Map<String, Object>> images, UserIdentity user) 123 { 124 Map<String, Object> rawUserPrefImage = _profileImageProvider.hasUserPrefImage(user); 125 126 // Only add image physically stored in userprefs (ie. coming from an upload) 127 String base64Source = ProfileImageSource.BASE64.name().toLowerCase(); 128 if (rawUserPrefImage != null && base64Source.equals(rawUserPrefImage.get("source"))) 129 { 130 Map<String, Object> image = new HashMap<>(); 131 image.put("source", ProfileImageSource.USERPREF.name().toLowerCase()); 132 133 images.add(image); 134 } 135 } 136 137 /** 138 * Add the gravatar image to the list if existing 139 * @param images The image list accumulator 140 * @param user The user 141 */ 142 protected void _addGravatarImage(List<Map<String, Object>> images, UserIdentity user) 143 { 144 String gravatarSource = ProfileImageSource.GRAVATAR.name().toLowerCase(); 145 146 if (_profileImageProvider.hasGravatarImage(user)) 147 { 148 Map<String, Object> image = new HashMap<>(); 149 image.put("source", gravatarSource); 150 151 images.add(image); 152 } 153 } 154 155 /** 156 * Add the image with initials to the list 157 * @param images The image list accumulator 158 * @param user The user 159 */ 160 protected void _addInitialsImage(List<Map<String, Object>> images, UserIdentity user) 161 { 162 String initialsSource = ProfileImageSource.INITIALS.name().toLowerCase(); 163 164 if (_profileImageProvider.hasInitialsImage(user)) 165 { 166 Map<String, Object> image = new HashMap<>(); 167 image.put("source", initialsSource); 168 169 images.add(image); 170 } 171 172 } 173 174 /** 175 * Add the local images to the list 176 * @param images The image list accumulator 177 * @param login The user login 178 */ 179 protected void _addLocalImages(List<Map<String, Object>> images, String login) 180 { 181 List<String> localImageIds = _profileImageProvider.getLocalImageIds(); 182 String localImageSource = ProfileImageSource.LOCALIMAGE.name().toLowerCase(); 183 184 for (String id : localImageIds) 185 { 186 if (_profileImageProvider.hasLocalImage(id)) 187 { 188 Map<String, Object> image = new HashMap<>(); 189 image.put("source", localImageSource); 190 191 // id parameter 192 Map<String, Object> parameters = new HashMap<>(); 193 parameters.put("id", id); 194 195 image.put("parameters", parameters); 196 197 images.add(image); 198 } 199 } 200 } 201 202 /** 203 * Add the default image 204 * @param images The image list accumulator 205 */ 206 protected void _addDefaultImage(List<Map<String, Object>> images) 207 { 208 String defaultSource = ProfileImageSource.DEFAULT.name().toLowerCase(); 209 210 Map<String, Object> image = new HashMap<>(); 211 image.put("source", defaultSource); 212 213 images.add(image); 214 } 215} 216