From 34ceeb488399cf9496a19cd1259413994945e939 Mon Sep 17 00:00:00 2001 From: Juan Leyva Date: Tue, 23 Nov 2021 12:01:11 +0100 Subject: [PATCH] MDL-69555 tool_mobile: Allow set auto-login min request time --- admin/tool/mobile/classes/api.php | 4 ++++ admin/tool/mobile/classes/external.php | 7 +++++-- admin/tool/mobile/lang/en/tool_mobile.php | 4 +++- admin/tool/mobile/settings.php | 12 ++++++++++++ admin/tool/mobile/tests/externallib_test.php | 10 ++++++++++ admin/tool/mobile/upgrade.txt | 1 + 6 files changed, 35 insertions(+), 3 deletions(-) diff --git a/admin/tool/mobile/classes/api.php b/admin/tool/mobile/classes/api.php index 32234db4a71..4049b887df7 100644 --- a/admin/tool/mobile/classes/api.php +++ b/admin/tool/mobile/classes/api.php @@ -306,6 +306,10 @@ class api { $settings->tool_mobile_filetypeexclusionlist = get_config('tool_mobile', 'filetypeexclusionlist'); $settings->tool_mobile_custommenuitems = get_config('tool_mobile', 'custommenuitems'); $settings->tool_mobile_apppolicy = get_config('tool_mobile', 'apppolicy'); + // This setting could be not set in some edge cases such as bad upgrade. + $mintimereq = get_config('tool_mobile', 'autologinmintimebetweenreq'); + $mintimereq = empty($mintimereq) ? 6 * MINSECS : $mintimereq; + $settings->tool_mobile_autologinmintimebetweenreq = $mintimereq; } if (empty($section) or $section == 'calendar') { diff --git a/admin/tool/mobile/classes/external.php b/admin/tool/mobile/classes/external.php index e72cab0a720..04c2b0bcf32 100644 --- a/admin/tool/mobile/classes/external.php +++ b/admin/tool/mobile/classes/external.php @@ -311,9 +311,12 @@ class external extends external_api { // Between each request 6 minutes are required. $last = get_user_preferences('tool_mobile_autologin_request_last', 0, $USER); // Check if we must reset the count. + $mintimereq = get_config('tool_mobile', 'autologinmintimebetweenreq'); + $mintimereq = empty($mintimereq) ? 6 * MINSECS : $mintimereq; $timenow = time(); - if ($timenow - $last < 6 * MINSECS) { - throw new moodle_exception('autologinkeygenerationlockout', 'tool_mobile'); + if ($timenow - $last < $mintimereq) { + $minutes = $mintimereq / MINSECS; + throw new moodle_exception('autologinkeygenerationlockout', 'tool_mobile', $minutes); } set_user_preference('tool_mobile_autologin_request_last', $timenow, $USER); diff --git a/admin/tool/mobile/lang/en/tool_mobile.php b/admin/tool/mobile/lang/en/tool_mobile.php index 101d48b1729..1d1704a4d65 100644 --- a/admin/tool/mobile/lang/en/tool_mobile.php +++ b/admin/tool/mobile/lang/en/tool_mobile.php @@ -28,7 +28,9 @@ $string['androidappid_desc'] = 'This setting may be left as default unless you h $string['apppolicy'] = 'App policy URL'; $string['apppolicy_help'] = 'The URL of a policy for app users which is listed on the About page in the app. If the field is left empty, the site policy URL will be used instead.'; $string['apprequired'] = 'This functionality is only available when accessed via the Moodle mobile or desktop app.'; -$string['autologinkeygenerationlockout'] = 'Auto-login key generation is blocked. You need to wait 6 minutes between requests.'; +$string['autologinkeygenerationlockout'] = 'Auto-login key generation is blocked. You need to wait {$a} minutes between requests.'; +$string['autologinmintimebetweenreq'] = 'Minimum time between auto-login requests.'; +$string['autologinmintimebetweenreq_desc'] = 'This setting determines the minimum time between auto-login requests from the app. You should set this setting to a low value if mobile app users are frequently asked to enter their credentials when visualising content embedded from the site.'; $string['autologinnotallowedtoadmins'] = 'Auto-login is not allowed for site admins.'; $string['cachedef_plugininfo'] = 'This stores the list of plugins with mobile addons'; $string['cachedef_subscriptiondata'] = 'This stores the Moodle app subscription information.'; diff --git a/admin/tool/mobile/settings.php b/admin/tool/mobile/settings.php index 98f6f126537..e9db68c89ba 100644 --- a/admin/tool/mobile/settings.php +++ b/admin/tool/mobile/settings.php @@ -130,6 +130,18 @@ if ($hassiteconfig) { new lang_string('minimumversion_key', 'tool_mobile'), new lang_string('minimumversion', 'tool_mobile'), '', PARAM_NOTAGS)); + $options = [ + 60 => new lang_string('numminutes', '', 1), + 180 => new lang_string('numminutes', '', 3), + 360 => new lang_string('numminutes', '', 6), + 900 => new lang_string('numminutes', '', 15), + 1800 => new lang_string('numminutes', '', 30), + 3600 => new lang_string('numminutes', '', 60) + ]; + $temp->add(new admin_setting_configselect('tool_mobile/autologinmintimebetweenreq', + new lang_string('autologinmintimebetweenreq', 'tool_mobile'), + new lang_string('autologinmintimebetweenreq_desc', 'tool_mobile'), 360, $options)); + $ADMIN->add('mobileapp', $temp); // Appearance related settings. diff --git a/admin/tool/mobile/tests/externallib_test.php b/admin/tool/mobile/tests/externallib_test.php index 9bdd0297c57..8b3491c666f 100644 --- a/admin/tool/mobile/tests/externallib_test.php +++ b/admin/tool/mobile/tests/externallib_test.php @@ -219,6 +219,7 @@ class tool_mobile_external_testcase extends externallib_advanced_testcase { array('name' => 'tool_mobile_filetypeexclusionlist', 'value' => ''), array('name' => 'tool_mobile_custommenuitems', 'value' => ''), array('name' => 'tool_mobile_apppolicy', 'value' => ''), + array('name' => 'tool_mobile_autologinmintimebetweenreq', 'value' => 6 * MINSECS), array('name' => 'calendartype', 'value' => $CFG->calendartype), array('name' => 'calendar_site_timeformat', 'value' => $CFG->calendar_site_timeformat), array('name' => 'calendar_startwday', 'value' => $CFG->calendar_startwday), @@ -387,6 +388,15 @@ class tool_mobile_external_testcase extends externallib_advanced_testcase { $result = external::get_autologin_key($token->privatetoken); $result = external_api::clean_returnvalue(external::get_autologin_key_returns(), $result); + // Change min time between requests to 30 seconds. + set_config('autologinmintimebetweenreq', 30, 'tool_mobile'); + + // Mock a previous request, 60 seconds ago. + $mocktime = time() - MINSECS; + set_user_preference('tool_mobile_autologin_request_last', $mocktime, $USER); + $result = external::get_autologin_key($token->privatetoken); // All good, we were expecint 30 seconds or more. + $result = external_api::clean_returnvalue(external::get_autologin_key_returns(), $result); + // We just requested one token, we must wait. $this->expectException('moodle_exception'); $this->expectExceptionMessage(get_string('autologinkeygenerationlockout', 'tool_mobile')); diff --git a/admin/tool/mobile/upgrade.txt b/admin/tool/mobile/upgrade.txt index f62c60fb2bc..6d71d33530a 100644 --- a/admin/tool/mobile/upgrade.txt +++ b/admin/tool/mobile/upgrade.txt @@ -4,6 +4,7 @@ Information provided here is intended especially for developers. === 4.0 === * The function tool_mobile\api::get_qrlogin_key() now requires as parameter an object with all the mobile plugin settings. +* The tool_mobile_external::get_config external function now returns the tool_mobile_autologinmintimebetweenreq setting. === 3.7 ===