Code: Select all
#!/bin/sh
# dump all the ldap Users and add them to the userdb
# most config stuff (db user, etc) is taken from mantis
# while this script works fine, it probably doesn't perform well
# with lots of users. in my scenario i only have a couple hundred
# users and i dont expect any problems.
# configure the following line so it knows how to access your server
LDAP="ldapsearch -LLL -D cn=Manager,dc=example,dc=com -wsecretpasswd "
for LDAP_UID in `$LDAP '(&(objectClass=inetOrgPerson)(uid=*))' uid | awk '/^uid:/ {print $2}'`; do
REALNAME=`$LDAP "(&(objectClass=inetOrgPerson)(uid=$LDAP_UID))" cn | awk -F ": " '/^cn:/ {print $2}'`
EMAIL=`$LDAP "(&(objectClass=inetOrgPerson)(uid=$LDAP_UID))" mail | awk '/^mail:/ {print $2}'`
MANTIS_USER_ID=`php -r 'include("/var/www/localhost/htdocs/mantisbt/config_defaults_inc.php");
include("/var/www/localhost/htdocs/mantisbt/config_inc.php");
include("/var/www/localhost/htdocs/mantisbt/core.php");
echo user_get_id_by_name("'$LDAP_UID'");'`
if [[ "$MANTIS_USER_ID" -eq "" ]]; then
echo '<?php
include("/var/www/localhost/htdocs/mantisbt/core/constant_inc.php");
include("/var/www/localhost/htdocs/mantisbt/config_defaults_inc.php");
include("/var/www/localhost/htdocs/mantisbt/config_inc.php");
include("/var/www/localhost/htdocs/mantisbt/core.php");
user_create("'$LDAP_UID'", "", "'$EMAIL'");
$user_id = user_get_id_by_name("'$LDAP_UID'");
user_set_field($user_id, "realname", "'$REALNAME'");' | php
else
echo '<?php
include("/var/www/localhost/htdocs/mantisbt/core/constant_inc.php");
include("/var/www/localhost/htdocs/mantisbt/config_defaults_inc.php");
include("/var/www/localhost/htdocs/mantisbt/config_inc.php");
include("/var/www/localhost/htdocs/mantisbt/core.php");
user_set_field('$MANTIS_USER_ID', "username", "'$LDAP_UID'");
user_set_field('$MANTIS_USER_ID', "realname", "'$REALNAME'");
user_set_field('$MANTIS_USER_ID', "email", "'$EMAIL'");' | php
fi
done
The users can be located anywhere in the ldap tree, all they need is objectClass=inetOrgPerson. The cn, uid and mail fields get used for realname, username and email. Users are identified by the uid/username so dont change that as admin in mantis or the affected user will just get recreated.
I run this script hourly with cron and haven't had any problems so far, but the system its on isn't really in productive use yet.
My Env: Mantis 1.1.2, php 5.2.6, gentoo 2.6.x. You will also need bash, awk, ldapsearch and the command line php executable.