001/*
002 *  Copyright 2024 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.odf.rights;
017
018import org.apache.avalon.framework.activity.Initializable;
019import org.apache.avalon.framework.component.Component;
020import org.apache.avalon.framework.service.ServiceException;
021import org.apache.avalon.framework.service.ServiceManager;
022import org.apache.avalon.framework.service.Serviceable;
023
024import org.ametys.core.cache.AbstractCacheManager;
025import org.ametys.core.cache.Cache;
026import org.ametys.core.user.UserIdentity;
027import org.ametys.odf.ProgramItem;
028import org.ametys.plugins.core.impl.cache.AbstractCacheKey;
029import org.ametys.plugins.repository.AmetysObjectIterable;
030import org.ametys.plugins.repository.AmetysObjectResolver;
031import org.ametys.runtime.i18n.I18nizableText;
032import org.ametys.runtime.plugin.component.AbstractLogEnabled;
033
034/**
035 * Helper for access controler on role attributes
036 */
037public class ODFRoleAccessControllerHelper extends AbstractLogEnabled implements Component, Serviceable, Initializable
038{
039    /** The avalon role */
040    public static final String ROLE = ODFRoleAccessControllerHelper.class.getName();
041    
042    private static final String __CACHE_ID = ODFRoleAccessControllerHelper.class.getName() + "$cache";
043    
044    /** The Ametys object resolver */
045    protected AmetysObjectResolver _resolver;
046    /** The cache manager */
047    protected AbstractCacheManager _cacheManager;
048    /** The ODF right heper */
049    protected ODFRightHelper _odfRightHelper;
050
051    public void service(ServiceManager smanager) throws ServiceException
052    {
053        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
054        _cacheManager = (AbstractCacheManager) smanager.lookup(AbstractCacheManager.ROLE);
055        _odfRightHelper = (ODFRightHelper) smanager.lookup(ODFRightHelper.ROLE);
056    }
057    
058    @Override
059    public void initialize() throws Exception
060    {
061        if (!_cacheManager.hasCache(__CACHE_ID))
062        {
063            _cacheManager.createRequestCache(__CACHE_ID,
064                new I18nizableText("plugin.odf", "PLUGINS_ODF_CACHE_ROLE_ACCESS_CONTROLLER_LABEL"),
065                new I18nizableText("plugin.odf", "PLUGINS_ODF_CACHE_ROLE_ACCESS_CONTROLLER_DESCRIPTION"),
066                false
067            );
068        }
069    }
070    
071    /**
072     * Determines if user has a ODF role on any program item
073     * @param user The user
074     * @param roleAttributeName the attribute name for role
075     * @return true if the user has a ODF role on at least a program item
076     */
077    public boolean hasODFRoleOnAnyProgramItem(UserIdentity user, String roleAttributeName)
078    {
079        Cache<CacheKey, Boolean> cache = _getCache();
080        
081        CacheKey key = CacheKey.of(user, roleAttributeName);
082        return cache.get(key, __ -> _hasODFRoleOnAnyProgramItem(user, roleAttributeName));
083    }
084    
085    private boolean _hasODFRoleOnAnyProgramItem(UserIdentity user, String roleAttributeName)
086    {
087        AmetysObjectIterable<ProgramItem> programItems = _odfRightHelper.getProgramItemsWithUserAsRole(user, roleAttributeName);
088        return programItems.getSize() != 0;
089    }
090    
091    static class CacheKey extends AbstractCacheKey
092    {
093        CacheKey(UserIdentity userIdentity, String roleAttributePath)
094        {
095            super(userIdentity, roleAttributePath);
096        }
097
098        static CacheKey of(UserIdentity userIdentity, String roleAttributePath)
099        {
100            return new CacheKey(userIdentity, roleAttributePath);
101        } 
102    }
103    
104    private Cache<CacheKey, Boolean> _getCache()
105    {
106        return _cacheManager.get(__CACHE_ID);
107    }
108}