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.portal.kernel.util.ProxyFactory;
018    
019    import java.util.List;
020    
021    import javax.naming.Binding;
022    import javax.naming.directory.Attribute;
023    import javax.naming.directory.Attributes;
024    import javax.naming.directory.SearchResult;
025    import javax.naming.ldap.LdapContext;
026    
027    /**
028     * @author Edward C. Han
029     */
030    public class PortalLDAPUtil {
031    
032            public static LdapContext getContext(long ldapServerId, long companyId)
033                    throws Exception {
034    
035                    return getInstance().getContext(ldapServerId, companyId);
036            }
037    
038            public static LdapContext getContext(
039                            long companyId, String providerURL, String principal,
040                            String credentials)
041                    throws Exception {
042    
043                    return getInstance().getContext(
044                            companyId, providerURL, principal, credentials);
045            }
046    
047            public static Binding getGroup(
048                            long ldapServerId, long companyId, String groupName)
049                    throws Exception {
050    
051                    return getInstance().getGroup(ldapServerId, companyId, groupName);
052            }
053    
054            public static Attributes getGroupAttributes(
055                            long ldapServerId, long companyId, LdapContext ldapContext,
056                            String fullDistinguishedName)
057                    throws Exception {
058    
059                    return getInstance().getGroupAttributes(
060                            ldapServerId, companyId, ldapContext, fullDistinguishedName);
061            }
062    
063            public static Attributes getGroupAttributes(
064                            long ldapServerId, long companyId, LdapContext ldapContext,
065                            String fullDistinguishedName, boolean includeReferenceAttributes)
066                    throws Exception {
067    
068                    return getInstance().getGroupAttributes(
069                            ldapServerId, companyId, ldapContext, fullDistinguishedName,
070                            includeReferenceAttributes);
071            }
072    
073            public static byte[] getGroups(
074                            long companyId, LdapContext ldapContext, byte[] cookie,
075                            int maxResults, String baseDN, String groupFilter,
076                            List<SearchResult> searchResults)
077                    throws Exception {
078    
079                    return getInstance().getGroups(
080                            companyId, ldapContext, cookie, maxResults, baseDN, groupFilter,
081                            searchResults);
082            }
083    
084            public static byte[] getGroups(
085                            long companyId, LdapContext ldapContext, byte[] cookie,
086                            int maxResults, String baseDN, String groupFilter,
087                            String[] attributeIds, List<SearchResult> searchResults)
088                    throws Exception {
089    
090                    return getInstance().getGroups(
091                            companyId, ldapContext, cookie, maxResults, baseDN, groupFilter,
092                            attributeIds, searchResults);
093            }
094    
095            public static byte[] getGroups(
096                            long ldapServerId, long companyId, LdapContext ldapContext,
097                            byte[] cookie, int maxResults, List<SearchResult> searchResults)
098                    throws Exception {
099    
100                    return getInstance().getGroups(
101                            ldapServerId, companyId, ldapContext, cookie, maxResults,
102                            searchResults);
103            }
104    
105            public static byte[] getGroups(
106                            long ldapServerId, long companyId, LdapContext ldapContext,
107                            byte[] cookie, int maxResults, String[] attributeIds,
108                            List<SearchResult> searchResults)
109                    throws Exception {
110    
111                    return getInstance().getGroups(
112                            ldapServerId, companyId, ldapContext, cookie, maxResults,
113                            attributeIds, searchResults);
114            }
115    
116            public static String getGroupsDN(long ldapServerId, long companyId)
117                    throws Exception {
118    
119                    return getInstance().getGroupsDN(ldapServerId, companyId);
120            }
121    
122            public static long getLdapServerId(
123                            long companyId, String screenName, String emailAddress)
124                    throws Exception {
125    
126                    return getInstance().getLdapServerId(
127                            companyId, screenName, emailAddress);
128            }
129    
130            public static Attribute getMultivaluedAttribute(
131                            long companyId, LdapContext ldapContext, String baseDN,
132                            String filter, Attribute attribute)
133                    throws Exception {
134    
135                    return getInstance().getMultivaluedAttribute(
136                            companyId, ldapContext, baseDN, filter, attribute);
137            }
138    
139            public static String getNameInNamespace(
140                            long ldapServerId, long companyId, Binding binding)
141                    throws Exception {
142    
143                    return getInstance().getNameInNamespace(
144                            ldapServerId, companyId, binding);
145            }
146    
147            public static Binding getUser(
148                            long ldapServerId, long companyId, String screenName,
149                            String emailAddress)
150                    throws Exception {
151    
152                    return getInstance().getUser(
153                            ldapServerId, companyId, screenName, emailAddress);
154            }
155    
156            public static Binding getUser(
157                            long ldapServerId, long companyId, String screenName,
158                            String emailAddress, boolean checkOriginalEmail)
159                    throws Exception {
160    
161                    return getInstance().getUser(
162                            ldapServerId, companyId, screenName, emailAddress,
163                            checkOriginalEmail);
164            }
165    
166            public static Attributes getUserAttributes(
167                            long ldapServerId, long companyId, LdapContext ldapContext,
168                            String fullDistinguishedName)
169                    throws Exception {
170    
171                    return getInstance().getUserAttributes(
172                            ldapServerId, companyId, ldapContext, fullDistinguishedName);
173            }
174    
175            public static byte[] getUsers(
176                            long companyId, LdapContext ldapContext, byte[] cookie,
177                            int maxResults, String baseDN, String userFilter,
178                            List<SearchResult> searchResults)
179                    throws Exception {
180    
181                    return getInstance().getUsers(
182                            companyId, ldapContext, cookie, maxResults, baseDN, userFilter,
183                            searchResults);
184            }
185    
186            public static byte[] getUsers(
187                            long companyId, LdapContext ldapContext, byte[] cookie,
188                            int maxResults, String baseDN, String userFilter,
189                            String[] attributeIds, List<SearchResult> searchResults)
190                    throws Exception {
191    
192                    return getInstance().getUsers(
193                            companyId, ldapContext, cookie, maxResults, baseDN, userFilter,
194                            attributeIds, searchResults);
195            }
196    
197            public static byte[] getUsers(
198                            long ldapServerId, long companyId, LdapContext ldapContext,
199                            byte[] cookie, int maxResults, List<SearchResult> searchResults)
200                    throws Exception {
201    
202                    return getInstance().getUsers(
203                            ldapServerId, companyId, ldapContext, cookie, maxResults,
204                            searchResults);
205            }
206    
207            public static byte[] getUsers(
208                            long ldapServerId, long companyId, LdapContext ldapContext,
209                            byte[] cookie, int maxResults, String[] attributeIds,
210                            List<SearchResult> searchResults)
211                    throws Exception {
212    
213                    return getInstance().getUsers(
214                            ldapServerId, companyId, ldapContext, cookie, maxResults,
215                            attributeIds, searchResults);
216            }
217    
218            public static String getUsersDN(long ldapServerId, long companyId)
219                    throws Exception {
220    
221                    return getInstance().getUsersDN(ldapServerId, companyId);
222            }
223    
224            public static boolean hasUser(
225                            long ldapServerId, long companyId, String screenName,
226                            String emailAddress)
227                    throws Exception {
228    
229                    return getInstance().hasUser(
230                            ldapServerId, companyId, screenName, emailAddress);
231            }
232    
233            public static boolean isGroupMember(
234                            long ldapServerId, long companyId, String groupDN, String userDN)
235                    throws Exception {
236    
237                    return getInstance().isGroupMember(
238                            ldapServerId, companyId, groupDN, userDN);
239            }
240    
241            public static boolean isUserGroupMember(
242                            long ldapServerId, long companyId, String groupDN, String userDN)
243                    throws Exception {
244    
245                    return getInstance().isUserGroupMember(
246                            ldapServerId, companyId, groupDN, userDN);
247            }
248    
249            public static byte[] searchLDAP(
250                            long companyId, LdapContext ldapContext, byte[] cookie,
251                            int maxResults, String baseDN, String filter, String[] attributeIds,
252                            List<SearchResult> searchResults)
253                    throws Exception {
254    
255                    return getInstance().searchLDAP(
256                            companyId, ldapContext, cookie, maxResults, baseDN, filter,
257                            attributeIds, searchResults);
258            }
259    
260            private static PortalLDAP getInstance() {
261                    return _instance;
262            }
263    
264            private static final PortalLDAP _instance =
265                    ProxyFactory.newServiceTrackedInstance(PortalLDAP.class);
266    
267    }