001/*
002 *   Licensed to the Apache Software Foundation (ASF) under one
003 *   or more contributor license agreements.  See the NOTICE file
004 *   distributed with this work for additional information
005 *   regarding copyright ownership.  The ASF licenses this file
006 *   to you under the Apache License, Version 2.0 (the
007 *   "License"); you may not use this file except in compliance
008 *   with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *   Unless required by applicable law or agreed to in writing,
013 *   software distributed under the License is distributed on an
014 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *   KIND, either express or implied.  See the License for the
016 *   specific language governing permissions and limitations
017 *   under the License.
018 *
019 */
020
021package org.apache.directory.server.core.authn.ppolicy;
022
023
024import java.util.HashMap;
025import java.util.Map;
026
027import org.apache.directory.api.ldap.model.name.Dn;
028import org.apache.directory.server.core.api.authn.ppolicy.PasswordPolicyConfiguration;
029
030
031/**
032 * A container to hold all the password policies defined in the server
033 * 
034 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
035 */
036public class PpolicyConfigContainer
037{
038
039    /** a map holding the entry specific password policies */
040    private Map<Dn, PasswordPolicyConfiguration> ppolicyConfigMap = new HashMap<>();
041
042    /** the default password policy Dn */
043    private Dn defaultPolicyDn;
044
045
046    /**
047     * add a entry specific policy
048     *
049     * @param configDn the Dn where this entry's password policy is defined
050     * @param policyConfig the password policy configuration
051     */
052    public void addPolicy( Dn configDn, PasswordPolicyConfiguration policyConfig )
053    {
054        if ( configDn == null )
055        {
056            throw new IllegalArgumentException( "password policy config's Dn cannot be null" );
057        }
058
059        ppolicyConfigMap.put( configDn, policyConfig );
060    }
061
062
063    /**
064     * @return true if atleast one entry specific password policy exists, false otherwise
065     */
066    public boolean hasCustomConfigs()
067    {
068        return !ppolicyConfigMap.isEmpty();
069    }
070
071
072    /**
073     * Get the password policy configuration defined at a given Dn
074     *  
075     * @param configDn the Dn where password policy was configured
076     * @return The found PasswordPolicyConfiguration instance
077     */
078    public PasswordPolicyConfiguration getPolicyConfig( Dn configDn )
079    {
080        return ppolicyConfigMap.get( configDn );
081    }
082
083
084    /**
085     * @return the default password policy, null if not configured
086     */
087    public PasswordPolicyConfiguration getDefaultPolicy()
088    {
089        return getPolicyConfig( defaultPolicyDn );
090    }
091
092
093    /**
094     * Set the default password policy configuration's Dn
095     * 
096     * @param defaultPolicyDn the default password policy configuration's Dn 
097     */
098    public void setDefaultPolicyDn( Dn defaultPolicyDn )
099    {
100        this.defaultPolicyDn = defaultPolicyDn;
101    }
102
103
104    /**
105     * deactivate an existing password policy.
106     *  
107     * @param ppolicyConfigDn the Dn of the password policy configuration
108     * @return the deactivated password policy config object of the given reference Dn, null otherwise
109     */
110    public PasswordPolicyConfiguration removePolicyConfig( Dn ppolicyConfigDn )
111    {
112        return ppolicyConfigMap.remove( ppolicyConfigDn );
113    }
114}