001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.security.ldap;
016    
017    import com.liferay.registry.Registry;
018    import com.liferay.registry.RegistryUtil;
019    import com.liferay.registry.ServiceTracker;
020    
021    import java.util.List;
022    
023    import javax.naming.Binding;
024    import javax.naming.directory.Attribute;
025    import javax.naming.directory.Attributes;
026    import javax.naming.directory.SearchResult;
027    import javax.naming.ldap.LdapContext;
028    
029    /**
030     * @author Edward C. Han
031     */
032    public class PortalLDAPUtil {
033    
034            public static LdapContext getContext(long ldapServerId, long companyId)
035                    throws Exception {
036    
037                    return getInstance().getContext(ldapServerId, companyId);
038            }
039    
040            public static LdapContext getContext(
041                            long companyId, String providerURL, String principal,
042                            String credentials)
043                    throws Exception {
044    
045                    return getInstance().getContext(
046                            companyId, providerURL, principal, credentials);
047            }
048    
049            public static Binding getGroup(
050                            long ldapServerId, long companyId, String groupName)
051                    throws Exception {
052    
053                    return getInstance().getGroup(ldapServerId, companyId, groupName);
054            }
055    
056            public static Attributes getGroupAttributes(
057                            long ldapServerId, long companyId, LdapContext ldapContext,
058                            String fullDistinguishedName)
059                    throws Exception {
060    
061                    return getInstance().getGroupAttributes(
062                            ldapServerId, companyId, ldapContext, fullDistinguishedName);
063            }
064    
065            public static Attributes getGroupAttributes(
066                            long ldapServerId, long companyId, LdapContext ldapContext,
067                            String fullDistinguishedName, boolean includeReferenceAttributes)
068                    throws Exception {
069    
070                    return getInstance().getGroupAttributes(
071                            ldapServerId, companyId, ldapContext, fullDistinguishedName,
072                            includeReferenceAttributes);
073            }
074    
075            public static byte[] getGroups(
076                            long companyId, LdapContext ldapContext, byte[] cookie,
077                            int maxResults, String baseDN, String groupFilter,
078                            List<SearchResult> searchResults)
079                    throws Exception {
080    
081                    return getInstance().getGroups(
082                            companyId, ldapContext, cookie, maxResults, baseDN, groupFilter,
083                            searchResults);
084            }
085    
086            public static byte[] getGroups(
087                            long companyId, LdapContext ldapContext, byte[] cookie,
088                            int maxResults, String baseDN, String groupFilter,
089                            String[] attributeIds, List<SearchResult> searchResults)
090                    throws Exception {
091    
092                    return getInstance().getGroups(
093                            companyId, ldapContext, cookie, maxResults, baseDN, groupFilter,
094                            attributeIds, searchResults);
095            }
096    
097            public static byte[] getGroups(
098                            long ldapServerId, long companyId, LdapContext ldapContext,
099                            byte[] cookie, int maxResults, List<SearchResult> searchResults)
100                    throws Exception {
101    
102                    return getInstance().getGroups(
103                            ldapServerId, companyId, ldapContext, cookie, maxResults,
104                            searchResults);
105            }
106    
107            public static byte[] getGroups(
108                            long ldapServerId, long companyId, LdapContext ldapContext,
109                            byte[] cookie, int maxResults, String[] attributeIds,
110                            List<SearchResult> searchResults)
111                    throws Exception {
112    
113                    return getInstance().getGroups(
114                            ldapServerId, companyId, ldapContext, cookie, maxResults,
115                            attributeIds, searchResults);
116            }
117    
118            public static String getGroupsDN(long ldapServerId, long companyId)
119                    throws Exception {
120    
121                    return getInstance().getGroupsDN(ldapServerId, companyId);
122            }
123    
124            public static long getLdapServerId(
125                            long companyId, String screenName, String emailAddress)
126                    throws Exception {
127    
128                    return getInstance().getLdapServerId(
129                            companyId, screenName, emailAddress);
130            }
131    
132            public static Attribute getMultivaluedAttribute(
133                            long companyId, LdapContext ldapContext, String baseDN,
134                            String filter, Attribute attribute)
135                    throws Exception {
136    
137                    return getInstance().getMultivaluedAttribute(
138                            companyId, ldapContext, baseDN, filter, attribute);
139            }
140    
141            public static String getNameInNamespace(
142                            long ldapServerId, long companyId, Binding binding)
143                    throws Exception {
144    
145                    return getInstance().getNameInNamespace(
146                            ldapServerId, companyId, binding);
147            }
148    
149            public static Binding getUser(
150                            long ldapServerId, long companyId, String screenName,
151                            String emailAddress)
152                    throws Exception {
153    
154                    return getInstance().getUser(
155                            ldapServerId, companyId, screenName, emailAddress);
156            }
157    
158            public static Binding getUser(
159                            long ldapServerId, long companyId, String screenName,
160                            String emailAddress, boolean checkOriginalEmail)
161                    throws Exception {
162    
163                    return getInstance().getUser(
164                            ldapServerId, companyId, screenName, emailAddress,
165                            checkOriginalEmail);
166            }
167    
168            public static Attributes getUserAttributes(
169                            long ldapServerId, long companyId, LdapContext ldapContext,
170                            String fullDistinguishedName)
171                    throws Exception {
172    
173                    return getInstance().getUserAttributes(
174                            ldapServerId, companyId, ldapContext, fullDistinguishedName);
175            }
176    
177            public static byte[] getUsers(
178                            long companyId, LdapContext ldapContext, byte[] cookie,
179                            int maxResults, String baseDN, String userFilter,
180                            List<SearchResult> searchResults)
181                    throws Exception {
182    
183                    return getInstance().getUsers(
184                            companyId, ldapContext, cookie, maxResults, baseDN, userFilter,
185                            searchResults);
186            }
187    
188            public static byte[] getUsers(
189                            long companyId, LdapContext ldapContext, byte[] cookie,
190                            int maxResults, String baseDN, String userFilter,
191                            String[] attributeIds, List<SearchResult> searchResults)
192                    throws Exception {
193    
194                    return getInstance().getUsers(
195                            companyId, ldapContext, cookie, maxResults, baseDN, userFilter,
196                            attributeIds, searchResults);
197            }
198    
199            public static byte[] getUsers(
200                            long ldapServerId, long companyId, LdapContext ldapContext,
201                            byte[] cookie, int maxResults, List<SearchResult> searchResults)
202                    throws Exception {
203    
204                    return getInstance().getUsers(
205                            ldapServerId, companyId, ldapContext, cookie, maxResults,
206                            searchResults);
207            }
208    
209            public static byte[] getUsers(
210                            long ldapServerId, long companyId, LdapContext ldapContext,
211                            byte[] cookie, int maxResults, String[] attributeIds,
212                            List<SearchResult> searchResults)
213                    throws Exception {
214    
215                    return getInstance().getUsers(
216                            ldapServerId, companyId, ldapContext, cookie, maxResults,
217                            attributeIds, searchResults);
218            }
219    
220            public static String getUsersDN(long ldapServerId, long companyId)
221                    throws Exception {
222    
223                    return getInstance().getUsersDN(ldapServerId, companyId);
224            }
225    
226            public static boolean hasUser(
227                            long ldapServerId, long companyId, String screenName,
228                            String emailAddress)
229                    throws Exception {
230    
231                    return getInstance().hasUser(
232                            ldapServerId, companyId, screenName, emailAddress);
233            }
234    
235            public static boolean isGroupMember(
236                            long ldapServerId, long companyId, String groupDN, String userDN)
237                    throws Exception {
238    
239                    return getInstance().isGroupMember(
240                            ldapServerId, companyId, groupDN, userDN);
241            }
242    
243            public static boolean isUserGroupMember(
244                            long ldapServerId, long companyId, String groupDN, String userDN)
245                    throws Exception {
246    
247                    return getInstance().isUserGroupMember(
248                            ldapServerId, companyId, groupDN, userDN);
249            }
250    
251            public static byte[] searchLDAP(
252                            long companyId, LdapContext ldapContext, byte[] cookie,
253                            int maxResults, String baseDN, String filter, String[] attributeIds,
254                            List<SearchResult> searchResults)
255                    throws Exception {
256    
257                    return getInstance().searchLDAP(
258                            companyId, ldapContext, cookie, maxResults, baseDN, filter,
259                            attributeIds, searchResults);
260            }
261    
262            private static PortalLDAP getInstance() {
263                    return _instance._serviceTracker.getService();
264            }
265    
266            private PortalLDAPUtil() {
267                    Registry registry = RegistryUtil.getRegistry();
268    
269                    _serviceTracker = registry.trackServices(PortalLDAP.class);
270    
271                    _serviceTracker.open();
272            }
273    
274            private static final PortalLDAPUtil _instance = new PortalLDAPUtil();
275    
276            private final ServiceTracker<PortalLDAP, PortalLDAP> _serviceTracker;
277    
278    }