Skip to content

Commit

Permalink
Allow override of config.php and move trigger to before session #74
Browse files Browse the repository at this point in the history
  • Loading branch information
brendanheywood committed Jan 2, 2025
1 parent fb793c4 commit 50d2566
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,17 @@ Here is where the commands for a condition set can be specified. These are appli
This command sets moodle core configurations to a specified value.

Note: `CFG` and `forced_plugin_setting` commands will not overwrite config set inside config.php by design as a security measure.
However you can allow this by setting:

```php
$CFG->somethingforced = 'original value';
$CFG->somethingforced_allow_abconfig = 1;
```

```
CFG,config,value
CFG,passwordpolicy,1
CFG,somethingforced,myoverride
```

##### forced_plugin_setting
Expand Down
25 changes: 22 additions & 3 deletions lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,22 @@
* @return void|null
*/
function tool_abconfig_after_config() {
// No longer used, uses before_session_start instead.
// Kept in case we want experiment options later to say when to inject the changes.
}

/**
* Before session
*
* At some point this will also get converted to hook in core.
*
* @return void|null
*/
function tool_abconfig_before_session_start() {

global $SESSION, $USER, $CFG;

try {
global $SESSION, $USER;

// Setup experiment manager.
$manager = new tool_abconfig_experiment_manager();
Expand Down Expand Up @@ -269,10 +283,15 @@ function tool_abconfig_execute_command_array($commandsencoded, $shortname, $js =
if ($command == 'CFG') {
$commandarray = explode(',', $commandstring, 3);

// Ensure that command hasnt already been set in config.php.
if (!array_key_exists($commandarray[1], $CFG->config_php_settings)) {
// Allow override if set in config.php already:
$allow = isset($CFG->{$commandarray[1] . '_allow_abconfig'});

// Ensure that command hasn't already been set in config.php.
if ($allow || !array_key_exists($commandarray[1], $CFG->config_php_settings)) {
$CFG->{$commandarray[1]} = $commandarray[2];
$CFG->config_php_settings[$commandarray[1]] = $commandarray[2];
} else {
error_log("abconfig: Can't override \$CFG->{$commandarray[1]} because already set in config.php!");
}
}
if ($command == 'forced_plugin_setting') {
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

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

$plugin->version = 2024060401; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2024060401; // Same as version.
$plugin->version = 2024060402; // The current plugin version (Date: YYYYMMDDXX).
$plugin->release = 2024060402; // Same as version.
$plugin->requires = 2014051217;
$plugin->supported = [38, 405]; // Available as of Moodle 3.8.0 or later.
$plugin->component = "tool_abconfig";
Expand Down

0 comments on commit 50d2566

Please sign in to comment.