public static function BulkAddCasUsers::userAdd in CAS 8
Same name and namespace in other branches
- 2.x src/Form/BulkAddCasUsers.php \Drupal\cas\Form\BulkAddCasUsers::userAdd()
Perform a single CAS user creation batch operation.
Callback for batch_set().
Parameters
string $cas_username: The CAS username, which will also become the Drupal username.
array $roles: An array of roles to assign to the user.
array $context: The batch context array, passed by reference.
string $email_hostname: The hostname to combine with the username to create the email address.
File
- src/
Form/ BulkAddCasUsers.php, line 129
Class
- BulkAddCasUsers
- Class BulkAddCasUsers.
Namespace
Drupal\cas\FormCode
public static function userAdd($cas_username, array $roles, $email_hostname, array &$context) {
$cas_user_manager = \Drupal::service('cas.user_manager');
// Back out of an account already has this CAS username.
$existing_uid = $cas_user_manager
->getUidForCasUsername($cas_username);
if ($existing_uid) {
$context['results']['messages']['already_exists'][] = $cas_username;
return;
}
$user_properties = [
'roles' => $roles,
'mail' => $cas_username . '@' . $email_hostname,
];
try {
/** @var \Drupal\user\UserInterface $user */
$user = $cas_user_manager
->register($cas_username, $user_properties, $cas_username);
$context['results']['messages']['created'][] = $user
->toLink()
->toString();
} catch (CasLoginException $e) {
\Drupal::logger('cas')
->error('CasLoginException when registering user with name %name: %e', [
'%name' => $cas_username,
'%e' => $e
->getMessage(),
]);
$context['results']['messages']['errors'][] = $cas_username;
return;
}
}