You are here

public function oauth2_loginController::oauth2_login_mo_login in OAuth2 Login 8

File

src/Controller/oauth2_loginController.php, line 66
Contains \Drupal\oauth2_login\Controller\DefaultController.

Class

oauth2_loginController

Namespace

Drupal\oauth2_login\Controller

Code

public function oauth2_login_mo_login() {
  $code = isset($_GET['code']) ? $_GET['code'] : '';
  $code = Html::escape($code);
  $state = isset($_GET['state']) ? $_GET['state'] : '';
  $state = Html::escape($state);
  if (isset($code) && isset($state)) {
    if (session_id() == '' || !isset($_SESSION)) {
      session_start();
    }
    if (!isset($code)) {
      if (isset($_GET['error_description'])) {
        exit($_GET['error_description']);
      }
      else {
        if (isset($_GET['error'])) {
          exit($_GET['error']);
        }
      }
      exit('Invalid response');
    }
    else {
      $currentappname = "";
      if (isset($_SESSION['appname']) && !empty($_SESSION['appname'])) {
        $currentappname = $_SESSION['appname'];
      }
      else {
        if (isset($state) && !empty($state)) {
          $currentappname = base64_decode($state);
        }
      }
      if (empty($currentappname)) {
        exit('No request found for this application.');
      }
    }
  }

  // Getting Access Token
  $app = array();
  $app = \Drupal::config('oauth2_login.settings')
    ->get('oauth2_login_appval');
  $name_attr = "";
  $email_attr = "";
  $name = "";
  $email = "";
  if (isset($app['oauth2_login_email_attr'])) {
    $email_attr = trim($app['oauth2_login_email_attr']);
  }
  if (isset($app['oauth2_login_name_attr'])) {
    $name_attr = trim($app['oauth2_login_name_attr']);
  }
  $accessToken = AccessToken::getAccessToken($app['access_token_ep'], 'authorization_code', $app['client_id'], $app['client_secret'], $code, $app['callback_uri']);
  if (!$accessToken) {
    print_r('Invalid token received.');
    exit;
  }
  $resourceownerdetailsurl = $app['user_info_ep'];
  if (substr($resourceownerdetailsurl, -1) == "=") {
    $resourceownerdetailsurl .= $accessToken;
  }
  $resourceOwner = UserResource::getResourceOwner($resourceownerdetailsurl, $accessToken);

  /*
   *   Test Configuration
   */
  if (isset($_COOKIE['Drupal_visitor_mo_oauth_test']) && $_COOKIE['Drupal_visitor_mo_oauth_test'] == true) {
    $_COOKIE['Drupal_visitor_mo_oauth_test'] = 0;
    $module_path = drupal_get_path('module', 'oauth2_login');
    $username = isset($resourceOwner['email']) ? $resourceOwner['email'] : 'User';
    \Drupal::configFactory()
      ->getEditable('oauth2_login.settings')
      ->set('oauth2_login_attr_list_from_server', $resourceOwner)
      ->save();
    echo '<div style="font-family:Calibri;padding:0 3%;">';
    echo '<div style="color: #3c763d;background-color: #dff0d8; padding:2%;margin-bottom:20px;text-align:center; border:1px solid #AEDB9A;
                        font-size:15pt;">
                        TEST SUCCESSFUL
                      </div>
                      <div style="display:block;text-align:center;margin-bottom:4%;">
                        <img style="width:15%;"src="' . $module_path . '/includes/images/green_check.png">
                      </div>';
    echo '<span style="font-size:13pt;"><b>Hello</b>, ' . $username . '</span><br/>
                      <p style="font-weight:bold;font-size:13pt;margin-left:1%;">ATTRIBUTES RECEIVED:</p>
                      <table style="border-collapse:collapse;border-spacing:0; display:table;width:100%; font-size:13pt;background-color:#EDEDED;">
                          <tr style="text-align:center;">
                              <td style="font-weight:bold;border:2px solid #949090;padding:2%;width: fit-content;">ATTRIBUTE NAME</td>
                              <td style="font-weight:bold;padding:2%;border:2px solid #949090; word-wrap:break-word;">ATTRIBUTE VALUE</td>
                          </tr>';
    self::testattrmappingconfig("", $resourceOwner);
    echo '</table></div>';
    echo '<div style="margin:3%;display:block;text-align:center;">
                        <input style="padding:1%;width:37%;background: #0091CD none repeat scroll 0% 0%;cursor: pointer;font-size:15px;
                            border-width: 1px;border-style: solid;border-radius: 3px;white-space: nowrap;box-sizing: border-box;border-color: #0073AA;
                            box-shadow: 0px 1px 0px rgba(120, 200, 230, 0.6) inset;color: #FFF;"type="button" value="Configure Attribute/Role Mapping"
                        onClick="close_and_redirect();">
                        <input style="padding:1%;width:100px;background: #0091CD none repeat scroll 0% 0%;cursor: pointer;font-size:15px;
                            border-width: 1px;border-style: solid;border-radius: 3px;white-space: nowrap;box-sizing: border-box;border-color: #0073AA;
                            box-shadow: 0px 1px 0px rgba(120, 200, 230, 0.6) inset;color: #FFF;"type="button" value="Done" onClick="self.close();">
                    </div>
                    <script>
                        function close_and_redirect(){
                            window.opener.redirect_to_attribute_mapping();
                            self.close();
                        }
                        function redirect_to_attribute_mapping(){
                            var baseurl = window.location.href.replace("config_clc","mapping");
                            window.location.href= baseurl;
                          }
                    </script>';
    return new Response();
    exit;
  }
  if (!empty($email_attr)) {
    $email = self::getnestedattribute($resourceOwner, $email_attr);
  }

  //$resourceOwner[$email_attr];
  if (!empty($name_attr)) {
    $name = self::getnestedattribute($resourceOwner, $name_attr);
  }

  //$resourceOwner[$name_attr];

  /*************==============Attributes not mapped check===============************/
  if (empty($email)) {
    echo "Email address not received. Check your <b>Attribute Mapping<b> configuration.";
    exit;
  }

  //Validates the email format
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo "Invalid email format of the received value";
    exit;
  }
  if (empty($name)) {
    $name = $email;
  }
  $account = '';
  if (!empty($email)) {
    $account = user_load_by_mail($email);
  }
  if ($account == null) {
    if (!empty($name) && isset($name)) {
      $account = user_load_by_name($name);
    }
  }
  global $base_url;
  global $user;
  $mo_count = "";
  $mo_count = \Drupal::config('oauth2_login.settings')
    ->get('oauth2_login_free_users');

  /**
   * Creating a new user in case the user does not exists in the Drupal database
   */
  if (!isset($account->uid)) {
    if ($mo_count <= 10) {
      $mo_count = $mo_count + 1;
      \Drupal::configFactory()
        ->getEditable('oauth2_login.settings')
        ->set('oauth2_login_free_users', $mo_count)
        ->save();
      $random_password = user_password(8);
      $new_user = [
        'name' => $name,
        'mail' => $email,
        'pass' => $random_password,
        'status' => 1,
      ];
      $account = User::create($new_user);
      $account
        ->save();
    }
    else {
      echo '<br><br><br><br><br><div style="color: #111010;background-color: #fadbdb; padding:2%;margin-bottom:20px;text-align:center;
                        border:1px solid #fadbdb;font-size:15pt;">
                        You can create only 10 new users in this version of the plugin/module.
                        <br>Please upgrade to the enterprise version of the plugin in order to create unlimited new users.</div>';
      return new Response();
    }
  }
  $user = \Drupal\user\Entity\User::load($account
    ->id());
  $edit = array();
  if (!empty(\Drupal::config('oauth2_login.settings')
    ->get('oauth2_login_base_url'))) {
    $baseUrlValue = \Drupal::config('oauth2_login.settings')
      ->get('oauth2_login_base_url');
  }
  else {
    $baseUrlValue = $base_url;
  }
  $edit['redirect'] = $baseUrlValue;
  user_login_finalize($account);
  $response = new RedirectResponse($edit['redirect']);
  $response
    ->send();
  return new Response();
}