You are here

public function miniorange_oauth_clientController::miniorange_oauth_client_mo_login in Drupal OAuth & OpenID Connect Login - OAuth2 Client SSO Login 8

1 string reference to 'miniorange_oauth_clientController::miniorange_oauth_client_mo_login'
miniorange_oauth_client.routing.yml in ./miniorange_oauth_client.routing.yml
miniorange_oauth_client.routing.yml

File

src/Controller/miniorange_oauth_clientController.php, line 118
Contains \Drupal\miniorange_oauth_client\Controller\DefaultController.

Class

miniorange_oauth_clientController

Namespace

Drupal\miniorange_oauth_client\Controller

Code

public function miniorange_oauth_client_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('miniorange_oauth_client.settings')
    ->get('miniorange_oauth_client_appval');
  $name_attr = "";
  $email_attr = "";
  $name = "";
  $email = "";
  if (isset($app['miniorange_oauth_client_email_attr'])) {
    $email_attr = trim($app['miniorange_oauth_client_email_attr']);
  }
  if (isset($app['miniorange_oauth_client_name_attr'])) {
    $name_attr = trim($app['miniorange_oauth_client_name_attr']);
  }
  $parse_from_header = \Drupal::config('miniorange_oauth_client.settings')
    ->get('miniorange_oauth_send_with_header_oauth');
  $parse_from_body = \Drupal::config('miniorange_oauth_client.settings')
    ->get('miniorange_oauth_send_with_body_oauth');
  if (!$parse_from_header == TRUE || !$parse_from_header == 1) {
    $parse_from_header = false;
  }
  if (!$parse_from_body == TRUE || !$parse_from_body == 1) {
    $parse_from_body = false;
  }
  $accessToken = self::getAccessToken($app['access_token_ep'], 'authorization_code', $app['client_id'], $app['client_secret'], $code, $app['callback_uri'], $parse_from_header, $parse_from_body);
  if (!$accessToken) {
    print_r('Invalid token received.');
    exit;
  }
  $resourceownerdetailsurl = $app['user_info_ep'];
  if (substr($resourceownerdetailsurl, -1) == "=") {
    $resourceownerdetailsurl .= $accessToken;
  }
  $resourceOwner = self::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::service('extension.list.module')
      ->getPath('miniorange_oauth_client');
    $username = isset($resourceOwner['email']) ? $resourceOwner['email'] : 'User';
    $someattrs = '';
    Utilities::show_attr($resourceOwner, $someattrs, 0, '', '<tr style="text-align:center;">', "<td style='font-weight:bold;padding:2%;border:2px solid #949090; word-wrap:break-word;'>");
    $resourceOwner_encoded = json_encode($resourceOwner);
    \Drupal::configFactory()
      ->getEditable('miniorange_oauth_client.settings')
      ->set('miniorange_oauth_client_attr_list_from_server', $resourceOwner_encoded)
      ->save();
    echo '<div style="font-family:Calibri;padding:0 3%;">';
    echo '<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><br><div style="background-color:#dff0d8;padding:1%;">Your Test Connection is successful. Now, follow the below steps to complete the last step of your configuration:</div><span style="font-size:13pt;"><br><b></b>Please select the <b>Attribute Name</b> in which you are getting <b>Email ID.</b><br><br></span><div style="background-color: #dddddd; margin-left: 2%; margin-right: 3%">';
    self::miniorange_oauth_client_update_email_username_attribute($resourceOwner);
    echo '<br>&emsp;<i style="font-size: small">You can also map the Username attribute from the Attribute and Role Mapping tab in the module.</i><br><br></div>
                    <br><i>Click on the <b>Done</b> button to save your changes.</i><br>';
    echo '<div style="margin:3%;display:block;text-align:center;"><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="save_and_done();"></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;
                          }

                        function save_and_done(){
                          var email_attr = document.getElementById("mo_oauth_email_attribute").value;
                          var index = window.location.href.indexOf("?");
                          var url = window.location.href.slice(0,index).replace("mo_login","mo_post_testconfig/?field_selected="+email_attr);
                          window.opener.location.href= url;
                          self.close();
                        }
                    </script>';
    echo '<p><b> ATTRIBUTES RECEIVED:</b></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>';
    echo $someattrs;
    echo '</table></div>';
    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];
  global $base_url;

  /*************==============Attributes not mapped check===============************/
  if (empty($email)) {
    echo '<div style="font-family:Calibri;padding:0 3%;">';
    echo '<div style="color: #a94442;background-color: #f2dede;padding: 15px;margin-bottom: 20px;text-align:center;border:1px solid #E6B3B2;font-size:18pt;"> ERROR</div><div style="color: #a94442;font-size:14pt; margin-bottom:20px;"><p><strong>Error: </strong>Email address does not received.</p><p>Check your <b>Attribute Mapping</b> configuration.</p><p><strong>Possible Cause: </strong>Email Attribute field is not configured.</p></div><div style="margin:3%;display:block;text-align:center;"></div><div style="margin:3%;display:block;text-align:center;"><form action="' . $base_url . '" method ="post"><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="submit" value="Done"></form></div>';
    exit;
    return new Response();
  }

  //Validates the email format
  if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
    echo '<div style="font-family:Calibri;padding:0 3%;">';
    echo '<div style="color: #a94442;background-color: #f2dede;padding: 15px;margin-bottom: 20px;text-align:center;border:1px solid #E6B3B2;font-size:18pt;"> ERROR</div><div style="color: #a94442;font-size:14pt; margin-bottom:20px;"><p><strong>Error: </strong>Invalid email format of the received value.</p><p>Check your <b>Attribute Mapping</b> configuration.</p><p><strong>Possible Cause: </strong>Email Attribute field is incorrectly configured.</p></div><div style="margin:3%;display:block;text-align:center;"></div><div style="margin:3%;display:block;text-align:center;"><form action="' . $base_url . '" method ="post"><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="submit" value="Done"></form></div>';
    exit;
    return new Response();
  }
  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 $user;

  /*************================================== Create user if not already present. ======================================*************/
  if (!isset($account->uid)) {
    echo '<div style="font-family:Calibri;padding:0 3%;">';
    echo '<div style="color: #a94442;background-color: #f2dede;padding: 15px;margin-bottom: 20px;text-align:center;border:1px solid #E6B3B2;font-size:18pt;"> ERROR</div><div style="color: #a94442;font-size:14pt; margin-bottom:20px;"><p><strong>Error: </strong>User Not Found in Drupal.</p><p>You can only log in the existing Drupal users in this version of the module.<br><br>Please upgrade to either the <a href="https://plugins.miniorange.com/drupal-oauth-client#pricing" target="_blank">Standard, Premium or the Enterprise </a> version of the module in order to create unlimited new users.</p></div><div style="margin:3%;display:block;text-align:center;"></div><div style="margin:3%;display:block;text-align:center;"><form action="' . $base_url . '" method ="post"><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="submit" value="Done"></form></div>';
    exit;
    return new Response();
  }
  $user = User::load($account
    ->id());
  $edit = array();
  if (!empty(\Drupal::config('miniorange_oauth_client.settings')
    ->get('miniorange_oauth_client_base_url'))) {
    $baseUrlValue = \Drupal::config('miniorange_oauth_client.settings')
      ->get('miniorange_oauth_client_base_url');
  }
  else {
    $baseUrlValue = $base_url;
  }
  $edit['redirect'] = $baseUrlValue;
  user_login_finalize($account);
  $redi = \Drupal::config('miniorange_oauth_client.settings')
    ->get('miniorange_oauth_redirect_url');
  $response = new RedirectResponse($redi);
  $response
    ->send();
  return new Response();
}