Vous avez reçu un message "Your GitLab account has been locked ..." ? Pas d'inquiétude : lisez cet article https://docs.gricad-pages.univ-grenoble-alpes.fr/help/unlock/

institution.php 2.07 KB
Newer Older
Francois Gannaz's avatar
Francois Gannaz committed
1
2
3
4
<?php

/**
 * @license http://www.gnu.org/licenses/gpl-3.0.html  GNU GPL v3
Francois Gannaz's avatar
Francois Gannaz committed
5
 * @copyright  2019 Université Grenoble Alpes
Francois Gannaz's avatar
Francois Gannaz committed
6
7
8
9
 */

namespace mod_labnbook\fetch;

Francois Gannaz's avatar
Francois Gannaz committed
10
11
12
/**
 * Send queries to the LabnBook API as an authenticated LnB institution.
 */
Francois Gannaz's avatar
Francois Gannaz committed
13
14
15
16
17
18
19
20
21
22
23
class institution extends moodlefetcher
{
    const OK = 200;
    const ERROR_NETWORK = 0;
    const ERROR_TOKEN_NOTVALID = 401;
    const ERROR_LOGIN_UNKNOWN = 403;
    const ERROR_CREATION_FAILED = 500;

    /**
     * @return int Cf constants: OK, ERROR_*
     */
Francois Gannaz's avatar
Francois Gannaz committed
24
25
    public function loginUser(): int
    {
Francois Gannaz's avatar
Francois Gannaz committed
26
        try {
27
            $this->fetch("POST", "/v1/auth/login");
Francois Gannaz's avatar
Francois Gannaz committed
28
29
30
31
32
33
34
        } catch (\Exception $e) {
            $error = $this->fetcher->getLastError();
            return (isset($error->code) ? (int) $error->code : 0);
        }
        return self::OK;
    }

Francois Gannaz's avatar
Francois Gannaz committed
35
36
    public function createUser($user)
    {
Francois Gannaz's avatar
Francois Gannaz committed
37
        try {
Francois Gannaz's avatar
Francois Gannaz committed
38
            $this->fetch("PUT", "/v1/user/create", (array) $user);
Francois Gannaz's avatar
Francois Gannaz committed
39
40
        } catch (\Exception $e) {
            $error = $this->fetcher->getLastError();
Francois Gannaz's avatar
Francois Gannaz committed
41
            return (isset($error->code) ? (int) $error->code : 0);
Francois Gannaz's avatar
Francois Gannaz committed
42
43
44
45
        }
        // creation successful
        return self::OK;
    }
46

47
48
49
    /**
     * Return a JWT token for the redirection page of LnB, so that the user ends on the user-binding page.
     *
Francois Gannaz's avatar
Francois Gannaz committed
50
     * @param string $returnUrl full URL, including protocol
51
52
53
54
     * @param string $role "learner" | "teacher"
     * @return string JWT
     * @throws \moodle_exception
     */
Francois Gannaz's avatar
Francois Gannaz committed
55
56
    public function getBindingTokenForRedirect(string $returnUrl, string $role): string
    {
57
58
59
60
61
62
63
64
65
66
67
        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);
68
    }
Francois Gannaz's avatar
Francois Gannaz committed
69
70
71
72
73
74

    protected function getSigningKey(): string
    {
        global $CFG;
        return $CFG->labnbook_api_key;
    }
Francois Gannaz's avatar
Francois Gannaz committed
75
}