From 817e396a840a933fa389f6304650025ef509d8ce Mon Sep 17 00:00:00 2001 From: Petr Skoda Date: Sat, 5 Nov 2011 15:06:32 +0100 Subject: [PATCH] MDL-30148 remember guest access key Remember password for course guest access and reuse it if the enrol cache is reset. Do not print guest access form when already using guest course access. --- enrol/guest/addinstance.php | 1 - enrol/guest/lib.php | 35 +++++++++++++++++++++++++++-------- enrol/guest/locallib.php | 3 +-- enrol/guest/settings.php | 1 - enrol/guest/version.php | 4 ++-- 5 files changed, 30 insertions(+), 14 deletions(-) diff --git a/enrol/guest/addinstance.php b/enrol/guest/addinstance.php index 63fb4067620..156e4e70a76 100644 --- a/enrol/guest/addinstance.php +++ b/enrol/guest/addinstance.php @@ -1,5 +1,4 @@ password) { + if ($instance->password !== '') { return array(new pix_icon('withpassword', get_string('pluginname', 'enrol_guest'), 'enrol_guest')); } else { return array(new pix_icon('withoutpassword', get_string('pluginname', 'enrol_guest'), 'enrol_guest')); @@ -73,7 +72,18 @@ class enrol_guest_plugin extends enrol_plugin { public function try_guestaccess(stdClass $instance) { global $USER, $CFG; - if (empty($instance->password)) { + $allow = false; + + if ($instance->password === '') { + $allow = true; + + } else if (isset($USER->enrol_guest_passwords[$instance->id])) { // this is a hack, ideally we should not add stuff to $USER... + if ($USER->enrol_guest_passwords[$instance->id] === $instance->password) { + $allow = true; + } + } + + if ($allow) { // Temporarily assign them some guest role for this context $context = get_context_instance(CONTEXT_COURSE, $instance->courseid); load_temp_course_role($context, $CFG->guestroleid); @@ -114,7 +124,12 @@ class enrol_guest_plugin extends enrol_plugin { public function enrol_page_hook(stdClass $instance) { global $CFG, $OUTPUT, $SESSION, $USER; - if (empty($instance->password)) { + if ($instance->password === '') { + return null; + } + + if (isset($USER->enrol['tempguest'][$instance->courseid]) and $USER->enrol['tempguest'][$instance->courseid] > time()) { + // no need to show the guest access when user can already enter course as guest return null; } @@ -124,13 +139,14 @@ class enrol_guest_plugin extends enrol_plugin { if ($instance->id == $instanceid) { if ($data = $form->get_data()) { - // set up primitive require_login() caching - unset($USER->enrol['enrolled'][$instance->courseid]); - $USER->enrol['tempguest'][$instance->courseid] = time() + 60*60*8; // 8 hours access before asking for pw again - // add guest role $context = get_context_instance(CONTEXT_COURSE, $instance->courseid); + $USER->enrol_guest_passwords[$instance->id] = $data->guestpassword; // this is a hack, ideally we should not add stuff to $USER... + if (isset($USER->enrol['tempguest'][$instance->courseid])) { + remove_temp_course_roles($context); + } load_temp_course_role($context, $CFG->guestroleid); + $USER->enrol['tempguest'][$instance->courseid] = ENROL_MAX_TIMESTAMP; // go to the originally requested page if (!empty($SESSION->wantsurl)) { @@ -282,6 +298,9 @@ class enrol_guest_plugin extends enrol_plugin { $instance->status = $data->{'enrol_guest_status_'.$i}; $instance->timemodified = time(); if ($instance->status == ENROL_INSTANCE_ENABLED) { + if ($instance->password !== $data->{'enrol_guest_password_'.$i}) { + $reset = true; + } $instance->password = $data->{'enrol_guest_password_'.$i}; } $DB->update_record('enrol', $instance); diff --git a/enrol/guest/locallib.php b/enrol/guest/locallib.php index de6179e5386..27e1ede9be7 100644 --- a/enrol/guest/locallib.php +++ b/enrol/guest/locallib.php @@ -1,5 +1,4 @@ instance; - if ($instance->password) { + if ($instance->password !== '') { if ($data['guestpassword'] !== $instance->password) { $plugin = enrol_get_plugin('guest'); if ($plugin->get_config('showhint')) { diff --git a/enrol/guest/settings.php b/enrol/guest/settings.php index 2a8d5f9f0a3..a9376cb0cc2 100644 --- a/enrol/guest/settings.php +++ b/enrol/guest/settings.php @@ -1,5 +1,4 @@ version = 2010081800; +$plugin->version = 2011110600; +$plugin->component = 'enrol_guest';