1
+ /*
2
+ * Copyright 2016 gitblit.com.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5
+ * use this file except in compliance with the License. You may obtain a copy of
6
+ * the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ * License for the specific language governing permissions and limitations under
14
+ * the License.
15
+ */
1
16
package com .gitblit .ldap ;
2
17
3
18
import java .net .URI ;
@@ -69,6 +84,16 @@ public static final String escapeLDAPSearchFilter(String filter) {
69
84
70
85
71
86
87
+ public static String getAccountBase (IStoredSettings settings ) {
88
+ return settings .getString (Keys .realm .ldap .accountBase , "" );
89
+ }
90
+
91
+ public static String getAccountPattern (IStoredSettings settings ) {
92
+ return settings .getString (Keys .realm .ldap .accountPattern , "(&(objectClass=person)(sAMAccountName=${username}))" );
93
+ }
94
+
95
+
96
+
72
97
public LdapConnection (IStoredSettings settings ) {
73
98
this .settings = settings ;
74
99
@@ -82,6 +107,16 @@ public LdapConnection(IStoredSettings settings) {
82
107
83
108
84
109
110
+ public String getAccountBase () {
111
+ return getAccountBase (settings );
112
+ }
113
+
114
+ public String getAccountPattern () {
115
+ return getAccountPattern (settings );
116
+ }
117
+
118
+
119
+
85
120
public boolean connect () {
86
121
try {
87
122
URI ldapUrl = new URI (settings .getRequiredString (Keys .realm .ldap .server ));
@@ -198,36 +233,6 @@ public boolean rebindAsUser() {
198
233
199
234
200
235
201
- public SearchResult search (SearchRequest request ) {
202
- try {
203
- return conn .search (request );
204
- } catch (LDAPSearchException e ) {
205
- logger .error ("Problem Searching LDAP [{}]" , e .getResultCode ());
206
- return e .getSearchResult ();
207
- }
208
- }
209
-
210
-
211
- public SearchResult search (String base , boolean dereferenceAliases , String filter , List <String > attributes ) {
212
- try {
213
- SearchRequest searchRequest = new SearchRequest (base , SearchScope .SUB , filter );
214
- if (dereferenceAliases ) {
215
- searchRequest .setDerefPolicy (DereferencePolicy .SEARCHING );
216
- }
217
- if (attributes != null ) {
218
- searchRequest .setAttributes (attributes );
219
- }
220
- SearchResult result = search (searchRequest );
221
- return result ;
222
-
223
- } catch (LDAPException e ) {
224
- logger .error ("Problem creating LDAP search" , e );
225
- return null ;
226
- }
227
- }
228
-
229
-
230
-
231
236
public boolean isAuthenticated (String userDn , String password ) {
232
237
verifyCurrentBinding ();
233
238
@@ -267,6 +272,51 @@ public boolean isAuthenticated(String userDn, String password) {
267
272
268
273
269
274
275
+
276
+ public SearchResult search (SearchRequest request ) {
277
+ try {
278
+ return conn .search (request );
279
+ } catch (LDAPSearchException e ) {
280
+ logger .error ("Problem Searching LDAP [{}]" , e .getResultCode ());
281
+ return e .getSearchResult ();
282
+ }
283
+ }
284
+
285
+
286
+ public SearchResult search (String base , boolean dereferenceAliases , String filter , List <String > attributes ) {
287
+ try {
288
+ SearchRequest searchRequest = new SearchRequest (base , SearchScope .SUB , filter );
289
+ if (dereferenceAliases ) {
290
+ searchRequest .setDerefPolicy (DereferencePolicy .SEARCHING );
291
+ }
292
+ if (attributes != null ) {
293
+ searchRequest .setAttributes (attributes );
294
+ }
295
+ SearchResult result = search (searchRequest );
296
+ return result ;
297
+
298
+ } catch (LDAPException e ) {
299
+ logger .error ("Problem creating LDAP search" , e );
300
+ return null ;
301
+ }
302
+ }
303
+
304
+
305
+ public SearchResult searchUser (String username , List <String > attributes ) {
306
+
307
+ String accountPattern = getAccountPattern ();
308
+ accountPattern = StringUtils .replace (accountPattern , "${username}" , escapeLDAPSearchFilter (username ));
309
+
310
+ return search (getAccountBase (), false , accountPattern , attributes );
311
+ }
312
+
313
+
314
+ public SearchResult searchUser (String username ) {
315
+ return searchUser (username , null );
316
+ }
317
+
318
+
319
+
270
320
private boolean verifyCurrentBinding () {
271
321
BindRequest lastBind = conn .getLastBindRequest ();
272
322
if (lastBind == currentBindRequest ) {
0 commit comments