From 1b5c0dd8d4a325cb57b68050e2332e0119d574a2 Mon Sep 17 00:00:00 2001 From: gthomas2 Date: Wed, 22 Feb 2017 17:19:27 +0000 Subject: [PATCH 1/2] MDL-57913 auth_db: Add scheduled task for syncing users --- auth/db/classes/task/sync_users.php | 63 +++++++++++++++++++++++++++++ auth/db/config.html | 13 ++++++ auth/db/db/tasks.php | 38 +++++++++++++++++ auth/db/lang/en/auth_db.php | 3 ++ auth/db/version.php | 2 +- 5 files changed, 118 insertions(+), 1 deletion(-) create mode 100644 auth/db/classes/task/sync_users.php create mode 100644 auth/db/db/tasks.php diff --git a/auth/db/classes/task/sync_users.php b/auth/db/classes/task/sync_users.php new file mode 100644 index 00000000000..59b7bc1ea97 --- /dev/null +++ b/auth/db/classes/task/sync_users.php @@ -0,0 +1,63 @@ +. + +/** + * Sync users task + * @package auth_db + * @author Guy Thomas + * @copyright Copyright (c) 2017 Blackboard Inc. + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace auth_db\task; + +defined('MOODLE_INTERNAL') || die(); + +/** + * Sync users task class + * @package auth_db + * @author Guy Thomas + * @copyright Copyright (c) 2017 Blackboard Inc. + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class sync_users extends \core\task\scheduled_task { + + /** + * Name for this task. + * + * @return string + */ + public function get_name() { + return get_string('auth_dbsyncuserstask', 'auth_db'); + } + + /** + * Run task for synchronising users. + */ + public function execute() { + if (!is_enabled_auth('db')) { + mtrace('auth_db plugin is disabled, synchronisation stopped', 2); + return; + } + + $dbauth = get_auth_plugin('db'); + $config = get_config('auth/db'); + $trace = new \text_progress_trace(); + $update = !empty($config->updateusers); + $dbauth->sync_users($trace, $update); + } + +} diff --git a/auth/db/config.html b/auth/db/config.html index 426e101d2a7..fb751d0325c 100644 --- a/auth/db/config.html +++ b/auth/db/config.html @@ -46,6 +46,9 @@ if (!isset($config->removeuser)) { $config->removeuser = AUTH_REMOVEUSER_KEEP; } + if (!isset($config->updateusers)) { + $config->updateusers = 0; + } $yesno = array( get_string('no'), get_string('yes') ); @@ -268,6 +271,16 @@ + + + + updateusers, false); ?> + + + + + + authtype, $user_fields, get_string('auth_dbextrafields', 'auth_db'), true, true, $this->get_custom_user_profile_fields()); diff --git a/auth/db/db/tasks.php b/auth/db/db/tasks.php new file mode 100644 index 00000000000..226c24aeaaa --- /dev/null +++ b/auth/db/db/tasks.php @@ -0,0 +1,38 @@ +. + +/** + * Task definition for auth_db. + * @author Guy Thomas + * @copyright Copyright (c) 2017 Blackboard Inc. + * @package auth_db + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +$tasks = array( + array( + 'classname' => '\auth_db\task\sync_users', + 'blocking' => 0, + 'minute' => 'R', + 'hour' => 'R', + 'day' => '*', + 'month' => '*', + 'dayofweek' => '*', + 'disabled' => 1 + ) +); diff --git a/auth/db/lang/en/auth_db.php b/auth/db/lang/en/auth_db.php index 7a9ae733506..857ae2c241c 100644 --- a/auth/db/lang/en/auth_db.php +++ b/auth/db/lang/en/auth_db.php @@ -56,10 +56,13 @@ $string['auth_dbsuspenduser'] = 'Suspended user {$a->name} id {$a->id}'; $string['auth_dbsuspendusererror'] = 'Error suspending user {$a}'; $string['auth_dbsybasequoting'] = 'Use sybase quotes'; $string['auth_dbsybasequotinghelp'] = 'Sybase style single quote escaping - needed for Oracle, MS SQL and some other databases. Do not use for MySQL!'; +$string['auth_dbsyncuserstask'] = 'Synchronise users task'; $string['auth_dbtable'] = 'Name of the table in the database'; $string['auth_dbtable_key'] = 'Table'; $string['auth_dbtype'] = 'The database type (See the ADOdb documentation for details)'; $string['auth_dbtype_key'] = 'Database'; +$string['auth_dbupdateusers'] = 'Update users'; +$string['auth_dbupdateusers_description'] = 'As well as inserting new users, update existing users.'; $string['auth_dbupdatinguser'] = 'Updating user {$a->name} id {$a->id}'; $string['auth_dbuser'] = 'Username with read access to the database'; $string['auth_dbuser_key'] = 'DB user'; diff --git a/auth/db/version.php b/auth/db/version.php index d2cbeba5980..ccb61d0a177 100644 --- a/auth/db/version.php +++ b/auth/db/version.php @@ -24,6 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2016120500; // The current plugin version (Date: YYYYMMDDXX) +$plugin->version = 2017022200; // The current plugin version (Date: YYYYMMDDXX) $plugin->requires = 2016112900; // Requires this Moodle version $plugin->component = 'auth_db'; // Full name of the plugin (used for diagnostics) From 0d9704d817709b4a5ed47729f9fd5ac7196e3bfe Mon Sep 17 00:00:00 2001 From: gthomas2 Date: Wed, 15 Mar 2017 16:42:45 +0000 Subject: [PATCH 2/2] MDL-57913 auth_db: warn CLI about scheduled task, abort CLI if scheduled task enabled --- auth/db/cli/sync_users.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/auth/db/cli/sync_users.php b/auth/db/cli/sync_users.php index f84bf2f7ca5..3d0b12b620b 100644 --- a/auth/db/cli/sync_users.php +++ b/auth/db/cli/sync_users.php @@ -81,6 +81,14 @@ if (!is_enabled_auth('db')) { cli_error('auth_db plugin is disabled, synchronisation stopped', 2); } +cli_problem('[AUTH DB] The sync users cron has been deprecated. Please use the scheduled task instead.'); + +// Abort execution of the CLI script if the \auth_db\task\sync_users is enabled. +$task = \core\task\manager::get_scheduled_task('auth_db\task\sync_users'); +if (!$task->get_disabled()) { + cli_error('[AUTH DB] The scheduled task sync_users is enabled, the cron execution has been aborted.'); +} + if (empty($options['verbose'])) { $trace = new null_progress_trace(); } else {