-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoodleFunctionBasedMatrixUserIdLoader.php
More file actions
executable file
·139 lines (104 loc) · 3.67 KB
/
MoodleFunctionBasedMatrixUserIdLoader.php
File metadata and controls
executable file
·139 lines (104 loc) · 3.67 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
declare(strict_types=1);
/**
* @package mod_matrix
* @copyright 2022, New Vector Ltd
* @license SPDX-License-Identifier: Apache-2.0
*/
namespace mod_matrix\Plugin\Infrastructure;
use mod_matrix\Matrix;
use mod_matrix\Plugin;
final class MoodleFunctionBasedMatrixUserIdLoader implements Plugin\Domain\MatrixUserIdLoader
{
public const USER_PROFILE_FIELD_NAME = 'matrix_user_id';
/**
* Méthodes d'authentification Moodle acceptées pour la génération
* automatique de l'identifiant Jokko à partir de l'email.
*/
private const SUPPORTED_AUTH_METHODS = ['oidc', 'cas', 'manual', 'ldap', 'shibboleth'];
private $configuration;
public function __construct(Plugin\Application\Configuration $configuration)
{
$this->configuration = $configuration;
}
public function load(object $user): ?Matrix\Domain\UserId
{
global $CFG;
require_once $CFG->dirroot . '/user/profile/lib.php';
profile_load_custom_fields($user);
if (!\property_exists($user, 'profile') || !\is_array($user->profile)) {
return null;
}
$value = $user->profile[self::USER_PROFILE_FIELD_NAME] ?? '';
if (empty($value) || !\is_string($value) || !\str_starts_with($value, '@')) {
// Génération automatique : on compose @{localpart-email}:{host}
// où host est déduit du homeserver configuré dans l'admin du plugin.
// Appliquée aux utilisateurs OIDC, CAS et manuels (voir SUPPORTED_AUTH_METHODS).
$generated = $this->autoGenerateFromEmail($user);
if (null === $generated) {
return null;
}
$value = $generated;
}
try {
return Matrix\Domain\UserId::fromString($value);
} catch (\InvalidArgumentException $exception) {
return null;
}
}
/**
* Génère un identifiant Jokko à partir de l'email de l'utilisateur
* et du homeserver configuré, sans consulter le champ profil existant.
* Utilisé par la tâche planifiée pour pré-remplir les champs vides.
*/
public function generateForUser(object $user): ?Matrix\Domain\UserId
{
$generated = $this->autoGenerateFromEmail($user);
if (null === $generated) {
return null;
}
try {
return Matrix\Domain\UserId::fromString($generated);
} catch (\InvalidArgumentException $exception) {
return null;
}
}
private function autoGenerateFromEmail(object $user): ?string
{
if (!isset($user->auth) || !\in_array($user->auth, self::SUPPORTED_AUTH_METHODS, true)) {
return null;
}
$localpart = $this->extractEmailLocalpart($user);
if (null === $localpart) {
return null;
}
$host = $this->homeserverHost();
if (null === $host) {
return null;
}
return \sprintf('@%s:%s', $localpart, $host);
}
private function extractEmailLocalpart(object $user): ?string
{
if (empty($user->email) || !\is_string($user->email)) {
return null;
}
$parts = \explode('@', $user->email, 2);
if ('' === $parts[0]) {
return null;
}
return $parts[0];
}
private function homeserverHost(): ?string
{
$homeserverUrl = $this->configuration->homeserverUrl()->toString();
if ('' === $homeserverUrl) {
return null;
}
$host = \parse_url($homeserverUrl, \PHP_URL_HOST);
if (!\is_string($host) || '' === $host) {
return null;
}
return $host;
}
}