aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/Lti/Controller/EnrollBaseController.php
blob: 0403daffdd1bc48e191857e2e0734683ce5f7216 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
namespace Studip\Lti\Controller;

use User;
use PageLayout;
use LtiToolModule;
use AccessDeniedException;
use AuthenticatedController;
use Studip\OAuth2\NegotiatesWithPsr7;

abstract class EnrollBaseController extends AuthenticatedController
{
    protected $allow_nobody = true;

    use NegotiatesWithPsr7;

    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        if (!LtiToolModule::isToolSharingEnabled()) {
            throw new AccessDeniedException();
        }

        PageLayout::disableSidebar();
        PageLayout::setBodyElementId('lti');
    }

    protected function storeUserLocale(User $user, ?string $locale): self
    {
        if ($locale && str_starts_with($locale, 'en')) {
            $_SESSION['_language'] = 'en_GB';
            $user->preferred_language = 'en_GB';
            $user->store();
        }

        return $this;
    }

    protected function validateCallbackData(string $callbackId): array
    {
        if (empty($_SESSION['callbacks'][$callbackId])) {
            throw new AccessDeniedException('Missing or invalid callback ID');
        }

        $callbackData = $_SESSION['callbacks'][$callbackId];
        if (
            $callbackData['context'] !== 'lti'
            || $callbackData['expires_at'] < time()
        ) {
            throw new AccessDeniedException('Invalid or expired callback data');
        }

        return $callbackData;
    }
}