From 6acbb4852242f2b2fc1e64e240db8dc424b1fdf8 Mon Sep 17 00:00:00 2001 From: Michael Fairchild <mfairchild365@gmail.com> Date: Thu, 14 Jul 2016 10:08:16 -0500 Subject: [PATCH] add a script to add a user --- plugins/cas_auth_unl/add_user.php | 62 +++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 plugins/cas_auth_unl/add_user.php diff --git a/plugins/cas_auth_unl/add_user.php b/plugins/cas_auth_unl/add_user.php new file mode 100644 index 00000000..f5f51d70 --- /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; -- GitLab