001/*
002 *  Copyright 2014 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.tags;
017
018import java.util.HashSet;
019import java.util.List;
020import java.util.Map;
021import java.util.Set;
022
023import org.ametys.cms.repository.Content;
024import org.ametys.cms.tag.CMSTag;
025import org.ametys.cms.tag.Tag;
026import org.ametys.cms.tag.TagProvider;
027import org.ametys.core.right.RightManager.RightResult;
028import org.ametys.plugins.repository.AmetysObject;
029import org.ametys.web.repository.page.Page;
030import org.ametys.web.repository.tag.WEBJCRTagProvider;
031
032/**
033 * SAX events for tags
034 */
035public class GetTagsAction extends org.ametys.cms.tag.GetCMSTagsAction
036{
037    @Override
038    protected boolean isUserAuthorized(Tag tag, List<String> objectTargetIds, Map<String, Object> jsParameters)
039    {
040        if (objectTargetIds != null && objectTargetIds.size() > 0)
041        {
042            CMSTag cmsTag = (CMSTag) tag;
043            boolean hasRight = false;
044            for (String objectId : objectTargetIds)
045            {
046                AmetysObject ao = _resolver.resolveById(objectId);
047                
048                if (ao instanceof Content && cmsTag.getTarget().getName().equals("CONTENT"))
049                {
050                    hasRight = isUserAuthorized (tag, (Content) ao) || hasRight;
051                }
052                else if (ao instanceof Page && cmsTag.getTarget().getName().equals("PAGE"))
053                {
054                    hasRight = isUserAuthorized (cmsTag, (Page) ao) || hasRight;
055                }
056            }
057            return hasRight;
058        }
059        
060        return true;
061    }
062    
063    /**
064     * Test if a tag is visible to the current user.
065     * @param tag the Tag object.
066     * @param page the current page (can be null).
067     * @return true if the user has access to the tag, false otherwise.
068     */
069    protected boolean isUserAuthorized(CMSTag tag, Page page)
070    {
071        if (page != null)
072        {
073            if (isPrivate(tag))
074            {
075                return _rightManager.hasRight(_getCurrentUser(), "Web_Rights_Page_Private_Tag", page) == RightResult.RIGHT_ALLOW;
076            }
077            else
078            {
079                return _rightManager.hasRight(_getCurrentUser(), "Web_Rights_Page_Tag", page) == RightResult.RIGHT_ALLOW
080                        || _rightManager.hasRight(_getCurrentUser(), "Web_Rights_Page_Private_Tag", page) == RightResult.RIGHT_ALLOW;
081            }
082        }
083        
084        return true;
085    }
086    
087    /**
088     * Get all providers IDs
089     * @param onlyCustomTags If true, return only JCR providers IDs.
090     * @return a set of providers IDs
091     */
092    @Override
093    protected Set<String> getProvidersIds(boolean onlyCustomTags)
094    {
095        if (onlyCustomTags)
096        {
097            Set<String> extensionsIds = new HashSet<>();
098            TagProvider<CMSTag> tagProvider = (TagProvider<CMSTag>) _tagProviderExtPt.getExtension(WEBJCRTagProvider.class.getName());
099            extensionsIds.add(tagProvider.getId());
100            return extensionsIds;
101        }
102        
103        return _tagProviderExtPt.getExtensionsIds();
104    }
105}