Skip to content

Commit f375a0f

Browse files
committed
fix: correct config import, start admin session before updating oauth issuers
- Uses \core\cron::setup_user() to log in as admin. - Also refactors and fixes log output. - Adds shebang and executable bit to php scripts.
1 parent f211186 commit f375a0f

2 files changed

Lines changed: 24 additions & 19 deletions

File tree

scripts/manage_oauth2_issuer.php

100644100755
Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1+
#!/usr/bin/env php
12
<?php
23
/**
34
* Idempotent Moodle OAuth2 Issuer Management Script
4-
*
5+
*
56
* Usage: php manage_oauth2_issuer.php /path/to/config.json
67
*/
78

89
define('CLI_SCRIPT', true);
9-
require(__DIR__ . '/../config.php');
10+
require('/var/www/html/config.php');
1011
require_once($CFG->libdir . '/clilib.php');
1112

12-
// 1. Get arguments
1313
list($options, $unrecognized) = cli_get_params(
1414
['help' => false],
1515
['h' => 'help']
@@ -28,7 +28,6 @@
2828
}
2929
$jsoncontent = file_get_contents($jsonfile);
3030
} else {
31-
// Read from stdin
3231
$jsoncontent = file_get_contents('php://stdin');
3332
}
3433

@@ -41,7 +40,6 @@
4140
cli_error("Error: Invalid JSON: " . json_last_error_msg());
4241
}
4342

44-
// 2. Validate required fields
4543
$required = ['name', 'baseurl', 'clientid', 'clientsecret'];
4644
foreach ($required as $req) {
4745
if (empty($config[$req])) {
@@ -50,7 +48,10 @@
5048
}
5149

5250
try {
53-
// 3. Find existing issuer by name or baseurl
51+
// Start session and log in as cron user (modified admin)
52+
\core\cron::setup_user();
53+
54+
// Find existing issuer by name or baseurl
5455
$issuers = \core\oauth2\api::get_all_issuers();
5556
$existing = null;
5657
foreach ($issuers as $iss) {
@@ -65,27 +66,28 @@
6566
unset($issuerdata->field_mappings);
6667

6768
if ($existing) {
68-
echo "Updating existing issuer: " . $existing->get('name') . " (ID: " . $existing->get('id') . ")";
69+
echo "Updating existing issuer: {$existing->get('name')} (ID: {$existing->get('id')})\n";
6970
$issuerdata->id = $existing->get('id');
7071
$issuer = \core\oauth2\api::update_issuer($issuerdata);
7172
} else {
72-
echo "Creating new issuer: " . $config['name'];
73+
echo "Creating new issuer: {$config['name']}... ";
7374
$issuer = \core\oauth2\api::create_issuer($issuerdata);
75+
echo "ID: {$issuer->get('id')}\n";
7476
}
7577

7678
$issuerid = $issuer->get('id');
7779

78-
// 4. Synchronize Endpoints (Discovery)
79-
echo "Synchronizing endpoints via discovery...";
80+
// Synchronize Endpoints (Discovery)
81+
echo "Synchronizing endpoints via discovery...\n";
8082
try {
81-
\core\oauth2\api::discover_endpoints($issuer);
83+
\core\oauth2\discovery\openidconnect::discover_endpoints($issuer);
8284
} catch (Exception $e) {
83-
echo "Warning: Discovery failed (this is normal if the provider doesn't support OIDC discovery): " . $e->getMessage();
85+
echo "Warning: Discovery failed (this is normal if the provider doesn't support OIDC discovery): {$e->getMessage()}\n";
8486
}
8587

86-
// 5. Manage Field Mappings (Idempotent)
88+
// Manage Field Mappings (Idempotent)
8789
if (!empty($config['field_mappings'])) {
88-
echo "Synchronizing field mappings...";
90+
echo "Synchronizing field mappings...\n";
8991

9092
// Get existing mappings for this issuer
9193
$existing_mappings = \core\oauth2\api::get_user_field_mappings($issuer);
@@ -95,7 +97,7 @@
9597
foreach ($existing_mappings as $em) {
9698
if ($em->get('internalfield') === $internal) {
9799
if ($em->get('externalfield') !== $external) {
98-
echo "Updating mapping for {$internal}: {$external}";
100+
echo "Updating mapping: {$internal} -> {$external} (was {$em->get('externalfield')})\n";
99101
$em->set('externalfield', $external);
100102
$em->save();
101103
}
@@ -105,7 +107,7 @@
105107
}
106108

107109
if (!$found) {
108-
echo "Creating new mapping: {$external} -> {$internal}";
110+
echo "Creating mapping: {$external} -> {$internal}\n";
109111
\core\oauth2\api::create_user_field_mapping((object)[
110112
'issuerid' => $issuerid,
111113
'externalfield' => $external,
@@ -115,8 +117,10 @@
115117
}
116118
}
117119

118-
echo "Successfully managed OAuth2 Issuer: " . $issuer->get('name');
120+
echo "Successfully managed OAuth2 Issuer: {$issuer->get('name')}\n";
119121

120122
} catch (Exception $e) {
121-
cli_error("CRITICAL ERROR: " . $e->getMessage());
123+
cli_error("CRITICAL ERROR:\n{$e->getMessage()}\n");
124+
} finally {
125+
\core\cron::reset_user_cache();
122126
}

scripts/plugin_cache_manager.php

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
#!/usr/bin/env php
12
<?php
23
/**
34
* Moodle Plugin Cache Manager
4-
*
5+
*
56
* Handles synchronization, downloading, and transparent patching of Moosh's plugin database.
67
*/
78

0 commit comments

Comments
 (0)