Bonjour,
Je développe un intranet, j'ai repris le code existant de mon entreprise et je m'occupe de la connexion LDAP. La connexion LDAP en elle-même semble bien fonctionner. Je suis bloquée depuis quelques temps.
Je m'explique: J'ai override LdapUser LdapUserProvider.
LdapUser
LdapUserProvider
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630 <?php namespace App\Security; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use FOS\UserBundle\Model\GroupInterface; use FOS\UserBundle\Model\UserInterface; use Symfony\Component\Ldap\Entry; use Symfony\Component\Security\Core\User\EquatableInterface; class LdapUser implements UserInterface, EquatableInterface { /** * @var mixed */ protected $id; /** * @var string */ protected $username; /** * @var string */ protected $usernameCanonical; /** * @var string */ protected $email; /** * @var string */ protected $emailCanonical; /** * @var bool */ protected $enabled; /** * The salt to use for hashing. * * @var string */ protected $salt; /** * Encrypted password. Must be persisted. * * @var string */ protected $password; /** * Plain password. Used for model validation. Must not be persisted. * * @var string */ protected $plainPassword; /** * @var \DateTime|null */ protected $lastLogin; protected $firstname; protected $lastname; /** * Random string sent to the user email address in order to verify it. * * @var string|null */ protected $confirmationToken; /** * @var \DateTime|null */ protected $passwordRequestedAt; /** * @var GroupInterface[]|Collection */ protected $groups; /** * @var array */ protected $roles; private $entry; private $extraFields; public function __construct(Entry $entry, string $username, ?string $password, array $roles = [], array $extraFields = []) { if (!$username) { throw new \InvalidArgumentException('The username cannot be empty.'); } parent::__construct(); $this->enabled = false; $this->roles = array(); $this->entry = $entry; $this->username = $username; $this->password = $password; $this->roles = $roles; $this->extraFields = $extraFields; } /** * @return string */ public function __toString() { return (string) $this->getUsername(); } /** * {@inheritdoc} */ public function addRole($role) { $role = strtoupper($role); if ($role === static::ROLE_DEFAULT) { return $this; } if (!in_array($role, $this->roles, true)) { $this->roles[] = $role; } return $this; } /** * {@inheritdoc} */ public function serialize() { return serialize(array( $this->password, $this->salt, $this->usernameCanonical, $this->username, $this->enabled, $this->id, $this->email, $this->emailCanonical, $this->firstname, )); } /** * {@inheritdoc} */ public function unserialize($serialized) { $data = unserialize($serialized); if (13 === count($data)) { // Unserializing a User object from 1.3.x unset($data[4], $data[5], $data[6], $data[9], $data[10]); $data = array_values($data); } elseif (11 === count($data)) { // Unserializing a User from a dev version somewhere between 2.0-alpha3 and 2.0-beta1 unset($data[4], $data[7], $data[8]); $data = array_values($data); } list( $this->password, $this->salt, $this->usernameCanonical, $this->username, $this->enabled, $this->id, $this->email, $this->emailCanonical, $this->firstname ) = $data; } /** * {@inheritdoc} */ public function eraseCredentials() { $this->password = null; $this->plainPassword = null; } /** * {@inheritdoc} */ public function getId() { return $this->id; } /** * {@inheritdoc} */ public function getUsername(): string { return $this->username; } /** * {@inheritdoc} */ public function getUsernameCanonical() { return $this->usernameCanonical; } public function getEntry(): Entry { return $this->entry; } /** * {@inheritdoc} */ public function getRoles(): array { $roles = $this->roles; foreach ($this->getGroups() as $group) { $roles = array_merge($roles, $group->getRoles()); } // we need to make sure to have at least one role $roles[] = static::ROLE_DEFAULT; return array_unique($roles); } /** * {@inheritdoc} */ public function getPassword(): ?string { return $this->password; } /** * {@inheritdoc} */ public function getSalt(): ?string { return null; } public function getLastName(): ?string{ return $this->lastname; } public function getFirstName(): ?string{ return $this->firstname; } public function getExtraFields(): array { return $this->extraFields; } /** * {@inheritdoc} */ public function isEqualTo(\Symfony\Component\Security\Core\User\UserInterface $user): bool { if (!$user instanceof self) { return false; } if ($this->getPassword() !== $user->getPassword()) { return false; } if ($this->getSalt() !== $user->getSalt()) { return false; } if ($this->getUsername() !== $user->getUsername()) { return false; } return true; } /** * {@inheritdoc} */ public function getEmail() { return $this->email; } /** * {@inheritdoc} */ public function getEmailCanonical() { return $this->emailCanonical; } /** * {@inheritdoc} */ public function getPlainPassword() { return $this->plainPassword; } /** * Gets the last login time. * * @return \DateTime|null */ public function getLastLogin() { return $this->lastLogin; } /** * {@inheritdoc} */ public function getConfirmationToken() { return $this->confirmationToken; } /** * {@inheritdoc} */ public function hasRole($role) { return in_array(strtoupper($role), $this->getRoles(), true); } /** * {@inheritdoc} */ public function isAccountNonExpired() { return true; } /** * {@inheritdoc} */ public function isAccountNonLocked() { return true; } /** * {@inheritdoc} */ public function isCredentialsNonExpired() { return true; } public function isEnabled() { return $this->enabled; } /** * {@inheritdoc} */ public function isSuperAdmin() { return $this->hasRole(static::ROLE_SUPER_ADMIN); } /** * {@inheritdoc} */ public function removeRole($role) { if (false !== $key = array_search(strtoupper($role), $this->roles, true)) { unset($this->roles[$key]); $this->roles = array_values($this->roles); } return $this; } /** * {@inheritdoc} */ public function setUsername($username) { $this->username = $username; return $this; } /** * {@inheritdoc} */ public function setUsernameCanonical($usernameCanonical) { $this->usernameCanonical = $usernameCanonical; return $this; } public function setLastName($lastname){ $this->lastname = $lastname; return $this; } public function setFirstName($firstname){ $this->firstname = $firstname; return $this; } /** * {@inheritdoc} */ public function setSalt($salt) { $this->salt = $salt; return $this; } /** * {@inheritdoc} */ public function setEmail($email) { $this->email = $email; return $this; } /** * {@inheritdoc} */ public function setEmailCanonical($emailCanonical) { $this->emailCanonical = $emailCanonical; return $this; } /** * {@inheritdoc} */ public function setEnabled($boolean) { $this->enabled = $boolean; return $this; } /** * {@inheritdoc} */ public function setPassword($password) { $this->password = $password; return $this; } /** * {@inheritdoc} */ public function setSuperAdmin($boolean) { if (true === $boolean) { $this->addRole(static::ROLE_SUPER_ADMIN); } else { $this->removeRole(static::ROLE_SUPER_ADMIN); } return $this; } /** * {@inheritdoc} */ public function setPlainPassword($password) { $this->plainPassword = $password; return $this; } /** * {@inheritdoc} */ public function setLastLogin(\DateTime $time = null) { $this->lastLogin = $time; return $this; } /** * {@inheritdoc} */ public function setConfirmationToken($confirmationToken) { $this->confirmationToken = $confirmationToken; return $this; } /** * {@inheritdoc} */ public function setPasswordRequestedAt(\DateTime $date = null) { $this->passwordRequestedAt = $date; return $this; } /** * Gets the timestamp that the user requested a password reset. * * @return null|\DateTime */ public function getPasswordRequestedAt() { return $this->passwordRequestedAt; } /** * {@inheritdoc} */ public function isPasswordRequestNonExpired($ttl) { return $this->getPasswordRequestedAt() instanceof \DateTime && $this->getPasswordRequestedAt()->getTimestamp() + $ttl > time(); } /** * {@inheritdoc} */ public function setRoles(array $roles) { $this->roles = array(); foreach ($roles as $role) { $this->addRole($role); } return $this; } /** * {@inheritdoc} */ public function getGroups() { return $this->groups ?: $this->groups = new ArrayCollection(); } /** * {@inheritdoc} */ public function getGroupNames() { $names = array(); foreach ($this->getGroups() as $group) { $names[] = $group->getName(); } return $names; } /** * {@inheritdoc} */ public function hasGroup($name) { return in_array($name, $this->getGroupNames()); } /** * {@inheritdoc} */ public function addGroup(GroupInterface $group) { if (!$this->getGroups()->contains($group)) { $this->getGroups()->add($group); } return $this; } /** * {@inheritdoc} */ public function removeGroup(GroupInterface $group) { if ($this->getGroups()->contains($group)) { $this->getGroups()->removeElement($group); } return $this; } }
Mon problème est une fois que je suis connectée et que je souhaite accéder à mon profil.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208 <?php namespace App\Security; use Doctrine\ORM\EntityManagerInterface; use App\Entity\User; use Symfony\Component\Ldap\Entry; use Symfony\Component\Ldap\Exception\ConnectionException; use Symfony\Component\Ldap\Exception\ExceptionInterface; use Symfony\Component\Ldap\LdapInterface; use Symfony\Component\Ldap\Ldap; use Symfony\Component\Security\Core\Exception\InvalidArgumentException; use Symfony\Component\Security\Core\Exception\UnsupportedUserException; use Symfony\Component\Security\Core\Exception\UsernameNotFoundException; use Symfony\Component\Security\Core\User\PasswordUpgraderInterface; use Symfony\Component\Security\Core\User\UserInterface; use Symfony\Component\Security\Core\User\UserProviderInterface; class LdapUserProvider implements UserProviderInterface, PasswordUpgraderInterface { private $ldap; private $baseDn; private $searchDn; private $searchPassword; private $defaultRoles; private $uidKey; private $defaultSearch; private $passwordAttribute; private $extraFields; //New private $em; public function __construct(Ldap $ldap, string $baseDn, EntityManagerInterface $em , string $searchDn = null, string $searchPassword = null, array $defaultRoles = [], string $uidKey = null, string $filter = null, string $passwordAttribute = null, array $extraFields = []) { if (null === $uidKey) { $uidKey = 'sAMAccountName'; } if (null === $filter) { $filter = '({uid_key}={username})'; } $this->ldap = $ldap; $this->baseDn = $baseDn; $this->searchDn = $searchDn; $this->searchPassword = $searchPassword; $this->defaultRoles = $defaultRoles; $this->uidKey = $uidKey; $this->defaultSearch = str_replace('{uid_key}', $uidKey, $filter); $this->passwordAttribute = $passwordAttribute; $this->extraFields = $extraFields; $this->em = $em; } /** * {@inheritdoc} */ public function loadUserByUsername($username) { try { $this->ldap->bind($this->searchDn, $this->searchPassword); $username = $this->ldap->escape($username, '', LdapInterface::ESCAPE_FILTER); $query = str_replace('{username}', $username, $this->defaultSearch); $search = $this->ldap->query($this->baseDn, $query); } catch (ConnectionException $e) { throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username), 0, $e); } $entries = $search->execute(); $count = \count($entries); if (!$count) { throw new UsernameNotFoundException(sprintf('User "%s" not found.', $username)); } if ($count > 1) { throw new UsernameNotFoundException('More than one user found.'); } return $this->loadUser($username, $entries[0]); } /** * {@inheritdoc} */ public function refreshUser(UserInterface $user) { if (!$user instanceof LdapUser || !$user instanceof User) { throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user))); } //New $userRepository = $this->em->getRepository("AppBundle:User"); $user = $userRepository->findOneBy(array("username" => $user->getUsername())); if($user === null){ throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user))); } return new LdapUser($user->getEntry(), $user->getUsername(), $user->getPassword(), $user->getRoles(), $user->getExtraFields()); } /** * {@inheritdoc} */ public function upgradePassword(UserInterface $user, string $newEncodedPassword): void { if (!$user instanceof LdapUser) { throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', \get_class($user))); } if (null === $this->passwordAttribute) { return; } try { $user->getEntry()->setAttribute($this->passwordAttribute, [$newEncodedPassword]); $this->ldap->getEntryManager()->update($user->getEntry()); $user->setPassword($newEncodedPassword); } catch (ExceptionInterface $e) { // ignore failed password upgrades } } /** * {@inheritdoc} */ public function supportsClass($class) { return LdapUser::class === $class; } /** * Loads a user from an LDAP entry. * * @param $username * @param Entry $entry * @return UserInterface */ protected function loadUser($username, Entry $entry) { $userRepository = $this->em->getRepository("App:User"); $user = $userRepository->findOneBy(array("username" => $username)); if ($user === null) { $user = new LdapUser($entry,$username); $user->setFirstname($entry->getAttribute("givenName")[0]); $user->setLastname($entry->getAttribute("sn")[0]); $user->setEmail($entry->getAttribute("mail")[0]); $user->setUsername($entry->getAttribute("uid")[0]); $user->setRoles($this->defaultRoles); $this->em->persist($user); $this->em->flush(); } else { $this->em->flush(); } return $user; } /** * Fetches the password from an LDAP entry. * * @param null|Entry $entry */ private function getPassword(Entry $entry) { if (null === $this->passwordAttribute) { return; } if (!$entry->hasAttribute($this->passwordAttribute)) { throw new InvalidArgumentException(sprintf('Missing attribute "%s" for user "%s".', $this->passwordAttribute, $entry->getDn())); } $values = $entry->getAttribute($this->passwordAttribute); if (1 !== count($values)) { throw new InvalidArgumentException(sprintf('Attribute "%s" has multiple values.', $this->passwordAttribute)); } return $values[0]; } private function getAttributeValue(Entry $entry, string $attribute) { var_dump("getAttributeValue ".$attribute); if (!$entry->hasAttribute($attribute)) { throw new InvalidArgumentException(sprintf('Missing attribute "%s" for user "%s".', $attribute, $entry->getDn())); } $values = $entry->getAttribute($attribute); if (1 !== \count($values)) { throw new InvalidArgumentException(sprintf('Attribute "%s" has multiple values.', $attribute)); } return $values[0]; } }
Le site a été développé avec Sonata et donc la page profile est faite avec des blocks sonata. J'accède bien à la page mais j'ai des erreurs car firstname et lastname ne sont pas des propriétés de mon $user. C'est étonnant car ça marche sur l'intranet en production et que en dev je n'ai touché à rien. En essayant de résoudre le problème, je me rends compte que mon $user est de type App/Entity/User et non un LdapUser comme je l'aurai pensé.
Mes questions sont:
- Est-ce grave si c'est un user et non un LdapUser? Cela veut-il dire que je ne suis pas connectée via LDAP?
- Je souhaite récupérer le givenName qui est dans mon LDAP pour chaque utilisateur mais je n'arrive pas à trouver où sont initialisés les utilisateurs lors de leur connexion
Merci d'avance pour votre temps, je suis vraiment coincée :/
Partager