|
| 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 | +package org.apache.cloudstack.ldap; |
| 20 | + |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.List; |
| 23 | + |
| 24 | +import javax.naming.NamingEnumeration; |
| 25 | +import javax.naming.NamingException; |
| 26 | +import javax.naming.directory.SearchControls; |
| 27 | +import javax.naming.directory.SearchResult; |
| 28 | +import javax.naming.ldap.LdapContext; |
| 29 | + |
| 30 | +import org.apache.commons.lang.StringUtils; |
| 31 | +import org.apache.log4j.Logger; |
| 32 | + |
| 33 | +public class ADLdapUserManagerImpl extends OpenLdapUserManagerImpl implements LdapUserManager { |
| 34 | + public static final Logger s_logger = Logger.getLogger(ADLdapUserManagerImpl.class.getName()); |
| 35 | + private static final String MICROSOFT_AD_NESTED_MEMBERS_FILTER = "memberOf:1.2.840.113556.1.4.1941"; |
| 36 | + |
| 37 | + @Override |
| 38 | + public List<LdapUser> getUsersInGroup(String groupName, LdapContext context) throws NamingException { |
| 39 | + if (StringUtils.isBlank(groupName)) { |
| 40 | + throw new IllegalArgumentException("ldap group name cannot be blank"); |
| 41 | + } |
| 42 | + |
| 43 | + String basedn = _ldapConfiguration.getBaseDn(); |
| 44 | + if (StringUtils.isBlank(basedn)) { |
| 45 | + throw new IllegalArgumentException("ldap basedn is not configured"); |
| 46 | + } |
| 47 | + |
| 48 | + final SearchControls searchControls = new SearchControls(); |
| 49 | + searchControls.setSearchScope(_ldapConfiguration.getScope()); |
| 50 | + searchControls.setReturningAttributes(_ldapConfiguration.getReturnAttributes()); |
| 51 | + |
| 52 | + NamingEnumeration<SearchResult> results = context.search(basedn, generateADGroupSearchFilter(groupName), searchControls); |
| 53 | + final List<LdapUser> users = new ArrayList<LdapUser>(); |
| 54 | + while (results.hasMoreElements()) { |
| 55 | + final SearchResult result = results.nextElement(); |
| 56 | + users.add(createUser(result)); |
| 57 | + } |
| 58 | + return users; |
| 59 | + } |
| 60 | + |
| 61 | + private String generateADGroupSearchFilter(String groupName) { |
| 62 | + final StringBuilder userObjectFilter = new StringBuilder(); |
| 63 | + userObjectFilter.append("(objectClass="); |
| 64 | + userObjectFilter.append(_ldapConfiguration.getUserObject()); |
| 65 | + userObjectFilter.append(")"); |
| 66 | + |
| 67 | + final StringBuilder memberOfFilter = new StringBuilder(); |
| 68 | + String groupCnName = _ldapConfiguration.getCommonNameAttribute() + "=" +groupName + "," + _ldapConfiguration.getBaseDn(); |
| 69 | + memberOfFilter.append("(" + MICROSOFT_AD_NESTED_MEMBERS_FILTER + ":="); |
| 70 | + memberOfFilter.append(groupCnName); |
| 71 | + memberOfFilter.append(")"); |
| 72 | + |
| 73 | + final StringBuilder result = new StringBuilder(); |
| 74 | + result.append("(&"); |
| 75 | + result.append(userObjectFilter); |
| 76 | + result.append(memberOfFilter); |
| 77 | + result.append(")"); |
| 78 | + |
| 79 | + s_logger.debug("group search filter = " + result); |
| 80 | + return result.toString(); |
| 81 | + } |
| 82 | +} |
0 commit comments