EuloginUser::getPgt()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
ccs 0
cts 1
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\EuloginBundle\Security\Core\User;
6
7
use drupol\CasBundle\Security\Core\User\CasUser;
8
9
/**
10
 * Class EuloginUser.
11
 */
12
final class EuloginUser implements EuloginUserInterface
13
{
14
    /**
15
     * @var \drupol\CasBundle\Security\Core\User\CasUser
16
     */
17
    private $user;
18
19
    /**
20
     * EuloginUser constructor.
21
     *
22
     * @param array<mixed> $data
23
     */
24 3
    public function __construct(array $data)
25
    {
26 3
        $this->user = new CasUser($this->normalizeUserData($data));
27 3
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function eraseCredentials(): void
33
    {
34
        // null
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function get(string $key, $default = null)
41
    {
42
        return $this->user->get($key, $default);
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 1
    public function getAssuranceLevel(): ?string
49
    {
50 1
        return $this->user->getAttribute('assuranceLevel');
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getAttribute(string $key, $default = null)
57
    {
58
        return $this->user->getAttribute($key, $default);
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 1
    public function getAttributes(): array
65
    {
66 1
        return $this->user->getAttributes();
67
    }
68
69
    /**
70
     * {@inheritdoc}
71
     */
72 1
    public function getAuthenticationFactors(): array
73
    {
74 1
        return $this->user->getAttribute('authenticationFactors', []);
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 1
    public function getDepartmentNumber(): ?string
81
    {
82 1
        return $this->user->getAttribute('departmentNumber');
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88 1
    public function getDomain(): ?string
89
    {
90 1
        return $this->user->getAttribute('domain');
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 1
    public function getDomainUsername(): ?string
97
    {
98 1
        return $this->user->getAttribute('domainUsername');
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 1
    public function getEmail(): ?string
105
    {
106 1
        return $this->user->getAttribute('email');
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 1
    public function getEmployeeNumber(): ?string
113
    {
114 1
        return $this->user->getAttribute('employeeNumber');
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 1
    public function getEmployeeType(): ?string
121
    {
122 1
        return $this->user->getAttribute('employeeType');
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 1
    public function getFirstName(): ?string
129
    {
130 1
        return $this->user->getAttribute('firstName');
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 1
    public function getGroups(): array
137
    {
138 1
        return $this->user->getAttribute('groups', []);
139
    }
140
141
    /**
142
     * {@inheritdoc}
143
     */
144 1
    public function getLastName(): ?string
145
    {
146 1
        return $this->user->getAttribute('lastName');
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 1
    public function getLocale(): ?string
153
    {
154 1
        return $this->user->getAttribute('locale');
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 1
    public function getLoginDate(): ?string
161
    {
162 1
        return $this->user->getAttribute('loginDate');
163
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168 1
    public function getOrgId(): ?string
169
    {
170 1
        return $this->user->getAttribute('orgId');
171
    }
172
173
    /**
174
     * {@inheritdoc}
175
     */
176
    public function getPassword()
177
    {
178
        return null;
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184
    public function getPgt(): ?string
185
    {
186
        return $this->user->getPgt();
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192
    public function getRoles()
193
    {
194
        $default = ['ROLE_CAS_AUTHENTICATED'];
195
196
        if ([] !== $roles = $this->getGroups()) {
197
            if (isset($roles['group'])) {
198
                return array_merge($roles['group'], $default);
199
            }
200
        }
201
202
        return $default;
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208
    public function getSalt()
209
    {
210
        return null;
211
    }
212
213
    /**
214 1
     * {@inheritdoc}
215
     */
216 1
    public function getSso(): ?string
217
    {
218
        return $this->user->getAttribute('sso');
219
    }
220
221
    /**
222 1
     * {@inheritdoc}
223
     */
224 1
    public function getStrengths(): array
225
    {
226
        return $this->user->getAttribute('strengths', []);
227
    }
228
229
    /**
230 1
     * {@inheritdoc}
231
     */
232 1
    public function getTelephoneNumber(): ?string
233
    {
234
        return $this->user->getAttribute('telephoneNumber');
235
    }
236
237
    /**
238 1
     * {@inheritdoc}
239
     */
240 1
    public function getTeleworkingPriority(): ?string
241
    {
242
        return $this->user->getAttribute('teleworkingPriority');
243
    }
244
245
    /**
246 1
     * {@inheritdoc}
247
     */
248 1
    public function getTicketType(): ?string
249
    {
250
        return $this->user->getAttribute('ticketType');
251
    }
252
253
    /**
254 1
     * {@inheritdoc}
255
     */
256 1
    public function getUid(): ?string
257
    {
258
        return $this->user->getAttribute('uid');
259
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264
    public function getUser(): string
265
    {
266
        return $this->user->getUser();
267
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272
    public function getUsername()
273
    {
274
        return $this->getUser();
275
    }
276
277
    /**
278
     * Normalize user data from EU Login to standard CAS user data.
279
     *
280
     * @param array<array|string> $data
281
     *   The data from EU Login
282
     *
283
     * @return array<array|string>
284 3
     *   The normalized data.
285
     */
286 3
    private function normalizeUserData(array $data): array
287 3
    {
288
        $storage = [];
289 3
        $rootAttributes = ['user', 'proxyGrantingTicket', 'proxies'];
290 3
291
        foreach ($rootAttributes as $rootAttribute) {
292 3
            $storage[$rootAttribute] = $data[$rootAttribute] ?? null;
293
        }
294 3
        $storage['attributes'] = array_diff_key($data, array_flip($rootAttributes));
295
296
        return array_filter($storage) + ['attributes' => []];
297
    }
298
}
299