Security update for Core, with self-updated composer
[yaffs-website] / web / core / modules / user / src / Plugin / migrate / destination / EntityUser.php
1 <?php
2
3 namespace Drupal\user\Plugin\migrate\destination;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Entity\ContentEntityInterface;
7 use Drupal\Core\Entity\EntityManagerInterface;
8 use Drupal\Core\Entity\EntityStorageInterface;
9 use Drupal\Core\Field\FieldTypePluginManagerInterface;
10 use Drupal\Core\Field\Plugin\Field\FieldType\EmailItem;
11 use Drupal\Core\Password\PasswordInterface;
12 use Drupal\migrate\Plugin\MigrationInterface;
13 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
14 use Drupal\migrate\Row;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
16
17 /**
18  * @MigrateDestination(
19  *   id = "entity:user"
20  * )
21  */
22 class EntityUser extends EntityContentBase {
23
24   /**
25    * The password service class.
26    *
27    * @var \Drupal\Core\Password\PasswordInterface
28    */
29   protected $password;
30
31   /**
32    * Builds an user entity destination.
33    *
34    * @param array $configuration
35    *   A configuration array containing information about the plugin instance.
36    * @param string $plugin_id
37    *   The plugin_id for the plugin instance.
38    * @param mixed $plugin_definition
39    *   The plugin implementation definition.
40    * @param \Drupal\migrate\Plugin\MigrationInterface $migration
41    *   The migration.
42    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
43    *   The storage for this entity type.
44    * @param array $bundles
45    *   The list of bundles this entity type has.
46    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
47    *   The entity manager service.
48    * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
49    *   The field type plugin manager service.
50    * @param \Drupal\Core\Password\PasswordInterface $password
51    *   The password service.
52    */
53   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, PasswordInterface $password) {
54     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager);
55     $this->password = $password;
56   }
57
58   /**
59    * {@inheritdoc}
60    */
61   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
62     $entity_type = static::getEntityTypeId($plugin_id);
63     return new static(
64       $configuration,
65       $plugin_id,
66       $plugin_definition,
67       $migration,
68       $container->get('entity.manager')->getStorage($entity_type),
69       array_keys($container->get('entity.manager')->getBundleInfo($entity_type)),
70       $container->get('entity.manager'),
71       $container->get('plugin.manager.field.field_type'),
72       $container->get('password')
73     );
74   }
75
76   /**
77    * {@inheritdoc}
78    * @throws \Drupal\migrate\MigrateException
79    */
80   public function import(Row $row, array $old_destination_id_values = []) {
81     // Do not overwrite the root account password.
82     if ($row->getDestinationProperty('uid') == 1) {
83       $row->removeDestinationProperty('pass');
84     }
85     return parent::import($row, $old_destination_id_values);
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   protected function save(ContentEntityInterface $entity, array $old_destination_id_values = []) {
92     // Do not overwrite the root account password.
93     if ($entity->id() != 1) {
94       // Set the pre_hashed password so that the PasswordItem field does not hash
95       // already hashed passwords. If the md5_passwords configuration option is
96       // set we need to rehash the password and prefix with a U.
97       // @see \Drupal\Core\Field\Plugin\Field\FieldType\PasswordItem::preSave()
98       $entity->pass->pre_hashed = TRUE;
99       if (isset($this->configuration['md5_passwords'])) {
100         $entity->pass->value = 'U' . $this->password->hash($entity->pass->value);
101       }
102     }
103     return parent::save($entity, $old_destination_id_values);
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   protected function processStubRow(Row $row) {
110     parent::processStubRow($row);
111     // Email address is not defined as required in the base field definition but
112     // is effectively required by the UserMailRequired constraint. This means
113     // that Entity::processStubRow() did not populate it - we do it here.
114     $field_definitions = $this->entityManager
115       ->getFieldDefinitions($this->storage->getEntityTypeId(),
116         $this->getKey('bundle'));
117     $mail = EmailItem::generateSampleValue($field_definitions['mail']);
118     $row->setDestinationProperty('mail', reset($mail));
119
120     // @todo Work-around for https://www.drupal.org/node/2602066.
121     $name = $row->getDestinationProperty('name');
122     if (is_array($name)) {
123       $name = reset($name);
124     }
125     if (Unicode::strlen($name) > USERNAME_MAX_LENGTH) {
126       $row->setDestinationProperty('name', Unicode::substr($name, 0, USERNAME_MAX_LENGTH));
127     }
128   }
129
130 }