You are here

class AcsfDuplicationScrubUserHandler in Acquia Cloud Site Factory Connector 8.2

Same name and namespace in other branches
  1. 8 src/Event/AcsfDuplicationScrubUserHandler.php \Drupal\acsf\Event\AcsfDuplicationScrubUserHandler

Handles the scrubbing of Drupal users.

Hierarchy

Expanded class hierarchy of AcsfDuplicationScrubUserHandler

1 file declares its use of AcsfDuplicationScrubUserHandler
AcsfDuplicationCommands.php in acsf_duplication/src/Commands/AcsfDuplicationCommands.php

File

src/Event/AcsfDuplicationScrubUserHandler.php, line 8

Namespace

Drupal\acsf\Event
View source
class AcsfDuplicationScrubUserHandler extends AcsfDuplicationScrubEntityHandler {

  /**
   * Constructor.
   *
   * @param AcsfEvent $event
   *   The event that has been initiated.
   */
  public function __construct(AcsfEvent $event) {
    $this->entityTypeId = 'user';
    parent::__construct($event);
  }

  /**
   * Implements AcsfEventHandler::handle().
   */
  public function handle() {
    $options = $this->event->context['scrub_options'];
    if ($options['retain_users']) {

      // We still want to log that we were here.
      $this
        ->consoleLog(dt('Entered @class', [
        '@class' => get_class($this),
      ]));
      return;
    }
    parent::handle();
  }

  /**
   * {@inheritdoc}
   */
  protected function deleteEntities(array $entities) {

    // Delete the entities one by one. This may be slower than mass deleting
    // them, but this way we can catch an exception without a mass delete
    // being fully rolled back.
    foreach ($entities as $entity) {
      try {
        $this
          ->reassignFiles($entity
          ->id());
        $entity
          ->delete();
      } catch (\Exception $e) {

        // OK, we'll live with not scrubbing this.
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  protected function getBaseQuery() {
    $entity_query = parent::getBaseQuery();
    $entity_query
      ->condition('uid', $this
      ->getPreservedUsers(), 'NOT IN');
    return $entity_query;
  }

  /**
   * Reassigns files owned by the given user ID to the anonymous user.
   *
   * Prior to deleting the user, re-assign {file_managed}.uid to anonymous.
   * Re-assign files only: allow nodes and comments to be deleted. It would be
   * more proper to call File::load_multiple(), iterate each loaded file entity,
   * set its uid property, and call save() (see comment_user_cancel() for a
   * similar example for comments). It would be even more proper if file.module
   * implemented hook_user_cancel(), so we could just call that hook. But for
   * performance, we just update the {file_managed} table directly.
   *
   * @param int $uid
   *   The user ID for which to reassign files.
   */
  protected function reassignFiles($uid) {
    \Drupal::database()
      ->update('file_managed')
      ->fields([
      'uid' => 0,
    ])
      ->condition('uid', $uid)
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcsfDuplicationScrubEntityHandler::$acsfVarStorage protected property The ACSF variable storage.
AcsfDuplicationScrubEntityHandler::$entityTypeId protected property The entity type to scrub.
AcsfDuplicationScrubEntityHandler::$entityTypeManager protected property The entity manager.
AcsfDuplicationScrubEntityHandler::$moduleHandler protected property The module handler.
AcsfDuplicationScrubEntityHandler::countRemaining public function Counts the entities that still need to be processed. 1
AcsfDuplicationScrubEntityHandler::getPreservedUsers protected function Gets a list of user IDs which should not be scrubbed. 1
AcsfDuplicationScrubEntityHandler::getSiteAdmins public function Gets a list of site admins.
AcsfDuplicationScrubUserHandler::deleteEntities protected function Deletes entities. Overrides AcsfDuplicationScrubEntityHandler::deleteEntities
AcsfDuplicationScrubUserHandler::getBaseQuery protected function Gets an initialized entity query instance. Overrides AcsfDuplicationScrubEntityHandler::getBaseQuery
AcsfDuplicationScrubUserHandler::handle public function Implements AcsfEventHandler::handle(). Overrides AcsfDuplicationScrubEntityHandler::handle
AcsfDuplicationScrubUserHandler::reassignFiles protected function Reassigns files owned by the given user ID to the anonymous user.
AcsfDuplicationScrubUserHandler::__construct public function Constructor. Overrides AcsfDuplicationScrubEntityHandler::__construct
AcsfEventHandler::$completed public property The time that the handler was completed.
AcsfEventHandler::$message public property Any messages triggered by the handler.
AcsfEventHandler::$started public property The time that the handler was started.
AcsfEventHandler::consoleLog public function Writes a log message to the console.