UserDevelGenerate.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. namespace Drupal\devel_generate\Plugin\DevelGenerate;
  3. use Drupal\Core\Datetime\DateFormatterInterface;
  4. use Drupal\Core\Entity\EntityStorageInterface;
  5. use Drupal\Core\Form\FormStateInterface;
  6. use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
  7. use Drupal\devel_generate\DevelGenerateBase;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. /**
  10. * Provides a UserDevelGenerate plugin.
  11. *
  12. * @DevelGenerate(
  13. * id = "user",
  14. * label = @Translation("users"),
  15. * description = @Translation("Generate a given number of users. Optionally delete current users."),
  16. * url = "user",
  17. * permission = "administer devel_generate",
  18. * settings = {
  19. * "num" = 50,
  20. * "kill" = FALSE,
  21. * "pass" = ""
  22. * }
  23. * )
  24. */
  25. class UserDevelGenerate extends DevelGenerateBase implements ContainerFactoryPluginInterface {
  26. /**
  27. * The user storage.
  28. *
  29. * @var \Drupal\Core\Entity\EntityStorageInterface
  30. */
  31. protected $userStorage;
  32. /**
  33. * The date formatter service.
  34. *
  35. * @var \Drupal\Core\Datetime\DateFormatterInterface
  36. */
  37. protected $dateFormatter;
  38. /**
  39. * Constructs a new UserDevelGenerate object.
  40. *
  41. * @param array $configuration
  42. * A configuration array containing information about the plugin instance.
  43. * @param string $plugin_id
  44. * The plugin_id for the plugin instance.
  45. * @param mixed $plugin_definition
  46. * The plugin implementation definition.
  47. * @param \Drupal\Core\Entity\EntityStorageInterface $entity_storage
  48. * The user storage.
  49. * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
  50. * The date formatter service.
  51. */
  52. public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $entity_storage, DateFormatterInterface $date_formatter) {
  53. parent::__construct($configuration, $plugin_id, $plugin_definition);
  54. $this->userStorage = $entity_storage;
  55. $this->dateFormatter = $date_formatter;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
  61. return new static(
  62. $configuration, $plugin_id, $plugin_definition,
  63. $container->get('entity.manager')->getStorage('user'),
  64. $container->get('date.formatter')
  65. );
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function settingsForm(array $form, FormStateInterface $form_state) {
  71. $form['num'] = array(
  72. '#type' => 'number',
  73. '#title' => $this->t('How many users would you like to generate?'),
  74. '#default_value' => $this->getSetting('num'),
  75. '#required' => TRUE,
  76. '#min' => 0,
  77. );
  78. $form['kill'] = array(
  79. '#type' => 'checkbox',
  80. '#title' => $this->t('Delete all users (except user id 1) before generating new users.'),
  81. '#default_value' => $this->getSetting('kill'),
  82. );
  83. $options = user_role_names(TRUE);
  84. unset($options[DRUPAL_AUTHENTICATED_RID]);
  85. $form['roles'] = array(
  86. '#type' => 'checkboxes',
  87. '#title' => $this->t('Which roles should the users receive?'),
  88. '#description' => $this->t('Users always receive the <em>authenticated user</em> role.'),
  89. '#options' => $options,
  90. );
  91. $form['pass'] = array(
  92. '#type' => 'textfield',
  93. '#title' => $this->t('Password to be set'),
  94. '#default_value' => $this->getSetting('pass'),
  95. '#size' => 32,
  96. '#description' => $this->t('Leave this field empty if you do not need to set a password'),
  97. );
  98. $options = array(1 => $this->t('Now'));
  99. foreach (array(3600, 86400, 604800, 2592000, 31536000) as $interval) {
  100. $options[$interval] = $this->dateFormatter->formatInterval($interval, 1) . ' ' . $this->t('ago');
  101. }
  102. $form['time_range'] = array(
  103. '#type' => 'select',
  104. '#title' => $this->t('How old should user accounts be?'),
  105. '#description' => $this->t('User ages will be distributed randomly from the current time, back to the selected time.'),
  106. '#options' => $options,
  107. '#default_value' => 604800,
  108. );
  109. return $form;
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. protected function generateElements(array $values) {
  115. $num = $values['num'];
  116. $kill = $values['kill'];
  117. $pass = $values['pass'];
  118. $age = $values['time_range'];
  119. $roles = array_filter($values['roles']);
  120. if ($kill) {
  121. $uids = $this->userStorage->getQuery()
  122. ->condition('uid', 1, '>')
  123. ->execute();
  124. $users = $this->userStorage->loadMultiple($uids);
  125. $this->userStorage->delete($users);
  126. $this->setMessage($this->formatPlural(count($uids), '1 user deleted', '@count users deleted.'));
  127. }
  128. if ($num > 0) {
  129. $names = array();
  130. while (count($names) < $num) {
  131. $name = $this->getRandom()->word(mt_rand(6, 12));
  132. $names[$name] = '';
  133. }
  134. if (empty($roles)) {
  135. $roles = array(DRUPAL_AUTHENTICATED_RID);
  136. }
  137. foreach ($names as $name => $value) {
  138. $account = $this->userStorage->create(array(
  139. 'uid' => NULL,
  140. 'name' => $name,
  141. 'pass' => $pass,
  142. 'mail' => $name . '@example.com',
  143. 'status' => 1,
  144. 'created' => REQUEST_TIME - mt_rand(0, $age),
  145. 'roles' => array_values($roles),
  146. // A flag to let hook_user_* know that this is a generated user.
  147. 'devel_generate' => TRUE,
  148. ));
  149. // Populate all fields with sample values.
  150. $this->populateFields($account);
  151. $account->save();
  152. }
  153. }
  154. $this->setMessage($this->t('@num_users created.', array('@num_users' => $this->formatPlural($num, '1 user', '@count users'))));
  155. }
  156. /**
  157. * {@inheritdoc}
  158. */
  159. public function validateDrushParams($args) {
  160. $values = array(
  161. 'num' => array_shift($args),
  162. 'roles' => drush_get_option('roles') ? explode(',', drush_get_option('roles')) : array(),
  163. 'kill' => drush_get_option('kill'),
  164. 'pass' => drush_get_option('pass', NULL),
  165. 'time_range' => 0,
  166. );
  167. return $values;
  168. }
  169. }