001/*
002 *  Copyright 2016 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.impl.userpref;
017
018import java.util.Date;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.logger.AbstractLogEnabled;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.avalon.framework.thread.ThreadSafe;
027
028import org.ametys.core.user.InvalidModificationException;
029import org.ametys.core.user.User;
030import org.ametys.core.user.UserIdentity;
031import org.ametys.core.user.UserManager;
032import org.ametys.core.user.directory.ModifiableUserDirectory;
033import org.ametys.core.user.directory.UserDirectory;
034import org.ametys.core.userpref.UserPreferencesException;
035import org.ametys.core.userpref.UserPreferencesStorage;
036
037/**
038 * This class is a implementation of {@link UserPreferencesStorage} based on {@link UserManager}.
039 * The supported user preferences are only the firstname, lastname and email of the user.
040 * If the user is issued from a non-modifiable user directory, the user preferences could not be edited.
041 */
042public class UserManagerUserPreferenceStorage extends AbstractLogEnabled implements UserPreferencesStorage, ThreadSafe, Serviceable
043{
044    private UserManager _userManager;
045
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        _userManager = (UserManager) smanager.lookup(UserManager.ROLE);
050    }
051
052    @Override
053    public Map<String, String> getUnTypedUserPrefs(UserIdentity userIdentity, String storageContext, Map<String, String> contextVars) throws UserPreferencesException
054    {
055        Map<String, String> userPrefs = new HashMap<>();
056        User user = _userManager.getUser(userIdentity);
057        
058        userPrefs.put("firstname", user.getFirstName());
059        userPrefs.put("lastname", user.getLastName());
060        userPrefs.put("email", user.getEmail());
061        
062        return userPrefs;
063    }
064
065    @Override
066    public void removeUserPreferences(UserIdentity user, String storageContext, Map<String, String> contextVars) throws UserPreferencesException
067    {
068        throw new UserPreferencesException("The user preferences issued from the users manager can not be removed ");
069    }
070
071    @Override
072    public void setUserPreferences(UserIdentity userIdentity, String storageContext, Map<String, String> contextVars, Map<String, String> preferences) throws UserPreferencesException
073    {
074        User user = _userManager.getUser(userIdentity);
075        
076        if (!_hasChanges(user, preferences))
077        {
078            return;
079        }
080        
081        UserDirectory userDirectory = user.getUserDirectory();
082        if (userDirectory instanceof ModifiableUserDirectory)
083        {
084            try
085            {
086                preferences.put("login", userIdentity.getLogin());
087                ((ModifiableUserDirectory) userDirectory).update(preferences);
088            }
089            catch (InvalidModificationException e)
090            {
091                throw new UserPreferencesException("Failed to update user informations", e);
092            }
093        }
094        else
095        {
096            throw new UserPreferencesException("Try to update user informations on a non-modifiable user directory");
097        }
098    }
099    
100    private boolean _hasChanges (User user, Map<String, String> preferences)
101    {
102        return (preferences.containsKey("firstname") && !preferences.get("firstname").equals(user.getFirstName()))
103                || (preferences.containsKey("lastname") && !preferences.get("lastname").equals(user.getLastName()))
104                || (preferences.containsKey("email") && !preferences.get("email").equals(user.getEmail()));
105    }
106
107    @Override
108    public String getUserPreferenceAsString(UserIdentity userIdentity, String storageContext, Map<String, String> contextVars, String id) throws UserPreferencesException
109    {
110        User user = _userManager.getUser(userIdentity);
111        
112        if ("firstname".equals(id))
113        {
114            return user.getFirstName();
115        }
116        else if ("lastname".equals(id))
117        {
118            return user.getLastName();
119        }
120        else if ("email".equals(id))
121        {
122            return user.getEmail();
123        }
124        return null;
125    }
126
127    @Override
128    public Long getUserPreferenceAsLong(UserIdentity user, String storageContext, Map<String, String> contextVars, String id) throws UserPreferencesException
129    {
130        return null;
131    }
132
133    @Override
134    public Date getUserPreferenceAsDate(UserIdentity user, String storageContext, Map<String, String> contextVars, String id) throws UserPreferencesException
135    {
136        return null;
137    }
138
139    @Override
140    public Boolean getUserPreferenceAsBoolean(UserIdentity user, String storageContext, Map<String, String> contextVars, String id) throws UserPreferencesException
141    {
142        return null;
143    }
144
145    @Override
146    public Double getUserPreferenceAsDouble(UserIdentity user, String storageContext, Map<String, String> contextVars, String id) throws UserPreferencesException
147    {
148        return null;
149    }
150    
151}