diff --git a/webservice/externallib.php b/webservice/externallib.php index 4daf43473ed..89467a3dbbe 100644 --- a/webservice/externallib.php +++ b/webservice/externallib.php @@ -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), ) ); } diff --git a/webservice/tests/externallib_test.php b/webservice/tests/externallib_test.php index 1fe5fcc0eb8..1a7480abd32 100644 --- a/webservice/tests/externallib_test.php +++ b/webservice/tests/externallib_test.php @@ -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']); } /**