People.php 20.9 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
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
 */
class People extends Entity
{
    protected $type = 'People';

    /**
     * Liste des classes d'objet nécessaires à la création d'une personne
     * 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',
        'sambaAccount',
        'sambaSamAcount',
        'posixAccount',
    ];

    /**
     * Liste des attributs contenant des dates
     *
     * @var array
     */
    protected $dateTimeAttributes = [];

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
    /**
     * Liste des attributs monovalués
     *
     * @var array
     */
    protected $monoValuedAttributes = [
        'cn',
        'dateDeNaissance',
        'displayName',
        'eduPersonOrgDN',
        'eduPersonPrimaryOrgUnitDN',
        'eduPersonPrincipalName',
        'givenName',
        'leoCode',
        'preferredLanguage',
        'schacDateOfBirth',
        'sexe',
        'supannAliasLogin',
        'supannCivilite',
        'supannCodeINE',
        'supannEmpId',
        'supannEntiteAffectationPrincipale',
        'supannEtuId',
        'supannListeRouge',
        'telephoneNumber',
    ];

    static protected $role_pattern = '/^\[role={SUPANN}(.*)\]\[type={SUPANN}(.*)\]\[code=(.*)\]\[libelle=(.*)\]$/';

76
77

    /**
78
79
     * Ajoute ou affecte une valeur à un attribut Ldap
     *
80
81
82
     * @param string $attrName
     * @param mixed $value
     * @param bool $append
83
84
     * @throws Exception
     * @throws LdapException
85
     */
86
    private function appendOrNot($attrName, $value, $append)
87
88
89
90
91
92
    {
        (!$append)
            ? $this->set($attrName, $value)
            : $this->add($attrName, $value);
    }

93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
    /**
     * Vérifie une valeur et supprime les valeurs nulles
     *
     * @param mixed $value
     * @return array
     */
    private function preFormat($value)
    {
        if (is_scalar($value)) {
            $value = [$value];
        }

        $value = array_filter($value);

        return $value;
    }

110
111
112
113
114
115
116
117
    /**
     * 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é
     *
118
     * @param array|string|null $value
119
120
     * @param bool $append
     * @return self
121
     * @throws Exception
122
123
     * @throws LdapException
     */
124
    public function setSn($value = null, $append = false)
125
    {
126
127
128
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatName'], $value);
        $this->appendOrNot('sn', $value, $append);
129
130
131
132
133
134
135

        return $this;
    }

    /**
     * Attribut Ldap "givenName"
     *
136
     * @param array|string|null $value
137
138
     * @param bool $append
     * @return self
139
     * @throws Exception
140
141
     * @throws LdapException
     */
142
    public function setGivenName($value = null, $append = false)
143
    {
144
145
146
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatName'], $value);
        $this->appendOrNot('givenName', $value, $append);
147
148
149
150
151
152
153

        return $this;
    }

    /**
     * Attribut Ldap "displayName"
     *
154
     * @param array|string|null $value
155
156
     * @param bool $append
     * @return self
157
     * @throws Exception
158
159
     * @throws LdapException
     */
160
    public function setDisplayName($value = null, $append = false)
161
    {
162
163
164
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatName'], $value);
        $this->appendOrNot('displayName', $value, $append);
165
166
167
168
169
170
171

        return $this;
    }

    /**
     * Attribut Ldap "cn"
     *
172
     * @param array|string|null $value
173
174
     * @param bool $append
     * @return self
175
     * @throws Exception
176
177
     * @throws LdapException
     */
178
    public function setCn($value = null, $append = false)
179
    {
180
181
182
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatName'], $value, [true]);
        $this->appendOrNot('cn', $value, $append);
183
184
185
186
187
188
189

        return $this;
    }

    /**
     * Attribut Ldap "dateDeNaissance"
     *
190
     * @param array|string|null $value
191
192
     * @param bool $append
     * @return self
193
     * @throws Exception
194
195
     * @throws LdapException
     */
196
    public function setDateDeNaissance($value = null, $append = false)
197
    {
198
199
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
200
201
202
203
            if (is_string($val)) {
                $val = new DateTime($val);
            }
            return $val->format('Ymd');
204
        }, $value);
205

206
        $this->appendOrNot('dateDeNaissance', $value, $append);
207
208
209
210
211
212
213

        return $this;
    }

    /**
     * Attribut Ldap "schacDateOfBirth"
     *
214
     * @param array|string|null $value
215
216
     * @param bool $append
     * @return self
217
     * @throws Exception
218
219
     * @throws LdapException
     */
220
    public function setSchacDateOfBirth($value = null, $append = false)
221
    {
222
223
        $value = $this->preFormat($value);
        $value = array_map(function ($val) {
224
225
226
227
            if (is_string($val)) {
                $val = new DateTime($val);
            }
            return $val->format('Ymd');
228
        }, $value);
229

230
        $this->appendOrNot('schacDateOfBirth', $value, $append);
231
232
233
234
235
236
237

        return $this;
    }

    /**
     * Attribut Ldap "sexe"
     *
238
     * @param array|string|null $value
239
240
     * @param bool $append
     * @return self
241
     * @throws Exception
242
243
     * @throws LdapException
     */
244
    public function setSexe($value = null, $append = false)
245
    {
246
247
248
249
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
            return in_array($v, ['F', 'M']);
        });
250

251
        $this->appendOrNot('sexe', $value, $append);
252

253
        return $this;
254
255
256
257
258
    }

    /**
     * Attribut Ldap "postalAddress"
     *
259
     * @param array|string|null $value
260
261
     * @param bool $append
     * @return self
262
     * @throws Exception
263
264
     * @throws LdapException
     */
265
    public function setPostalAddress($value = null, $append = false)
266
    {
267
268
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
269
270
271
            return preg_match('/.*\$.*\$.*\$.*\$.*\$.*/', $v);
        });

272
        $this->appendOrNot('postalAddress', $value, $append);
273
274
275
276
277
278
279

        return $this;
    }

    /**
     * Attribut Ldap "telephoneNumber"
     *
280
     * @param array|string|null $value
281
282
     * @param bool $append
     * @return self
283
     * @throws Exception
284
285
     * @throws LdapException
     */
286
    public function setTelephoneNumber($value = null, $append = false)
287
    {
288
289
290
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatTel'], $value);
        $this->appendOrNot('telephoneNumber', $value, $append);
291
292
293
294
295
296
297

        return $this;
    }

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

        return $this;
    }

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

        return $this;
    }

    /**
     * Attribut Ldap "preferredLanguage"
     *
334
     * @param array|string|null $value
335
336
     * @param bool $append
     * @return self
337
     * @throws Exception
338
339
     * @throws LdapException
     */
340
    public function setPreferredLanguage($value = null, $append = false)
341
    {
342
343
344
        $value = $this->preFormat($value);
        $value = array_map('strtolower', $value);
        $this->appendOrNot('preferredLanguage', $value, $append);
345
346
347
348
349
350
351

        return $this;
    }

    /**
     * Attribut Ldap "leoCode"
     *
352
     * @param array|string|null $value
353
354
     * @param bool $append
     * @return self
355
     * @throws Exception
356
357
     * @throws LdapException
     */
358
    public function setLeoCode($value = null, $append = false)
359
    {
360
361
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
362
363
364
            return preg_match('/^\d+$/', $v);
        });

365
        $this->appendOrNot('leoCode', $value, $append);
366
367
368
369
370
371
372

        return $this;
    }

    /**
     * Attribut Ldap "supannALiasLogin"
     *
373
     * @param array|string|null $value
374
375
     * @param bool $append
     * @return self
376
     * @throws Exception
377
378
     * @throws LdapException
     */
379
    public function setSupannAliasLogin($value = null, $append = false)
380
    {
381
382
383
        $value = $this->preFormat($value);
        $value = array_map('strtolower', $value);
        $value = array_filter($value, function ($v) {
384
385
386
            return preg_match('/^[0-9a-z\-]+$/', $v);
        });

387
        $this->appendOrNot('supannAliasLogin', $value, $append);
388
389
390
391
392
393
394

        return $this;
    }

    /**
     * Attribut Ldap "supannEmpId"
     *
395
     * @param array|string|null $value
396
397
     * @param bool $append
     * @return self
398
     * @throws Exception
399
400
     * @throws LdapException
     */
401
    public function setSupannEmpId($value = null, $append = false)
402
    {
403
        $value = $this->preFormat($value);
404

405
406
407
        $this->appendOrNot('supannEmpId', $value, $append);

        return $this;
408
409
410
411
412
    }

    /**
     * Attribut Ldap "supannEtuId"
     *
413
     * @param array|string|null $value
414
415
     * @param bool $append
     * @return self
416
     * @throws Exception
417
418
     * @throws LdapException
     */
419
    public function setSupannEtuId($value = null, $append = false)
420
    {
421
        $value = $this->preFormat($value);
422

423
424
425
        $this->appendOrNot('supannEtuId', $value, $append);

        return $this;
426
427
428
429
430
    }

    /**
     * Attribut Ldap "supannCodeINE"
     *
431
     * @param array|string|null $value
432
433
     * @param bool $append
     * @return self
434
     * @throws Exception
435
436
     * @throws LdapException
     */
437
    public function setSupannCodeINE($value = null, $append = false)
438
    {
439
440
441
        $value = $this->preFormat($value);
        $value = array_map('strtoupper', $value);
        $value = array_filter($value, function ($v) {
442
443
444
            return preg_match('/^[0-9]{10}[A-Z]{1}$/', $v);
        });

445
        $this->appendOrNot('supannCodeINE', $value, $append);
446
447
448
449
450
451
452

        return $this;
    }

    /**
     * Attribut Ldap "supannCivilite"
     *
453
     * @param array|string|null $value
454
455
     * @param bool $append
     * @return self
456
     * @throws Exception
457
458
     * @throws LdapException
     */
459
    public function setSupannCivilite($value = null, $append = false)
460
    {
461
462
463
464
        $value = $this->preFormat($value);
        $value = array_filter($value, function ($v) {
            return in_array($v, ['Mme', 'M.']);
        });
465

466
        $this->appendOrNot('supannCivilite', $value, $append);
467

468
        return $this;
469
470
471
472
473
    }

    /**
     * Attribut Ldap "supannListeRouge"
     *
474
     * @param array|string|null $value
475
476
     * @param bool $append
     * @return self
477
     * @throws Exception
478
479
     * @throws LdapException
     */
480
    public function setSupannListeRouge($value = null, $append = false)
481
    {
482
483
484
        $value = $this->preFormat($value);
        $value = array_map([$this, 'formatBoolean'], $value);
        $this->appendOrNot('supannListeRouge', $value, $append);
485
486
487
488
489
490
491

        return $this;
    }

    /**
     * Attribut Ldap "supannMailPerso"
     *
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
     * @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
538
539
     * @param bool $append
     * @return self
540
     * @throws Exception
541
542
     * @throws LdapException
     */
543
    public function setSupannEntiteAffectation($value = null, $append = false)
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
        $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);
        $value = array_map(function ($val) {
            if (is_string($val)) {
                return sprintf('%s%s', $this->getLabel('SUPANN'), $val);
            } else {
                return null;
            }
        }, $value);

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

        return $this;
    }

    /**
     * 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);
        $value = array_map(function($v) {
            list($identifiant, $domaine) = explode('@', strtolower($v));
            if($domaine != self::$etablissement_domaine) {
                $v = sprintf('%s@%s', $v, self::$etablissement_domaine);
            }
            return $v;
        }, $value);
        $value = array_filter($value, function ($v) {
            return preg_match('/^[0-9a-z\-]+@'.self::$etablissement_domaine.'$/', $v);
        });

        $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);
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
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
791
792
793
794
795
796
797
798
799
800
801

        return $this;
    }

    /**
     * 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');
    }
}