Guest User

Untitled

a guest
Dec 16th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.77 KB | None | 0 0
  1. diff --git a/password_policy.install b/password_policy.install
  2. index f32e5e5..2283c17 100644
  3. --- a/password_policy.install
  4. +++ b/password_policy.install
  5. @@ -13,19 +13,18 @@ function password_policy_install() {
  6. return;
  7. }
  8.  
  9. - // Set user password reset timestamp to now.
  10. - $timestamp = gmdate(DATETIME_DATETIME_STORAGE_FORMAT, REQUEST_TIME);
  11. - /** @var \Drupal\user\UserInterface[] $users */
  12. - $users = \Drupal::entityTypeManager()->getStorage('user')->loadMultiple();
  13. - // @todo Get rid of updating all users.
  14. - foreach ($users as $user) {
  15. - if ($user->getAccountName() == NULL) {
  16. - continue;
  17. - }
  18. - $user
  19. - ->set('field_last_password_reset', $timestamp)
  20. - ->set('field_password_expiration', '0')
  21. - ->save();
  22. + // Get all the existing user ids.
  23. + $user_ids = \Drupal::entityTypeManager()->getStorage('user')->getQuery()->execute();
  24. +
  25. + foreach ($user_ids as $uid) {
  26. + // Create the queue item.
  27. + /** @var \Drupal\Core\Queue\QueueFactory $queue_factory */
  28. + $queue_factory = \Drupal::service('queue');
  29. + /** @var \Drupal\Core\Queue\QueueInterface $queue */
  30. + $queue = $queue_factory->get('password_policy_update_existing_users');
  31. + $item = new \stdClass();
  32. + $item->uid = $uid;
  33. + $queue->createItem($item);
  34. }
  35.  
  36. // Rebuild user entity form display for new fields.
  37. @@ -55,6 +54,17 @@ function password_policy_install() {
  38. ->save();
  39. }
  40.  
  41. +/**
  42. + * Implements hook_uninstall.
  43. + */
  44. +function password_policy_uninstall() {
  45. + // Delete the queue.
  46. + $queue_factory = \Drupal::service('queue');
  47. + /** @var \Drupal\Core\Queue\QueueInterface $queue */
  48. + $queue = $queue_factory->get('password_policy_update_existing_users');
  49. + $queue->deleteQueue();
  50. +}
  51. +
  52. /**
  53. * Implements hook_update_N().
  54. *
  55. diff --git a/password_policy_history/password_policy_history.install b/password_policy_history/password_policy_history.install
  56. index ddd8ba5..2ec3c35 100644
  57. --- a/password_policy_history/password_policy_history.install
  58. +++ b/password_policy_history/password_policy_history.install
  59. @@ -5,31 +5,36 @@
  60. * Installation and update functions for password policy history.
  61. */
  62.  
  63. -use Drupal\Core\Database\Database;
  64. -
  65. /**
  66. * Implements hook_install().
  67. */
  68. function password_policy_history_install() {
  69. - // Add current user passwords.
  70. - $users = entity_load_multiple('user');
  71. - $connection = Database::getConnection();
  72. + // Get all the existing user ids.
  73. + $user_ids = \Drupal::entityTypeManager()->getStorage('user')->getQuery()->execute();
  74.  
  75. - foreach ($users as $user) {
  76. - $hashed_pass = $user->getPassword();
  77. - if ($hashed_pass) {
  78. - $values = [
  79. - $user->id(),
  80. - $hashed_pass,
  81. - time(),
  82. - ];
  83. - $connection->insert('password_policy_history')
  84. - ->fields(['uid', 'pass_hash', 'timestamp'], $values)
  85. - ->execute();
  86. - }
  87. + foreach ($user_ids as $uid) {
  88. + // Create the queue item.
  89. + /** @var \Drupal\Core\Queue\QueueFactory $queue_factory */
  90. + $queue_factory = \Drupal::service('queue');
  91. + /** @var \Drupal\Core\Queue\QueueInterface $queue */
  92. + $queue = $queue_factory->get('password_policy_history_create_existing_user_data');
  93. + $item = new \stdClass();
  94. + $item->uid = $uid;
  95. + $queue->createItem($item);
  96. }
  97. }
  98.  
  99. +/**
  100. + * Implements hook_uninstall().
  101. + */
  102. +function password_policy_history_uninstall() {
  103. + // Delete the queue.
  104. + $queue_factory = \Drupal::service('queue');
  105. + /** @var \Drupal\Core\Queue\QueueInterface $queue */
  106. + $queue = $queue_factory->get('password_policy_history_create_existing_user_data');
  107. + $queue->deleteQueue();
  108. +}
  109. +
  110. /**
  111. * Implements hook_schema().
  112. */
  113. diff --git a/password_policy_history/src/Plugin/QueueWorker/PasswordPolicyHistoryCronQueueWorker.php b/password_policy_history/src/Plugin/QueueWorker/PasswordPolicyHistoryCronQueueWorker.php
  114. new file mode 100644
  115. index 0000000..642ed2a
  116. --- /dev/null
  117. +++ b/password_policy_history/src/Plugin/QueueWorker/PasswordPolicyHistoryCronQueueWorker.php
  118. @@ -0,0 +1,16 @@
  119. +<?php
  120. +
  121. +namespace Drupal\password_policy_history\Plugin\QueueWorker;
  122. +
  123. +/**
  124. + * Processes password_policy_update_existing_users queue.
  125. + *
  126. + * @QueueWorker(
  127. + * id = "password_policy_history_create_existing_user_data",
  128. + * title = @Translation("Password policy history create existing user data queue"),
  129. + * cron = {"time" = 30}
  130. + * )
  131. + */
  132. +class PasswordPolicyHistoryCronQueueWorker extends PasswordPolicyHistoryQueueWorkerBase {
  133. +
  134. +}
  135. diff --git a/password_policy_history/src/Plugin/QueueWorker/PasswordPolicyHistoryQueueWorkerBase.php b/password_policy_history/src/Plugin/QueueWorker/PasswordPolicyHistoryQueueWorkerBase.php
  136. new file mode 100644
  137. index 0000000..28f0b5c
  138. --- /dev/null
  139. +++ b/password_policy_history/src/Plugin/QueueWorker/PasswordPolicyHistoryQueueWorkerBase.php
  140. @@ -0,0 +1,65 @@
  141. +<?php
  142. +
  143. +namespace Drupal\password_policy_history\Plugin\QueueWorker;
  144. +
  145. +use Drupal\Core\Database\Connection;
  146. +use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  147. +use Drupal\Core\Queue\QueueWorkerBase;
  148. +use Drupal\user\Entity\User;
  149. +use Symfony\Component\DependencyInjection\ContainerInterface;
  150. +
  151. +/**
  152. + * Class PasswordPolicyHistoryQueueWorkerBase.
  153. + *
  154. + * @package Drupal\password_policy_history\Plugin\QueueWorker
  155. + */
  156. +abstract class PasswordPolicyHistoryQueueWorkerBase extends QueueWorkerBase implements ContainerFactoryPluginInterface {
  157. +
  158. + /**
  159. + * Database connection object.
  160. + *
  161. + * @var \Drupal\Core\Database\Connection
  162. + */
  163. + protected $connection;
  164. +
  165. + /**
  166. + * Constructs a PasswordPolicyHistoryQueueWorkerBase object.
  167. + */
  168. + public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $connection) {
  169. + parent::__construct($configuration, $plugin_id, $plugin_definition);
  170. + $this->connection = $connection;
  171. + }
  172. +
  173. + /**
  174. + * {@inheritdoc}
  175. + */
  176. + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  177. + return new static(
  178. + $configuration,
  179. + $plugin_id,
  180. + $plugin_definition,
  181. + $container->get('database')
  182. + );
  183. + }
  184. +
  185. + /**
  186. + * {@inheritdoc}
  187. + */
  188. + public function processItem($item) {
  189. + $uid = $item->uid;
  190. + $user = User::load($uid);
  191. +
  192. + $hashed_pass = $user->getPassword();
  193. + if ($hashed_pass) {
  194. + $values = [
  195. + $user->id(),
  196. + $hashed_pass,
  197. + time(),
  198. + ];
  199. + $this->connection->insert('password_policy_history')
  200. + ->fields(['uid', 'pass_hash', 'timestamp'], $values)
  201. + ->execute();
  202. + }
  203. + }
  204. +
  205. +}
  206. diff --git a/src/Plugin/QueueWorker/PasswordPolicyCronQueueWorker.php b/src/Plugin/QueueWorker/PasswordPolicyCronQueueWorker.php
  207. new file mode 100644
  208. index 0000000..1110adf
  209. --- /dev/null
  210. +++ b/src/Plugin/QueueWorker/PasswordPolicyCronQueueWorker.php
  211. @@ -0,0 +1,18 @@
  212. +<?php
  213. +
  214. +namespace Drupal\password_policy\Plugin\QueueWorker;
  215. +
  216. +use Symfony\Component\DependencyInjection\ContainerInterface;
  217. +
  218. +/**
  219. + * Processes password_policy_update_existing_users queue.
  220. + *
  221. + * @QueueWorker(
  222. + * id = "password_policy_update_existing_users",
  223. + * title = @Translation("Password policy update existing users queue"),
  224. + * cron = {"time" = 30}
  225. + * )
  226. + */
  227. +class PasswordPolicyCronQueueWorker extends PasswordPolicyQueueWorkerBase {
  228. +
  229. +}
  230. diff --git a/src/Plugin/QueueWorker/PasswordPolicyQueueWorkerBase.php b/src/Plugin/QueueWorker/PasswordPolicyQueueWorkerBase.php
  231. new file mode 100644
  232. index 0000000..83588bb
  233. --- /dev/null
  234. +++ b/src/Plugin/QueueWorker/PasswordPolicyQueueWorkerBase.php
  235. @@ -0,0 +1,53 @@
  236. +<?php
  237. +
  238. +namespace Drupal\password_policy\Plugin\QueueWorker;
  239. +
  240. +use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  241. +use Drupal\Core\Queue\QueueWorkerBase;
  242. +use Drupal\user\Entity\User;
  243. +use Symfony\Component\DependencyInjection\ContainerInterface;
  244. +
  245. +/**
  246. + * Class PasswordPolicyQueueWorkerBase.
  247. + *
  248. + * @package Drupal\passwrod_policy\Plugin\QueueWorker
  249. + */
  250. +abstract class PasswordPolicyQueueWorkerBase extends QueueWorkerBase implements ContainerFactoryPluginInterface {
  251. +
  252. + /**
  253. + * Constructs a PasswordPolicyQueueWorkerBase object.
  254. + */
  255. + public function __construct(array $configuration, $plugin_id, $plugin_definition) {
  256. + parent::__construct($configuration, $plugin_id, $plugin_definition);
  257. + }
  258. +
  259. + /**
  260. + * {@inheritdoc}
  261. + */
  262. + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  263. + return new static(
  264. + $configuration,
  265. + $plugin_id,
  266. + $plugin_definition
  267. + );
  268. + }
  269. +
  270. + /**
  271. + * {@inheritdoc}
  272. + */
  273. + public function processItem($item) {
  274. + $uid = $item->uid;
  275. + $user = User::load($uid);
  276. +
  277. + // Set user password reset timestamp to now.
  278. + $timestamp = gmdate(DATETIME_DATETIME_STORAGE_FORMAT, REQUEST_TIME);
  279. + if ($user->getAccountName() == NULL) {
  280. + return NULL;
  281. + }
  282. + $user
  283. + ->set('field_last_password_reset', $timestamp)
  284. + ->set('field_password_expiration', '0')
  285. + ->save();
  286. + }
  287. +
  288. +}
Add Comment
Please, Sign In to add comment