aboutsummaryrefslogtreecommitdiff
path: root/lib/classes/LTI13a/Identity.php
blob: 238ad926e1809827f666b7536037f4d8af70eae1 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php

namespace Studip\LTI13a;

use OAT\Library\Lti1p3Core\Message\Payload\MessagePayloadInterface;
use OAT\Library\Lti1p3Core\User\UserIdentityInterface;
use OAT\Library\Lti1p3Core\Util\Collection\CollectionInterface;


class Identity implements UserIdentityInterface
{
    protected \User $user;

    protected array $allowed_optional_fields = [];

    public function __construct(\User $user, \LtiTool $tool)
    {
        $this->user = $user;

        $privacy_settings = \LtiToolPrivacySettings::findOneBySQL(
            '`tool_id` = :tool_id AND `user_id` = :user_id',
            ['tool_id' => $tool->id, 'user_id' => $user->id]
        );
        if ($privacy_settings) {
            $this->allowed_optional_fields = explode(',', $privacy_settings->allowed_optional_fields);
        }
    }

    #[\Override]
    public function getIdentifier(): string
    {
        return $this->user->id;
    }

    #[\Override]
    public function getName(): ?string
    {
        return $this->user->getFullName();
    }

    #[\Override]
    public function getEmail(): ?string
    {
        return $this->user->email;
    }

    #[\Override]
    public function getGivenName(): ?string
    {
        return $this->user->vorname;
    }

    #[\Override]
    public function getFamilyName(): ?string
    {
        return $this->user->nachname;
    }

    #[\Override]
    public function getMiddleName(): ?string
    {
        return '';
    }

    #[\Override]
    public function getLocale(): ?string
    {
        if (!in_array('lang', $this->allowed_optional_fields)) {
            return '';
        }
        return $this->user->preferred_language;
    }

    #[\Override]
    public function getPicture(): ?string
    {
        if (!in_array('avatar_url', $this->allowed_optional_fields)) {
            return '';
        }
        return \Avatar::getAvatar($this->user->id)->getURL(\Avatar::MEDIUM);
    }

    #[\Override]
    public function getAdditionalProperties(): CollectionInterface
    {
        return [];
    }

    #[\Override]
    public function normalize(): array
    {
        return [
            MessagePayloadInterface::CLAIM_SUB => $this->getIdentifier(),
            MessagePayloadInterface::CLAIM_USER_NAME => $this->getName(),
            MessagePayloadInterface::CLAIM_USER_EMAIL => $this->getEmail(),
            MessagePayloadInterface::CLAIM_USER_GIVEN_NAME => $this->getGivenName(),
            MessagePayloadInterface::CLAIM_USER_FAMILY_NAME => $this->getFamilyName(),
            MessagePayloadInterface::CLAIM_USER_MIDDLE_NAME => $this->getMiddleName(),
            MessagePayloadInterface::CLAIM_USER_LOCALE => $this->getLocale(),
            MessagePayloadInterface::CLAIM_USER_PICTURE => $this->getPicture()
        ];
    }
}