Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Differences Between: [Versions 402 and 403]

   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  namespace core\external;
  18  
  19  use context_course;
  20  use core\moodlenet\moodlenet_client;
  21  use core\moodlenet\utilities;
  22  use core\oauth2\api;
  23  use core_external\external_api;
  24  use core_external\external_function_parameters;
  25  use core_external\external_single_structure;
  26  use core_external\external_value;
  27  use core_external\external_warnings;
  28  use moodle_url;
  29  
  30  /**
  31   * The external API to check whether a user has authorized for a given MoodleNet OAuth 2 issuer.
  32   *
  33   * @package    core
  34   * @copyright  2023 Huong Nguyen <huongnv13@gmail.com>
  35   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   */
  37  class moodlenet_auth_check extends external_api {
  38  
  39      /**
  40       * Returns description of parameters.
  41       *
  42       * @return external_function_parameters
  43       * @since Moodle 4.2
  44       */
  45      public static function execute_parameters(): external_function_parameters {
  46          return new external_function_parameters([
  47              'issuerid' => new external_value(PARAM_INT, 'OAuth 2 issuer ID', VALUE_REQUIRED),
  48              'courseid' => new external_value(PARAM_INT, 'Course ID', VALUE_REQUIRED),
  49          ]);
  50      }
  51  
  52      /**
  53       * External function to check if the user is already authorized with MoodleNet.
  54       *
  55       * @param int $issuerid Issuer Id.
  56       * @param int $courseid The course ID that contains the activity which being shared
  57       * @return array
  58       * @since Moodle 4.2
  59       */
  60      public static function execute(int $issuerid, int $courseid): array {
  61          global $USER;
  62          [
  63              'issuerid' => $issuerid,
  64              'courseid' => $courseid,
  65          ] = self::validate_parameters(self::execute_parameters(), [
  66              'issuerid' => $issuerid,
  67              'courseid' => $courseid,
  68          ]);
  69  
  70          // Check capability.
  71          $coursecontext = context_course::instance($courseid);
  72          $usercanshare = utilities::can_user_share($coursecontext, $USER->id);
  73          if (!$usercanshare) {
  74              return self::return_errors($courseid, 'errorpermission',
  75                  get_string('nopermissions', 'error', get_string('moodlenet:sharetomoodlenet', 'moodle')));
  76          }
  77  
  78          // Get the issuer.
  79          $issuer = api::get_issuer($issuerid);
  80          // Validate the issuer and check if it is enabled or not.
  81          if (!utilities::is_valid_instance($issuer)) {
  82              return self::return_errors($issuerid, 'errorissuernotenabled', get_string('invalidparameter', 'debug'));
  83          }
  84  
  85          $returnurl = new moodle_url('/admin/moodlenet_oauth2_callback.php');
  86          $returnurl->param('issuerid', $issuerid);
  87          $returnurl->param('callback', 'yes');
  88          $returnurl->param('sesskey', sesskey());
  89  
  90          // Get the OAuth Client.
  91          if (!$oauthclient = api::get_user_oauth_client($issuer, $returnurl, moodlenet_client::API_SCOPE_CREATE_RESOURCE, true)) {
  92              return self::return_errors($issuerid, 'erroroauthclient', get_string('invalidparameter', 'debug'));
  93          }
  94  
  95          $status = false;
  96          $warnings = [];
  97          $loginurl = '';
  98  
  99          if (!$oauthclient->is_logged_in()) {
 100              $loginurl = $oauthclient->get_login_url()->out(false);
 101          } else {
 102              $status = true;
 103          }
 104  
 105          return [
 106              'status' => $status,
 107              'loginurl' => $loginurl,
 108              'warnings' => $warnings,
 109          ];
 110      }
 111  
 112      /**
 113       * Describes the data returned from the external function.
 114       *
 115       * @return external_single_structure
 116       * @since Moodle 4.2
 117       */
 118      public static function execute_returns(): external_single_structure {
 119          return new external_single_structure([
 120              'loginurl' => new external_value(PARAM_RAW, 'Login url'),
 121              'status' => new external_value(PARAM_BOOL, 'status: true if success'),
 122              'warnings' => new external_warnings(),
 123          ]);
 124      }
 125  
 126      /**
 127       * Handle return error.
 128       *
 129       * @param int $itemid Item id
 130       * @param string $warningcode Warning code
 131       * @param string $message Message
 132       * @return array
 133       */
 134      protected static function return_errors(int $itemid, string $warningcode, string $message): array {
 135          $warnings[] = [
 136              'item' => $itemid,
 137              'warningcode' => $warningcode,
 138              'message' => $message,
 139          ];
 140  
 141          return [
 142              'status' => false,
 143              'loginurl' => '',
 144              'warnings' => $warnings,
 145          ];
 146      }
 147  }