mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-06 10:59:59 +00:00
Moodle 5.3 marks `external_generate_token()` as a final deprecation
(`#[deprecated('\core_external\util::generate_token', since: '4.4',
final: true)]` in public/lib/externallib.php), and
`core\deprecation::emit_deprecation_notice()` throws a coding_exception
for every final deprecation, whatever the debug level. The MCP
provisioning script called it whenever no permanent token existed yet,
so every fresh install of the variant that enables MCP failed at
"Enable web services and mint the MCP bearer" (main run 39494396920,
Moodle#1: `Error code: codingerror` from deprecation.php:273 via
externallib.php:30).
The variant had not run in CI since the Moodle 5.3 bump, which is why
this surfaced only now.
Mint the token with `\core_external\util::generate_token()`, which
takes the service record instead of its id, and create the service
account with `\core\user::create_user()` in place of
`user_create_user()`, deprecated since 5.3 (MDL-82650). Neither needs
externallib.php or user/lib.php any more.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
94 lines
3.2 KiB
PHP
94 lines
3.2 KiB
PHP
<?php
|
|
// nocheck: mirrored-unit-test - a CLI_SCRIPT that requires Moodle's config.php and
|
|
// the webservice library at the top level and then writes through set_config and the
|
|
// webservice manager; nothing loads without a provisioned Moodle install
|
|
define('CLI_SCRIPT', true);
|
|
|
|
require(getenv('MOODLE_CONFIG'));
|
|
require_once($CFG->dirroot . '/webservice/lib.php');
|
|
|
|
$shortname = (string) getenv('MCP_SERVICE_SHORTNAME');
|
|
$servicename = (string) getenv('MCP_SERVICE_NAME');
|
|
$functions = array_filter(array_map('trim', explode(',', (string) getenv('MCP_SERVICE_FUNCTIONS'))));
|
|
|
|
set_config('enablewebservices', 1);
|
|
|
|
$protocols = array_filter(array_map('trim', explode(',', (string) get_config('core', 'webserviceprotocols'))));
|
|
if (!in_array('mcp', $protocols, true)) {
|
|
$protocols[] = 'mcp';
|
|
set_config('webserviceprotocols', implode(',', $protocols));
|
|
}
|
|
|
|
$webservice = new webservice();
|
|
$service = $webservice->get_external_service_by_shortname($shortname);
|
|
if (!$service) {
|
|
$webservice->add_external_service((object) [
|
|
'name' => $servicename,
|
|
'shortname' => $shortname,
|
|
'enabled' => 1,
|
|
'restrictedusers' => 0,
|
|
'downloadfiles' => 0,
|
|
'uploadfiles' => 0,
|
|
]);
|
|
$service = $webservice->get_external_service_by_shortname($shortname, MUST_EXIST);
|
|
}
|
|
|
|
foreach ($functions as $function) {
|
|
if (!$DB->record_exists('external_functions', ['name' => $function])) {
|
|
continue;
|
|
}
|
|
if (!$webservice->service_function_exists($function, $service->id)) {
|
|
$webservice->add_external_function_to_service($function, $service->id);
|
|
}
|
|
}
|
|
|
|
$username = (string) getenv('MCP_ACCOUNT_USERNAME');
|
|
$capabilities = array_filter(array_map('trim', explode(',', (string) getenv('MCP_ROLE_CAPABILITIES'))));
|
|
$systemcontext = context_system::instance();
|
|
|
|
$account = $DB->get_record('user', [
|
|
'username' => $username,
|
|
'mnethostid' => $CFG->mnet_localhost_id,
|
|
]);
|
|
if (!$account) {
|
|
$new = new stdClass();
|
|
$new->username = $username;
|
|
$new->email = (string) getenv('MCP_ACCOUNT_EMAIL');
|
|
$new->password = (string) getenv('MCP_ACCOUNT_PASSWORD');
|
|
$new->firstname = 'Infinito';
|
|
$new->lastname = 'MCP';
|
|
$new->auth = 'manual';
|
|
$new->confirmed = 1;
|
|
$new->mnethostid = $CFG->mnet_localhost_id;
|
|
$accountid = \core\user::create_user($new, true, false);
|
|
$account = $DB->get_record('user', ['id' => $accountid], '*', MUST_EXIST);
|
|
}
|
|
|
|
$roleshortname = (string) getenv('MCP_ROLE_SHORTNAME');
|
|
$roleid = $DB->get_field('role', 'id', ['shortname' => $roleshortname]);
|
|
if (!$roleid) {
|
|
$roleid = create_role((string) getenv('MCP_ROLE_NAME'), $roleshortname, '', 'user');
|
|
set_role_contextlevels($roleid, [CONTEXT_SYSTEM]);
|
|
}
|
|
foreach ($capabilities as $capability) {
|
|
assign_capability($capability, CAP_ALLOW, $roleid, $systemcontext->id, true);
|
|
}
|
|
role_assign($roleid, $account->id, $systemcontext->id);
|
|
|
|
$token = $DB->get_field('external_tokens', 'token', [
|
|
'externalserviceid' => $service->id,
|
|
'userid' => $account->id,
|
|
'tokentype' => EXTERNAL_TOKEN_PERMANENT,
|
|
], IGNORE_MULTIPLE);
|
|
|
|
if (!$token) {
|
|
$token = \core_external\util::generate_token(
|
|
EXTERNAL_TOKEN_PERMANENT,
|
|
$service,
|
|
$account->id,
|
|
$systemcontext
|
|
);
|
|
}
|
|
|
|
echo $token . PHP_EOL;
|