diff --git a/CHANGES.md b/CHANGES.md index 291f046..9fd6814 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.1-r3 * 2024-08-19 - Fix behat tests which broke due to Github actions changes 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 5761085..ff5193c 100644 --- a/version.php +++ b/version.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'auth_ldap_syncplus'; -$plugin->version = 2023010502; +$plugin->version = 2023010503; $plugin->release = 'v4.1-r3'; $plugin->requires = 2022112800; $plugin->supported = [401, 401];