Verified Commit b4e5eec5 authored by David Beniamine's avatar David Beniamine
Browse files

Conform to new LabNbook user bind/create system

See Labnbook#401
parent 69be58d0
......@@ -32,36 +32,29 @@ class institution extends moodlefetcher
return self::OK;
}
public function createUser($user)
{
try {
$this->fetch("PUT", "/v1/user/create", (array) $user);
} catch (\Exception $e) {
$error = $this->fetcher->getLastError();
return (isset($error->code) ? (int) $error->code : 0);
}
// creation successful
return self::OK;
}
/**
* 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"
* @param StdClass $user
* @return string JWT
* @throws \moodle_exception
*/
public function getBindingTokenForRedirect(string $returnUrl, string $role): string
public function getBindingTokenForRedirect(string $returnUrl, string $role, $user): string
{
if ($role !== 'learner' && $role !== 'teacher') {
throw new \moodle_exception("Invalid role value in fetch.institution.getBindingToken()");
}
$payload = [
'path' => "/user/bind",
'path' => "/user/preBind",
'forward' => [
'role_ext' => $role,
'return_to' => $returnUrl,
'login' => $user->username,
'user_name' => $user->lastname,
'first_name' => $user->firstname,
'email' => $user->email,
],
];
return $this->fetcher->encodeJwtToken('/v1/redirect', $payload);
......
......@@ -21,16 +21,11 @@ require_login($course);
$fetchInstitution = new fetch\institution();
$context = context_course::instance($courseid);
$isTeacher = has_capability('mod/labnbook:addinstance', $context);
if ($create && !fetch\user::isAuthenticated()) {
global $USER;
if (createUser($USER, $isTeacher)) {
redirect($returnUrl);
}
}
$role = ($isTeacher ? 'teacher' : 'learner');
/* @var $PAGE moodle_page */
global $PAGE;
global $USER;
$PAGE->set_url('/mod/labnbook/user-binding.php');
$PAGE->set_title("LabNbook");
$PAGE->set_heading("LabNbook");
......@@ -49,35 +44,12 @@ echo $OUTPUT->header();
<div class="row">
<div class="col" style="text-align: center">
<form action="<?= $fetchInstitution->getRedirectUrl(); ?>" method="GET">
<input type="hidden" name="token" value="<?= $fetchInstitution->getBindingTokenForRedirect($CFG->wwwroot . $returnUrl, $role); ?>" />
<input type="hidden" name="token" value="<?= $fetchInstitution->getBindingTokenForRedirect($CFG->wwwroot . $returnUrl, $role, $USER); ?>" />
<button type="submit" class="btn btn-primary">Se connecter à LabNbook</button>
</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>
<div class="col" style="text-align: center">
<form action="" method="POST">
<input type="hidden" name="return" value="<?= \base64_encode($returnUrl); ?>" />
<input type="hidden" name="courseid" value="<?= $COURSE->id; ?>" />
<input type="hidden" name="create" value="1" />
<button type="submit" class="btn btn-primary">Créer un nouveau compte dans LabNbook</button>
</form>
<div>Un compte sera créé dans LabNbook et associé à ce compte Moodle.</div>
</div>
</div>
</div>
<?php
echo $OUTPUT->footer();
function createUser($user, $isTeacher)
{
$fetchInstitution = new fetch\institution();
$success = $fetchInstitution->createUser([
'first_name' => $user->firstname,
'user_name' => $user->lastname,
'login' => $user->username,
'email' => $user->email,
'role' => ($isTeacher ? 'teacher' : 'learner'),
]);
return ($success === fetch\institution::OK);
}
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