Skip to content
Snippets Groups Projects
Commit 6acbb485 authored by Michael Fairchild's avatar Michael Fairchild
Browse files

add a script to add a user

parent e0364c63
No related branches found
No related tags found
No related merge requests found
<?php
require_once __DIR__ . '/../../elgg/vendor/autoload.php';
if (!isset($argv[1])) {
echo "usage: php add_user.php username" . PHP_EOL;
exit();
}
\Elgg\Application::start();
$uid = $argv[1];
$info = array();
if (!$json = @file_get_contents(UnlCAS::DIRECTORY_URL . '?uid=' . $uid . '&format=json')) {
echo 'unable to get that ID' . PHP_EOL;
exit();
}
if (!$json = json_decode($json, true)) {
echo 'error decoding data' . PHP_EOL;
exit();
}
$map = array(
'givenName' => 'first_name',
'sn' => 'last_name',
'mail' => 'email'
);
foreach ($map as $from => $to) {
if (isset($json[$from][0])) {
$info[$to] = $json[$from][0];
}
}
$info['name'] = $info['first_name'] . ' ' . $info['last_name'];
if (!isset($info['email'])) {
echo 'no email is available' . PHP_EOL;
exit();
}
$password = generate_random_cleartext_password();
$elgg_uid = str_replace('-', '_', $uid);
$elgg_uid = 'unl_' . $elgg_uid;
try {
$user_guid = register_user($elgg_uid, $password, $info['name'], $info['email'], false);
} catch (RegistrationException $e) {
//Looks like we had an invalid email address... ask for it again.
echo 'Unable to register, probably due to invalid email address or the user already exists' . PHP_EOL;
exit();
}
if (!$user_guid) {
echo 'Unable to register' . PHP_EOL;
exit();
}
echo 'User added: ' . $elgg_uid . PHP_EOL;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment