You are here

class SimpleFBConnectController in Simple FB Connect 8

Same name and namespace in other branches
  1. 8.3 src/Controller/SimpleFbConnectController.php \Drupal\simple_fb_connect\Controller\SimpleFbConnectController
  2. 8.2 src/Controller/SimpleFbConnectController.php \Drupal\simple_fb_connect\Controller\SimpleFbConnectController

Hierarchy

Expanded class hierarchy of SimpleFBConnectController

File

src/Controller/SimpleFBConnectController.php, line 17
Contains \Drupal\simple_fb_connect\Controller\SimpleFBConnectController.

Namespace

Drupal\simple_fb_connect\Controller
View source
class SimpleFBConnectController extends ControllerBase {

  //Define constructor
  public function unified_login_register() {
    $facebook = facebook_client();
    $fb_user = $facebook
      ->getUser();
    if ($fb_user) {
      $fb_user_profile = $facebook
        ->api('/me');
      if (isset($fb_user_profile['email'])) {
        $query = db_select('users_field_data', 'u');

        // @TODO Use $this->connection() instead as suggested by Adam
        $query
          ->condition('u.mail', String::checkPlain($fb_user_profile['email']));
        $query
          ->fields('u', array(
          'uid',
        ));
        $query
          ->range(0, 1);
        $drupal_user_id = 0;
        $result = $query
          ->execute()
          ->fetchAll(\PDO::FETCH_ASSOC);
        if (count($result)) {
          $drupal_user_id = $result[0]['uid'];
        }
        if ($drupal_user_id) {

          //$user_obj = user_load($drupal_user_id);
          $user_obj = User::load($drupal_user_id);
          if ($user_obj
            ->isActive()) {
            user_login_finalize($user_obj);
            drupal_set_message(t('You have been logged in with the username !username', array(
              '!username' => $user_obj
                ->getUsername(),
            )));

            //@TODO Replace the reidrection with simple_fb_connect_post_login_url

            //return $this->redirect(\Drupal::config('simple_fb_connect.settings')->get('simple_fb_connect_post_login_url'));
            return $this
              ->redirect('user.page');
          }
          else {
            drupal_set_message($this
              ->t('You could not be logged in as your account is blocked. Contact site administrator.'), 'error');
            return $this
              ->redirect('user.page');
          }
        }
        else {
          if (!\Drupal::config('simple_fb_connect.settings')
            ->get('simple_fb_connect_login_only')) {

            //create the drupal user

            //This will generate a random password, you could set your own here
            $fb_username = isset($fb_user_profile['username']) ? $fb_user_profile['username'] : $fb_user_profile['name'];
            $drupal_username_generated = simple_fb_connect_unique_user_name(String::checkPlain($fb_username));
            $password = user_password(8);

            //set up the user fields
            $fields = array(
              'name' => $drupal_username_generated,
              'mail' => String::checkPlain($fb_user_profile['email']),
              'pass' => $password,
              'status' => 1,
              'init' => 'email address',
              'roles' => array(
                DRUPAL_AUTHENTICATED_RID => 'authenticated user',
              ),
            );
            if (\Drupal::config('simple_fb_connect.settings')
              ->get('simple_fb_connect_user_pictures')) {

              //@TODO default it to SIMPLE_FB_CONNECT_DEFAULT_DIMENSIONS_STRING
              $dimensions_in_text = \Drupal::config('simple_fb_connect.settings')
                ->get('user_picture_dimensions');
              $dimensions = explode('x', $dimensions_in_text);
              if (count($dimensions) == 2) {
                $width = $dimensions[0];
                $height = $dimensions[1];
              }
              else {
                $width = SIMPLE_FB_CONNECT_DEFAULT_WIDTH;
                $height = SIMPLE_FB_CONNECT_DEFAULT_HEIGHT;
              }
              $pic_url = "https://graph.facebook.com/" . String::checkPlain($fb_user_profile['id']) . "/picture?width={$width}&height={$height}";
              $result = \Drupal::httpClient()
                ->get($pic_url);
              $file = 0;
              if ($result
                ->getStatusCode() == 200) {

                //@TODO: get default path
                $picture_directory = file_default_scheme() . '://' . 'pictures/';
                file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY);
                $file = file_save_data($result
                  ->getBody(), $picture_directory . '/' . String::checkPlain($fb_user_profile['id']) . '.jpg', FILE_EXISTS_RENAME);
              }
              else {

                // Error handling.
              }
              if (is_object($file)) {
                $fields['user_picture'] = $file
                  ->id();
              }
            }

            //the first parameter is left blank so a new user is created
            $account = entity_create('user', $fields);
            $account
              ->save();

            // If you want to send the welcome email, use the following code
            // Manually set the password so it appears in the e-mail.
            $account->password = $fields['pass'];

            // Send the e-mail through the user module.

            //@TODO

            //drupal_mail('user', 'register_no_approval_required', $account->mail, NULL, array('account' => $account), variable_get('site_mail', 'admin@drupalsite.com'));
            drupal_set_message(t('You have been registered with the username !username', array(
              '!username' => $account
                ->getUsername(),
            )));
            return $this
              ->redirect('simple_fb_connect_login');
          }
          else {
            drupal_set_message(t('There was no account with the email addresse !email found. Please register before trying to login.', array(
              '!email' => String::checkPlain($fb_user_profile['email']),
            )), 'error');
            return $this
              ->redirect('user.page');
          }
        }
      }
      else {
        drupal_set_message(t('Though you have authorised the Facebook app to access your profile, you have revoked the permission to access email address. Please contact site administrator.'), 'error');
        return $this
          ->redirect('user.page');
      }
    }
    else {
      if (!isset($_REQUEST['error'])) {
        if (\Drupal::config('simple_fb_connect.settings')
          ->get('simple_fb_connect_appid')) {
          $scope_string = '';

          //                    // Make sure at least one module implements our hook
          //                    @TODO
          //                    if (sizeof(module_implements('simple_fb_scope_info')) > 0) {
          //                        // Call modules that implement the hook, and let them change scope.
          //                        $scopes = module_invoke_all('simple_fb_scope_info', array());
          //                        $scope_string = implode(',', $scopes);
          //                    }
          $scope_string .= ',email';
          $login_url_params = array(
            'scope' => $scope_string,
            'fbconnect' => 1,
            'redirect_uri' => 'http://' . $_SERVER['HTTP_HOST'] . request_uri(),
          );
          $login_url = $facebook
            ->getLoginUrl($login_url_params);

          //@TODO

          //drupal_goto($login_url);

          //return $this->redirect($login_url);
          return new RedirectResponse($login_url);
        }
        else {
          drupal_set_message(t('Facebook App ID Missing. Can not perform Login now. Contact Site administrator.'), 'error');
          return $this
            ->redirect('user.page');
        }
      }
      else {
        if ($_REQUEST['error'] == SIMPLE_FB_CONNECT_PERMISSION_DENIED_PARAMETER) {
          drupal_set_message(t('Could not login with facebook. You did not grant permission for this app on facebook to access your email address.'), 'error');
        }
        else {
          drupal_set_message(t('There was a problem in logging in with facebook. Contact site administrator.'), 'error');
        }
        return $this
          ->redirect('user.page');
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 40
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SimpleFBConnectController::unified_login_register public function
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.