aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/LTI13a/RegistrationManager.php
blob: 05bcc2e269e0a9daa1c3db119ee45a30d250597a (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php

namespace Studip\LTI13a;

use OAT\Library\Lti1p3Core\Registration\RegistrationRepositoryInterface;
use OAT\Library\Lti1p3Core\Registration\RegistrationInterface;

class RegistrationManager implements RegistrationRepositoryInterface
{
    protected ?\LtiResourceLink $link = null;

    public function setResourceLink(\LtiResourceLink $link)
    {
        $this->link = $link;
    }

    #[\Override]
    public function find(string $identifier): ?RegistrationInterface
    {
        //The identifier is the ID of a tool.
        $tool = \LtiTool::find($identifier);
        $link = null;
        if (!$tool) {
            //Attempt to find the tool and a resource link.
            $id_parts = explode('_', $identifier);
            $tool = \LtiTool::find($id_parts[0]);
            $link = \LtiResourceLink::find($id_parts[1]);
        }
        if (!$tool) {
            return null;
        }
        return new Registration($tool, $link);
    }

    /**
     * @inheritDoc
     */
    #[\Override]
    public function findAll(): array
    {
        $tools = \LtiTool::findBySQL('TRUE');
        $registrations = [];
        foreach ($tools as $tool) {
            $registrations[] = new Registration($tool);
        }
        return $registrations;
    }

    #[\Override]
    public function findByClientId(string $clientId): ?RegistrationInterface
    {
        //Find a registration by its client-ID. The client-ID is equivalent to the tool-ID in Stud.IP.
        if (!$clientId) {
            //Nothing to search for.
            return null;
        }
        $tool = \LtiTool::find($clientId);
        if ($tool) {
            return new Registration($tool, $this->link);
        }
        return null;
    }

    #[\Override]
    public function findByPlatformIssuer(string $issuer, string $clientId = null): ?RegistrationInterface
    {
        //Only handle requests for registrations of this Stud.IP:
        $platform_config = \Studip\LTI13a\PlatformManager::getPlatformConfiguration();
        if ($issuer !== $platform_config->getAudience()) {
            //Invalid issuer.
            return null;
        }
        return $this->findByClientId($clientId);
    }

    #[\Override]
    public function findByToolIssuer(string $issuer, string $clientId = null): ?RegistrationInterface
    {
        //Tool registrations are not supported at this moment.
        return null;
    }
}