At work I've got LDAP (Active Directory) integration working just fine. However, I also wanted to pull profile information (Job Title, Department, Phone Number, etc.) out of Active Directory so there's a central repository for that type of information.
Of course there's the LDAPdata which can integrate data from Active Directory into Profile module fields, but I wanted some solution to make Profiles into Nodes and integrating LDAP data into that node as CCK fields. I looked around for ideas or solutions to this problem but didn't find much. (Please if there's already a better solution, point me to that.)
New to Drupal module development, I hacked the below code together mostly from code stolen from the LDAPdata module. I would love some feedback on the code (since this is the first custom module I've done) as well as any ways to improve it. It's very hack-ish right now, with a lot of stuff hard coded into the module, and later on that could be pushed out into configuration pages and better tie-in to the LDAP module and CCK fields on the Bio node.
Here it is, any feedback is welcome.
<?php
$GLOBALS['ldapbio_ldap'] = new LDAPInterface();
/<strong>
* Implements hook_user()
</strong>/
function ldapbio_user($op, &$edit, &$user, $category = NULL) {
switch($op) {
case 'login':
ldapbio_user_login($user);
break;
case 'after_update':
ldapbio_user_login($user);
break;
}
}
/*************************************/
function ldapbio_user_login(&$user) {
global $ldapbio_ldap;
if (!$user->ldap_authentified) {
return;
}
if (!_ldapbio_ldap_init($user)) {
return;
}
if (!$ldapbio_ldap->connect('user@example.com', 'password')) { //here's an account that has read rights to your LDAP
watchdog('user', "User load: user $user->name's data could not be read in the LDAP directory", WATCHDOG_WARNING);
return;
}
$entry = $ldapbio_ldap->retrieveAttributes($user->ldap_dn);
$node = array( //here are the fields we want to replace with LDAP data
"title" => $entry['displayname'][0],
"promote" => 0,
"uid" => $user->uid,
"type" => 'bio',
"status" => 1,
"revision" =>1,
"field_title" => array(0 => array('value' => $entry['title'][0])),
"field_department" => array(0 => array('value' => $entry['department'][0])),
"field_email" => array(0 => array('value' => strtolower($entry['mail'][0]))),
"field_phone" => array(0 => array('value' => $entry['telephonenumber'][0])),
"field_fax" => array(0 => array('value' => $entry['facsimiletelephonenumber'][0])),
);
if ($row = db_fetch_object(db_query("SELECT nid FROM {bio} WHERE uid = '%s'", $user->uid))) { //this determines if there's already a bio node for this user, if so it updates it
$node["nid"] = $row->nid;
}
if ($node = node_submit($node)) {
node_save($node);
}
}
function _ldapbio_ldap_init(&$user) {
global $ldapbio_ldap;
if ($row = db_fetch_object(db_query("SELECT * FROM {ldapauth} WHERE status = '%s' AND name = '%s'", 1, $user->ldap_config))) {
$ldapbio_ldap = new LDAPInterface();
$ldapbio_ldap->setOption('name', $row->name);
$ldapbio_ldap->setOption('server', $row->server);
$ldapbio_ldap->setOption('port', $row->port);
$ldapbio_ldap->setOption('tls', $row->tls);
$ldapbio_ldap->setOption('encrypted', $row->encrypted);
$ldapbio_ldap->setOption('basedn', $row->basedn);
$ldapbio_ldap->setOption('user_attr', $row->user_attr);
return $ldapbio_ldap;
}
else {
return;
}
}
?>