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   * External admin page class that allows a callback to be provided to determine whether page can be accessed
  19   *
  20   * @package     core_admin
  21   * @copyright   2019 Marina Glancy
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  namespace core_admin\local\externalpage;
  26  
  27  use admin_externalpage;
  28  
  29  defined('MOODLE_INTERNAL') || die();
  30  
  31  require_once("{$CFG->libdir}/adminlib.php");
  32  
  33  /**
  34   * Admin externalpage class
  35   *
  36   * @package     core_admin
  37   * @copyright   2019 Marina Glancy
  38   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  39   */
  40  class accesscallback extends admin_externalpage {
  41  
  42      /** @var callable $accesscheckcallback */
  43      protected $accesscheckcallback;
  44  
  45      /**
  46       * Class constructor
  47       *
  48       * @param string $name
  49       * @param string $visiblename
  50       * @param string $url
  51       * @param callable $accesscheckcallback The callback method that will be executed to check whether user has access to
  52       *     this page. The setting instance ($this) is passed as an argument to the callback. Should return boolean value
  53       * @param bool $hidden
  54       */
  55      public function __construct(string $name, string $visiblename, string $url, callable $accesscheckcallback,
  56              bool $hidden = false) {
  57  
  58          $this->accesscheckcallback = $accesscheckcallback;
  59  
  60          parent::__construct($name, $visiblename, $url, [], $hidden);
  61      }
  62  
  63      /**
  64       * Determines if the current user has access to this external page based on access callback
  65       *
  66       * @return bool
  67       */
  68      public function check_access() {
  69          return ($this->accesscheckcallback)($this);
  70      }
  71  }