First cut of field locks support. functional but missing strings and support for LDAP/DB modules.

- Extended set_config()
- Implemented get_config() which takes over $CFG loading in setup.php
- admin/auth.php has special handling if post vars starting in pluginconfig_
- admin/auth.php print_auth_lock_options() prints a form fragment -- being called from most plugins now
- user/edit.php follows the new convention when locking down fields, both javascript UI and on POST.
This commit is contained in:
martinlanghoff
2005-05-31 05:36:41 +00:00
parent fa84313875
commit cdbba24134
13 changed files with 155 additions and 36 deletions
+43 -2
View File
@@ -23,11 +23,27 @@
$config = (array)$config;
validate_form($config, $err);
// extract and sanitize the auth key explicitly
$modules = get_list_of_plugins("auth");
if (in_array($config['auth'], $modules)) {
$auth = $config['auth'];
} else {
notify("Error defining the authentication method");
}
if (count($err) == 0) {
print_header();
foreach ($config as $name => $value) {
if (! set_config($name, $value)) {
notify("Problem saving config $name as $value");
if (preg_match('/^pluginconfig_(.+?)$/', $name, $matches)) {
$plugin = "auth/$auth";
$name = $matches[1];
if (! set_config($name, $value, $plugin)) {
notify("Problem saving config $name as $value for plugin $plugin");
}
} else { // normal handling for
if (! set_config($name, $value)) {
notify("Problem saving config $name as $value");
}
}
}
redirect("auth.php?sesskey=$USER->sesskey", get_string("changessaved"), 1);
@@ -198,5 +214,30 @@ function validate_form(&$form, &$err) {
return;
}
//
// Good enough for most auth plugins
// but some may want a custom one if they are offering
// other options
// Note: pluginconfig_ fields have special handling.
function print_auth_lock_options ($auth, $user_fields, $helptext='', $refreshopts, $updateopts) {
echo '<tr><td colspan="3">';
print_heading(get_string('auth_fieldlocks', 'auth'));
echo '<td/></tr>';
$lockoptions = array ('unlocked' => get_string('auth_unlocked', 'auth'),
'unlockedifempty' => get_string('auth_unlockedifempty', 'auth'),
'locked' => get_string('auth_locked', 'auth'));
$pluginconfig = get_config("auth/$auth");
foreach ($user_fields as $field) {
echo '<tr valign="top"><td align="right">';
echo get_string($field);
echo '</td><td>';
choose_from_menu($lockoptions, "pluginconfig_field_lock_{$field}", $pluginconfig->{"field_lock_$field"}, "");
echo "</td><td>$helptext</td></tr>";
$helptext = '&nbsp;';
}
}
?>
+2
View File
@@ -1,2 +1,4 @@
<!-- No config needed -->
<div align="center"><?php print_string('none'); ?></div>
<?php print_auth_lock_options($auth, $user_fields, get_string('auth_fieldlocks_help', 'auth'), false, false); ?>
+1
View File
@@ -166,3 +166,4 @@
</tr>
<?php print_auth_lock_options($auth, $user_fields, get_string('auth_fieldlocks_help', 'auth'), false, false); ?>
+1
View File
@@ -57,3 +57,4 @@
<?php helpbutton("text", get_string("helptext")) ?>
</td>
</tr>
<?php print_auth_lock_options($auth, $user_fields, get_string('auth_fieldlocks_help', 'auth'), false, false); ?>
+1 -1
View File
@@ -8,4 +8,4 @@
<?php helpbutton("text", get_string("helptext")) ?>
</td>
</tr>
<?php print_auth_lock_options($auth, $user_fields, get_string('auth_fieldlocks_help', 'auth'), false, false); ?>
+1
View File
@@ -40,3 +40,4 @@
<?php helpbutton("text", get_string("helptext")) ?>
</td>
</tr>
<?php print_auth_lock_options($auth, $user_fields, get_string('auth_fieldlocks_help', 'auth'), false, false); ?>
+1
View File
@@ -1,2 +1,3 @@
<!-- No config needed -->
<div align="center"><?php print_string('none'); ?></div>
<?php print_auth_lock_options($auth, $user_fields, get_string('auth_fieldlocks_help', 'auth'), false, false); ?>
+2 -1
View File
@@ -6,4 +6,5 @@
<?php print_string("authinstructions","auth") ?>
<?php helpbutton("text", get_string("helptext")) ?>
</td>
</tr>
</tr>
<?php print_auth_lock_options($auth, $user_fields, get_string('auth_fieldlocks_help', 'auth'), false, false); ?>
+2
View File
@@ -71,3 +71,5 @@
<?php helpbutton("text", get_string("helptext")) ?>
</td>
</tr>
<?php print_auth_lock_options($auth, $user_fields, get_string('auth_fieldlocks_help', 'auth'), false, false); ?>
+85 -10
View File
@@ -387,29 +387,104 @@ function optional_variable(&$var, $default=0) {
*
* Set a key/value pair in both this session's {@link $CFG} global variable
* and in the 'config' database table for future sessions.
*
* Can also be used to update keys for plugin-scoped configs in config_plugin table.
* In that case it doesn't affect $CFG.
*
* @param string $name the key to set
* @param string $value the value to set
* @param string $plugin (optional) the plugin scope
* @uses $CFG
* @return bool
*/
function set_config($name, $value) {
function set_config($name, $value, $plugin=NULL) {
/// No need for get_config because they are usually always available in $CFG
global $CFG;
$CFG->$name = $value; // So it's defined for this invocation at least
if (get_field('config', 'name', 'name', $name)) {
return set_field('config', 'value', $value, 'name', $name);
} else {
$config->name = $name;
$config->value = $value;
return insert_record('config', $config);
if (empty($plugin)) {
$CFG->$name = $value; // So it's defined for this invocation at least
if (get_field('config', 'name', 'name', $name)) {
return set_field('config', 'value', $value, 'name', $name);
} else {
$config->name = $name;
$config->value = $value;
return insert_record('config', $config);
}
} else { // plugin scope
if ($id = get_field('config_plugins', 'id', 'name', $name, 'plugin', $plugin)) {
return set_field('config_plugins', 'value', $value, 'id', $id);
} else {
$config->plugin = $plugin;
$config->name = $name;
$config->value = $value;
return insert_record('config_plugins', $config);
}
}
}
/**
* Get configuration values from the global config table
* or the config_plugins table.
*
* If called with no parameters it will do the right thing
* generating $CFG safely from the database without overwriting
* existing values.
*
* @param string $plugin
* @param string $name
* @uses $CFG
* @return hash-like object or single value
*
*/
function get_config($plugin=NULL, $name=NULL) {
global $CFG;
if (!empty($name)) { // the user is asking for a specific value
if (!empty($plugin)) {
return get_record('config_plugins', 'plugin' , $plugin, 'name', $name);
} else {
return get_record('config', 'name', $name);
}
}
// the user is after a recordset
if (!empty($plugin)) {
if ($configs=get_records('config_plugins', 'plugin', $plugin, '', 'name,value')) {
$configs = (array)$configs;
$localcfg = array();
foreach ($configs as $config) {
$localcfg[$config->name] = $config->value;
}
return (object)$localcfg;
} else {
return false;
}
} else {
// this was originally in setup.php
if ($configs = get_records('config')) {
$localcfg = (array)$CFG;
foreach ($configs as $config) {
if (!isset($localcfg[$config->name])) {
$localcfg[$config->name] = $config->value;
} else {
if ($localcfg[$config->name] != $config->value ) {
// complain if the DB has a different
// value than config.php does
error_log("\$CFG->{$config->name} in config.php ({$localcfg[$config->name]}) overrides database setting ({$config->value})");
}
}
}
$localcfg = (object)$localcfg;
}
return $localcfg;
}
}
/**
* Refresh current $USER session global variable with all their current preferences.
* @uses $USER
+1 -15
View File
@@ -147,21 +147,7 @@ global $THEME;
/// Load up any configuration from the config table
if ($configs = get_records('config')) {
$CFG = (array)$CFG;
foreach ($configs as $config) {
if (!isset($CFG[$config->name])) {
$CFG[$config->name] = $config->value;
} else {
error_log("\$CFG->$config->name in config.php overrides database setting");
}
}
$CFG = (object)$CFG;
unset($configs);
unset($config);
}
$CFG = get_config();
/// Turn on SQL logging if required
if (!empty($CFG->logsql)) {
+4
View File
@@ -2900,6 +2900,10 @@ function print_textarea($usehtmleditor, $rows, $cols, $width, $height, $name, $v
}
if ($usehtmleditor) {
// cleanup bad html that may have made it to the db
$value = clean_text($value);
if (!empty($courseid) and isteacher($courseid)) {
echo ($scriptcount < 1) ? '<script type="text/javascript" src="'. $CFG->wwwroot .'/lib/editor/htmlarea.php?id='. $courseid .'"></script>'."\n" : '';
} else {
+11 -7
View File
@@ -126,11 +126,13 @@
// override locked values
if (!isadmin()) {
$fields = get_user_fieldnames();
$authconfig = get_config( 'auth/' . $user->auth );
foreach ($fields as $field) {
$configvariable = 'auth_user_'.$field.'_editlock';
if (!empty($CFG->$configvariable)) {
if (isset($usernew->$field) && $user->$field !== $usernew->$field) {
$usernew->$field = $user->$field;
$configvariable = 'field_lock_' . $field;
if ( $authconfig->{$configvariable} === 'locked'
|| ($authconfig->{$configvariable} === 'unlockedifempty' && !empty($user->$field)) ) {
if (!empty( $user->$field)) {
$usernew->$field = $user->$field;
}
}
}
@@ -323,9 +325,11 @@
echo '<script type="text/javascript">'."\n";
echo '<!--'."\n";
foreach ($fields as $field) {
$configvariable = 'auth_user_'.$field.'_editlock';
if (!empty($CFG->$configvariable)) {
$authconfig = get_config( 'auth/' . $user->auth );
foreach ($fields as $field) {
$configvariable = 'field_lock_' . $field;
if ( $authconfig->{$configvariable} === 'locked'
|| ($authconfig->{$configvariable} === 'unlockedifempty' && !empty($user->$field)) ) {
echo "eval('document.form.$field.disabled=true');\n";
}
}