001/*
002 *  Copyright 2016 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.core.datasource;
017
018import java.io.File;
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.commons.collections.MapUtils;
024
025import org.ametys.plugins.core.impl.checker.LDAPConnectionChecker;
026import org.ametys.runtime.parameter.ParameterChecker;
027import org.ametys.runtime.parameter.ParameterCheckerTestFailureException;
028import org.ametys.runtime.util.AmetysHomeHelper;
029
030/**
031 * This component handles SQL data sources. 
032 * It is associated with the configuration file $AMETYS_HOME/config/datasources-ldap.xml 
033 */
034public class LDAPDataSourceManager extends AbstractDataSourceManager
035{
036    /** Avalon Role */
037    public static final String ROLE = LDAPDataSourceManager.class.getName();
038    
039    /** LDAP parameter's name for base URL */
040    public static final String PARAM_BASE_URL = "baseURL";
041    /** LDAP parameter's name for base DN */
042    public static final String PARAM_BASE_DN = "baseDN";
043    /** LDAP parameter's name for use SSL */
044    public static final String PARAM_USE_SSL = "useSSL";
045    /** LDAP parameter's name for alias dereferencing */
046    public static final String PARAM_ALIAS_DEREFERENCING = "aliasDereferencing";
047    /** LDAP parameter's name for follow referrals property */
048    public static final String PARAM_FOLLOW_REFERRALS = "followReferrals";
049    /** LDAP parameter's name for authentication method */
050    public static final String PARAM_AUTHENTICATION_METHOD = "authenticationMethod";
051    /** LDAP parameter's name for administrator DN */
052    public static final String PARAM_ADMIN_DN = "adminDN";
053    /** LDAP parameter's name for administration password */
054    public static final String PARAM_ADMIN_PASSWORD = "adminPassword";
055    
056    /** The id of the internal DataSource */
057    public static final String LDAP_DATASOURCE_PREFIX = "LDAP-";
058    
059    private static String __filename;
060    
061    /**
062     * Set the config filename. Only use for tests.
063     * @param filename Name with path of the config file
064     */
065    public static void setFilename(String filename)
066    {
067        __filename = filename;
068    }
069    
070    @Override
071    public File getFileConfiguration()
072    {
073        if (__filename != null)
074        {
075            return new File(__filename);
076        }
077        
078        return new File(AmetysHomeHelper.getAmetysHomeConfig(), "datasources-ldap.xml");
079    }
080    
081    @Override
082    protected String getDataSourcePrefixId()
083    {
084        return LDAP_DATASOURCE_PREFIX;
085    }
086    
087    @Override
088    public void checkParameters(Map<String, String> rawParameters) throws ParameterCheckerTestFailureException
089    {
090        // Order the parameters
091        List<String> values = new ArrayList<> ();
092        values.add(rawParameters.get(PARAM_BASE_URL));
093        values.add(rawParameters.get(PARAM_AUTHENTICATION_METHOD));
094        values.add(rawParameters.get(PARAM_ADMIN_DN));
095        values.add(rawParameters.get(PARAM_ADMIN_PASSWORD));
096        values.add(rawParameters.get(PARAM_USE_SSL));
097        values.add(rawParameters.get(PARAM_FOLLOW_REFERRALS));
098        values.add(rawParameters.get(PARAM_BASE_DN));
099        
100        ParameterChecker paramChecker = new LDAPConnectionChecker();
101        paramChecker.check(values);
102    }
103    
104    @Override
105    protected void createDataSource(DataSourceDefinition dataSource)
106    {
107        // Empty
108    }
109    
110    @Override
111    protected void deleteDataSource(DataSourceDefinition dataSource)
112    {
113        // Empty
114    }
115
116    @Override
117    protected void editDataSource(DataSourceDefinition dataSource)
118    {
119        // Empty
120    }
121    
122    @Override
123    protected void internalSetDefaultDataSource()
124    {
125        // Arbitrarily set the LDAP default data source
126        if (MapUtils.isNotEmpty(_dataSourcesDef))
127        {
128            DataSourceDefinition defaultDataSourceDef = _dataSourcesDef.values().iterator().next();
129            defaultDataSourceDef.setDefault(true);
130            _dataSourcesDef.put(defaultDataSourceDef.getId(), defaultDataSourceDef);
131            
132            saveConfiguration();
133            editDataSource(defaultDataSourceDef);
134            
135        }
136    }
137}