Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 401 and 402] [Versions 401 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_user\external;
  18  
  19  use context_system;
  20  use core_user\devicekey;
  21  use external_api;
  22  use external_function_parameters;
  23  use external_single_structure;
  24  use external_value;
  25  use external_warnings;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  require_once("$CFG->libdir/externallib.php");
  29  
  30  /**
  31   * Update public key against registered user device.
  32   *
  33   * @package     core
  34   * @copyright   Alex Morris <alex.morris@catalyst.net.nz>
  35   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  36   * @since       Moodle 4.2
  37   */
  38  class update_user_device_public_key extends external_api {
  39  
  40      /**
  41       * Returns description of method parameters.
  42       *
  43       * @return external_function_parameters
  44       */
  45      public static function execute_parameters(): external_function_parameters {
  46          return new external_function_parameters([
  47              'uuid' => new external_value(PARAM_RAW, 'the device UUID'),
  48              'appid' => new external_value(PARAM_NOTAGS, 'The app id, something like com.moodle.moodlemobile'),
  49              'publickey' => new external_value(PARAM_RAW, 'the app generated public key'),
  50          ]);
  51      }
  52  
  53      /**
  54       * Update public key against registered user device.
  55       *
  56       * @param string $uuid The device UUID.
  57       * @param string $appid The app id, usually something like com.moodle.moodlemobile.
  58       * @param string $publickey The app generated public key.
  59       * @return array Status and list of possible warnings
  60       */
  61      public static function execute($uuid, $appid, $publickey): array {
  62          [
  63              'uuid' => $uuid,
  64              'appid' => $appid,
  65              'publickey' => $publickey
  66          ] = self::validate_parameters(self::execute_parameters(), [
  67              'uuid' => $uuid,
  68              'appid' => $appid,
  69              'publickey' => $publickey
  70          ]);
  71  
  72          $context = context_system::instance();
  73          self::validate_context($context);
  74  
  75          $warnings = [];
  76  
  77          $status = devicekey::update_device_public_key($uuid, $appid, $publickey);
  78          if (!$status) {
  79              $warnings[] = [
  80                  'item' => $uuid,
  81                  'warningcode' => 'devicedoesnotexist',
  82                  'message' => 'Could not find a device with the specified device UUID and app ID for this user'
  83              ];
  84          }
  85  
  86          return [
  87              'status' => $status,
  88              'warnings' => $warnings,
  89          ];
  90      }
  91  
  92      /**
  93       * Returns description of method result value.
  94       *
  95       * @return external_single_structure
  96       * @since Moodle 4.2
  97       */
  98      public static function execute_returns(): external_single_structure {
  99          return new external_single_structure([
 100              'status' => new external_value(PARAM_BOOL, 'Whether the request was successful'),
 101              'warnings' => new external_warnings()
 102          ]);
 103      }
 104  }