diff --git a/password_policy.install b/password_policy.install index 72f07fe..2b4916f 100644 --- a/password_policy.install +++ b/password_policy.install @@ -9,6 +9,10 @@ * Implements hook_install(). */ function password_policy_install() { + if (\Drupal::isConfigSyncing()) { + return; + } + // Set user password reset timestamp to now. $timestamp = \Drupal::service("date.formatter")->format(\Drupal::time()->getRequestTime(), "custom", DATETIME_DATETIME_STORAGE_FORMAT, DATETIME_STORAGE_TIMEZONE); /** @var \Drupal\user\UserInterface[] $users */ diff --git a/src/EventSubscriber/PasswordPolicyEventSubscriber.php b/src/EventSubscriber/PasswordPolicyEventSubscriber.php index a593ad0..0c737b4 100644 --- a/src/EventSubscriber/PasswordPolicyEventSubscriber.php +++ b/src/EventSubscriber/PasswordPolicyEventSubscriber.php @@ -2,6 +2,8 @@ namespace Drupal\password_policy\EventSubscriber; +use Drupal\Core\Config\ConfigEvents; +use Drupal\Core\Config\ConfigImporterEvent; use Drupal\Core\Url; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpKernel\KernelEvents; @@ -55,12 +57,43 @@ class PasswordPolicyEventSubscriber implements EventSubscriberInterface { } } + /** + * Updates password reset value for all users. + * + * @param \Drupal\Core\Config\ConfigImporterEvent $event + * The config importer event. + */ + public function onConfigImport(ConfigImporterEvent $event) { + $modules = $event->getConfigImporter()->getExtensionChangelist('module', 'install'); + + if (!in_array('password_policy', $modules)) { + return; + } + + $timestamp = gmdate(DATETIME_DATETIME_STORAGE_FORMAT, REQUEST_TIME); + + /** @var \Drupal\user\UserInterface[] $users */ + $users = \Drupal::entityTypeManager()->getStorage('user')->loadMultiple(); + + // @todo Get rid of updating all users. + foreach ($users as $user) { + if ($user->getAccountName() == NULL) { + continue; + } + $user + ->set('field_last_password_reset', $timestamp) + ->set('field_password_expiration', '0') + ->save(); + } + } + /** * {@inheritdoc} */ public static function getSubscribedEvents() { // TODO - Evaluate if there is a better place to add this check. $events[KernelEvents::REQUEST][] = ['checkForUserPasswordExpiration']; + $events[ConfigEvents::IMPORT][] = ['onConfigImport']; return $events; } diff --git a/tests/src/Functional/ConfigImportTest.php b/tests/src/Functional/ConfigImportTest.php new file mode 100644 index 0000000..25f081b --- /dev/null +++ b/tests/src/Functional/ConfigImportTest.php @@ -0,0 +1,38 @@ +drupalLogin($this->drupalCreateUser(['synchronize configuration'])); + // Export config. + $this->copyConfig($this->container->get('config.storage'), $this->container->get('config.storage.sync')); + // Disable module. + \Drupal::service('module_installer')->uninstall(['password_policy']); + // Import config. + $this->configImporter()->import(); + } + +}