You are here

class AuthenticationByUser in CMS Content Sync 8

Same name and namespace in other branches
  1. 2.1.x src/Controller/AuthenticationByUser.php \Drupal\cms_content_sync\Controller\AuthenticationByUser
  2. 2.0.x src/Controller/AuthenticationByUser.php \Drupal\cms_content_sync\Controller\AuthenticationByUser

Class AuthenticationByUser.

Ask the Sync Core to use a local user account to authenticate against this site.

Hierarchy

Expanded class hierarchy of AuthenticationByUser

2 files declare their use of AuthenticationByUser
DrupalApplication.php in src/SyncCoreInterface/DrupalApplication.php
FlowForm.php in src/Form/FlowForm.php

File

src/Controller/AuthenticationByUser.php, line 16

Namespace

Drupal\cms_content_sync\Controller
View source
class AuthenticationByUser {

  /**
   * @var string KEY_USERNAME
   */
  public const KEY_USERNAME = 'userName';

  /**
   * @var string KEY_USERNAME
   */
  public const KEY_PASSWORD = 'userPass';

  /**
   * @var \Drupal\Core\KeyValueStore\KeyValueDatabaseFactory
   */
  protected $keyValueDatabase;

  /**
   * @var \Drupal\user\Entity\UserDataInterface
   */
  protected $userData;

  /**
   * Constructs a new EntityListBuilder object.
   *
   * @param \Drupal\user\Entity\UserDataInterface $user_data
   */
  public function __construct(KeyValueDatabaseFactory $key_value_database, UserDataInterface $user_data) {
    $this->keyValueDatabase = $key_value_database;
    $this->userData = $user_data;
  }

  /**
   * Singleton.
   *
   * @return AuthenticationByUser
   */
  public static function getInstance() {
    static $instance = null;
    if ($instance) {
      return $instance;
    }
    return $instance = self::createInstance(\Drupal::getContainer());
  }

  /**
   * Create a new instance of the controller with the services of the given container.
   *
   * @return AuthenticationByUser
   */
  public static function createInstance(ContainerInterface $container) {
    return new static($container
      ->get('keyvalue.database'), $container
      ->get('user.data'));
  }

  /**
   * @throws \Exception
   *
   * @returns array
   */
  public function getLoginData() {
    static $loginData = null;
    if (!empty($loginData)) {
      return $loginData;
    }
    $user = User::load(CMS_CONTENT_SYNC_USER_ID);

    // During the installation from an existing config for some reason CMS_CONTENT_SYNC_USER_ID is not set right after
    // the installation of the module, so we hae to double check that.
    if (is_null(CMS_CONTENT_SYNC_USER_ID)) {
      $user = User::load($this->keyValueDatabase
        ->get('cms_content_sync_user')
        ->get('uid'));
    }
    if (!$user) {
      throw new \Exception("Content Sync User not found. Encrypted data can't be read or saved.");
    }
    $loginData = $this->userData
      ->get('cms_content_sync', $user
      ->id(), 'sync_data');
    if (!$loginData) {
      throw new \Exception('No credentials for sync user found.');
    }
    $encryption_profile = EncryptionProfile::load(CMS_CONTENT_SYNC_ENCRYPTION_PROFILE_NAME);
    foreach ($loginData as $key => $value) {
      $loginData[$key] = \Drupal::service('encryption')
        ->decrypt($value, $encryption_profile);
    }
    return $loginData;
  }

  /**
   * @return string
   */
  public function getUsername() {
    return $this
      ->getLoginData()[self::KEY_USERNAME];
  }

  /**
   * @return string
   */
  public function getPassword() {
    return $this
      ->getLoginData()[self::KEY_PASSWORD];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthenticationByUser::$keyValueDatabase protected property
AuthenticationByUser::$userData protected property
AuthenticationByUser::createInstance public static function Create a new instance of the controller with the services of the given container.
AuthenticationByUser::getInstance public static function Singleton.
AuthenticationByUser::getLoginData public function @returns array
AuthenticationByUser::getPassword public function
AuthenticationByUser::getUsername public function
AuthenticationByUser::KEY_PASSWORD public constant
AuthenticationByUser::KEY_USERNAME public constant
AuthenticationByUser::__construct public function Constructs a new EntityListBuilder object.