Monday, September 6, 2010

Redmine - Bulk Import for LDAP users

If you configure LDAP for authentication, users are not visible unless they have at least logged in once. This is not ideal in terms of usability. You have to tell your users to login but they can't do anything because there are not member of any project (yet) and you can't add them because they don't exist (chicken and egg).


To solve this we created a PHP script to implement Bulk Import in Redmine database.

/* ----------------- PHP script ------------------- */





//Postgres Variables

$pg_sqldb_host = "pghost";
$pg_sqldb_user = "pguser";
$pg_sqldb_pass = "pgpass";
$pg_sqldb_name = "redmine";


// LDAP variables
$ldaphost = "ldap.server.com";  // your ldap servers
$ldapport = 389;  // your ldap server's port number


// Connecting to LDAP
$ldapconn = ldap_connect($ldaphost, $ldapport) or die("Could not connect to $ldaphost");


ldap_set_option($ldapconn, LDAP_OPT_PROTOCOL_VERSION, 3);
ldap_set_option($ldapconn, LDAP_OPT_REFERRALS, 0);


$login = ldap_bind( $ldapconn, "ldapuser", "ldappass" );


$attributes = array("uid", "displayname", "mail");
$filter = "(uid=*)";


$result = ldap_search($ldapconn, $search_base, $filter, $attributes) or die("error in query") ;


$data = ldap_get_entries($ldapconn, $result);


$redmine_link = pg_connect("host=$pg_sqldb_host port=5432 dbname=$pg_sqldb_name user=$pg_sqldb_user password=$pg_sqldb_pass") or die("Could not connect to Redmine: " . pg_last_error());




$query = "select MAX(id)+1 from users";
$result = pg_query($redmine_link, $query);
$nextid = pg_fetch_result($result, 0, 0);


for ($i=0; $i<=$data["count"];$i++) {


$query = "select id from users where login='" . $data[$i]["uid"][0] . "'";
$result = pg_query($redmine_link, $query);


$rows = pg_num_rows($result);

if($rows == 0 )
{
$name = split(" " , $data[$i]["displayname"][0]);


$query = "INSERT INTO users (id, login, firstname, lastname, mail,        mail_notification, admin, status, language, auth_source_id, created_on, type)
VALUES($nextid,'" . $data[$i]["uid"][0] . "','" . $name[0] . "','" . $name[1] . "','" . $data[$i]["mail"][0] . "',false,false,1,'en',$auth_source_id,CURRENT_TIMESTAMP,'User')";


$result = pg_query($redmine_link, $query);
$rows = pg_affected_rows($result);


//user doesn't exists add user
echo ("User " . $data[$i]["uid"][0] . " added successfully
");

$nextid++;
}
else
{
echo ("User " . $data[$i]["uid"][0] . " already exists
");

}
}


ldap_unbind($login);
ldap_close($ldapconn);


print("Users imported successfully
");



?>

/* ----------------- end of script ------------------- */

You may add script in schedule to run every 24 hours.

Linux : add php script in crontab. 
0 0 * * * *  root /user/bin/php /path/phpscript >> /var/log/logfile

Windows: add php script in windows scheduler.

Comments are most welcome.

1 comment:

Unknown said...


Thank you a lot for sharing this with all of us you really recognise what you are speaking approximately! Bookmarked. Please also visit my site =). We will have a hyperlink trade agreement between us craigslist colorado springs