1
0
mirror of https://github.com/moodle/moodle.git synced 2025-04-24 01:44:27 +02:00

MDL-67807 webservice: Return concurrent sessions information

This commit is contained in:
Juan Leyva 2021-11-19 14:03:37 +01:00
parent eab63d2cfe
commit c7e7229ccd
2 changed files with 27 additions and 0 deletions

@ -214,6 +214,12 @@ class core_webservice_external extends external_api {
// Current theme.
$siteinfo['theme'] = clean_param($PAGE->theme->name, PARAM_THEME); // We always clean to avoid problem with old sites.
$siteinfo['limitconcurrentlogins'] = (int) $CFG->limitconcurrentlogins;
if (!empty($CFG->limitconcurrentlogins)) {
// For performance, only when enabled.
$siteinfo['usersessionscount'] = $DB->count_records('sessions', ['userid' => $USER->id]);
}
return $siteinfo;
}
@ -283,6 +289,9 @@ class core_webservice_external extends external_api {
'usercalendartype' => new external_value(PARAM_PLUGIN, 'Calendar typed used by the user.', VALUE_OPTIONAL),
'userissiteadmin' => new external_value(PARAM_BOOL, 'Whether the user is a site admin or not.', VALUE_OPTIONAL),
'theme' => new external_value(PARAM_THEME, 'Current theme for the user.', VALUE_OPTIONAL),
'limitconcurrentlogins' => new external_value(PARAM_INT, 'Number of concurrent sessions allowed', VALUE_OPTIONAL),
'usersessionscount' => new external_value(PARAM_INT, 'Number of active sessions for current user.
Only returned when limitconcurrentlogins is used.', VALUE_OPTIONAL),
)
);
}

@ -166,6 +166,24 @@ class core_webservice_externallib_testcase extends externallib_advanced_testcase
$this->assertTrue($siteinfo['userissiteadmin']);
$this->assertEmpty($USER->theme);
$this->assertEquals($PAGE->theme->name, $siteinfo['theme']);
$this->assertEquals($CFG->limitconcurrentlogins, $siteinfo['limitconcurrentlogins']);
$this->assertFalse(isset($siteinfo['usersessionscount']));
$CFG->limitconcurrentlogins = 1;
$record = new stdClass();
$record->state = 0;
$record->sessdata = null;
$record->userid = $USER->id;
$record->timemodified = time();
$record->firstip = $record->lastip = '10.0.0.1';
$record->sid = md5('hokus1');
$record->timecreated = time();
$DB->insert_record('sessions', $record);
$siteinfo = core_webservice_external::get_site_info();
$siteinfo = external_api::clean_returnvalue(core_webservice_external::get_site_info_returns(), $siteinfo);
$this->assertEquals($CFG->limitconcurrentlogins, $siteinfo['limitconcurrentlogins']);
$this->assertEquals(1, $siteinfo['usersessionscount']);
}
/**