View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
18   *
19   */
20  
21  package org.apache.directory.server.core.authn.ppolicy;
22  
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.directory.api.ldap.model.name.Dn;
28  import org.apache.directory.server.core.api.authn.ppolicy.PasswordPolicyConfiguration;
29  
30  
31  /**
32   * A container to hold all the password policies defined in the server
33   * 
34   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
35   */
36  public class PpolicyConfigContainer
37  {
38  
39      /** a map holding the entry specific password policies */
40      private Map<Dn, PasswordPolicyConfiguration> ppolicyConfigMap = new HashMap<>();
41  
42      /** the default password policy Dn */
43      private Dn defaultPolicyDn;
44  
45  
46      /**
47       * add a entry specific policy
48       *
49       * @param configDn the Dn where this entry's password policy is defined
50       * @param policyConfig the password policy configuration
51       */
52      public void addPolicy( Dn configDn, PasswordPolicyConfiguration policyConfig )
53      {
54          if ( configDn == null )
55          {
56              throw new IllegalArgumentException( "password policy config's Dn cannot be null" );
57          }
58  
59          ppolicyConfigMap.put( configDn, policyConfig );
60      }
61  
62  
63      /**
64       * @return true if atleast one entry specific password policy exists, false otherwise
65       */
66      public boolean hasCustomConfigs()
67      {
68          return !ppolicyConfigMap.isEmpty();
69      }
70  
71  
72      /**
73       * Get the password policy configuration defined at a given Dn
74       *  
75       * @param configDn the Dn where password policy was configured
76       * @return The found PasswordPolicyConfiguration instance
77       */
78      public PasswordPolicyConfiguration getPolicyConfig( Dn configDn )
79      {
80          return ppolicyConfigMap.get( configDn );
81      }
82  
83  
84      /**
85       * @return the default password policy, null if not configured
86       */
87      public PasswordPolicyConfiguration getDefaultPolicy()
88      {
89          return getPolicyConfig( defaultPolicyDn );
90      }
91  
92  
93      /**
94       * Set the default password policy configuration's Dn
95       * 
96       * @param defaultPolicyDn the default password policy configuration's Dn 
97       */
98      public void setDefaultPolicyDn( Dn defaultPolicyDn )
99      {
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 }