Skip to content

Commit

Permalink
Add get_site_admins webservice function
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhancox authored and tim-schilling committed Mar 4, 2024
1 parent 0b41723 commit b03ea96
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 0 deletions.
80 changes: 80 additions & 0 deletions classes/external/get_site_admins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace local_aspiredu\external;

defined('MOODLE_INTERNAL') || die();

require_once($CFG->dirroot . '/user/externallib.php');
require_once("$CFG->dirroot/lib/externallib.php");

use external_api;
use external_function_parameters;
use external_multiple_structure;
use external_single_structure;
use external_warnings;
use local_aspiredu\local\lib;

/**
* Get users by role external function.
*
* @package local_aspiredu
* @copyright 2022 3ipunt
* @author Guillermo gomez Arias <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class get_site_admins extends external_api {

/**
* Parameters.
*
* @return external_function_parameters
*/
public static function execute_parameters(): external_function_parameters {
return new external_function_parameters([]);
}

/**
* Returns a list of users given a list of roles.
*
* @param array $roleids
* @param int|null $page current page
* @param int|null $perpage items per page
* @return array of warnings and users
*/
public static function execute(): array {
global $CFG, $DB;

return [
'users' => lib::get_users($DB->get_records_list('user','id', explode(',', $CFG->siteadmins))),
'warnings' => [],
];
}

/**
* Describes the get_users_by_roles return value.
*
* @return external_single_structure
*/
public static function execute_returns() {
return new external_single_structure(
[
'users' => new external_multiple_structure(\core_user_external::user_description()),
'warnings' => new external_warnings(),
]
);
}
}
8 changes: 8 additions & 0 deletions db/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@
'type' => 'read',
'capabilities' => 'moodle/user:viewdetails, moodle/user:viewhiddendetails, moodle/course:useremail',
],
'local_aspiredu_get_site_admins' => [
'classname' => '\local_aspiredu\external\get_site_admins',
'methodname' => 'execute',
'description' => 'Return a list of users who are site admins',
'type' => 'read',
'capabilities' => 'moodle/user:viewdetails, moodle/user:viewhiddendetails, moodle/course:useremail',
],
];

// We define the services to install as pre-build services. A pre-build service is not editable by administrator.
Expand Down Expand Up @@ -119,6 +126,7 @@
'local_aspiredu_get_plugin_info',
'local_aspiredu_get_users_by_roles',
'local_aspiredu_get_users_by_capabilities',
'local_aspiredu_get_site_admins',
],
'restrictedusers' => 1,
'enabled' => 1,
Expand Down
54 changes: 54 additions & 0 deletions tests/get_site_admins_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.

namespace local_aspiredu;

defined('MOODLE_INTERNAL') || die();

use local_aspiredu\external\get_site_admins;

global $CFG;
require_once($CFG->dirroot . '/webservice/tests/helpers.php');

/**
* The external function get_site_admins test class.
*
* @package local_aspiredu
* @copyright 2022 3ipunt
* @author Guillermo gomez Arias <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \local_aspiredu\external\get_site_admins
*/
class get_site_admins_test extends \externallib_advanced_testcase {

/**
* @runInSeparateProcess
*/
public function test_get_site_admins() {
$this->resetAfterTest();

$datagenerator = $this->getDataGenerator();
$user = $datagenerator->create_user();
$user2 = $datagenerator->create_user();

$users = get_site_admins::execute();
$users = \external_api::clean_returnvalue(get_site_admins::execute_returns(), $users);

$this->assertCount(0, $users['warnings']);
$this->assertCount(1, $users['users']);
$this->assertEquals('[email protected]', $users['users'][0]['email']);
}
}

0 comments on commit b03ea96

Please sign in to comment.