Created
December 13, 2017 21:54
-
-
Save kmoll/d23c7243262561ba60a9953fd03d9c4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/password_policy.install b/password_policy.install | |
index f32e5e5..2283c17 100644 | |
--- a/password_policy.install | |
+++ b/password_policy.install | |
@@ -13,19 +13,18 @@ function password_policy_install() { | |
return; | |
} | |
- // Set user password reset timestamp to now. | |
- $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(); | |
+ // Get all the existing user ids. | |
+ $user_ids = \Drupal::entityTypeManager()->getStorage('user')->getQuery()->execute(); | |
+ | |
+ foreach ($user_ids as $uid) { | |
+ // Create the queue item. | |
+ /** @var \Drupal\Core\Queue\QueueFactory $queue_factory */ | |
+ $queue_factory = \Drupal::service('queue'); | |
+ /** @var \Drupal\Core\Queue\QueueInterface $queue */ | |
+ $queue = $queue_factory->get('password_policy_update_existing_users'); | |
+ $item = new \stdClass(); | |
+ $item->uid = $uid; | |
+ $queue->createItem($item); | |
} | |
// Rebuild user entity form display for new fields. | |
@@ -55,6 +54,17 @@ function password_policy_install() { | |
->save(); | |
} | |
+/** | |
+ * Implements hook_uninstall. | |
+ */ | |
+function password_policy_uninstall() { | |
+ // Delete the queue. | |
+ $queue_factory = \Drupal::service('queue'); | |
+ /** @var \Drupal\Core\Queue\QueueInterface $queue */ | |
+ $queue = $queue_factory->get('password_policy_update_existing_users'); | |
+ $queue->deleteQueue(); | |
+} | |
+ | |
/** | |
* Implements hook_update_N(). | |
* | |
diff --git a/password_policy_history/password_policy_history.install b/password_policy_history/password_policy_history.install | |
index ddd8ba5..2ec3c35 100644 | |
--- a/password_policy_history/password_policy_history.install | |
+++ b/password_policy_history/password_policy_history.install | |
@@ -5,31 +5,36 @@ | |
* Installation and update functions for password policy history. | |
*/ | |
-use Drupal\Core\Database\Database; | |
- | |
/** | |
* Implements hook_install(). | |
*/ | |
function password_policy_history_install() { | |
- // Add current user passwords. | |
- $users = entity_load_multiple('user'); | |
- $connection = Database::getConnection(); | |
+ // Get all the existing user ids. | |
+ $user_ids = \Drupal::entityTypeManager()->getStorage('user')->getQuery()->execute(); | |
- foreach ($users as $user) { | |
- $hashed_pass = $user->getPassword(); | |
- if ($hashed_pass) { | |
- $values = [ | |
- $user->id(), | |
- $hashed_pass, | |
- time(), | |
- ]; | |
- $connection->insert('password_policy_history') | |
- ->fields(['uid', 'pass_hash', 'timestamp'], $values) | |
- ->execute(); | |
- } | |
+ foreach ($user_ids as $uid) { | |
+ // Create the queue item. | |
+ /** @var \Drupal\Core\Queue\QueueFactory $queue_factory */ | |
+ $queue_factory = \Drupal::service('queue'); | |
+ /** @var \Drupal\Core\Queue\QueueInterface $queue */ | |
+ $queue = $queue_factory->get('password_policy_history_create_existing_user_data'); | |
+ $item = new \stdClass(); | |
+ $item->uid = $uid; | |
+ $queue->createItem($item); | |
} | |
} | |
+/** | |
+ * Implements hook_uninstall(). | |
+ */ | |
+function password_policy_history_uninstall() { | |
+ // Delete the queue. | |
+ $queue_factory = \Drupal::service('queue'); | |
+ /** @var \Drupal\Core\Queue\QueueInterface $queue */ | |
+ $queue = $queue_factory->get('password_policy_history_create_existing_user_data'); | |
+ $queue->deleteQueue(); | |
+} | |
+ | |
/** | |
* Implements hook_schema(). | |
*/ | |
diff --git a/password_policy_history/src/Plugin/QueueWorker/PasswordPolicyHistoryCronQueueWorker.php b/password_policy_history/src/Plugin/QueueWorker/PasswordPolicyHistoryCronQueueWorker.php | |
new file mode 100644 | |
index 0000000..642ed2a | |
--- /dev/null | |
+++ b/password_policy_history/src/Plugin/QueueWorker/PasswordPolicyHistoryCronQueueWorker.php | |
@@ -0,0 +1,16 @@ | |
+<?php | |
+ | |
+namespace Drupal\password_policy_history\Plugin\QueueWorker; | |
+ | |
+/** | |
+ * Processes password_policy_update_existing_users queue. | |
+ * | |
+ * @QueueWorker( | |
+ * id = "password_policy_history_create_existing_user_data", | |
+ * title = @Translation("Password policy history create existing user data queue"), | |
+ * cron = {"time" = 30} | |
+ * ) | |
+ */ | |
+class PasswordPolicyHistoryCronQueueWorker extends PasswordPolicyHistoryQueueWorkerBase { | |
+ | |
+} | |
diff --git a/password_policy_history/src/Plugin/QueueWorker/PasswordPolicyHistoryQueueWorkerBase.php b/password_policy_history/src/Plugin/QueueWorker/PasswordPolicyHistoryQueueWorkerBase.php | |
new file mode 100644 | |
index 0000000..28f0b5c | |
--- /dev/null | |
+++ b/password_policy_history/src/Plugin/QueueWorker/PasswordPolicyHistoryQueueWorkerBase.php | |
@@ -0,0 +1,65 @@ | |
+<?php | |
+ | |
+namespace Drupal\password_policy_history\Plugin\QueueWorker; | |
+ | |
+use Drupal\Core\Database\Connection; | |
+use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
+use Drupal\Core\Queue\QueueWorkerBase; | |
+use Drupal\user\Entity\User; | |
+use Symfony\Component\DependencyInjection\ContainerInterface; | |
+ | |
+/** | |
+ * Class PasswordPolicyHistoryQueueWorkerBase. | |
+ * | |
+ * @package Drupal\password_policy_history\Plugin\QueueWorker | |
+ */ | |
+abstract class PasswordPolicyHistoryQueueWorkerBase extends QueueWorkerBase implements ContainerFactoryPluginInterface { | |
+ | |
+ /** | |
+ * Database connection object. | |
+ * | |
+ * @var \Drupal\Core\Database\Connection | |
+ */ | |
+ protected $connection; | |
+ | |
+ /** | |
+ * Constructs a PasswordPolicyHistoryQueueWorkerBase object. | |
+ */ | |
+ public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $connection) { | |
+ parent::__construct($configuration, $plugin_id, $plugin_definition); | |
+ $this->connection = $connection; | |
+ } | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
+ return new static( | |
+ $configuration, | |
+ $plugin_id, | |
+ $plugin_definition, | |
+ $container->get('database') | |
+ ); | |
+ } | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public function processItem($item) { | |
+ $uid = $item->uid; | |
+ $user = User::load($uid); | |
+ | |
+ $hashed_pass = $user->getPassword(); | |
+ if ($hashed_pass) { | |
+ $values = [ | |
+ $user->id(), | |
+ $hashed_pass, | |
+ time(), | |
+ ]; | |
+ $this->connection->insert('password_policy_history') | |
+ ->fields(['uid', 'pass_hash', 'timestamp'], $values) | |
+ ->execute(); | |
+ } | |
+ } | |
+ | |
+} | |
diff --git a/src/Plugin/QueueWorker/PasswordPolicyCronQueueWorker.php b/src/Plugin/QueueWorker/PasswordPolicyCronQueueWorker.php | |
new file mode 100644 | |
index 0000000..1110adf | |
--- /dev/null | |
+++ b/src/Plugin/QueueWorker/PasswordPolicyCronQueueWorker.php | |
@@ -0,0 +1,18 @@ | |
+<?php | |
+ | |
+namespace Drupal\password_policy\Plugin\QueueWorker; | |
+ | |
+use Symfony\Component\DependencyInjection\ContainerInterface; | |
+ | |
+/** | |
+ * Processes password_policy_update_existing_users queue. | |
+ * | |
+ * @QueueWorker( | |
+ * id = "password_policy_update_existing_users", | |
+ * title = @Translation("Password policy update existing users queue"), | |
+ * cron = {"time" = 30} | |
+ * ) | |
+ */ | |
+class PasswordPolicyCronQueueWorker extends PasswordPolicyQueueWorkerBase { | |
+ | |
+} | |
diff --git a/src/Plugin/QueueWorker/PasswordPolicyQueueWorkerBase.php b/src/Plugin/QueueWorker/PasswordPolicyQueueWorkerBase.php | |
new file mode 100644 | |
index 0000000..83588bb | |
--- /dev/null | |
+++ b/src/Plugin/QueueWorker/PasswordPolicyQueueWorkerBase.php | |
@@ -0,0 +1,53 @@ | |
+<?php | |
+ | |
+namespace Drupal\password_policy\Plugin\QueueWorker; | |
+ | |
+use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
+use Drupal\Core\Queue\QueueWorkerBase; | |
+use Drupal\user\Entity\User; | |
+use Symfony\Component\DependencyInjection\ContainerInterface; | |
+ | |
+/** | |
+ * Class PasswordPolicyQueueWorkerBase. | |
+ * | |
+ * @package Drupal\passwrod_policy\Plugin\QueueWorker | |
+ */ | |
+abstract class PasswordPolicyQueueWorkerBase extends QueueWorkerBase implements ContainerFactoryPluginInterface { | |
+ | |
+ /** | |
+ * Constructs a PasswordPolicyQueueWorkerBase object. | |
+ */ | |
+ public function __construct(array $configuration, $plugin_id, $plugin_definition) { | |
+ parent::__construct($configuration, $plugin_id, $plugin_definition); | |
+ } | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
+ return new static( | |
+ $configuration, | |
+ $plugin_id, | |
+ $plugin_definition | |
+ ); | |
+ } | |
+ | |
+ /** | |
+ * {@inheritdoc} | |
+ */ | |
+ public function processItem($item) { | |
+ $uid = $item->uid; | |
+ $user = User::load($uid); | |
+ | |
+ // Set user password reset timestamp to now. | |
+ $timestamp = gmdate(DATETIME_DATETIME_STORAGE_FORMAT, REQUEST_TIME); | |
+ if ($user->getAccountName() == NULL) { | |
+ return NULL; | |
+ } | |
+ $user | |
+ ->set('field_last_password_reset', $timestamp) | |
+ ->set('field_password_expiration', '0') | |
+ ->save(); | |
+ } | |
+ | |
+} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment