Commit 58b0ad55 authored by Francois Gannaz's avatar Francois Gannaz
Browse files

LnB API changed: /redirect instead of /user/link

parent 73534e1a
...@@ -62,12 +62,30 @@ class institution extends moodlefetcher ...@@ -62,12 +62,30 @@ class institution extends moodlefetcher
return self::OK; return self::OK;
} }
public function getBindingToken($returnUrl) : string { /**
return $this->fetcher->encodeJwtToken('/v1/user/link', ['url' => $returnUrl]); * Return a JWT token for the redirection page of LnB, so that the user ends on the user-binding page.
*
* @param string $returnUrl Full URL, including protocol.
* @param string $role "learner" | "teacher"
* @return string JWT
* @throws \moodle_exception
*/
public function getBindingTokenForRedirect(string $returnUrl, string $role) : string {
if ($role !== 'learner' && $role !== 'teacher') {
throw new \moodle_exception("Invalid role value in fetch.institution.getBindingToken()");
}
$payload = [
'path' => "/user/bind",
'forward' => [
'role_ext' => $role,
'return_to' => $returnUrl,
],
];
return $this->fetcher->encodeJwtToken('/v1/redirect', $payload);
} }
public function getLoginUrl() : string { public function getRedirectUrl() : string {
global $CFG; global $CFG;
return rtrim($CFG->labnbook_api_url, '/') . '/v1/user/link'; return rtrim($CFG->labnbook_api_url, '/') . '/v1/redirect';
} }
} }
...@@ -22,13 +22,14 @@ require_login($course); ...@@ -22,13 +22,14 @@ require_login($course);
$fetchInstitution = new fetch\institution(); $fetchInstitution = new fetch\institution();
$context = context_course::instance($courseid); $context = context_course::instance($courseid);
$isTeacher = has_capability('mod/labnbook:addinstance', $context);
if ($create && !fetch\user::isAuthenticated()) { if ($create && !fetch\user::isAuthenticated()) {
global $USER; global $USER;
$isTeacher = has_capability('mod/labnbook:addinstance', $context);
if (createUser($USER, $isTeacher)) { if (createUser($USER, $isTeacher)) {
redirect($returnUrl); redirect($returnUrl);
} }
} }
$role = ($isTeacher ? 'teacher' : 'learner');
/* @var $PAGE moodle_page */ /* @var $PAGE moodle_page */
global $PAGE; global $PAGE;
...@@ -49,8 +50,8 @@ echo $OUTPUT->header(); ...@@ -49,8 +50,8 @@ echo $OUTPUT->header();
<div class="container-fluid"> <div class="container-fluid">
<div class="row"> <div class="row">
<div class="col" style="text-align: center"> <div class="col" style="text-align: center">
<form action="<?= $fetchInstitution->getLoginUrl() ?>" method="GET"> <form action="<?= $fetchInstitution->getRedirectUrl() ?>" method="GET">
<input type="hidden" name="token" value="<?= $fetchInstitution->getBindingToken($CFG->wwwroot . $returnUrl) ?>" /> <input type="hidden" name="token" value="<?= $fetchInstitution->getBindingTokenForRedirect($CFG->wwwroot . $returnUrl, $role) ?>" />
<button type="submit" class="btn btn-primary">Se connecter à LabNbook</button> <button type="submit" class="btn btn-primary">Se connecter à LabNbook</button>
</form> </form>
<div>Si vous possédez déjà un compte dans LabNbook, ce lien permettra de vous authentifier dans LabNbook et de mettre à jour votre compte Moodle.</div> <div>Si vous possédez déjà un compte dans LabNbook, ce lien permettra de vous authentifier dans LabNbook et de mettre à jour votre compte Moodle.</div>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment