Skip to content

Commit edcce43

Browse files
author
Andreas Grabs
committed
mod_collabora: use session handler to check an existing session
1 parent 6ec6ffe commit edcce43

File tree

5 files changed

+87
-10
lines changed

5 files changed

+87
-10
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ moodle-mod_collabora
33

44
Changes
55
-------
6+
### v4.5.2
7+
* 2025-01-21 - Since Moodle 4.5, you have to check an existing session by using the session handler (#45).
8+
* 2025-01-21 - Fix wrong type param in repair.php
9+
* 2025-01-21 - Adjust github workflow to be more restrictive
610

711
### v4.5.1
812
* 2024-11-04 - Fix wrong sort param int get_area_files while loading the current group file.

classes/api/base_filesystem.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,19 @@ public static function get_accepted_types() {
218218
];
219219
}
220220

221+
/**
222+
* Does the PHP session with given id exist?
223+
*
224+
* The session must exist in actual session backend and the session must not be timed out.
225+
* With this check, we ensure that the callback calls belong to a user who is actually logged in.
226+
*
227+
* @param string $sid
228+
* @return bool
229+
*/
230+
public static function session_exists($sid) {
231+
return \mod_collabora\session::session_exists($sid);
232+
}
233+
221234
/**
222235
* Constructor.
223236
*

classes/api/collabora_fs.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ class collabora_fs extends base_filesystem {
4848
*/
4949
public static function get_userid_from_token($token) {
5050
global $DB;
51-
$sql = 'SELECT ct.id, ct.userid from {collabora_token} ct
52-
JOIN {sessions} s ON s.userid = ct.userid AND s.sid = ct.sid
51+
$sql = 'SELECT * from {collabora_token} ct
5352
WHERE ct.token = :token
5453
';
5554

@@ -59,6 +58,11 @@ public static function get_userid_from_token($token) {
5958
return false;
6059
}
6160

61+
// Check the user has a valid session in moodle.
62+
if (!static::session_exists($tokenrec->sid)) {
63+
return false;
64+
}
65+
6266
return $tokenrec->userid;
6367
}
6468

@@ -72,10 +76,9 @@ public static function remove_unused_tokens() {
7276

7377
$recordset = $DB->get_recordset('collabora_token');
7478

75-
$select = 'sid = :sid AND userid > 0';
7679
foreach ($recordset as $tokenrec) {
77-
$params = ['sid' => $tokenrec->sid];
78-
if (!$DB->record_exists_select('sessions', $select, $params)) {
80+
// Check the user has a valid session in moodle.
81+
if (!static::session_exists($tokenrec->sid)) {
7982
$DB->delete_records('collabora_token', ['id' => $tokenrec->id]);
8083
}
8184
}
@@ -495,15 +498,17 @@ public function get_user_token() {
495498
'userid' => $this->user->id,
496499
'sid' => session_id(),
497500
];
498-
$sql = 'SELECT ct.id, ct.token from {collabora_token} ct
499-
JOIN {sessions} s ON s.userid = ct.userid AND s.sid = ct.sid
501+
$sql = 'SELECT * from {collabora_token} ct
500502
WHERE ct.userid = :userid AND ct.sid = :sid
501503
';
502504

503505
$tokenrec = $DB->get_record_sql($sql, $params);
504506

507+
// Check the user has a valid session in moodle.
505508
if (!empty($tokenrec->token)) {
506-
return $tokenrec->token;
509+
if (static::session_exists($tokenrec->sid)) {
510+
return $tokenrec->token;
511+
}
507512
}
508513
// Create a new token record.
509514
$tokenrec = new \stdClass();

classes/session.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
// This file is part of Moodle - http://moodle.org/
3+
//
4+
// Moodle is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// Moodle is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
16+
17+
namespace mod_collabora;
18+
19+
use mod_collabora\api\collabora_fs;
20+
21+
/**
22+
* Helper class for sessions.
23+
*
24+
* Currently it is only used to find out whether or not a user has a valid session.
25+
*
26+
* @package mod_collabora
27+
*
28+
* @author Andreas Grabs <[email protected]>
29+
* @copyright 2025 Humboldt-Universität zu Berlin <[email protected]>
30+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31+
*/
32+
class session {
33+
34+
/**
35+
* Does the PHP session with given id exist?
36+
*
37+
* The session must exist in actual session backend and the session must not be timed out.
38+
* With this check, we ensure that the callback calls belong to a user who is actually logged in.
39+
*
40+
* @param string $sid
41+
* @return bool
42+
*/
43+
public static function session_exists($sid) {
44+
$handlerclass = \core\session\manager::get_handler_class();
45+
/** @var \core\session\handler $handler */
46+
$handler = new $handlerclass();
47+
try {
48+
$handler->init();
49+
} catch (\Exception $e) {
50+
debugging($e->getMessage());
51+
return false;
52+
}
53+
return $handler->session_exists($sid);
54+
}
55+
}

version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
*/
2424
defined('MOODLE_INTERNAL') || die;
2525

26-
$plugin->version = 2024103001;
27-
$plugin->release = 'v4.5.1 (2024110400)';
26+
$plugin->version = 2024103002;
27+
$plugin->release = 'v4.5.2 (2025012100)';
2828
$plugin->requires = 2022111800; // Moodle 4.1.
2929
$plugin->component = 'mod_collabora';
3030
$plugin->maturity = MATURITY_BETA;

0 commit comments

Comments
 (0)