o365-2.0.3/modules/o365_sso/o365_sso.install
modules/o365_sso/o365_sso.install
<?php
/**
* @file
* Install and update hooks for the o365_sso module.
*/
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
/**
* Implements hook_install().
*/
function o365_sso_install() {
module_set_weight('o365_sso', 100);
}
/**
* Set module weigh and enable external auth module.
*/
function o365_sso_update_9001() {
module_set_weight('o365_sso', 100);
\Drupal::service('module_installer')->install(['externalauth']);
}
/**
* Removed.
*/
function o365_sso_update_9002() {
}
/**
* Automatically enable the permissions for anonymous users for the login page.
*/
function o365_sso_update_9003() {
}
/**
* Remove the access o365_sso login page permission for anonymous users.
*/
function o365_sso_update_9004() {
// Removed on behalf of o365_sso_update_9005.
}
/**
* Remove the access o365_sso login page permission for anonymous users.
*/
function o365_sso_update_9005() {
$anonymous = Role::load('anonymous');
if ($anonymous && $anonymous->hasPermission('access o365_sso login page')) {
$anonymous->revokePermission('access o365_sso login page');
$anonymous->save();
}
}
/**
* Enable the new "Log out from Microsoft 365" by default for security reasons.
*/
function o365_sso_update_10001() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('o365_sso.settings');
$config->set('logout_office', TRUE);
$config->save(TRUE);
}
/**
* Update all existing o365 SSO users to lowercase their email.
*/
function o365_sso_update_10002(&$sandbox) {
if (!isset($sandbox['total'])) {
$results = \Drupal::database()->select('authmap', 'a')
->fields('a', ['uid'])
->condition('a.provider', 'o365_sso')
->range(0, 25)
->execute();
$users = $results->fetchAll();
$sandbox['total'] = count($users);
$sandbox['current'] = 0;
if (empty($sandbox['total'])) {
$sandbox['#finished'] = 1;
return;
}
}
$usersPerBatch = 25;
$results = \Drupal::database()->select('authmap', 'a')
->fields('a', ['uid'])
->condition('a.provider', 'o365_sso')
->range($sandbox['current'], $usersPerBatch)
->execute();
$users = $results->fetchAll();
if (empty($users)) {
$sandbox['#finished'] = 1;
return;
}
foreach ($users as $user) {
$user = User::load($user->uid);
if ($user) {
$user->setEmail(strtolower($user->getEmail()));
$user->save();
}
$sandbox['current']++;
}
\Drupal::messenger()
->addMessage($sandbox['current'] . ' users processed.');
if ($sandbox['current'] >= $sandbox['total']) {
$sandbox['#finished'] = 1;
}
else {
$sandbox['#finished'] = ($sandbox['current'] / $sandbox['total']);
}
}
