1
+ var ldap = require ( 'ldapjs' ) ;
2
+ var ldapCfg = require ( '../../config' ) ;
3
+
4
+
5
+ var client = ldap . createClient ( {
6
+ url : ldapCfg . ldap_host_url ,
7
+ idleTimeout : 200
8
+ } ) ;
9
+
10
+ function bind ( ) {
11
+ client = ldap . createClient ( {
12
+ url : ldapCfg . ldap_host_url ,
13
+ idleTimeout : 200
14
+ } ) ;
15
+ client . bind ( ldapCfg . ldap_bind_user , ldapCfg . ldap_bind_password , function ( err ) {
16
+ if ( err != null ) {
17
+ console . log ( err ) ;
18
+ } else {
19
+ console . log ( 'LDAP-Client successful binded.' ) ;
20
+ }
21
+ } ) ;
22
+ }
23
+
24
+
25
+ exports . changePassword = function ( userId , oldPassword , newPassword ) {
26
+ bind ( ) ;
27
+ client . search ( ldapCfg . ldap_base_dn , opts = {
28
+ filter : ldapCfg . ldap_user_prefix + userId ,
29
+ scope : 'sub' ,
30
+ attributes : [ 'dn' ]
31
+ } , function ( err , res ) {
32
+ res . on ( 'searchEntry' , function ( entry ) {
33
+ var userDN = entry . object . dn ;
34
+ client . bind ( userDN , oldPassword , function ( err ) {
35
+ client . modify ( userDN , [
36
+ new ldap . Change ( {
37
+ operation : 'replace' ,
38
+ modification : {
39
+ userPassword : newPassword
40
+ }
41
+ } )
42
+ ] , function ( err ) {
43
+ if ( err ) {
44
+ console . log ( err . code ) ;
45
+ console . log ( err . name ) ;
46
+ console . log ( err . message ) ;
47
+ }
48
+ else {
49
+ console . log ( 'Password changed!' ) ;
50
+ }
51
+ } ) ;
52
+ } ) ;
53
+ } ) ;
54
+ res . on ( 'end' , function ( result ) {
55
+ console . log ( 'Status: ' + result . status ) ;
56
+ } ) ;
57
+ } ) ;
58
+
59
+ } ;
60
+
61
+ exports . userExists = function ( userId ) {
62
+ bind ( ) ;
63
+ client . search ( ldapCfg . ldap_base_dn , opts = {
64
+ filter : ldapCfg . ldap_user_prefix + userId ,
65
+ scope : 'sub' ,
66
+ attributes : [ 'dn' ]
67
+ } , function ( err , res ) {
68
+ if ( err != null )
69
+ console . log ( err ) ;
70
+
71
+ res . on ( 'searchEntry' , function ( _entry ) {
72
+ client . userAvailable = true ;
73
+ console . log ( 'entry: ' + JSON . stringify ( _entry . object ) ) ;
74
+ } ) ;
75
+
76
+ res . on ( 'error' , function ( err ) {
77
+ console . error ( 'error: ' + err . message ) ;
78
+ } ) ;
79
+
80
+ res . on ( 'end' , function ( result ) {
81
+ console . log ( 'LDAP-Client successful search. Status: ' + result . status ) ;
82
+ client . unbind ( ) ;
83
+ } ) ;
84
+
85
+ } ) ;
86
+
87
+ } ;
0 commit comments