How do you update a thumbnailPhoto in Active Directory using NodeJS and ldapjs?

566 views Asked by At

I'm new to working with Active Directory in general. I'm trying to update the thumbnailPhoto attribute using ldapjs npm package I have my code setup to be able to update attributes in general and it works well.

I get my user like so:

const customeParser = function(entry, raw, callback){ if (raw.hasOwnProperty("thumbnailPhoto")){ entry.thumbnailPhoto = raw.thumbnailPhoto; } callback(entry) }

find(filter, cb) {
        const client = ldap.createClient(this.ldapOptions)

        client.on('error', err => {
            console.error(err.message)
        })

        //Serach for users
        client.bind(
            this.options.dn,
            this.options.password,
            (err) => {
                if (err) {
                    console.error((new Date).toUTCString(), 'Initial Bind Error', err)
                    client.unbind(() => {
                        client.destroy()
                        cb(err)
                    })
                } else {
                    client.search(
                        'DC=foo,DC=local', {
                        scope: 'sub',
                        attributes: [
                            'distinguishedName',
                            'name',
                            'sn',
                            'givenName',
                            'mail',
                            'sAMAccountName',
                            'telephoneNumber',
                            'thumbnailPhoto', 
                            // 'photoURL', 
                            // 'profileImage',
                            'extensionAttribute1',
                            'msExchExtensionCustomAttribute1'

                        ],
                        entryParser: customeParser,
                        filter: `${filter}`
                    },
                        (err, ee) => {


                            if (err) {
                                console.log((new Date).toUTCString(), 'SEARCH RESULT', err)
                                client.unbind(() => {
                                    client.destroy()
                                    cb(err)
                                })
                            }
                            ee.on('searchEntry', (entry) => {
                                ee.on('end', () => {
                                    client.unbind(() => {
                                        client.destroy()
                                        cb(null, entry.object)
                                    })
                                })
                            });
                        });
                }
            });
    }

I call my modifyUser method from my LDAP class like so:

let changes = {
  thumbnailPhoto: 'http://<ip>:<port>/img/photo.jpg'
}

ad.modifyUser(user.dn, changes, function (err, mod) {
      if (err) {
        console.log('ERROR: ' + JSON.stringify(err));
        return;
      }

      if (!mod) {
        console.log('Search: ' + mod + ' not found.');
      } else {
        console.log('MOD: ', JSON.stringify(mod));
      }
    })

modifyUser:

modifyUser(user, attributes, cb) {
        const client = ldap.createClient(this.ldapOptions)

        client.on('error', err => {
            console.error(err.message)
        })

        //Serach for users
        client.bind(
            this.options.dn,
            this.options.password,
            (err) => {
                if (err) {
                    console.error((new Date).toUTCString(), 'Initial Bind Error', err)
                    client.unbind(() => {
                        client.destroy()
                        cb(err)
                    })
                } else {
                    var change = new ldap.Change({
                        operation: 'replace',
                        modification: attributes
                      });
                      console.log('CHANGE: ', attributes)
                      client.modify(user, change, function(err) {
                          if(err) console.error('ERROR: ', err);
                      },
                        (err, ee) => {
                            

                            if (err) {
                                console.log((new Date).toUTCString(), 'SEARCH RESULT', err)
                                client.unbind(() => {
                                    client.destroy()
                                    cb(err)
                                })
                            }
                            ee.on('searchEntry', (entry) => {
                                ee.on('end', () => {
                                    client.unbind(() => {
                                        client.destroy()
                                        cb(null, entry.object)
                                    })
                                })
                            });
                        });
                }
            });
    }

When I update the photo using my LDAP Tool it converts it to what i believe to be an octet or some sort of hex value. How do i mimic that in nodejs?

Am I on the right track? All other attributes get updated instantly using the code above, but the image fails to update...

Also is there a way to update multiple attributes at once? I get the error that only one attribute can be modified at a time.

Thanks in advance for your time!

0

There are 0 answers