View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.jetspeed.security.spi.impl.ldap;
18  
19  import java.security.Principal;
20  
21  import javax.naming.directory.Attributes;
22  import javax.naming.directory.BasicAttribute;
23  import javax.naming.directory.BasicAttributes;
24  
25  import org.apache.commons.lang.StringUtils;
26  import org.apache.jetspeed.security.SecurityException;
27  import org.apache.jetspeed.security.impl.RolePrincipalImpl;
28  
29  /***
30   * <p>
31   * DAO for handling group objects.
32   * </p>
33   * 
34   * @author Davy De Waele
35   */
36  public class LdapRoleDaoImpl extends LdapPrincipalDaoImpl
37  {
38  
39      /***
40       * <p>
41       * Default constructor.
42       * </p>
43       * 
44       * @throws SecurityException A {@link SecurityException}.
45       */
46      public LdapRoleDaoImpl() throws SecurityException
47      {
48          super();
49      }
50  
51      /***
52       * <p>
53       * Initializes the dao.
54       * </p>
55       * 
56       * @param ldapConfig Holds the ldap binding configuration.
57       * @throws SecurityException A {@link SecurityException}.
58       */
59      public LdapRoleDaoImpl(LdapBindingConfig ldapConfig) throws SecurityException
60      {
61          super(ldapConfig);
62      }
63  
64      /***
65       * <p>
66       * A template method for defining the attributes for a particular LDAP class.
67       * </p>
68       * 
69       * @param principalUid The principal uid.
70       * @return The LDAP attributes object for the particular class.
71       */
72      protected Attributes defineLdapAttributes(final String principalUid)
73      {
74          Attributes attrs = new BasicAttributes(true);
75          BasicAttribute classes = new BasicAttribute("objectclass");
76  
77          for (int i=0;i<getObjectClasses().length;i++)
78          	classes.add(getObjectClasses()[i]);
79          attrs.put(classes);
80          attrs.put(getEntryPrefix(), principalUid);
81          if(!StringUtils.isEmpty(getRoleObjectRequiredAttributeClasses()))
82          	attrs.put(getRoleObjectRequiredAttributeClasses(), "");
83          for (int i=0;i<getAttributes().length;i++)
84          	attrs.put(parseAttr(getAttributes()[i],principalUid)[0], parseAttr(getAttributes()[i],principalUid)[1]);
85          return attrs;
86      }
87  
88      /***
89       * @see org.apache.jetspeed.security.spi.impl.ldap.LdapPrincipalDaoImpl#getDnSuffix()
90       */
91      protected String getDnSuffix()
92      {
93          return this.getRoleFilterBase();
94      }
95  
96      /***
97       * <p>
98       * Creates a GroupPrincipal object.
99       * </p>
100      * 
101      * @param principalUid The principal uid.
102      * @return A group principal object.
103      */
104     protected Principal makePrincipal(String principalUid)
105     {
106         return new RolePrincipalImpl(principalUid);
107     }
108 
109 	protected String getEntryPrefix() {
110 		return this.getRoleIdAttribute();
111 	}
112 	
113 	protected String getSearchSuffix() {
114 		return this.getRoleFilter();
115 	}
116 
117 	protected String getSearchDomain() {
118 		return this.getRoleFilterBase();
119 	}	
120 
121 	protected String[] getObjectClasses() {
122 		return this.getRoleObjectClasses();
123 	}
124 
125 	protected String getUidAttributeForPrincipal() {
126 		return this.getRoleUidAttribute();
127 	}
128 
129 	protected String[] getAttributes() {
130 		return getRoleAttributes();
131 	}
132 	
133 	
134 }
135