Create a gist now

Instantly share code, notes, and snippets.

#!/usr/bin/perl -w
#
# here's a script (in Perl) that can produce the ldif file directly from you database. You just need to adjust the values mentionned in the script.
#
# The script assumes you LDAP server uses inetOrgPerson objects to hold your users. If you use other object types (e.g., posixAccount), you may need to add or remove attributes from the list in the script.
#
# The script outputs the LDIF contents to screen, so you should redirect the output to a file. Then remove the entries for guest, admin and other users you don't want to create in your LDAP server, and once you are satisfied, use ldapadd to import the users in your LDAP server.
#
# Hope this helps.
#
# Saludos. Iñaki.
# link: http://moodle.org/mod/forum/discuss.php?d=114680
# Author: Nurettin Senyer
# e-mail: nurettin.senyer@bil.omu.edu.tr
use MIME::Base64 qw(encode_base64);
use DBI;
# ----- Configure these values below to match your moodle setup -------#
$moodle_db = 'moodle';
$moodle_user = 'moodle';
$moodle_password = '55xxx1xxx';
# ----- Configure these values below to match your LDAP setup ------#
$moodle_ou = "moodleusers";
$ou = "ou=$moodle_ou,dc=debuntu,dc=local";
$objectClass = "inetOrgPerson";
# ----------------------------------------------------------------------------- #
$dbh = DBI->connect('DBI:mysql:'.$moodle_db, $moodle_user, $moodle_password);
$sth = $dbh->prepare('select username, firstname, lastname, email, password from mdl_user where deleted = 0');
$sth->execute();
print "# $moodle_ou organizationalUnit i icin gerekli\n";
print "dn: $ou\n";
print "objectClass:organizationalUnit\n";
print "objectClass: top\n";
print "ou: $moodle_ou\n\n";
print "# moodle db sindeki kullanicilar\n";
while (@row = $sth->fetchrow_array()) {
print "dn: cn=$row[0],$ou\n";
print "objectClass: $objectClass\n";
print "uid: $row[0]\n";
print "givenName: $row[1]\n";
print "sn: $row[2]\n";
print "mail: $row[3]\n";
if ($row[4] ne '') {
$password = '{MD5}'. encode_base64(pack ("H32", $row[4]));
print "userPassword: $password\n";
}
print "\n";
}
$sth->finish();
$dbh->disconnect();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment