class FacebookAuthController in Open Social 8
Same name and namespace in other branches
- 8.9 modules/custom/social_auth_facebook/src/Controller/FacebookAuthController.php \Drupal\social_auth_facebook\Controller\FacebookAuthController
- 8.2 modules/custom/social_auth_facebook/src/Controller/FacebookAuthController.php \Drupal\social_auth_facebook\Controller\FacebookAuthController
- 8.3 modules/custom/social_auth_facebook/src/Controller/FacebookAuthController.php \Drupal\social_auth_facebook\Controller\FacebookAuthController
- 8.4 modules/custom/social_auth_facebook/src/Controller/FacebookAuthController.php \Drupal\social_auth_facebook\Controller\FacebookAuthController
- 8.5 modules/custom/social_auth_facebook/src/Controller/FacebookAuthController.php \Drupal\social_auth_facebook\Controller\FacebookAuthController
- 8.6 modules/custom/social_auth_facebook/src/Controller/FacebookAuthController.php \Drupal\social_auth_facebook\Controller\FacebookAuthController
- 8.7 modules/custom/social_auth_facebook/src/Controller/FacebookAuthController.php \Drupal\social_auth_facebook\Controller\FacebookAuthController
- 8.8 modules/custom/social_auth_facebook/src/Controller/FacebookAuthController.php \Drupal\social_auth_facebook\Controller\FacebookAuthController
Class FacebookAuthController.
@package Drupal\social_auth_facebook\Controller
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\social_auth_facebook\Controller\FacebookAuthController
Expanded class hierarchy of FacebookAuthController
File
- modules/
custom/ social_auth_facebook/ src/ Controller/ FacebookAuthController.php, line 17
Namespace
Drupal\social_auth_facebook\ControllerView source
class FacebookAuthController extends ControllerBase {
protected $networkManager;
protected $authManager;
/**
* Contains access token to work with API.
*
* @var string
*/
protected $accessToken;
/**
* FacebookAuthController constructor.
*/
public function __construct(NetworkManager $network_manager, FacebookAuthManager $auth_manager) {
$this->networkManager = $network_manager;
$this->authManager = $auth_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('plugin.network.manager'), $container
->get('social_auth_facebook.auth_manager'));
}
/**
* Returns the redirect response.
*
* @param string $type
* Type of action. "login" or "register".
*
* @return \Drupal\Core\Routing\TrustedRedirectResponse|\Symfony\Component\HttpFoundation\RedirectResponse
* Returns a RedirectResponse.
*/
protected function getRedirectResponse($type) {
$sdk = $this
->getSdk($type);
if ($sdk instanceof RedirectResponse) {
return $sdk;
}
$this->authManager
->setSdk($sdk);
$url = $this->authManager
->getAuthenticationUrl($type);
return new TrustedRedirectResponse($url);
}
/**
* Response for path 'user/login/facebook'.
*
* Redirects the user to FB for authentication.
*/
public function userLogin() {
return $this
->getRedirectResponse('login');
}
/**
* Authorizes the user after redirect from Facebook.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* Returns a RedirectResponse.
*/
public function userLoginCallback() {
$sdk = $this
->getSdk('login');
if ($sdk instanceof RedirectResponse) {
return $sdk;
}
$this->authManager
->setSdk($sdk);
$profile = $this
->getProfile('login');
if ($profile instanceof RedirectResponse) {
return $profile;
}
// Check whether user account exists.
$account = $this
->entityTypeManager()
->getStorage('user')
->loadByProperties([
'facebook_id' => $profile
->getField('id'),
]);
if (!$account) {
return $this
->redirect('social_auth_facebook.user_login_notice');
}
$account = current($account);
if (!$account
->get('status')->value) {
drupal_set_message($this
->t('Your account is blocked. Contact the site administrator.'), 'error');
return $this
->redirect('user.login');
}
// Authorize the user and redirect him to the edit account page.
user_login_finalize($account);
$url = $this
->getUrlGenerator()
->getPathFromRoute('entity.user.canonical', [
'user' => $account
->id(),
]);
\Drupal::request()->query
->set('destination', $url);
return new RedirectResponse($url);
}
/**
* Response for path 'user/register/facebook'.
*
* Redirects the user to FB for registration.
*/
public function userRegister() {
return $this
->getRedirectResponse('register');
}
/**
* Registers the new account after redirect from Facebook.
*
* @return \Symfony\Component\HttpFoundation\RedirectResponse
* Return a RedirectResponse.
*/
public function userRegisterCallback() {
$sdk = $this
->getSdk('register');
if ($sdk instanceof RedirectResponse) {
return $sdk;
}
$this->authManager
->setSdk($sdk);
$profile = $this
->getProfile('register');
if ($profile instanceof RedirectResponse) {
return $profile;
}
// Check whether user account exists. If account already exists,
// authorize the user and redirect him to the account page.
$account = $this
->entityTypeManager()
->getStorage('user')
->loadByProperties([
'facebook_id' => $profile
->getField('id'),
]);
if ($account) {
$account = current($account);
if (!$account
->get('status')->value) {
drupal_set_message($this
->t('You already have account on this site, but your account is blocked. Contact the site administrator.'), 'error');
return $this
->redirect('user.register');
}
user_login_finalize($account);
return $this
->redirect('entity.user.canonical', [
'user' => $account
->id(),
]);
}
// Save email and name to storage to use for auto fill the registration
// form.
$data_handler = $this->networkManager
->createInstance('social_auth_facebook')
->getDataHandler();
$data_handler
->set('access_token', $this->accessToken);
$data_handler
->set('mail', $profile
->getField('email'));
$data_handler
->set('name', $profile
->getField('name'));
drupal_set_message($this
->t('You are now connected with @network, please continue registration', [
'@network' => $this
->t('Facebook'),
]));
return $this
->redirect('user.register', [
'provider' => 'facebook',
]);
}
/**
* Returns the SDK instance or RedirectResponse when error occurred.
*
* @param string $type
* Type of action, "login" or "register".
*
* @return mixed|\Symfony\Component\HttpFoundation\RedirectResponse
* Returns an instance of the SDK or a Redirect Response.
*/
public function getSdk($type) {
$network_manager = $this->networkManager
->createInstance('social_auth_facebook');
if (!$network_manager
->isActive()) {
drupal_set_message($this
->t('@network is disabled. Contact the site administrator', [
'@network' => $this
->t('Facebook'),
]), 'error');
return $this
->redirect('user.' . $type);
}
if (!($sdk = $network_manager
->getSdk())) {
drupal_set_message($this
->t('@network Auth not configured properly. Contact the site administrator.', [
'@network' => $this
->t('Facebook'),
]), 'error');
return $this
->redirect('user.' . $type);
}
return $sdk;
}
/**
* Loads access token, then loads profile.
*
* @param string $type
* The type.
*
* @return object
* Returns an object.
*/
public function getProfile($type) {
// Get the OAuth token from Facebook.
if (!($access_token = $this->authManager
->getAccessToken($type))) {
drupal_set_message($this
->t('@network login failed. Token is not valid.', [
'@network' => $this
->t('Facebook'),
]), 'error');
return $this
->redirect('user.' . $type);
}
// Get user's Facebook profile from Facebook API.
if (!($profile = $this->authManager
->getProfile()) || !$profile
->getField('id')) {
drupal_set_message($this
->t('@network login failed, could not load @network profile. Contact the site administrator.', [
'@network' => $this
->t('Facebook'),
]), 'error');
return $this
->redirect('user.' . $type);
}
$this->accessToken = $access_token;
return $profile;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
FacebookAuthController:: |
protected | property | Contains access token to work with API. | |
FacebookAuthController:: |
protected | property | ||
FacebookAuthController:: |
protected | property | ||
FacebookAuthController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
FacebookAuthController:: |
public | function | Loads access token, then loads profile. | |
FacebookAuthController:: |
protected | function | Returns the redirect response. | |
FacebookAuthController:: |
public | function | Returns the SDK instance or RedirectResponse when error occurred. | |
FacebookAuthController:: |
public | function | Response for path 'user/login/facebook'. | |
FacebookAuthController:: |
public | function | Authorizes the user after redirect from Facebook. | |
FacebookAuthController:: |
public | function | Response for path 'user/register/facebook'. | |
FacebookAuthController:: |
public | function | Registers the new account after redirect from Facebook. | |
FacebookAuthController:: |
public | function | FacebookAuthController constructor. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |