LDAP User import per Cron

Post about your customizations to share with others.

Moderators: Developer, Contributor

Post Reply
hairmare
Posts: 1
Joined: 01 Oct 2008, 13:31

LDAP User import per Cron

Post by hairmare »

In a setup i recently did i needed to add users to the mantisdb automatically. I wrote the following script for that:

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
Most of the work is done using user_create and user_set_field (for insert and updates respectively) from user_api.php.

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.
Tesla986
Posts: 5
Joined: 27 Jan 2016, 17:40

Re: LDAP User import per Cron

Post by Tesla986 »

Hi,

what kind of script is it?

if i well understood you have a Linux or Unix situation, isn't it?

Colud be possible run this script on MS Windows?

thank you
Post Reply