001/*
002 *  Copyright 2020 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.userdirectory.action;
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.Redirector;
028import org.apache.cocoon.environment.SourceResolver;
029import org.apache.commons.lang3.StringUtils;
030
031import org.ametys.cms.repository.Content;
032import org.ametys.cms.transformation.xslt.ResolveURIComponent;
033import org.ametys.core.user.CurrentUserProvider;
034import org.ametys.core.user.UserIdentity;
035import org.ametys.plugins.userdirectory.UserDirectoryHelper;
036
037/**
038 * Get the internal uri of a user's avatar
039 *
040 */
041public class GetProfileImageUriAction extends ServiceableAction
042{
043    /** Attribute path for user's image */
044    public static final String USER_CONTENT_IMAGE_PATH = "illustration/image";
045    
046    private CurrentUserProvider _currentUserProvider;
047    private UserDirectoryHelper _userDirectoryHelper;
048
049    @Override
050    public void service(ServiceManager smanager) throws ServiceException
051    {
052        super.service(manager);
053        _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE);
054        _userDirectoryHelper = (UserDirectoryHelper) smanager.lookup(UserDirectoryHelper.ROLE);
055    }
056    
057    
058    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
059    {
060        Map<String, String> result = new HashMap<>();
061        
062        UserIdentity user = _getUser(parameters);
063        
064        int size = parameters.getParameterAsInteger("size", 0);
065        int maxSize = parameters.getParameterAsInteger("maxSize", 0);
066        boolean download = parameters.getParameterAsBoolean("download", false);
067        String lang = parameters.getParameter("lang");
068        
069        if (user == null)
070        {
071            result.put("imageUri", "skin://resources/img/avatar.png");
072        }
073        else
074        {
075            Content userContent = _userDirectoryHelper.getUserContent(user, lang);
076            if (userContent != null && userContent.hasValue(USER_CONTENT_IMAGE_PATH))
077            {
078                String imgUri = USER_CONTENT_IMAGE_PATH + "?contentId=" + userContent.getId();
079                result.put("imageUri", ResolveURIComponent.resolveCroppedImage("attribute", imgUri, size, size, false, false, true));
080            }
081            else
082            {
083                StringBuilder sb = new StringBuilder("cocoon://_plugins/core-ui/user/");
084                sb.append(user.getPopulationId())
085                    .append("/")
086                    .append(user.getLogin())
087                    .append("/image_")
088                    .append(size);
089                
090                List<String> params = new ArrayList<>();
091                if (download)
092                {
093                    params.add("download=true");
094                }
095                if (maxSize > 0)
096                {
097                    params.add("maxSize=" + maxSize);
098                }
099                
100                if (params.size() > 0)
101                {
102                    sb.append("?").append(String.join("&", params));
103                }
104                result.put("imageUri", sb.toString());
105            }
106        }
107        
108        return result;
109    }
110    
111    
112    private UserIdentity _getUser(Parameters parameters)
113    {
114        String login =  parameters.getParameter("login", StringUtils.EMPTY);
115        String populationId =  parameters.getParameter("populationId", StringUtils.EMPTY);
116        // Default to current user if login not provided, except for the default source for which a login is not needed.
117        if (StringUtils.isEmpty(login) || StringUtils.isEmpty(populationId))
118        {
119            return _currentUserProvider.getUser();
120        }
121        else
122        {
123            return new UserIdentity(login, populationId);
124        }
125    }
126}