Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * This file to proccess Oauth2 connects for backpack.
  19   *
  20   * @package    core_badges
  21   * @subpackage badges
  22   * @copyright  2020 Tung Thai
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   * @author     Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
  25   */
  26  
  27  namespace core_badges\oauth2;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  require_once($CFG->libdir.'/authlib.php');
  32  
  33  use stdClass;
  34  
  35  /**
  36   * Proccess Oauth2 connects to backpack site.
  37   *
  38   * @copyright  2020 Tung Thai
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   * @author     Tung Thai <Tung.ThaiDuc@nashtechglobal.com>
  41   */
  42  class auth extends \auth_oauth2\auth {
  43  
  44      /**
  45       * To complete data after login.
  46       *
  47       * @param client $client object.
  48       * @param string $redirecturl the url redirect.
  49       */
  50      public function complete_data(\core_badges\oauth2\client $client, $redirecturl) {
  51          global $DB, $USER;
  52  
  53          $userinfo = $client->get_userinfo();
  54          $badgebackpack = new stdClass();
  55          $badgebackpack->userid = $USER->id;
  56          if ($userinfo && isset($userinfo->email)) {
  57              $badgebackpack->email = $userinfo->email;
  58          } else {
  59              $badgebackpack->email = $USER->email;
  60          }
  61          $badgebackpack->externalbackpackid = $client->backpack->id;
  62          $badgebackpack->backpackuid = 0;
  63          $badgebackpack->autosync = 0;
  64          $badgebackpack->password = '';
  65          $record = $DB->get_record('badge_backpack', ['userid' => $USER->id,
  66              'externalbackpackid' => $client->backpack->id]);
  67          if (!$record) {
  68              $DB->insert_record('badge_backpack', $badgebackpack);
  69          } else {
  70              $badgebackpack->id = $record->id;
  71              $DB->update_record('badge_backpack', $badgebackpack);
  72          }
  73  
  74          redirect($redirecturl, get_string('backpackconnected', 'badges'), null,
  75              \core\output\notification::NOTIFY_SUCCESS);
  76      }
  77  
  78      /**
  79       * Check user has been logged the backpack site.
  80       *
  81       * @param int $externalbackpackid ID of external backpack.
  82       * @param int $userid ID of user.
  83       * @return bool
  84       */
  85      public static function is_logged_oauth2($externalbackpackid, $userid) {
  86          global $USER;
  87          if (empty($userid)) {
  88              $userid = $USER->id;
  89          }
  90          $persistedtoken = badge_backpack_oauth2::get_record(['externalbackpackid' => $externalbackpackid, 'userid' => $userid]);
  91          if ($persistedtoken) {
  92              return true;
  93          }
  94          return false;
  95      }
  96  }