You are here

class FacebookAuthManager in Open Social 8.7

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

Class FacebookAuthManager.

@package Drupal\social_auth_facebook

Hierarchy

Expanded class hierarchy of FacebookAuthManager

2 files declare their use of FacebookAuthManager
FacebookAuthController.php in modules/custom/social_auth_facebook/src/Controller/FacebookAuthController.php
FacebookLinkController.php in modules/custom/social_auth_facebook/src/Controller/FacebookLinkController.php
1 string reference to 'FacebookAuthManager'
social_auth_facebook.services.yml in modules/custom/social_auth_facebook/social_auth_facebook.services.yml
modules/custom/social_auth_facebook/social_auth_facebook.services.yml
1 service uses FacebookAuthManager
social_auth_facebook.auth_manager in modules/custom/social_auth_facebook/social_auth_facebook.services.yml
\Drupal\social_auth_facebook\FacebookAuthManager

File

modules/custom/social_auth_facebook/src/FacebookAuthManager.php, line 17

Namespace

Drupal\social_auth_facebook
View source
class FacebookAuthManager extends AuthManager {

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

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

  /**
   * {@inheritdoc}
   */
  public function getAuthenticationUrl($type, array $scope = [
    'public_profile',
    'email',
  ]) {
    $helper = $this->sdk
      ->getRedirectLoginHelper();
    return $helper
      ->getLoginUrl($this
      ->getRedirectUrl($type), $scope);
  }

  /**
   * {@inheritdoc}
   */
  public function getAccessToken($type) {
    $helper = $this->sdk
      ->getRedirectLoginHelper();
    $redirect_url = $this
      ->getRedirectUrl($type);
    try {
      $access_token = $helper
        ->getAccessToken($redirect_url);
    } catch (FacebookResponseException $e) {
      $this->loggerFactory
        ->get('social_auth_facebook')
        ->error('Could not get Facebook access token. FacebookResponseException: @message', [
        '@message' => $e
          ->getMessage(),
      ]);
      return NULL;
    } catch (FacebookSDKException $e) {
      $this->loggerFactory
        ->get('social_auth_facebook')
        ->error('Could not get Facebook access token. FacebookSDKException: @message', [
        '@message' => $e
          ->getMessage(),
      ]);
      return NULL;
    }
    if ($access_token) {
      $this->sdk
        ->setDefaultAccessToken($access_token);
      return $access_token;
    }
    $this->loggerFactory
      ->get('social_auth_facebook')
      ->error('Could not get Facebook access token. User cancelled the dialog in Facebook or return URL was not valid.');
    return NULL;
  }

  /**
   * {@inheritdoc}
   */
  public function getProfile() {
    try {
      $this->profile = $this->sdk
        ->get('/me?fields=id,name,email,first_name,last_name')
        ->getGraphNode();
    } catch (FacebookResponseException $e) {
      $this->loggerFactory
        ->get('social_auth_facebook')
        ->error('Could not load Facebook user profile: FacebookResponseException: @message', [
        '@message' => $e
          ->getMessage(),
      ]);
      return NULL;
    } catch (FacebookSDKException $e) {
      $this->loggerFactory
        ->get('social_auth_facebook')
        ->error('Could not load Facebook user profile: FacebookSDKException: @message', [
        '@message' => $e
          ->getMessage(),
      ]);
      return NULL;
    }
    return $this->profile;
  }

  /**
   * {@inheritdoc}
   */
  public function getProfilePicture() {
    $resolution = $this
      ->getPreferredResolution();
    $query = [
      'redirect' => 'false',
    ];
    if (is_array($resolution)) {
      $query += $resolution;
    }
    try {
      $graph_node = $this->sdk
        ->get('/me/picture?' . http_build_query($query))
        ->getGraphNode();
      $is_silhouette = (bool) $graph_node
        ->getField('is_silhouette');
      if ($is_silhouette) {
        return FALSE;
      }
      return $graph_node
        ->getField('url');
    } catch (FacebookResponseException $e) {
      $this->loggerFactory
        ->get('social_auth_facebook')
        ->error('Could not load Facebook profile picture: FacebookResponseException: @message', [
        '@message' => $e
          ->getMessage(),
      ]);
      return NULL;
    } catch (FacebookSDKException $e) {
      $this->loggerFactory
        ->get('social_auth_facebook')
        ->error('Could not load Facebook profile picture: FacebookSDKException: @message', [
        '@message' => $e
          ->getMessage(),
      ]);
      return NULL;
    }
    return FALSE;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getAccountId() {
    if ($profile = $this
      ->getProfile()) {
      return $profile
        ->getField('id');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getFirstName() {
    if ($profile = $this
      ->getProfile()) {
      return $profile
        ->getField('first_name');
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getLastName() {
    if ($profile = $this
      ->getProfile()) {
      return $profile
        ->getField('last_name');
    }
  }

}

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.
FacebookAuthManager::getAccessToken public function Reads user's access token from social network. Overrides AuthManagerInterface::getAccessToken
FacebookAuthManager::getAccountId public function Returns an account ID on a social network. Overrides AuthManagerInterface::getAccountId
FacebookAuthManager::getAuthenticationUrl public function Returns the login URL where user will be redirected for authentication. Overrides AuthManagerInterface::getAuthenticationUrl
FacebookAuthManager::getFirstName public function Returns first name on a social network if it possible. Overrides AuthManagerInterface::getFirstName
FacebookAuthManager::getLastName public function Returns last name on a social network if it possible. Overrides AuthManagerInterface::getLastName
FacebookAuthManager::getProfile public function Returns object of a user profile. Overrides AuthManagerInterface::getProfile
FacebookAuthManager::getProfilePicture public function Returns URL of a profile picture. Overrides AuthManagerInterface::getProfilePicture
FacebookAuthManager::getSocialNetworkKey public function Returns key-name of a social network. Overrides AuthManagerInterface::getSocialNetworkKey
FacebookAuthManager::setAccessToken public function Set access token to AuthManager to use it for API calls. Overrides AuthManagerInterface::setAccessToken
FacebookAuthManager::setSdk public function Set instance of SDK. Overrides AuthManagerInterface::setSdk