You are here

class Authmap in External Authentication 8

Same name in this branch
  1. 8 src/Authmap.php \Drupal\externalauth\Authmap
  2. 8 src/Plugin/migrate/source/Authmap.php \Drupal\externalauth\Plugin\migrate\source\Authmap
  3. 8 src/Plugin/migrate/destination/Authmap.php \Drupal\externalauth\Plugin\migrate\destination\Authmap
Same name and namespace in other branches
  1. 2.0.x src/Authmap.php \Drupal\externalauth\Authmap

Class Authmap.

@package Drupal\externalauth

Hierarchy

Expanded class hierarchy of Authmap

1 file declares its use of Authmap
AuthmapTest.php in tests/src/Unit/AuthmapTest.php
3 string references to 'Authmap'
d6_authmap.yml in migration_templates/d6_authmap.yml
migration_templates/d6_authmap.yml
d7_authmap.yml in migration_templates/d7_authmap.yml
migration_templates/d7_authmap.yml
externalauth.services.yml in ./externalauth.services.yml
externalauth.services.yml
1 service uses Authmap
externalauth.authmap in ./externalauth.services.yml
Drupal\externalauth\Authmap

File

src/Authmap.php, line 13

Namespace

Drupal\externalauth
View source
class Authmap implements AuthmapInterface {

  /**
   * The connection object used for this data.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $connection;

  /**
   * {@inheritdoc}
   *
   * @param \Drupal\Core\Database\Connection $connection
   *   The connection object used for this data.
   */
  public function __construct(Connection $connection) {
    $this->connection = $connection;
  }

  /**
   * {@inheritdoc}
   */
  public function save(UserInterface $account, $provider, $authname, $data = NULL) {
    if (!is_scalar($data)) {
      $data = serialize($data);
    }

    // If a mapping (for the same provider) from this authname to a different
    // account already exists, this throws an exception. If a mapping (for the
    // same provider) to this account already exists, the currently stored
    // authname is overwritten.
    $this->connection
      ->merge('authmap')
      ->keys([
      'uid' => $account
        ->id(),
      'provider' => $provider,
    ])
      ->fields([
      'authname' => $authname,
      'data' => $data,
    ])
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function get($uid, $provider) {
    $authname = $this->connection
      ->select('authmap', 'am')
      ->fields('am', [
      'authname',
    ])
      ->condition('uid', $uid)
      ->condition('provider', $provider)
      ->range(0, 1)
      ->execute()
      ->fetchObject();
    if ($authname) {
      return $authname->authname;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function getAuthData($uid, $provider) {
    $data = $this->connection
      ->select('authmap', 'am')
      ->fields('am', [
      'authname',
      'data',
    ])
      ->condition('uid', $uid)
      ->condition('provider', $provider)
      ->range(0, 1)
      ->execute()
      ->fetchAssoc();
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function getAll($uid) {
    $query = $this->connection
      ->select('authmap', 'am')
      ->fields('am', [
      'provider',
      'authname',
    ])
      ->condition('uid', $uid)
      ->orderBy('provider', 'ASC')
      ->execute();
    $result = $query
      ->fetchAllAssoc('provider');
    if ($result) {
      foreach ($result as $provider => $data) {
        $result[$provider] = $data->authname;
      }
    }
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function getUid($authname, $provider) {
    $authname = $this->connection
      ->select('authmap', 'am')
      ->fields('am', [
      'uid',
    ])
      ->condition('authname', $authname)
      ->condition('provider', $provider)
      ->range(0, 1)
      ->execute()
      ->fetchObject();
    if ($authname) {
      return $authname->uid;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function delete($uid, $provider = NULL) {
    $query = $this->connection
      ->delete('authmap')
      ->condition('uid', $uid);
    if ($provider) {
      $query
        ->condition('provider', $provider);
    }
    $query
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function deleteProvider($provider) {
    $this->connection
      ->delete('authmap')
      ->condition('provider', $provider)
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Authmap::$connection protected property The connection object used for this data.
Authmap::delete public function Delete authmap entries for a given Drupal user ID. Overrides AuthmapInterface::delete
Authmap::deleteProvider public function Delete all authmap entries for a given provider. Overrides AuthmapInterface::deleteProvider
Authmap::get public function Get the external authname for a given user ID. Overrides AuthmapInterface::get
Authmap::getAll public function Get all external authnames for a given user ID. Overrides AuthmapInterface::getAll
Authmap::getAuthData public function Get the external authname & extra data for a given user ID. Overrides AuthmapInterface::getAuthData
Authmap::getUid public function Get a Drupal user ID based on an authname. Overrides AuthmapInterface::getUid
Authmap::save public function Save an external authname for a given Drupal user. Overrides AuthmapInterface::save
Authmap::__construct public function