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    /** a map holding the entry specific password policies */
039    private Map<Dn, PasswordPolicyConfiguration> ppolicyConfigMap = new HashMap<>();
040
041    /** the default password policy Dn */
042    private Dn defaultPolicyDn;
043
044
045    /**
046     * add a entry specific policy
047     *
048     * @param configDn the Dn where this entry's password policy is defined
049     * @param policyConfig the password policy configuration
050     */
051    public void addPolicy( Dn configDn, PasswordPolicyConfiguration policyConfig )
052    {
053        if ( configDn == null )
054        {
055            throw new IllegalArgumentException( "password policy config's Dn cannot be null" );
056        }
057
058        ppolicyConfigMap.put( configDn, policyConfig );
059    }
060
061
062    /**
063     * @return true if atleast one entry specific password policy exists, false otherwise
064     */
065    public boolean hasCustomConfigs()
066    {
067        return !ppolicyConfigMap.isEmpty();
068    }
069
070
071    /**
072     * Get the password policy configuration defined at a given Dn
073     *  
074     * @param configDn the Dn where password policy was configured
075     * @return The found PasswordPolicyConfiguration instance
076     */
077    public PasswordPolicyConfiguration getPolicyConfig( Dn configDn )
078    {
079        return ppolicyConfigMap.get( configDn );
080    }
081
082
083    /**
084     * @return the default password policy, null if not configured
085     */
086    public PasswordPolicyConfiguration getDefaultPolicy()
087    {
088        return getPolicyConfig( defaultPolicyDn );
089    }
090
091
092    /**
093     * Set the default password policy configuration's Dn
094     * 
095     * @param defaultPolicyDn the default password policy configuration's Dn 
096     */
097    public void setDefaultPolicyDn( Dn defaultPolicyDn )
098    {
099        this.defaultPolicyDn = defaultPolicyDn;
100    }
101
102
103    /**
104     * deactivate an existing password policy.
105     *  
106     * @param ppolicyConfigDn the Dn of the password policy configuration
107     * @return the deactivated password policy config object of the given reference Dn, null otherwise
108     */
109    public PasswordPolicyConfiguration removePolicyConfig( Dn ppolicyConfigDn )
110    {
111        return ppolicyConfigMap.remove( ppolicyConfigDn );
112    }
113}