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.09 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
6
 * @package    mod_labnbook
 * @copyright  2019 Université Grenoble Alpes
Francois Gannaz's avatar
Francois Gannaz committed
7
8
9
10
 */

namespace mod_labnbook\fetch;

Francois Gannaz's avatar
Francois Gannaz committed
11
12
13
/**
 * Send queries to the LabnBook API as an authenticated LnB institution.
 */
Francois Gannaz's avatar
Francois Gannaz committed
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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;

    protected function getSigningKey() : string {
        global $CFG;
        return $CFG->labnbook_api_key;
    }

    /**
     * @return int Cf constants: OK, ERROR_*
     */
    public function loginUser() : int {
        try {
32
            $this->fetch("POST", "/v1/auth/login");
Francois Gannaz's avatar
Francois Gannaz committed
33
34
35
36
37
38
39
40
41
        } catch (\Exception $e) {
            $error = $this->fetcher->getLastError();
            return (isset($error->code) ? (int) $error->code : 0);
        }
        return self::OK;
    }

    public function createUser($user) {
        try {
42
            $this->fetch("PUT", "/v1/user/create", ['data' => $user]);
Francois Gannaz's avatar
Francois Gannaz committed
43
44
        } catch (\Exception $e) {
            $error = $this->fetcher->getLastError();
Francois Gannaz's avatar
Francois Gannaz committed
45
            return (isset($error->code) ? (int) $error->code : 0);
Francois Gannaz's avatar
Francois Gannaz committed
46
47
48
49
        }
        // creation successful
        return self::OK;
    }
50

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
    /**
     * 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);
71
    }
Francois Gannaz's avatar
Francois Gannaz committed
72
}