Skip to content
Snippets Groups Projects
Commit 5bbaee8b authored by Kevin Paxman's avatar Kevin Paxman
Browse files

EXPHR: get LDAP lookup working for bulk account creation

parent ad200c89
No related branches found
No related tags found
3 merge requests!297Feature/istwcms 5983 bulk account creation,!274Draft: ISTWCMS-5551: fixing office hours display,!260Feature/istwcms 5668 a5kulkar rename references to publications
...@@ -68,32 +68,36 @@ class UwAddUsersForm extends FormBase { ...@@ -68,32 +68,36 @@ class UwAddUsersForm extends FormBase {
// Don't process long user IDs. // Don't process long user IDs.
if (strlen($user) > 8) { if (strlen($user) > 8) {
$this->messenger()->addError($this->t('The user ID <em>@user</em> is too long and was skipped.', array('@user' => $user))); $this->messenger()->addError($this->t('The user ID <em>@user</em> is too long and was skipped.', [ '@user' => $user ] ));
continue; continue;
} }
// Don't process user IDs with invalid characters. // Don't process user IDs with invalid characters.
if (!preg_match('/^[a-z0-9]+$/', $user)) { if (!preg_match('/^[a-z0-9]+$/', $user)) {
$this->messenger()->addError($this->t('The user ID <em>@user</em> contains invalid characters and was skipped.', array('@user' => $user))); $this->messenger()->addError($this->t('The user ID <em>@user</em> contains invalid characters and was skipped.', [ '@user' => $user ] ));
continue; continue;
} }
$existing_user = user_load_by_name($user); $existing_user = user_load_by_name($user);
if ($existing_user) { if ($existing_user) {
$this->messenger()->addError($this->t('The user ID <a href="@link"><em>@user</em></a> already exists and was skipped.', array('@link' => \Drupal::service('path_alias.manager')->getAliasByPath('/user/' . $existing_user->uid->value),'@user' => $user))); $this->messenger()->addError($this->t('The user ID <a href="@link"><em>@user</em></a> already exists and was skipped.',
[
'@link' => \Drupal::service('path_alias.manager')->getAliasByPath('/user/' . $existing_user->uid->value),
'@user' => $user,
]
));
continue; continue;
} }
// Seems OK. // Seems OK - move on.
// Create a strong random password. // Create a strong random password.
$sets[] = 'abcdefghijklmnopqrstuvwxyz'; $sets[] = 'abcdefghijklmnopqrstuvwxyz';
$sets[] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $sets[] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
$sets[] = '0123456789'; $sets[] = '0123456789';
$sets[] = '!@#$%^&*()-_=+[]{}?`~"'; $sets[] = '!@#$%^&*()-_=+[]{}?`~"';
$password = ''; $password = '';
$password_length = rand(16,32); $password_length = rand(16, 32);
while(strlen($password) < $password_length) { while (strlen($password) < $password_length) {
foreach ($sets as $set) { foreach ($sets as $set) {
$password .= $set[array_rand(str_split($set))]; $password .= $set[array_rand(str_split($set))];
if (strlen($password) == $password_length) { if (strlen($password) == $password_length) {
...@@ -103,19 +107,33 @@ class UwAddUsersForm extends FormBase { ...@@ -103,19 +107,33 @@ class UwAddUsersForm extends FormBase {
} }
$password = str_shuffle($password); $password = str_shuffle($password);
// Do the LDAP lookup.
$person = \Drupal::service('uw_cfg_common.uw_ldap')->lookupPerson($user);
if (!$person) {
$this->messenger()->addError($this->t('The user ID <em>@user</em> could not be found (or was set to private) and was skipped.', [ '@user' => $user ] ));
continue;
}
$firstname = empty($person['givenname'][0]) ? 'Firstname' : $person['givenname'][0];
$lastname = empty($person['sn'][0]) ? 'Lastname' : $person['sn'][0];
$email = empty($person['mail'][0]) ? $user . '@uwaterloo.ca' : $person['mail'][0];
// Add the user. // Add the user.
// TODO: look up info from WatIAM.
$new_user = \Drupal\user\Entity\User::create(); $new_user = \Drupal\user\Entity\User::create();
$new_user->setPassword($password); $new_user->setPassword($password);
$new_user->enforceIsNew(); $new_user->enforceIsNew();
$new_user->setEmail($user . '@uwaterloo.ca'); $new_user->setEmail($email);
$new_user->setUsername($user); $new_user->setUsername($user);
$new_user->set('field_uw_first_name', 'Firstname'); $new_user->set('field_uw_first_name', $firstname);
$new_user->set('field_uw_last_name', 'Lastname'); $new_user->set('field_uw_last_name', $lastname);
$new_user->activate(); $new_user->activate();
$new_user->save(); $new_user->save();
$this->messenger()->addStatus($this->t('Created a new user with the user ID <a href="@link"><em>@user</em></a>.', array('@link' => \Drupal::service('path_alias.manager')->getAliasByPath('/user/' . $new_user->uid->value),'@user' => $user))); $this->messenger()->addStatus($this->t('Created a new user with the user ID <a href="@link"><em>@user</em></a>.',
[
'@link' => \Drupal::service('path_alias.manager')->getAliasByPath('/user/' . $new_user->uid->value),
'@user' => $user,
]
));
} }
// Set the message that the users have been created. // Set the message that the users have been created.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment