Skip to content

Commit

Permalink
Fixed the magic 1.3 issues.
Browse files Browse the repository at this point in the history
vasanthlmsace committed Oct 16, 2023
1 parent d90a43d commit 6783563
Showing 5 changed files with 59 additions and 29 deletions.
2 changes: 1 addition & 1 deletion db/install.xml
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="auth_magic_loginlinks" COMMENT="Store the all magic authentication user keys">
<TABLE NAME="auth_magic_loginlinks" COMMENT="Stores data for magic authentication links">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="userid" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false"/>
7 changes: 7 additions & 0 deletions lang/en/auth_magic.php
Original file line number Diff line number Diff line change
@@ -54,6 +54,13 @@
If you need help, please contact the site administrator,
{$a->admin}';
$string['notexists_loginlinkmsg'] = 'Hi {$a->fullname},
You do not have a magic login link, so you don\'t have access to the site using the magic authentication.
If you need help, please contact the site administrator,
{$a->admin}';

$string['pluginisdisabled'] = 'The magic authentication plugin is disabled.';
$string['sentlinktouser'] = "If you supplied a correct email address, an email containing a magic login link should have been sent to you.";
$string['sentlinktousername'] = "If you supplied a correct username, an email containing a magic login link should have been sent to your email address.";
73 changes: 46 additions & 27 deletions lib.php
Original file line number Diff line number Diff line change
@@ -36,27 +36,6 @@ function auth_magic_get_user_login_link($userid) {
}


/**
* Get user login link expires.
* @param int $userid
* @return string time.
*/
function auth_magic_get_user_login_link_expires($userid) {
global $DB;
$expiry = $DB->get_field('auth_magic_loginlinks', 'loginexpiry', array('userid' => $userid));
if ($expiry && $expiry > time()) {
$t = $expiry - time();
$hours = floor($t / 3600);
$minutes = floor(($t % 3600) / 60);
$seconds = $t % 60;
$strhours = get_string('hours');
$strmins = get_string('minutes');
$strseconds = get_string('seconds');
return sprintf("%02d $strhours %02d $strmins %02d $strseconds", $hours, $minutes, $seconds);
}
return get_string('currentlylinkexpiry', 'auth_magic');
}

/**
* Send message to user using message api.
*
@@ -97,8 +76,8 @@ function auth_magic_messagetouser($userto, $subject, $messageplain, $messagehtml
function auth_magic_sent_loginlink_touser($userid, $otherauth = false, $expired = false) {
$site = get_site();
$user = \core_user::get_user($userid);
$auth = get_auth_plugin('magic');
if ($otherauth) {
$auth = get_auth_plugin('magic');
$auth->create_magic_instance($user, false);
}
$loginlink = auth_magic_get_user_login_link($userid);
@@ -107,12 +86,20 @@ function auth_magic_sent_loginlink_touser($userid, $otherauth = false, $expired
$data->sitename = format_string($site->fullname);
$data->admin = generate_email_signoff();
$data->fullname = fullname($user);
$data->link = $loginlink;
$data->expiry = auth_magic_get_user_login_link_expires($userid);
if ($expired) {
$messageplain = get_string('expiredloginlinkmsg', 'auth_magic', $data);
if (get_config('auth_magic', 'loginkeytype') == 'more' && empty($loginlink)) {
$auth->create_magic_instance($user, false);
$loginlink = auth_magic_get_user_login_link($user->id);
}
if (!empty($loginlink)) {
$data->link = $loginlink;
$data->expiry = auth_magic_get_user_magic_link_expires($userid, 'loginexpiry');
if ($expired) {
$messageplain = get_string('expiredloginlinkmsg', 'auth_magic', $data);
} else {
$messageplain = get_string('loginlinkmessage', 'auth_magic', $data);
}
} else {
$messageplain = get_string('loginlinkmessage', 'auth_magic', $data);
$messageplain = get_string('notexists_loginlinkmsg', 'auth_magic', $data);
}
$messagehtml = text_to_html($messageplain, false, false, true);
$user->mailformat = 1; // Always send HTML version as well.
@@ -121,6 +108,38 @@ function auth_magic_sent_loginlink_touser($userid, $otherauth = false, $expired

}


/**
* Get user login link expires.
* @param int $userid
* @param string $type
* @return string time.
*/
function auth_magic_get_user_magic_link_expires($userid, $type) {
global $DB;
$expiry = $DB->get_field('auth_magic_loginlinks', $type, array('userid' => $userid));
return $expiry ? auth_magic_expirytime_convert_datestring($expiry) : '';
}

/**
* Convert expirytime to date.
* @param int $expiry
* @return string value.
*/
function auth_magic_expirytime_convert_datestring($expiry) {
if ($expiry && $expiry > time()) {
$t = $expiry - time();
$hours = floor($t / 3600);
$minutes = floor(($t % 3600) / 60);
$seconds = $t % 60;
$strhours = get_string('hours');
$strmins = get_string('minutes');
$strseconds = get_string('seconds');
return sprintf("%02d $strhours %02d $strmins %02d $strseconds", $hours, $minutes, $seconds);
}
return get_string('currentlylinkexpiry', 'auth_magic');
}

/**
* Sent the information for non magic auth users.
* @param int $userid
4 changes: 4 additions & 0 deletions login.php
Original file line number Diff line number Diff line change
@@ -106,6 +106,10 @@
$auth->check_userkey_type($keyvalue);

$key = validate_user_key($keyvalue, 'auth/magic', null);
if (get_config('auth_magic', 'loginkeytype') == 'once') {
delete_user_key('auth/magic', $key->userid);
$DB->delete_records('auth_magic_loginlinks', array('userid' => $key->userid));
}
if (isloggedin()) {
if ($USER->id != $key->userid) {
// Logout the current user if it's different to one that associated to the valid key.
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
@@ -24,7 +24,7 @@

defined('MOODLE_INTERNAL') || die();

$plugin->version = 2023101000; // The current plugin version (Date: YYYYMMDDXX).
$plugin->version = 2023101600; // The current plugin version (Date: YYYYMMDDXX).
$plugin->requires = 2020060900; // Requires this Moodle version.
$plugin->component = 'auth_magic'; // Full name of the plugin (used for diagnostics).
$plugin->release = 'v1.1';

0 comments on commit 6783563

Please sign in to comment.