You are here

class LinkedInAuthManager in Open Social 8.7

Same name and namespace in other branches
  1. 8.9 modules/custom/social_auth_linkedin/src/LinkedInAuthManager.php \Drupal\social_auth_linkedin\LinkedInAuthManager
  2. 8 modules/custom/social_auth_linkedin/src/LinkedInAuthManager.php \Drupal\social_auth_linkedin\LinkedInAuthManager
  3. 8.2 modules/custom/social_auth_linkedin/src/LinkedInAuthManager.php \Drupal\social_auth_linkedin\LinkedInAuthManager
  4. 8.3 modules/custom/social_auth_linkedin/src/LinkedInAuthManager.php \Drupal\social_auth_linkedin\LinkedInAuthManager
  5. 8.4 modules/custom/social_auth_linkedin/src/LinkedInAuthManager.php \Drupal\social_auth_linkedin\LinkedInAuthManager
  6. 8.5 modules/custom/social_auth_linkedin/src/LinkedInAuthManager.php \Drupal\social_auth_linkedin\LinkedInAuthManager
  7. 8.6 modules/custom/social_auth_linkedin/src/LinkedInAuthManager.php \Drupal\social_auth_linkedin\LinkedInAuthManager
  8. 8.8 modules/custom/social_auth_linkedin/src/LinkedInAuthManager.php \Drupal\social_auth_linkedin\LinkedInAuthManager

Class LinkedInAuthManager.

@package Drupal\social_auth_linkedin

Hierarchy

Expanded class hierarchy of LinkedInAuthManager

2 files declare their use of LinkedInAuthManager
LinkedInAuthController.php in modules/custom/social_auth_linkedin/src/Controller/LinkedInAuthController.php
LinkedInLinkController.php in modules/custom/social_auth_linkedin/src/Controller/LinkedInLinkController.php
1 string reference to 'LinkedInAuthManager'
social_auth_linkedin.services.yml in modules/custom/social_auth_linkedin/social_auth_linkedin.services.yml
modules/custom/social_auth_linkedin/social_auth_linkedin.services.yml
1 service uses LinkedInAuthManager
social_auth_linkedin.auth_manager in modules/custom/social_auth_linkedin/social_auth_linkedin.services.yml
\Drupal\social_auth_linkedin\LinkedInAuthManager

File

modules/custom/social_auth_linkedin/src/LinkedInAuthManager.php, line 16

Namespace

Drupal\social_auth_linkedin
View source
class LinkedInAuthManager extends AuthManager {

  /**
   * {@inheritdoc}
   */
  public function getSocialNetworkKey() {
    return LinkedInAuthSettings::getSocialNetworkKey();
  }

  /**
   * {@inheritdoc}
   */
  public function setSdk($sdk) {
    if (!$sdk instanceof Client) {
      throw new InvalidArgumentException('SDK object should be instance of \\LinkedIn\\Client class');
    }
    $this->sdk = $sdk;
  }

  /**
   * {@inheritdoc}
   */
  public function getAuthenticationUrl($type, array $scope = [
    Scope::READ_LITE_PROFILE,
    Scope::READ_EMAIL_ADDRESS,
  ]) {
    $redirect_url = $this
      ->getRedirectUrl($type);

    // Set the redirect for the third party with our own.
    $this->sdk
      ->setRedirectUrl($redirect_url);
    return $this->sdk
      ->getLoginUrl([
      'scope' => implode(',', $scope),
    ]);
  }

  /**
   * {@inheritdoc}
   */
  public function getAccessToken($type = '') {
    try {

      // Set the RedirectUrl before retrieving the access token.
      $redirect_url = $this
        ->getRedirectUrl($type);
      $this->sdk
        ->setRedirectUrl($redirect_url);
      $access_token = $this->sdk
        ->getAccessToken($_GET['code']);
      return $access_token;
    } catch (LinkedInException $e) {
      $this->loggerFactory
        ->get('social_auth_linkedin')
        ->error('Could not get LinkedIn access token. LinkedInException: @message', [
        '@message' => $e
          ->getMessage(),
      ]);
      return NULL;
    }
    if ($access_token) {
      $this->sdk
        ->setAccessToken($access_token);
      return $access_token;
    }
    $this->loggerFactory
      ->get('social_auth_linkedin')
      ->error('Could not get LinkedIn access token. User cancelled the dialog in LinkedIn or return URL was not valid.');
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getProfile() {
    if (!$this->profile) {

      // Add basic profile information.
      if (($profile = $this->sdk
        ->get('me', [
        'fields' => 'id,firstName,lastName',
      ])) && !isset($profile['errorCode'])) {
        $this->profile['id'] = $profile['id'];
        $this->profile['basic_information'] = $profile;
      }
      if (($profile = $this->sdk
        ->get('me', [
        'projection' => '(id,profilePicture(displayImage~:playableStreams))',
      ])) && !isset($profile['errorCode'])) {
        $this->profile['profile_picture'] = $profile;
      }
      if (($profile = $this->sdk
        ->get('emailAddress', [
        'q' => 'members',
        'projection' => '(elements*(handle~))',
      ])) && !isset($profile['errorCode'])) {
        $this->profile['email_information'] = $profile;
      }
    }
    return $this->profile;
  }

  /**
   * {@inheritdoc}
   */
  public function getProfilePicture() {
    if (!empty($this->profile['profile_picture']['profilePicture']['displayImage~']['elements'])) {
      $profile_picture = end($this->profile['profile_picture']['profilePicture']['displayImage~']['elements']);
      return $profile_picture['identifiers'][0]['identifier'];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setAccessToken($access_token) {
    $this->sdk
      ->setAccessToken($access_token);
  }

  /**
   * {@inheritdoc}
   */
  public function getAccountId() {
    return isset($this->profile['id']) ? $this->profile['id'] : NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getFirstName() {
    if (!empty($this->profile['basic_information']['firstName'])) {
      return array_values($this->profile['basic_information']['firstName']['localized'])[0];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getLastName() {
    if (!empty($this->profile['basic_information']['lastName'])) {
      return array_values($this->profile['basic_information']['lastName']['localized'])[0];
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getEmailAddress() {
    if (!empty($this->profile['email_information']['elements'])) {
      return $this->profile['email_information']['elements'][0]['handle~']['emailAddress'];
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AuthManager::$entityFieldManager protected property
AuthManager::$fieldPicture protected property Contains the field definition with a profile picture.
AuthManager::$loggerFactory protected property
AuthManager::$profile protected property Contains object of a profile received from a social network.
AuthManager::$sdk protected property Object of SDK to work with API of social network.
AuthManager::$urlGenerator protected property
AuthManager::getPreferredResolution public function Determines preferred profile pic resolution from account settings. Overrides AuthManagerInterface::getPreferredResolution
AuthManager::getRedirectUrl public function Returns URL to authorize/registration depending on type. Overrides AuthManagerInterface::getRedirectUrl
AuthManager::getUsername public function Returns user on a social network if it possible. Overrides AuthManagerInterface::getUsername 1
AuthManager::setFieldPicture public function Set an instance of a field definition that contains picture. Overrides AuthManagerInterface::setFieldPicture
AuthManager::__construct public function AuthManager constructor.
LinkedInAuthManager::getAccessToken public function Reads user's access token from social network. Overrides AuthManagerInterface::getAccessToken
LinkedInAuthManager::getAccountId public function Returns an account ID on a social network. Overrides AuthManagerInterface::getAccountId
LinkedInAuthManager::getAuthenticationUrl public function Returns the login URL where user will be redirected for authentication. Overrides AuthManagerInterface::getAuthenticationUrl
LinkedInAuthManager::getEmailAddress public function
LinkedInAuthManager::getFirstName public function Returns first name on a social network if it possible. Overrides AuthManagerInterface::getFirstName
LinkedInAuthManager::getLastName public function Returns last name on a social network if it possible. Overrides AuthManagerInterface::getLastName
LinkedInAuthManager::getProfile public function Returns object of a user profile. Overrides AuthManagerInterface::getProfile
LinkedInAuthManager::getProfilePicture public function Returns URL of a profile picture. Overrides AuthManagerInterface::getProfilePicture
LinkedInAuthManager::getSocialNetworkKey public function Returns key-name of a social network. Overrides AuthManagerInterface::getSocialNetworkKey
LinkedInAuthManager::setAccessToken public function Set access token to AuthManager to use it for API calls. Overrides AuthManagerInterface::setAccessToken
LinkedInAuthManager::setSdk public function Set instance of SDK. Overrides AuthManagerInterface::setSdk