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.linkdirectory.right;
017
018import java.util.HashSet;
019import java.util.Map;
020import java.util.Set;
021
022import org.apache.avalon.framework.context.Context;
023import org.apache.avalon.framework.context.ContextException;
024import org.apache.avalon.framework.context.Contextualizable;
025import org.apache.avalon.framework.service.ServiceException;
026import org.apache.avalon.framework.service.ServiceManager;
027import org.apache.cocoon.components.ContextHelper;
028import org.apache.commons.lang3.StringUtils;
029
030import org.ametys.core.right.AccessController;
031import org.ametys.core.right.RightsException;
032import org.ametys.core.user.CurrentUserProvider;
033import org.ametys.core.user.UserIdentity;
034import org.ametys.plugins.core.impl.right.AbstractProfileStorageBasedAccessController;
035import org.ametys.plugins.linkdirectory.DirectoryHelper;
036import org.ametys.plugins.linkdirectory.Link;
037import org.ametys.plugins.linkdirectory.repository.DefaultLink;
038import org.ametys.plugins.repository.AmetysObjectIterable;
039import org.ametys.runtime.i18n.I18nizableText;
040import org.ametys.web.WebHelper;
041import org.ametys.web.repository.site.Site;
042import org.ametys.web.repository.site.SiteManager;
043import org.ametys.web.repository.sitemap.Sitemap;
044
045/**
046 * {@link AccessController} for a {@link Link}
047 */
048public class LinkDirectoryAccessController extends AbstractProfileStorageBasedAccessController implements Contextualizable
049{
050    /** the link directory context category */
051    public static final I18nizableText LINK_DIRECTORY_CONTEXT_CATEGORY = new I18nizableText("plugin.link-directory", "PLUGINS_LINKDIRECTORY_LINKDIRECTORY_RIGHT_ASSIGNMENT_CONTEXT_LABEL");
052    
053    /** The link directory helper */
054    protected DirectoryHelper _directoryHelper;
055    /** The site manager */
056    protected SiteManager _siteManager;
057    
058    /** The current user provider */
059    protected CurrentUserProvider _currentUserProvider;
060    
061    private Context _context;
062
063    public void contextualize(Context context) throws ContextException
064    {
065        _context = context;
066    }
067    
068    @Override
069    public void service(ServiceManager manager) throws ServiceException
070    {
071        super.service(manager);
072        _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE);
073        _directoryHelper = (DirectoryHelper) manager.lookup(DirectoryHelper.ROLE);
074        _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE);
075    }
076    
077    public boolean supports(Object object)
078    {
079        return object instanceof Link;
080    }
081    
082    @Override
083    protected boolean ignoreOnHasAnyPermission()
084    {
085        return true;
086    }
087    
088    @Override
089    protected Set< ? extends Object> _convertWorkspaceToRootRightContexts(Set<Object> workspacesContexts)
090    {
091        String siteName = WebHelper.getSiteName(ContextHelper.getRequest(_context));
092        Site site = _siteManager.getSite(siteName);
093        if (site != null)
094        {
095            UserIdentity user = _currentUserProvider.getUser();
096            Set<Object> roots = new HashSet<>();
097            try (AmetysObjectIterable<Sitemap> sitemaps = site.getSitemaps())
098            {
099                for (Sitemap sitemap: sitemaps)
100                {
101                    roots.add(_directoryHelper.getLinksNode(site, sitemap.getName()));
102                    if (user != null)
103                    {
104                        roots.add(_directoryHelper.getLinksForUserNode(site, siteName, user));
105                    }
106                }
107            }
108            return roots;
109        }
110        return null;
111    }
112    
113    @Override
114    protected I18nizableText getObjectLabelForExplanation(Object object) throws RightsException
115    {
116        return new I18nizableText("plugin.link-directory", "PLUGINS_LINKDIRECTORY_LINK_ACCESS_CONTROLLER_LINK_CONTEXT_LABEL", Map.of("title", getObjectLabel(object)));
117    }
118    
119    public I18nizableText getObjectLabel(Object object) throws RightsException
120    {
121        if (object instanceof DefaultLink link)
122        {
123            String title = link.getTitle();
124            if (StringUtils.isNotBlank(title))
125            {
126                return new I18nizableText(link.getLanguage().toUpperCase() + " > " + title + " (" + link.getUrl() + ")");
127            }
128            else
129            {
130                return new I18nizableText(link.getLanguage().toUpperCase() + " > " + link.getUrl());
131            }
132        }
133        throw new RightsException("Unsupported object " + object.toString());
134    }
135    
136    @Override
137    public I18nizableText getObjectCategory(Object object)
138    {
139        return LINK_DIRECTORY_CONTEXT_CATEGORY;
140    }
141}