Node.js 如何使用NodeJS和ldapjs更新Active Directory中的缩略图照片?
一般来说,我不熟悉使用Active Directory。我正在尝试使用Node.js 如何使用NodeJS和ldapjs更新Active Directory中的缩略图照片?,node.js,active-directory,ldap,ldapjs,octetstring,Node.js,Active Directory,Ldap,Ldapjs,Octetstring,一般来说,我不熟悉使用Active Directory。我正在尝试使用ldapjs更新thumbnailPhoto属性,我的代码设置可以更新属性,并且运行良好 我的用户是这样的: const customeParser = function(entry, raw, callback){ if (raw.hasOwnProperty("thumbnailPhoto")){ entry.thumbnailPhoto = raw.thumbnailPhoto; } callback
ldapjs
更新thumbnailPhoto
属性,我的代码设置可以更新属性,并且运行良好
我的用户是这样的:
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)
})
})
});
});
}
});
}
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(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)
})
})
});
});
}
});
}
我从LDAP类调用我的modifyUser
方法,如下所示:
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)
})
})
});
});
}
});
}
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(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)
})
})
});
});
}
});
}
当我使用LDAP工具更新照片时,它会将其转换为我认为的八位字节或某种十六进制值。我如何在nodejs中模仿它
我走对了吗?使用上面的代码可以立即更新所有其他属性,但图像无法更新
还有一种方法可以一次更新多个属性吗?我得到的错误是一次只能修改一个属性
提前感谢您的时间 我正在尝试创建一个缓冲区,但似乎不起作用…base64字符串太长。。。有人吗?我不太了解NodeJS,但是
thumbnailPhoto
只是图像文件的一个字节数组。它没有存储为base64。感谢@GabrielLuci,我将尝试研究如何将图像转换为字节数组…当我将其转换为字节数组时,我得到以下错误:error:LDAPError[ConstraintViolationError]:00002082:Aterrr:DSID-031518F9,#1:0:00002082:DSID-031518F9,问题1005(约束类型),数据0,Att 160023(缩略图照片):len 1209421
您是否尝试过使用此处描述的toString方法?