By ronnqvist on
Hi!
I'm developing my very first Drupal module, so I'm probably doing something wrong... or else I just found a bug. Hope you can help me find out what I might have done wrong. :)
My module is supposed to populate certain values from LDAP to the user's profile upon each login, not doing authentication as opposed to the ldap_integration module. (I'm going to use it alongside with the webserver_auth module.)
The problem is that user_save doesn't do anything for me, except for when the user doesn't have an LDAP entry... then it generates a new entry into the users table in the database (but not containing any useful info of course).
// $Id$
/*
simpldap module
Populates certain values from LDAP to the user's profile upon each login,
hence this is not an authentication module (such as the much more complex ldap_auth module).
(a bit of code stolen from: http://drupal.org/node/111768 )
*/
function simpldap_user($op, &$edit, &$account, $category = NULL) {
global $user;
if ($op == 'login') {
$ldap_server = 'ldap://ldap.arcada.fi/';
$ds=ldap_connect($ldap_server);
if ($ds) {
$user_name = $user->name;
$r=ldap_bind($ds);
// TODO - What to do if user doesn't exist?
$sr=ldap_search($ds, "ou=People,dc=arcada,dc=fi", "(uid=$user_name)");
$info = ldap_get_entries($ds, $sr);
$user_mail = $info[0]['mail'][0];
$user_cn = $info[0]['cn'][0];
// TODO - Make any empty field's value NULL before writing to profile.
# Inspired from: http://drupal.org/node/28379
$myuser = user_load($user->uid);
$ldap_profile = array(
'mail' => $user_mail,
'cn' => $user_cn,
);
user_save($myuser, $ldap_profile);
ldap_close($ds);
} else {
drupal_set_message(t('Unable to contact Ldap server %name.', array('%name' => check_plain($ldap_server))));
return;
}
}
}
Thanks for the help in advance! :)
cheers, Simon
Comments
Mystery solved
The problem is that the webserver_auth module never pulls off user_login only user_load. I solved the problem by changing
to
I don't know if this is a bad idea. The good thing is that it works without having to change anything in the webserver_auth module... :) Another good thing about it is that the users never can get into the illusion of changing something unchangeable, finding it changed back upon the next login. Instead he'll immediately see that nothing was changed. However, the users ability to change these ldap populated values should be disabled in the first place.
The downside is that it causes extra load on the ldap server (and the database server), but in my case performance probably won't be such a big issue.
Another downside is that with webserver_auth it's called upon each pageload, unless you do this: http://drupal.org/node/137908
BTW. user_save() doesn't work like that, I got my thing working using db_query() instead.