|
| 1 | +#!/usr/bin/env php |
1 | 2 | <?php |
2 | 3 | /** |
3 | 4 | * Idempotent Moodle OAuth2 Issuer Management Script |
4 | | - * |
| 5 | + * |
5 | 6 | * Usage: php manage_oauth2_issuer.php /path/to/config.json |
6 | 7 | */ |
7 | 8 |
|
8 | 9 | define('CLI_SCRIPT', true); |
9 | | -require(__DIR__ . '/../config.php'); |
| 10 | +require('/var/www/html/config.php'); |
10 | 11 | require_once($CFG->libdir . '/clilib.php'); |
11 | 12 |
|
12 | | -// 1. Get arguments |
13 | 13 | list($options, $unrecognized) = cli_get_params( |
14 | 14 | ['help' => false], |
15 | 15 | ['h' => 'help'] |
|
28 | 28 | } |
29 | 29 | $jsoncontent = file_get_contents($jsonfile); |
30 | 30 | } else { |
31 | | - // Read from stdin |
32 | 31 | $jsoncontent = file_get_contents('php://stdin'); |
33 | 32 | } |
34 | 33 |
|
|
41 | 40 | cli_error("Error: Invalid JSON: " . json_last_error_msg()); |
42 | 41 | } |
43 | 42 |
|
44 | | -// 2. Validate required fields |
45 | 43 | $required = ['name', 'baseurl', 'clientid', 'clientsecret']; |
46 | 44 | foreach ($required as $req) { |
47 | 45 | if (empty($config[$req])) { |
|
50 | 48 | } |
51 | 49 |
|
52 | 50 | 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 |
54 | 55 | $issuers = \core\oauth2\api::get_all_issuers(); |
55 | 56 | $existing = null; |
56 | 57 | foreach ($issuers as $iss) { |
|
65 | 66 | unset($issuerdata->field_mappings); |
66 | 67 |
|
67 | 68 | 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"; |
69 | 70 | $issuerdata->id = $existing->get('id'); |
70 | 71 | $issuer = \core\oauth2\api::update_issuer($issuerdata); |
71 | 72 | } else { |
72 | | - echo "Creating new issuer: " . $config['name']; |
| 73 | + echo "Creating new issuer: {$config['name']}... "; |
73 | 74 | $issuer = \core\oauth2\api::create_issuer($issuerdata); |
| 75 | + echo "ID: {$issuer->get('id')}\n"; |
74 | 76 | } |
75 | 77 |
|
76 | 78 | $issuerid = $issuer->get('id'); |
77 | 79 |
|
78 | | - // 4. Synchronize Endpoints (Discovery) |
79 | | - echo "Synchronizing endpoints via discovery..."; |
| 80 | + // Synchronize Endpoints (Discovery) |
| 81 | + echo "Synchronizing endpoints via discovery...\n"; |
80 | 82 | try { |
81 | | - \core\oauth2\api::discover_endpoints($issuer); |
| 83 | + \core\oauth2\discovery\openidconnect::discover_endpoints($issuer); |
82 | 84 | } 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"; |
84 | 86 | } |
85 | 87 |
|
86 | | - // 5. Manage Field Mappings (Idempotent) |
| 88 | + // Manage Field Mappings (Idempotent) |
87 | 89 | if (!empty($config['field_mappings'])) { |
88 | | - echo "Synchronizing field mappings..."; |
| 90 | + echo "Synchronizing field mappings...\n"; |
89 | 91 |
|
90 | 92 | // Get existing mappings for this issuer |
91 | 93 | $existing_mappings = \core\oauth2\api::get_user_field_mappings($issuer); |
|
95 | 97 | foreach ($existing_mappings as $em) { |
96 | 98 | if ($em->get('internalfield') === $internal) { |
97 | 99 | if ($em->get('externalfield') !== $external) { |
98 | | - echo "Updating mapping for {$internal}: {$external}"; |
| 100 | + echo "Updating mapping: {$internal} -> {$external} (was {$em->get('externalfield')})\n"; |
99 | 101 | $em->set('externalfield', $external); |
100 | 102 | $em->save(); |
101 | 103 | } |
|
105 | 107 | } |
106 | 108 |
|
107 | 109 | if (!$found) { |
108 | | - echo "Creating new mapping: {$external} -> {$internal}"; |
| 110 | + echo "Creating mapping: {$external} -> {$internal}\n"; |
109 | 111 | \core\oauth2\api::create_user_field_mapping((object)[ |
110 | 112 | 'issuerid' => $issuerid, |
111 | 113 | 'externalfield' => $external, |
|
115 | 117 | } |
116 | 118 | } |
117 | 119 |
|
118 | | - echo "Successfully managed OAuth2 Issuer: " . $issuer->get('name'); |
| 120 | + echo "Successfully managed OAuth2 Issuer: {$issuer->get('name')}\n"; |
119 | 121 |
|
120 | 122 | } 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(); |
122 | 126 | } |
0 commit comments