From 7b96c2d6a7c7672fc38deae85ea4427de3e37390 Mon Sep 17 00:00:00 2001 From: Alexander Bias Date: Tue, 12 Nov 2024 10:03:15 +0100 Subject: [PATCH] Improvement: Change location of event observer code, resolves #26 --- CHANGES.md | 4 +++ classes/eventobservers.php | 65 ++++++++++++++++++++++++++++++++++++++ db/events.php | 3 +- eventhandler.php | 53 ------------------------------- version.php | 2 +- 5 files changed, 71 insertions(+), 56 deletions(-) create mode 100644 classes/eventobservers.php delete mode 100644 eventhandler.php diff --git a/CHANGES.md b/CHANGES.md index 69a202c..9b4c101 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,10 @@ moodle-auth_ldap_syncplus Changes ------- +### Unreleased + +* 2024-11-12 - Improvement: Change location of event observer code, resolves #26 + ### v4.5-r1 * 2024-10-14 - Upgrade: Adopt changes from MDL-81960 and use new \core\url class diff --git a/classes/eventobservers.php b/classes/eventobservers.php new file mode 100644 index 0000000..206d477 --- /dev/null +++ b/classes/eventobservers.php @@ -0,0 +1,65 @@ +. + +/** + * Auth plugin "LDAP SyncPlus" - Event observers + * + * @package auth_ldap_syncplus + * @copyright 2014 Alexander Bias, Ulm University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +namespace auth_ldap_syncplus; + +use moodle_exception; + +/** + * Observer class containing methods monitoring various events. + * + * @package auth_ldap_syncplus + * @copyright 2014 Alexander Bias, Ulm University + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ +class eventobservers { + /** + * User created event observer. + * + * @param \core\event\base $event The event that triggered the handler. + */ + public static function user_created(\core\event\base $event) { + global $DB; + + // Do only if user id is enclosed in $eventdata. + if (!empty($event->relateduserid)) { + + // Get user data. + $user = $DB->get_record('user', ['id' => $event->relateduserid]); + + // Do if user was found. + if (!empty($user->username)) { + + // Do only if user has ldap_syncplus authentication. + if (isset($user->auth) && $user->auth == 'ldap_syncplus') { + + // Update user. + // Actually, we would want to call auth_plugin_base::update_user_record() + // which is lighter, but this function is unfortunately protected since Moodle 3.5. + update_user_record($user->username); + } + } + } + } +} diff --git a/db/events.php b/db/events.php index 217e36e..0fdb57a 100644 --- a/db/events.php +++ b/db/events.php @@ -27,7 +27,6 @@ $observers = [ [ 'eventname' => '\core\event\user_created', - 'includefile' => '/auth/ldap_syncplus/eventhandler.php', - 'callback' => 'update_user_onevent', + 'callback' => '\auth_ldap_syncplus\eventobservers::user_created', ], ]; diff --git a/eventhandler.php b/eventhandler.php deleted file mode 100644 index 48c8ebd..0000000 --- a/eventhandler.php +++ /dev/null @@ -1,53 +0,0 @@ -. - -/** - * Auth plugin "LDAP SyncPlus" - Event handler - * - * @package auth_ldap_syncplus - * @copyright 2014 Alexander Bias, Ulm University - * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later - */ - -/** - * Event handler function. - * - * @param object $eventdata Event data - * @return void - */ -function update_user_onevent($eventdata) { - global $DB; - - // Do only if user id is enclosed in $eventdata. - if (!empty($eventdata->relateduserid)) { - - // Get user data. - $user = $DB->get_record('user', ['id' => $eventdata->relateduserid]); - - // Do if user was found. - if (!empty($user->username)) { - - // Do only if user has ldap_syncplus authentication. - if (isset($user->auth) && $user->auth == 'ldap_syncplus') { - - // Update user. - // Actually, we would want to call auth_plugin_base::update_user_record() - // which is lighter, but this function is unfortunately protected since Moodle 3.5. - update_user_record($user->username); - } - } - } -} diff --git a/version.php b/version.php index 5967a9f..c6a2470 100644 --- a/version.php +++ b/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'auth_ldap_syncplus'; -$plugin->version = 2024100700; +$plugin->version = 2024100701; $plugin->release = 'v4.5-r1'; $plugin->requires = 2024100700; $plugin->supported = [405, 405];