diff --git a/plugins/cas_auth_unl/add_user.php b/plugins/cas_auth_unl/add_user.php new file mode 100644 index 0000000000000000000000000000000000000000..f5f51d704ef12134c03d098319d60d60f61ae93a --- /dev/null +++ b/plugins/cas_auth_unl/add_user.php @@ -0,0 +1,62 @@ +<?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;