People.php 35.1 KB
Newer Older
1
2
3
4
5
6
<?php

namespace UnicaenLdap\Entity\Base;

use DateTime;
use UnicaenLdap\Entity\Entity;
7
8
9
use UnicaenLdap\Entity\Structure;
use UnicaenLdap\Exception;
use Zend\Ldap\Dn;
10
11
12
13
14
use Zend\Ldap\Exception\LdapException;

/**
 * Classe mère des entités de la branche "people" de l'annuaire LDAP.
 *
15
 * @author David Surville <david.surville@unicaen.fr>
16
17
18
19
20
21
 */
class People extends Entity
{
    protected $type = 'People';

    /**
22
     * Liste des classes d'objet nécessaires à la création d'une entité "People"
23
24
25
26
27
28
29
30
31
32
33
34
     * Il est nécessaire d'ajouter la classe 'ucbnEtu' ou 'ucbnEmp' selon le
     * statut de la personne.
     *
     * @var array
     */
    protected $objectClass = [
        'top',
        'inetOrgPerson',
        'organizationalPerson',
        'person',
        'eduPerson',
        'supannPerson',
35
        'posixAccount',
36
37
        'sambaAccount',
        'sambaSamAcount',
38
39
        "schacPersonalCharacteristics",
        "schacEntryMetadata"
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
    /**
     * Liste des attributs autorisés pour une entité "People"
     *
     * @var array
     */
    protected $authorizedAttributes = [
        // Attributes classes
        'objectClass',
        // Attributes
        'cn',
        'displayName',
        'dateDeNaissance',
        'eduPersonPrincipalName',
        'eduPersonNickname', // no getter and setter
        'eduPersonAffiliation',
        'eduPersonPrimaryAffiliation',
        'eduPersonOrgDN',
        'eduPersonOrgUnitDN',
        'eduPersonPrimaryOrgUnitDN',
        'dateFinInscription',
        'facsimileTelephoneNumber', // no getter and setter
        'gidNumber',
        'givenName',
        'homeDirectory',
        'labeledURI', // no getter and setter
        'leoCode',
        'loginShell',
        'mail',
        'mobile',
        'pager',
        'postalAddress',
        'preferredLanguage',
        'rid',
        'sambaSID',
        'schacDateOfBirth',
        'schacExpiryDate',
        'sexe',
        'sn',
        'supannActivite',
        'supannAffectation',
        'supannAliasLogin',
        'supannAutreTelephone',
        'supannCivilite',
        'supannCodeINE',
        'supannEmpCorps',
        'supannEmpId',
        'supannEntiteAffectation',
        'supannEntiteAffectationPrincipale',
        'supannEtablissement',
        'supannEtuAnneeInscription',
        'supannEtuCursusAnnee',
        'supannEtuDiplome',
        'supannEtuElementPedagogique',
        'supannEtuEtape',
        'supannEtuId',
        'supannEtuInscription',
        'supannEtuRegimeInscription',
        'supannEtuSecteurDisciplinaire',
        'supannEtuTypeDiplome',
        'supannListeRouge',
        'supannMailPerso',
        'supannParrainDN',
        'supannRoleGenerique',
        'supannRoleEntite',
        'supannRefId',
        'supannTypeEntiteAffectation',
        'telephoneNumber',
        'title', // no getter and setter
        'ucbnAnneePostBac',
        'ucbnCodeEtape',
        'ucbnEtuComplementInscription',
        'ucbnFonctionStructurelle',
        'ucbnOrganisme',
        'ucbnPrivateAddress',
        'ucbnPrivateAddressBis',
        'ucbnSecteurDisciplinaire',
        'ucbnServiceADE',
        'ucbnServicePapercut',
        'ucbnSiteLocalisation',
        'ucbnSousStructure',
        'ucbnStatus',
        'ucbnStructureRecherche',
        'unicaenMonEtupass',
        'unicaenTermsOfUse',
        'userCertificate',
        'uidNumber',
    ];

130
131
132
133
134
    /**
     * Liste des attributs contenant des dates
     *
     * @var array
     */
135
136
137
    protected $dateTimeAttributes = [
        'schacExpiryDate',
    ];
138

139
140
141
142
143
144
145
146
    /**
     * Liste des attributs monovalués
     *
     * @var array
     */
    protected $monoValuedAttributes = [
        'cn',
        'dateDeNaissance',
147
        'dateFinInscription',
148
149
        'displayName',
        'eduPersonOrgDN',
150
        'eduPersonPrimaryAffiliation',
151
152
153
154
155
156
        'eduPersonPrimaryOrgUnitDN',
        'eduPersonPrincipalName',
        'givenName',
        'leoCode',
        'preferredLanguage',
        'schacDateOfBirth',
157
        'schacExpiryDate',
158
159
160
161
162
163
164
165
166
167
168
        'sexe',
        'supannAliasLogin',
        'supannCivilite',
        'supannCodeINE',
        'supannEmpId',
        'supannEntiteAffectationPrincipale',
        'supannEtuId',
        'supannListeRouge',
        'telephoneNumber',
    ];

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
    /**
     * Liste des valeurs autorisées pour les attributs "eduPersonAffiliation" et "eduPersonPrimaryAffiliation"
     *
     * - affiliate : personne qui ne dépend pas de l'établissement (exemple : un partenaire extérieur). La valeur
     * "member" est exclue pour ces personnes.
     * - alum : ancien étudiant conservant des relations avec l'établissement.
     * - emeritus :  professeur ayant obtenu l'éméritat dans l'établissement.
     * - employee : tout personnel rémunéré par l'établissement, quelque soit son activité.
     * - faculty : personnel dont l'activité principale (dans l'établissement) est pédagogique, d'enseignement ou/et de
     * recherche. La valeur “member” est positionnée si ce personnel est géré par l'établissement.
     * - member : personne inscrite dans la (les) base(s) de gestion des étudiants ou celle(s) des personnels.
     * - registered-reader : lecteur de bibliothèque autorisé.
     * - researcher : personne assurant une activité de recherche. La valeur “member” est positionnée si ce personnel
     * est géré par l'établissement.
     * - retired :  personne à la retraite conservant des relations avec l'établissement.
     * - staff : personnel dont l'activité principale (dans l'établissement) est autre qu'enseignant ou chercheur
     * (typiquement BIATOS).
     * - student : personne suivant une formation quelconque dans l'établissement. Si la valeur “member” est positionnée,
     * la personne est enregistrée dans la base des étudiants.
     * - teacher : personnel assurant une activité d'enseignement.
     *
     * @var array
     */
    protected $eduPersonAffiliationAuthorizedValues = [
        'affiliate',
        'alum',
        'emeritus',
        'employee',
        'faculty',
        'member',
        'registered-reader',
        'researcher',
        'retired',
        'staff',
        'student',
        'teacher',
    ];

    /**
     * Liste des valeurs autorisées pour l'attribut "ucbnStatus"
     *
     * @var array
     */
    protected $ucbnStatusAuthorizedValues = [
        'ALUMNI',
        'APPRENANT',
        'AUDITEUR_LIBRE',
        'CONTRACTUEL',
        'ETUDIANT',
218
        'HEBERGE',
219
220
221
222
223
224
225
226
227
228
229
230
231
        'INVITE',
        'LECTEUR_SCD',
        'TITULAIRE',
    ];

    /**
     * Liste des patterns utilisés pour différents attributs
     */
    static protected $structure_pattern = '/^(?<code>[\w-]+);(?<libelle>.+)$/';
    static protected $postal_address_pattern = '/^(.*)\$(.*)\$(.*)\$(.*)\$(.*)\$(.*)$/';
    static protected $role_pattern = '/^\[role=(?<role>[\w-]+)\]\[type=(?<type>[\w-]+)\]\[code=(?<code>[\w-]+)\]\[libelle=(?<libelle>.+)\]$/';
    static protected $role_src_pattern = '/^(?<code>[\d]{4});(?<libelle>.+)$/';
    static protected $refid_pattern = '/^\{(?<etiquette>[\w-:]+)\}(?<identifiant>.+)$/';
232

233
234

    /**
235
236
     * Ajoute ou affecte une valeur à un attribut Ldap
     *
237
238
239
     * @param string $attrName
     * @param mixed $value
     * @param bool $append
240
241
     * @throws Exception
     * @throws LdapException
242
     */
243
    private function appendOrNot($attrName, $value, $append)
244
245
246
247
248
249
    {
        (!$append)
            ? $this->set($attrName, $value)
            : $this->add($attrName, $value);
    }

250
251
252
253
254
255
256
257
    /**
     * Vérifie une valeur et supprime les valeurs nulles
     *
     * @param mixed $value
     * @return array
     */
    private function preFormat($value)
    {
258
259
260
        if (is_scalar($value)
            || is_null($value)
            || is_a($value, 'DateTime')) {
261
262
263
264
265
266
267
268
            $value = [$value];
        }

        $value = array_filter($value);

        return $value;
    }

269
270
271
272
273
274
275
276
    /**
     * Attribut Ldap "sn"
     *
     * Nom d'un individu
     * Doit contenir le nom d'usage. Il est possible d'ajouter le nom de famille en seconde valeur.
     *
     * Multivalué
     *
277
     * @param array|string|null $value
278
279
     * @param bool $append
     * @return self
280
     * @throws Exception
281
282
     * @throws LdapException
     */
283
    public function setSn($value = null, $append = false)
284
    {
285
286
287
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatName'], $value);
        $this->appendOrNot('sn', $value, $append);
288
289
290
291
292
293
294

        return $this;
    }

    /**
     * Attribut Ldap "givenName"
     *
295
     * @param array|string|null $value
296
297
     * @param bool $append
     * @return self
298
     * @throws Exception
299
300
     * @throws LdapException
     */
301
    public function setGivenName($value = null, $append = false)
302
    {
303
304
305
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatName'], $value);
        $this->appendOrNot('givenName', $value, $append);
306
307
308
309
310
311
312

        return $this;
    }

    /**
     * Attribut Ldap "displayName"
     *
313
     * @param array|string|null $value
314
315
     * @param bool $append
     * @return self
316
     * @throws Exception
317
318
     * @throws LdapException
     */
319
    public function setDisplayName($value = null, $append = false)
320
    {
321
322
323
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatName'], $value);
        $this->appendOrNot('displayName', $value, $append);
324
325
326
327
328
329
330

        return $this;
    }

    /**
     * Attribut Ldap "cn"
     *
331
     * @param array|string|null $value
332
333
     * @param bool $append
     * @return self
334
     * @throws Exception
335
336
     * @throws LdapException
     */
337
    public function setCn($value = null, $append = false)
338
    {
339
340
341
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatName'], $value, [true]);
        $this->appendOrNot('cn', $value, $append);
342
343
344
345
346
347
348

        return $this;
    }

    /**
     * Attribut Ldap "dateDeNaissance"
     *
349
350
351
     * @deprecated Deprecated, use {@link self::setSchacDateOfBirth}
     *
     * @param array|string|DateTime|null $value
352
353
     * @param bool $append
     * @return self
354
     * @throws Exception
355
356
     * @throws LdapException
     */
357
    public function setDateDeNaissance($value = null, $append = false)
358
    {
359
360
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
361
362
363
364
            if (is_string($val)) {
                $val = new DateTime($val);
            }
            return $val->format('Ymd');
365
        }, $value);
366

367
        $this->appendOrNot('dateDeNaissance', $value, $append);
368
369
370
371
372
373
374

        return $this;
    }

    /**
     * Attribut Ldap "schacDateOfBirth"
     *
375
     * @param array|string|DateTime|null $value
376
377
     * @param bool $append
     * @return self
378
     * @throws Exception
379
380
     * @throws LdapException
     */
381
    public function setSchacDateOfBirth($value = null, $append = false)
382
    {
383
384
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
385
386
387
388
            if (is_string($val)) {
                $val = new DateTime($val);
            }
            return $val->format('Ymd');
389
        }, $value);
390

391
        $this->appendOrNot('schacDateOfBirth', $value, $append);
392
393
394
395

        return $this;
    }

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
    /**
     * Attribut Ldap "dateFinInscription"
     *
     * @deprecated Deprecated, use {@link self::setSchacExpiryDate}
     *
     * @param array|string|DateTime|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setDateFinInscription($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                $val = new DateTime($val);
            }
            return $val->format('Ymd');
        }, $value);

        $this->appendOrNot('dateFinInscription', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "schacDateOfBirth"
     *
     * @param array|string|DateTime|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setSchacExpiryDate($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                $val = new DateTime($val, new \DateTimeZone('+0000')); // définition du timezone à +0h
            }
            return (int)$val->format('U');
        }, $value);

        $this->appendOrNot('schacExpiryDate', $value, $append);

        return $this;
    }

446
447
448
    /**
     * Attribut Ldap "sexe"
     *
449
     * @param array|string|null $value
450
451
     * @param bool $append
     * @return self
452
     * @throws Exception
453
454
     * @throws LdapException
     */
455
    public function setSexe($value = null, $append = false)
456
    {
457
458
459
460
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
            return in_array($v, ['F', 'M']);
        });
461

462
        $this->appendOrNot('sexe', $value, $append);
463

464
        return $this;
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
    /**
     * Attribut Ldap "eduPersonAffiliation"
     *
     * @param array|string|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setEduPersonAffiliation($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
            return in_array($v, $this->eduPersonAffiliationAuthorizedValues);
        });

        $this->appendOrNot('eduPersonAffiliation', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "eduPersonPrimaryAffiliation"
     *
     * @param array|string|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setEduPersonPrimaryAffiliation($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
            return in_array($v, $this->eduPersonAffiliationAuthorizedValues);
        });

        $this->appendOrNot('eduPersonPrimaryAffiliation', $value, $append);

        return $this;
    }

509
510
511
    /**
     * Attribut Ldap "postalAddress"
     *
512
     * @param array|string|null $value
513
514
     * @param bool $append
     * @return self
515
     * @throws Exception
516
517
     * @throws LdapException
     */
518
    public function setPostalAddress($value = null, $append = false)
519
    {
520
521
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
522
            return preg_match(self::$postal_address_pattern, $v);
523
524
        });

525
        $this->appendOrNot('postalAddress', $value, $append);
526
527
528
529
530
531
532

        return $this;
    }

    /**
     * Attribut Ldap "telephoneNumber"
     *
533
     * @param array|string|null $value
534
535
     * @param bool $append
     * @return self
536
     * @throws Exception
537
538
     * @throws LdapException
     */
539
    public function setTelephoneNumber($value = null, $append = false)
540
    {
541
542
543
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatTel'], $value);
        $this->appendOrNot('telephoneNumber', $value, $append);
544
545
546
547
548
549
550

        return $this;
    }

    /**
     * Attribut Ldap "supannAutreTelephone"
     *
551
     * @param array|string|null $value
552
553
     * @param bool $append
     * @return self
554
     * @throws Exception
555
556
     * @throws LdapException
     */
557
    public function setSupannAutreTelephone($value = null, $append = false)
558
    {
559
560
561
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatTel'], $value);
        $this->appendOrNot('supannAutreTelephone', $value, $append);
562
563
564
565
566
567
568

        return $this;
    }

    /**
     * Attribut Ldap "mobile"
     *
569
     * @param array|string|null $value
570
571
     * @param bool $append
     * @return self
572
     * @throws Exception
573
574
     * @throws LdapException
     */
575
    public function setMobile($value = null, $append = false)
576
    {
577
578
579
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatTel'], $value);
        $this->appendOrNot('mobile', $value, $append);
580
581
582
583
584
585
586

        return $this;
    }

    /**
     * Attribut Ldap "preferredLanguage"
     *
587
     * @param array|string|null $value
588
589
     * @param bool $append
     * @return self
590
     * @throws Exception
591
592
     * @throws LdapException
     */
593
    public function setPreferredLanguage($value = null, $append = false)
594
    {
595
596
597
        $value = $this->preFormat($value);
        $value = array_map('strtolower', $value);
        $this->appendOrNot('preferredLanguage', $value, $append);
598
599
600
601
602
603
604

        return $this;
    }

    /**
     * Attribut Ldap "leoCode"
     *
605
     * @param array|string|null $value
606
607
     * @param bool $append
     * @return self
608
     * @throws Exception
609
610
     * @throws LdapException
     */
611
    public function setLeoCode($value = null, $append = false)
612
    {
613
614
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
615
616
617
            return preg_match('/^\d+$/', $v);
        });

618
        $this->appendOrNot('leoCode', $value, $append);
619
620
621
622
623
624
625

        return $this;
    }

    /**
     * Attribut Ldap "supannALiasLogin"
     *
626
     * @param array|string|null $value
627
628
     * @param bool $append
     * @return self
629
     * @throws Exception
630
631
     * @throws LdapException
     */
632
    public function setSupannAliasLogin($value = null, $append = false)
633
    {
634
635
636
        $value = $this->preFormat($value);
        $value = array_map('strtolower', $value);
        $value = array_filter($value, function ($v) {
637
638
639
            return preg_match('/^[0-9a-z\-]+$/', $v);
        });

640
        $this->appendOrNot('supannAliasLogin', $value, $append);
641
642
643
644
645
646
647

        return $this;
    }

    /**
     * Attribut Ldap "supannEmpId"
     *
648
     * @param array|string|null $value
649
650
     * @param bool $append
     * @return self
651
     * @throws Exception
652
653
     * @throws LdapException
     */
654
    public function setSupannEmpId($value = null, $append = false)
655
    {
656
        $value = $this->preFormat($value);
657

658
659
660
        $this->appendOrNot('supannEmpId', $value, $append);

        return $this;
661
662
663
664
665
    }

    /**
     * Attribut Ldap "supannEtuId"
     *
666
     * @param array|string|null $value
667
668
     * @param bool $append
     * @return self
669
     * @throws Exception
670
671
     * @throws LdapException
     */
672
    public function setSupannEtuId($value = null, $append = false)
673
    {
674
        $value = $this->preFormat($value);
675

676
677
678
        $this->appendOrNot('supannEtuId', $value, $append);

        return $this;
679
680
681
682
683
    }

    /**
     * Attribut Ldap "supannCodeINE"
     *
684
     * @param array|string|null $value
685
686
     * @param bool $append
     * @return self
687
     * @throws Exception
688
689
     * @throws LdapException
     */
690
    public function setSupannCodeINE($value = null, $append = false)
691
    {
692
693
694
        $value = $this->preFormat($value);
        $value = array_map('strtoupper', $value);
        $value = array_filter($value, function ($v) {
695
696
697
            return preg_match('/^[0-9]{10}[A-Z]{1}$/', $v);
        });

698
        $this->appendOrNot('supannCodeINE', $value, $append);
699
700
701
702
703
704
705

        return $this;
    }

    /**
     * Attribut Ldap "supannCivilite"
     *
706
     * @param array|string|null $value
707
708
     * @param bool $append
     * @return self
709
     * @throws Exception
710
711
     * @throws LdapException
     */
712
    public function setSupannCivilite($value = null, $append = false)
713
    {
714
715
716
717
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
            return in_array($v, ['Mme', 'M.']);
        });
718

719
        $this->appendOrNot('supannCivilite', $value, $append);
720

721
        return $this;
722
723
724
725
726
    }

    /**
     * Attribut Ldap "supannListeRouge"
     *
727
     * @param array|string|null $value
728
729
     * @param bool $append
     * @return self
730
     * @throws Exception
731
732
     * @throws LdapException
     */
733
    public function setSupannListeRouge($value = null, $append = false)
734
    {
735
736
737
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatBoolean'], $value);
        $this->appendOrNot('supannListeRouge', $value, $append);
738
739
740
741
742
743
744

        return $this;
    }

    /**
     * Attribut Ldap "supannMailPerso"
     *
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
     * @param array|string|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setSupannMailPerso($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_filter(filter_var_array($value, FILTER_VALIDATE_EMAIL));
        $this->appendOrNot('supannMailPerso', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "supannAffectation"
     *
     * @param array|string|Structure|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setSupannAffectation($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                return preg_match('/.+;.+/', $val) ? $val : null;
            } elseif ($val instanceof Structure) {
                return sprintf('%s;%s', substr($val->get('supannCodeEntite'), 3), $val->get('description'));
            } else {
                return null;
            }
        }, $value);

        $this->appendOrNot('supannAffectation', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "supannEntiteAffectation"
     *
     * @param array|string|Structure|null $value
791
792
     * @param bool $append
     * @return self
793
     * @throws Exception
794
795
     * @throws LdapException
     */
796
    public function setSupannEntiteAffectation($value = null, $append = false)
797
    {
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                if (0 !== strpos($val, $this->getService()->getCodeStructurePrefixe())) {
                    $val = $this->getService()->getCodeStructurePrefixe() . $val;
                }
                return $val;
            } elseif ($val instanceof Structure) {
                return $val->get('supannCodeEntite');
            } else {
                return null;
            }
        }, $value);

        $this->appendOrNot('supannEntiteAffectation', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "supannEntiteAffectationPrincipale"
     *
     * @param array|string|Structure|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setSupannEntiteAffectationPrincipale($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                if (0 !== strpos($val, $this->getService()->getCodeStructurePrefixe())) {
                    $val = $this->getService()->getCodeStructurePrefixe() . $val;
                }
                return $val;
            } elseif ($val instanceof Structure) {
                return $val->get('supannCodeEntite');
            } else {
                return null;
            }
        }, $value);

        $this->appendOrNot('supannEntiteAffectationPrincipale', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "supannTypeEntiteAffectation"
     *
     * @param array|string|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setSupannTypeEntiteAffectation($value = null, $append = false)
    {
        $value = $this->preFormat($value);
859
860
        $supannLabel = $this->getLabel('SUPANN');
        $value = array_map(function ($val) use ($supannLabel) {
861
            if (is_string($val)) {
862
                return preg_match("/^$supannLabel.*$/", $val) ? $val : sprintf('%s%s', $supannLabel, $val);
863
            } else {
864
                return false;
865
866
867
            }
        }, $value);

868
        $this->appendOrNot('supannTypeEntiteAffectation', array_filter($value), $append);
869
870
871
872

        return $this;
    }

873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
    /**
     * Attribut Ldap "ucbnSousStructure"
     *
     * @param array|string|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setUcbnSousStructure($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
            return preg_match(self::$structure_pattern, $v);

        });

        $this->appendOrNot('ucbnSousStructure', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "ucbnStructureRecherche"
     *
     * @param array|string|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setUcbnStructureRecherche($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
            return preg_match(self::$structure_pattern, $v);

        });

        $this->appendOrNot('ucbnStructureRecherche', $value, $append);

        return $this;
    }

917
918
919
920
921
922
923
924
925
926
927
928
    /**
     * Attribut Ldap "eduPersonPrincipalName"
     *
     * @param array|string|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setEduPersonPrincipalName($value = null, $append = false)
    {
        $value = $this->preFormat($value);
929
930
931
        $value = array_map(function ($v) {
            list($identifiant, $domaine) = array_pad(explode('@', strtolower($v)), 2, null);
            if ($domaine != self::$etablissement_domaine) {
932
933
934
935
936
                $v = sprintf('%s@%s', $v, self::$etablissement_domaine);
            }
            return $v;
        }, $value);
        $value = array_filter($value, function ($v) {
937
            return preg_match('/^[0-9a-z\-]+@' . self::$etablissement_domaine . '$/', $v);
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
        });

        $this->appendOrNot('eduPersonPrincipalName', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "eduPersonOrgDN"
     *
     * @param array|string|Dn|Structure|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setEduPersonOrgDN($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                return Dn::checkDn($val) ? $val : null;
            } elseif ($val instanceof Dn) {
                return $val->toString();
            } elseif ($val instanceof Structure) {
                return $val->getDn();
            } else {
                return null;
            }
        }, $value);

        $this->appendOrNot('eduPersonOrgDN', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "eduPersonOrgUnitDN"
     *
     * @param array|string|Dn|Structure|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setEduPersonOrgUnitDN($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                return Dn::checkDn($val) ? $val : null;
            } elseif ($val instanceof Dn) {
                return $val->toString();
            } elseif ($val instanceof Structure) {
                return $val->getDn();
            } else {
                return null;
            }
        }, $value);

        $this->appendOrNot('eduPersonOrgUnitDN', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "eduPersonPrimaryOrgUnitDN"
     *
     * @param array|string|Dn|Structure|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setEduPersonPrimaryOrgUnitDN($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                return Dn::checkDn($val) ? $val : null;
            } elseif ($val instanceof Dn) {
                return $val->toString();
            } elseif ($val instanceof Structure) {
                return $val->getDn();
            } else {
                return null;
            }
        }, $value);

        $this->appendOrNot('eduPersonPrimaryOrgUnitDN', $value, $append);
1028
1029
1030
1031

        return $this;
    }

1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
    /**
     * Attribut Ldap "supannRoleGenerique"
     *
     * @param array|string|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setSupannRoleGenerique($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $supannLabel = $this->getLabel('SUPANN');
        $value = array_map(function ($val) use ($supannLabel) {
            if (is_string($val)) {
                return preg_match("/^$supannLabel.*$/", $val) ? $val : sprintf('%s%s', $supannLabel, $val);
            } else {
                return false;
            }
        }, $value);

        $this->appendOrNot('supannRoleGenerique', array_filter($value), $append);

        return $this;
    }

    /**
     * Attribut Ldap "supannRoleEntite"
     *
     * @param array|string|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setSupannRoleEntite($value = null, $append = false)
    {
        $value = $this->preFormat($value);

        $supannLabel = $this->getLabel('SUPANN');
        $value = array_map(function ($v) use ($supannLabel) {
1073
            if (preg_match(self::$role_pattern, $v, $matches)) {
1074
1075
1076
1077
1078
                /**
                 * @var string $role
                 * @var string $type
                 * @var string $code
                 */
1079
                foreach (['role', 'type'] as $part) {
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
                    $$part = preg_match("/^$supannLabel.*$/", $matches[$part])
                        ? $matches[$part]
                        : sprintf('%s%s', $supannLabel, $matches[$part]);
                }

                $prefixe = $this->getService()->getCodeStructurePrefixe();
                $code = (0 !== strpos($matches['code'], $prefixe))
                    ? sprintf('%s%s', $prefixe, $matches['code'])
                    : $matches['code'];
                return preg_replace(self::$role_pattern, "[role=$role][type=$type][code=$code][libelle=\\4]", $v, 1);
1090
            } else {
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
                return false;
            }

        }, $value);

        $this->appendOrNot('supannRoleEntite', array_filter($value), $append);

        return $this;
    }

    /**
1102
     * Attribut Ldap "ucbnFonctionStructurelle"
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
     *
     * @param array|string|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setUcbnFonctionStructurelle($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
            return preg_match(self::$role_src_pattern, $v);

        });

        $this->appendOrNot('ucbnFonctionStructurelle', $value, $append);

        return $this;
    }

1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
    /**
     * Attribut Ldap "supannParrainDN"
     *
     * @param array|string|Dn|Structure|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setSupannParrainDN($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                return Dn::checkDn($val) ? $val : null;
            } elseif ($val instanceof Dn) {
                return $val->toString();
            } elseif ($val instanceof \UnicaenLdap\Entity\People) {
                return $val->getDn();
            } else {
                return null;
            }
        }, $value);

        $this->appendOrNot('supannParrainDN', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "supannRefId"
     *
     * @param array|string|null $value
     * @param bool $append
     * @return self
     * @throws Exception
     * @throws LdapException
     */
    public function setSupannRefId($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
            return preg_match(self::$refid_pattern, $v);
        });

        $this->appendOrNot('supannRefId', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "ucbnStatus"
     *
     * @param array|string|null $value
     * @param bool $append
     * @return $this
     * @throws Exception
     * @throws LdapException
     */
    public function setUcbnStatus($value = null, $append = false)
    {
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
            return in_array($v, $this->ucbnStatusAuthorizedValues);
        });

        $this->appendOrNot('ucbnStatus', $value, $append);

        return $this;
    }

    /**
     * Attribut Ldap "unicaenTermsOfUse"
     *
     * @param array|string|null $value
     * @param bool $append
     * @return $this
     * @throws Exception
     * @throws LdapException
     */
    public function setUnicaenTermsOfUse($value = null, $append = false)
    {
        $value = $this->preFormat($value);

        $this->appendOrNot('unicaenTermsOfUse', $value, $append);

        return $this;
    }

1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
    /**
     * Retourne les structures auxquelles appartiennent la personne
     *
     * @return Structure[]
     */
    public function getEduPersonOrgUnit()
    {
        $structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
        $dn = $this->eduPersonOrgUnitDN;
        if (empty($dn)) return null;

        return $structure->getAllBy($dn, 'dn');
    }

    /**
     * Retourne la structure principale à laquelle appartient la personne
     *
     * @return Structure
     */
    public function getEduPersonPrimaryOrgUnit()
    {
        $structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
        $dn = $this->eduPersonPrimaryOrgUnitDN;
        if (empty($dn)) return null;

        return $structure->getBy($dn, 'dn');
    }

    /**
     * Retourne la structure d'affectation de la personne
     *
     * @todo à terminer
     * @return Structure[]
     * @throws \Exception
     */
    public function getSupannEntiteAffectation()
    {
        throw new \Exception('Méthode pas finie');
        $structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');
        $codes = $this->getNode()->getAttribute('supannEntiteAffectation');
        var_dump($codes);

        return $structure->getBy($dn, 'dn');
    }

    /**
     * Retourne la structure d'affectation de la personne
     *
     * @todo à terminer
     * @return Structure
     * @throws \Exception
     */
    public function getSupannEntiteAffectationPrincipale()
    {
        throw new \Exception('Méthode pas finie');
        $structure = $this->getService()->getServiceLocator()->get('ldapServiceStructure');

        $codes = [];
        $affectations = $this->getNode()->getAttribute('supannAffectation');


        list($code, $description) = explode(';', $this->supannAffectation);
        $code = $this->supannAffectation;
        if (empty($dn)) return null;

        return $structure->getBy($dn, 'dn');
    }
}