1
0
mirror of https://github.com/moodle/moodle.git synced 2025-04-07 09:23:31 +02:00

MDL-65547 tool_mobile: Display QR in user profile

This commit is contained in:
Juan Leyva 2020-04-14 00:20:30 +02:00
parent f24b0795ef
commit 9df5151013
4 changed files with 95 additions and 11 deletions
admin/tool/mobile

@ -31,6 +31,8 @@ use moodle_url;
use moodle_exception;
use lang_string;
use curl;
use core_qrcode;
use stdClass;
/**
* API exposed by tool_mobile, to be used mostly by external functions and the plugin settings.
@ -51,6 +53,8 @@ class api {
const LOGIN_KEY_TTL = 60;
/** @var string URL of the Moodle Apps Portal */
const MOODLE_APPS_PORTAL_URL = 'https://apps.moodle.com';
/** @var int seconds a QR login key will expire. */
const LOGIN_QR_KEY_TTL = 600;
/**
* Returns a list of Moodle plugins supporting the mobile app.
@ -336,6 +340,7 @@ class api {
/**
* Creates an auto-login key for the current user, this key is restricted by time and ip address.
* This key is used for automatically login the user in the site when the Moodle app opens the site in a mobile browser.
*
* @return string the key
* @since Moodle 3.2
@ -351,6 +356,24 @@ class api {
return create_user_key('tool_mobile', $USER->id, null, $iprestriction, $validuntil);
}
/**
* Creates a QR login key for the current user, this key is restricted by time and ip address.
* This key is used for automatically login the user in the site when the user scans a QR code in the Moodle app.
*
* @return string the key
* @since Moodle 3.9
*/
public static function get_qrlogin_key() {
global $USER;
// Delete previous keys.
delete_user_key('tool_mobile', $USER->id);
// Create a new key.
$iprestriction = getremoteaddr(null);
$validuntil = time() + self::LOGIN_QR_KEY_TTL;
return create_user_key('tool_mobile', $USER->id, null, $iprestriction, $validuntil);
}
/**
* Get a list of the Mobile app features.
*
@ -601,4 +624,23 @@ class api {
return $warnings;
}
/**
* Generates a QR code for automatic login from the mobile app.
*
* @param stdClass $mobilesettings tool_mobile settings
* @return string base64 data image contents
*/
public static function generate_login_qrcode(stdClass $mobilesettings) {
global $CFG, $USER;
$urlscheme = !empty($mobilesettings->forcedurlscheme) ? $mobilesettings->forcedurlscheme : 'moodlemobile';
$qrloginkey = static::get_qrlogin_key();
$data = $urlscheme . '://' . $CFG->wwwroot . '?qrlogin=' . $qrloginkey . '&userid=' . $USER->id;
$qrcode = new core_qrcode($data);
$imagedata = 'data:image/png;base64,' . base64_encode($qrcode->getBarcodePngData(5, 5));
return $imagedata;
}
}

@ -60,6 +60,8 @@ $string['disabledfeatures_desc'] = 'Select here the features you want to disable
$string['displayerrorswarning'] = 'Display debug messages (debugdisplay) is enabled. It should be disabled.';
$string['downloadcourse'] = 'Download course';
$string['downloadcourses'] = 'Download courses';
$string['enableqrlogin'] = 'Enable QR login';
$string['enableqrlogin_desc'] = 'If enabled, a QR code that will log users in into the site will be displayed in the users profile page. Only works in sites with https enabled.';
$string['enablesmartappbanners'] = 'Enable App Banners';
$string['enablesmartappbanners_desc'] = 'If enabled, a banner promoting the mobile app will be displayed when accessing the site using a mobile browser.';
$string['forcedurlscheme'] = 'If you want to allow only your custom branded app to be opened via a browser window, then specify its URL scheme here. If you want to allow only the official app, then set the default value. Leave the field empty if you want to allow any app.';
@ -96,6 +98,9 @@ $string['oauth2identityproviders'] = 'OAuth 2 identity providers';
$string['offlineuse'] = 'Offline use';
$string['pluginname'] = 'Moodle app tools';
$string['pluginnotenabledorconfigured'] = 'Plugin not enabled or configured.';
$string['qrcodeformobileapplogin'] = 'QR code for mobile app login';
$string['qrcodeformobileapploginabout'] = 'Scan this code in your mobile app and you will be automatically logged in into this site. The code expires in {$a} minutes.';
$string['qrsiteadminsnotallowed'] = 'For security reasons login via QR code is not allowed to site administrators.';
$string['readingthisemailgettheapp'] = 'Reading this in an email? <a href="{$a}">Download the mobile app and receive notifications on your mobile device</a>.';
$string['remoteaddons'] = 'Remote add-ons';
$string['selfsignedoruntrustedcertificatewarning'] = 'It seems that the HTTPS certificate is self-signed or not trusted. The mobile app will only work with trusted sites.';
@ -108,3 +113,4 @@ $string['getmoodleonyourmobile'] = 'Get the mobile app';
$string['privacy:metadata:preference:tool_mobile_autologin_request_last'] = 'The date of the last auto-login key request. Between each request 6 minutes are required.';
$string['privacy:metadata:core_userkey'] = 'User\'s keys used to create auto-login key for the current user.';
$string['responsivemainmenuitems'] = 'Responsive menu items';
$string['viewqrcode'] = 'View QR code';

@ -126,24 +126,53 @@ function tool_mobile_myprofile_navigation(\core_user\output\myprofile\tree $tree
return;
}
if (!$url = tool_mobile_create_app_download_url()) {
return;
$newnodes = [];
$mobilesettings = get_config('tool_mobile');
// Check if we should display a QR code for quick login.
if (is_https() && !empty($mobilesettings->enableqrlogin)) {
$qrcodeforappstr = get_string('qrcodeformobileapplogin', 'tool_mobile');
if (is_siteadmin()) {
$mobileqr = get_string('qrsiteadminsnotallowed', 'tool_mobile');
} else {
$qrcodeimg = tool_mobile\api::generate_login_qrcode($mobilesettings);
$minutes = tool_mobile\api::LOGIN_QR_KEY_TTL / MINSECS;
$mobileqr = get_string('qrcodeformobileapploginabout', 'tool_mobile', $minutes);
$mobileqr .= html_writer::link('#qrcode', get_string('viewqrcode', 'tool_mobile'),
['class' => 'btn btn-primary mt-2', 'data-toggle' => 'collapse', 'role' => 'button', 'aria-expanded' => 'false']);
$mobileqr .= html_writer::div(html_writer::img($qrcodeimg, $qrcodeforappstr), 'collapse mt-4', ['id' => 'qrcode']);
}
$newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappqr', $qrcodeforappstr, null, null, $mobileqr);
}
// Check if the user is using the app, encouraging him to use it otherwise.
$userhastoken = tool_mobile_user_has_token($user->id);
$mobilecategory = new core_user\output\myprofile\category('mobile', get_string('mobileapp', 'tool_mobile'),
'loginactivity');
$tree->add_category($mobilecategory);
$mobilestrconnected = null;
if ($userhastoken) {
$mobilestr = get_string('mobileappconnected', 'tool_mobile');
} else {
$mobilestr = get_string('mobileappenabled', 'tool_mobile', $url->out());
$mobilestrconnected = get_string('mobileappconnected', 'tool_mobile');
} else if ($url = tool_mobile_create_app_download_url()) {
$mobilestrconnected = get_string('mobileappenabled', 'tool_mobile', $url->out());
}
$node = new core_user\output\myprofile\node('mobile', 'mobileappnode', $mobilestr, null);
$tree->add_node($node);
if ($mobilestrconnected) {
$newnodes[] = new core_user\output\myprofile\node('mobile', 'mobileappnode', $mobilestrconnected, null);
}
// Add nodes, if any.
if (!empty($newnodes)) {
$mobilecat = new core_user\output\myprofile\category('mobile', get_string('mobileapp', 'tool_mobile'), 'loginactivity');
$tree->add_category($mobilecat);
foreach ($newnodes as $node) {
$tree->add_node($node);
}
}
}
/**

@ -58,6 +58,9 @@ if ($hassiteconfig) {
// Type of login.
$temp = new admin_settingpage('mobileauthentication', new lang_string('mobileauthentication', 'tool_mobile'));
$temp->add(new admin_setting_heading('tool_mobile/moodleappsportalfeaturesauth', '', $featuresnotice));
$options = array(
tool_mobile\api::LOGIN_VIA_APP => new lang_string('loginintheapp', 'tool_mobile'),
tool_mobile\api::LOGIN_VIA_BROWSER => new lang_string('logininthebrowser', 'tool_mobile'),
@ -67,6 +70,10 @@ if ($hassiteconfig) {
new lang_string('typeoflogin', 'tool_mobile'),
new lang_string('typeoflogin_desc', 'tool_mobile'), 1, $options));
$temp->add(new admin_setting_configcheckbox('tool_mobile/enableqrlogin',
new lang_string('enableqrlogin', 'tool_mobile'),
new lang_string('enableqrlogin_desc', 'tool_mobile'), 1));
$temp->add(new admin_setting_configtext('tool_mobile/forcedurlscheme',
new lang_string('forcedurlscheme_key', 'tool_mobile'),
new lang_string('forcedurlscheme', 'tool_mobile'), 'moodlemobile', PARAM_NOTAGS));