001/*
002 *  Copyright 2011 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.web.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025
026import org.ametys.core.observation.Event;
027import org.ametys.core.observation.ObservationManager;
028import org.ametys.core.right.RightManager.RightResult;
029import org.ametys.core.ui.Callable;
030import org.ametys.core.ui.SimpleMenu;
031import org.ametys.core.user.UserManager;
032import org.ametys.plugins.repository.AmetysObjectResolver;
033import org.ametys.web.ObservationConstants;
034import org.ametys.web.repository.content.ModifiableWebContent;
035
036/**
037 * This element represents the privacy level of contents
038 */
039public class ContentPrivacyMenu extends SimpleMenu
040{
041    /** Runtime users manager */
042    protected UserManager _userManager;
043    /** Repository content */
044    protected AmetysObjectResolver _resolver;
045    private ObservationManager _observationManager;
046    
047    @Override
048    public void service(ServiceManager smanager) throws ServiceException
049    {
050        super.service(smanager);
051        _userManager = (UserManager) smanager.lookup(UserManager.ROLE);
052        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
053        _observationManager = (ObservationManager) smanager.lookup(ObservationManager.ROLE);
054    }
055    
056    /**
057     * Update the privacy level of a list of contents
058     * @param contentsId The ids of the contents
059     * @param level The level of privacy
060     * @return the results
061     */
062    @Callable (rights = Callable.SKIP_BUILTIN_CHECK)
063    public Map<String, Object> updatePrivacy(List<String> contentsId, String level)
064    {
065        List<String> allRightIds = new ArrayList<>();
066        List<Map<String, Object>> noRightContents = new ArrayList<>();
067        
068        for (String contentId : contentsId)
069        {
070            ModifiableWebContent content = _resolver.resolveById(contentId);
071            
072            if (_rightManager.currentUserHasRight("WEB_Rights_Content_ChangePrivacy", content) == RightResult.RIGHT_ALLOW)
073            {
074                content.setPrivacyLevel(level);
075                content.saveChanges();
076                
077                // Notify observers that privacy status has changed
078                Map<String, Object> eventParams = new HashMap<>();
079                eventParams.put(org.ametys.cms.ObservationConstants.ARGS_CONTENT, content);
080                eventParams.put("level", level);
081                _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_PRIVACY_CHANGED, _currentUserProvider.getUser(), eventParams));
082                
083                allRightIds.add(contentId);
084            }
085            else
086            {
087                noRightContents.add(Map.of("id", content.getId(), "title", content.getTitle()));
088            }
089            
090        }
091        
092        return Map.of("allright-contents", allRightIds, "noright-contents", noRightContents);
093    }
094}