001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.security.spi.impl.ldap;
018:
019: import java.security.Principal;
020:
021: import javax.naming.directory.Attributes;
022: import javax.naming.directory.BasicAttribute;
023: import javax.naming.directory.BasicAttributes;
024:
025: import org.apache.commons.lang.StringUtils;
026: import org.apache.jetspeed.security.SecurityException;
027: import org.apache.jetspeed.security.impl.RolePrincipalImpl;
028:
029: /**
030: * <p>
031: * DAO for handling group objects.
032: * </p>
033: *
034: * @author Davy De Waele
035: */
036: public class LdapRoleDaoImpl extends LdapPrincipalDaoImpl {
037:
038: /**
039: * <p>
040: * Default constructor.
041: * </p>
042: *
043: * @throws SecurityException A {@link SecurityException}.
044: */
045: public LdapRoleDaoImpl() throws SecurityException {
046: super ();
047: }
048:
049: /**
050: * <p>
051: * Initializes the dao.
052: * </p>
053: *
054: * @param ldapConfig Holds the ldap binding configuration.
055: * @throws SecurityException A {@link SecurityException}.
056: */
057: public LdapRoleDaoImpl(LdapBindingConfig ldapConfig)
058: throws SecurityException {
059: super (ldapConfig);
060: }
061:
062: /**
063: * <p>
064: * A template method for defining the attributes for a particular LDAP class.
065: * </p>
066: *
067: * @param principalUid The principal uid.
068: * @return The LDAP attributes object for the particular class.
069: */
070: protected Attributes defineLdapAttributes(final String principalUid) {
071: Attributes attrs = new BasicAttributes(true);
072: BasicAttribute classes = new BasicAttribute("objectclass");
073:
074: for (int i = 0; i < getObjectClasses().length; i++)
075: classes.add(getObjectClasses()[i]);
076: attrs.put(classes);
077: attrs.put(getEntryPrefix(), principalUid);
078: if (!StringUtils
079: .isEmpty(getRoleObjectRequiredAttributeClasses())) {
080: String key = getRoleObjectRequiredAttributeClasses();
081: if (key.indexOf(',') >= 0) {
082: String[] allKeys = key.split(",");
083: for (int i = 0; i < allKeys.length; i++)
084: attrs.put(allKeys[i], "");
085: } else {
086: attrs.put(getRoleObjectRequiredAttributeClasses(), "");
087: }
088: }
089: for (int i = 0; i < getAttributes().length; i++)
090: attrs.put(parseAttr(getAttributes()[i], principalUid)[0],
091: parseAttr(getAttributes()[i], principalUid)[1]);
092: return attrs;
093: }
094:
095: /**
096: * @see org.apache.jetspeed.security.spi.impl.ldap.LdapPrincipalDaoImpl#getDnSuffix()
097: */
098: protected String getDnSuffix() {
099: return this .getRoleFilterBase();
100: }
101:
102: /**
103: * <p>
104: * Creates a GroupPrincipal object.
105: * </p>
106: *
107: * @param principalUid The principal uid.
108: * @return A group principal object.
109: */
110: protected Principal makePrincipal(String principalUid) {
111: return new RolePrincipalImpl(principalUid);
112: }
113:
114: protected String getEntryPrefix() {
115: return this .getRoleIdAttribute();
116: }
117:
118: protected String getSearchSuffix() {
119: return this .getRoleFilter();
120: }
121:
122: protected String getSearchDomain() {
123: return this .getRoleFilterBase();
124: }
125:
126: protected String[] getObjectClasses() {
127: return this .getRoleObjectClasses();
128: }
129:
130: protected String getUidAttributeForPrincipal() {
131: return this .getRoleUidAttribute();
132: }
133:
134: protected String[] getAttributes() {
135: return getRoleAttributes();
136: }
137:
138: }
|