Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\Core\Field\FieldTypePluginManagerInterface;
9 use Drupal\Core\Field\Plugin\Field\FieldType\EmailItem;
10 use Drupal\Core\Password\PasswordInterface;
11 use Drupal\migrate\Plugin\MigrationInterface;
12 use Drupal\migrate\Plugin\migrate\destination\EntityContentBase;
13 use Drupal\migrate\Row;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15
16 /**
17  * Provides a destination plugin for migrating user entities.
18  *
19  * Example:
20  *
21  * The example below migrates users and preserves original passwords from a
22  * source that has passwords as MD5 hashes without salt. The passwords will be
23  * salted and re-hashed before they are saved to the destination Drupal
24  * database. The MD5 hash used in the example is a hash of 'password'.
25  *
26  * The example uses the EmbeddedDataSource source plugin for the sake of
27  * simplicity. The mapping between old user_ids and new Drupal uids is saved in
28  * the migration map table.
29  * @code
30  * id: custom_user_migration
31  * label: Custom user migration
32  * source:
33  *   plugin: embedded_data
34  *   data_rows:
35  *     -
36  *       user_id: 1
37  *       name: johnsmith
38  *       mail: johnsmith@example.com
39  *       hash: '5f4dcc3b5aa765d61d8327deb882cf99'
40  *   ids:
41  *     user_id:
42  *       type: integer
43  * process:
44  *   name: name
45  *   mail: mail
46  *   pass: hash
47  *   status:
48  *     plugin: default_value
49  *     default_value: 1
50  * destination:
51  *   plugin: entity:user
52  *   md5_passwords: true
53  * @endcode
54  *
55  * For configuration options inherited from the parent class, refer to
56  * \Drupal\migrate\Plugin\migrate\destination\EntityContentBase.
57  *
58  * The example above is about migrating an MD5 password hash. For more examples
59  * on different password hash types and a list of other user properties, refer
60  * to the handbook documentation:
61  * @see https://www.drupal.org/docs/8/api/migrate-api/migrate-destination-plugins-examples/migrating-users
62  *
63  * @MigrateDestination(
64  *   id = "entity:user"
65  * )
66  */
67 class EntityUser extends EntityContentBase {
68
69   /**
70    * The password service class.
71    *
72    * @var \Drupal\Core\Password\PasswordInterface
73    */
74   protected $password;
75
76   /**
77    * Builds an user entity destination.
78    *
79    * @param array $configuration
80    *   A configuration array containing information about the plugin instance.
81    * @param string $plugin_id
82    *   The plugin_id for the plugin instance.
83    * @param mixed $plugin_definition
84    *   The plugin implementation definition.
85    * @param \Drupal\migrate\Plugin\MigrationInterface $migration
86    *   The migration.
87    * @param \Drupal\Core\Entity\EntityStorageInterface $storage
88    *   The storage for this entity type.
89    * @param array $bundles
90    *   The list of bundles this entity type has.
91    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
92    *   The entity manager service.
93    * @param \Drupal\Core\Field\FieldTypePluginManagerInterface $field_type_manager
94    *   The field type plugin manager service.
95    * @param \Drupal\Core\Password\PasswordInterface $password
96    *   The password service.
97    */
98   public function __construct(array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration, EntityStorageInterface $storage, array $bundles, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, PasswordInterface $password) {
99     parent::__construct($configuration, $plugin_id, $plugin_definition, $migration, $storage, $bundles, $entity_manager, $field_type_manager);
100     $this->password = $password;
101   }
102
103   /**
104    * {@inheritdoc}
105    */
106   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) {
107     $entity_type = static::getEntityTypeId($plugin_id);
108     return new static(
109       $configuration,
110       $plugin_id,
111       $plugin_definition,
112       $migration,
113       $container->get('entity.manager')->getStorage($entity_type),
114       array_keys($container->get('entity.manager')->getBundleInfo($entity_type)),
115       $container->get('entity.manager'),
116       $container->get('plugin.manager.field.field_type'),
117       $container->get('password')
118     );
119   }
120
121   /**
122    * {@inheritdoc}
123    * @throws \Drupal\migrate\MigrateException
124    */
125   public function import(Row $row, array $old_destination_id_values = []) {
126     // Do not overwrite the root account password.
127     if ($row->getDestinationProperty('uid') == 1) {
128       $row->removeDestinationProperty('pass');
129     }
130     return parent::import($row, $old_destination_id_values);
131   }
132
133   /**
134    * {@inheritdoc}
135    */
136   protected function save(ContentEntityInterface $entity, array $old_destination_id_values = []) {
137     // Do not overwrite the root account password.
138     if ($entity->id() != 1) {
139       // Set the pre_hashed password so that the PasswordItem field does not hash
140       // already hashed passwords. If the md5_passwords configuration option is
141       // set we need to rehash the password and prefix with a U.
142       // @see \Drupal\Core\Field\Plugin\Field\FieldType\PasswordItem::preSave()
143       $entity->pass->pre_hashed = TRUE;
144       if (isset($this->configuration['md5_passwords'])) {
145         $entity->pass->value = 'U' . $this->password->hash($entity->pass->value);
146       }
147     }
148     return parent::save($entity, $old_destination_id_values);
149   }
150
151   /**
152    * {@inheritdoc}
153    */
154   protected function processStubRow(Row $row) {
155     parent::processStubRow($row);
156     // Email address is not defined as required in the base field definition but
157     // is effectively required by the UserMailRequired constraint. This means
158     // that Entity::processStubRow() did not populate it - we do it here.
159     $field_definitions = $this->entityManager
160       ->getFieldDefinitions($this->storage->getEntityTypeId(),
161         $this->getKey('bundle'));
162     $mail = EmailItem::generateSampleValue($field_definitions['mail']);
163     $row->setDestinationProperty('mail', reset($mail));
164
165     // @todo Work-around for https://www.drupal.org/node/2602066.
166     $name = $row->getDestinationProperty('name');
167     if (is_array($name)) {
168       $name = reset($name);
169     }
170     if (mb_strlen($name) > USERNAME_MAX_LENGTH) {
171       $row->setDestinationProperty('name', mb_substr($name, 0, USERNAME_MAX_LENGTH));
172     }
173   }
174
175   /**
176    * {@inheritdoc}
177    */
178   public function getHighestId() {
179     $highest_id = parent::getHighestId();
180
181     // Every Drupal site must have a user with UID of 1 and it's normal for
182     // migrations to overwrite this user.
183     if ($highest_id === 1) {
184       return 0;
185     }
186     return $highest_id;
187   }
188
189 }