Search moodle.org's
Developer Documentation

See Release Notes

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

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