diff --git a/lib/rsslib.php b/lib/rsslib.php index 4f625d1c43ab0..3286d5df1d735 100644 --- a/lib/rsslib.php +++ b/lib/rsslib.php @@ -413,10 +413,15 @@ function rss_geterrorxmlfile($errortype = 'rsserror') { function rss_get_userid_from_token($token) { global $DB; - $sql = 'SELECT u.id FROM {user} u - JOIN {user_private_key} k ON u.id = k.userid - WHERE u.deleted = 0 AND u.confirmed = 1 - AND u.suspended = 0 AND k.value = ?'; + $sql = "SELECT u.id + FROM {user} u + JOIN {user_private_key} k ON u.id = k.userid + WHERE u.deleted = 0 + AND u.confirmed = 1 + AND u.suspended = 0 + AND k.script = 'rss' + AND k.value = ?"; + return $DB->get_field_sql($sql, array($token), IGNORE_MISSING); } diff --git a/lib/tests/rsslib_test.php b/lib/tests/rsslib_test.php index ff0674595b89f..3585601018e37 100644 --- a/lib/tests/rsslib_test.php +++ b/lib/tests/rsslib_test.php @@ -20,7 +20,7 @@ global $CFG; require_once($CFG->libdir.'/simplepie/moodle_simplepie.php'); - +require_once($CFG->libdir . '/rsslib.php'); /** * These tests rely on the rsstest.xml file on download.moodle.org, @@ -140,4 +140,19 @@ public function test_redirect() { $this->assertSame('Moodle News', $feed->get_title()); $this->assertSame('http://moodle.org/mod/forum/view.php?f=1', $feed->get_link()); } + + /** + * Test that we can get the right user ID based on the provided private key (token). + * + * @covers ::rss_get_userid_from_token + */ + public function test_rss_get_userid_from_token() { + global $USER; + + $this->resetAfterTest(); + $this->setGuestUser(); + + $key = rss_get_token($USER->id); + $this->assertSame(rss_get_userid_from_token($key), $USER->id); + } }