001/*
002 *  Copyright 2017 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.proxiedcontent;
017
018import java.util.Map;
019
020import org.apache.avalon.framework.component.Component;
021import org.apache.avalon.framework.logger.AbstractLogEnabled;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.avalon.framework.service.Serviceable;
025
026import org.ametys.core.authentication.CredentialProvider;
027import org.ametys.core.ui.Callable;
028import org.ametys.core.user.population.UserPopulation;
029import org.ametys.core.user.population.UserPopulationDAO;
030import org.ametys.plugins.extrausermgt.authentication.cas.CASCredentialProvider;
031
032/**
033 * Helper for the revamping service
034 */
035public class RevampingHelper extends AbstractLogEnabled implements Serviceable, Component
036{
037    /** Avalon Role */
038    public static final String ROLE = RevampingHelper.class.getName();
039    
040    private static final String __PARAM_PROXY_TICKETS = "authentication.cas.requestProxyTickets"; 
041    
042    /** The DAO for user populations */
043    protected UserPopulationDAO _userPopulationDAO;
044
045    public void service(ServiceManager manager) throws ServiceException
046    {
047        _userPopulationDAO = (UserPopulationDAO) manager.lookup(UserPopulationDAO.ROLE);
048    }
049    
050    /**
051     * Check if the revamping through CAS is currently available or not
052     * @return True if the revamping through CAS is available
053     */
054    @Callable
055    public boolean isCasAvailable()
056    {
057        for (UserPopulation userPopulation : _userPopulationDAO.getEnabledUserPopulations(false))
058        {
059            for (CredentialProvider credentialProvider : userPopulation.getCredentialProviders())
060            {
061                if (credentialProvider instanceof CASCredentialProvider)
062                {
063                    Map<String, Object> parameterValues = credentialProvider.getParameterValues();
064                    if (parameterValues.containsKey(__PARAM_PROXY_TICKETS) && (boolean) parameterValues.getOrDefault(__PARAM_PROXY_TICKETS, false))
065                    {
066                        return true;
067                    }
068                }
069            }
070        }
071        
072        return false;
073    }
074
075}