class_supannAccount.inc 17.6 KB
Newer Older
1
2
<?php
/*
3
  This code is part of FusionDirectory (http://www.fusiondirectory.org)
4

5
  Copyright (C) 2010-2012 Antoine Gallavardin
6
  Copyright (C) 2013-2020 FusionDirectory
7

8
9
10
11
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
12

13
14
15
16
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
17

18
19
20
21
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
22

23
class supannAccount extends simplePlugin
24
{
25
  protected $displayHeader = TRUE;
26

27
28
  protected $entitesCache = [];

29
  static function plInfo (): array
30
  {
31
    return [
32
      'plShortName'   => _('SupAnn'),
33
      'plDescription' => _('SupAnn information management plugin'),
34
      'plFilter'      => '(&(objectClass=eduPerson)(objectClass=supannPerson))',
35
36
      'plIcon'        => 'geticon.php?context=applications&icon=supann&size=48',
      'plSmallIcon'   => 'geticon.php?context=applications&icon=supann&size=16',
37
      'plSelfModify'  => TRUE,
38
      'plPriority'    => 14,
39
40
      'plObjectClass' => ['eduPerson','supannPerson'],
      'plObjectType'  => ['user'],
41

42
      'plProvidedAcls'  => parent::generatePlProvidedAcls(static::getAttributesInfo())
43
    ];
44
45
46
  }

  // The main function : information about attributes
47
  static function getAttributesInfo (): array
48
  {
49
50
    return [
      'identity' => [
51
        'name'  => _('Identity'),
52
        'attrs' => [
53
          new SelectAttribute(
54
            _('Civilite'), _('supannCivilite - Civility for this person'),
55
            'supannCivilite', FALSE,
56
            ['', 'M.', 'Mme', 'Mlle']
57
58
59
60
61
62
          ),
          new StringAttribute(
            _('Alias login'), _('supannAliasLogin - An alias for the login of this user'),
            'supannAliasLogin', FALSE
          ),
          new StringAttribute(
63
            _('eduPersonPrincipalName'), _('eduPersonPrincipalName - A name that looks like <id>@<domain> which is unique for this domain, and has not be assigned to anyone else recently'),
64
            'eduPersonPrincipalName', FALSE
65
66
          ),
          new StringAttribute(
67
            _('Nickname'), _('eduPersonNickname - Can contain a nickname for this user'),
68
            'eduPersonNickname', FALSE
69
          ),
70
71
72
73
74
          new SetAttribute(
            new StringAttribute(
              _('Ref ids'), _('supannRefId - IDs/links for this user on other systems'),
              'supannRefId', FALSE,
              '', '',
Mortier Benoit's avatar
Mortier Benoit committed
75
76
               // Validation regexp: it must have a prefix
              '/^{[^}]+}.+$/'
77
78
            )
          ),
79
80
81
        ]
      ],
      'contact' => [
82
        'name'  => _('Contact'),
83
        'attrs' => [
84
          new SetAttribute(
85
            new PhoneNumberAttribute(
86
87
88
89
              _('Other phone numbers'), _('supannAutreTelephone - Other phone numbers for this user'),
              'supannAutreTelephone', FALSE
            )
          ),
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
          new SetAttribute(
            new CompositeAttribute(
              _('supannTelephonePrive - Private phone numbers for this user'),
              'supannTelephonePrive',
              [
                new SelectAttribute(
                  '', _('Phone number type'),
                  'supannTelephonePrive_prefix', TRUE,
                  ['MOBPERSO','FIXEPERSO','MOBPARENTS','FIXEPARENTS','MOBPRO','FIXEPRO','SECOURS'],
                  '',
                  [_('Personal mobile'),_('Personal landline'),_('Legal representative mobile'),_('Legal representative landline'),_('Professional mobile'),_('Professional landline'),_('Secours')]
                ),
                new PhoneNumberAttribute(
                  '', _('Phone number'),
                  'supannTelephonePrive_number', TRUE
                ),
              ],
              '/^{(.+)}(.*)$/',
              '{%s}%s',
              '',
              _('Private phone numbers')
            )
          ),
113
114
          new SetAttribute(
            new MailAttribute(
115
              _('Other email addresses'), _('supannAutreMail - Other email addresses for this user. Each must be unique'),
116
117
118
119
120
              'supannAutreMail', FALSE
            )
          ),
          new SetAttribute(
            new MailAttribute(
121
              _('Personal email addresses'), _('supannMailPerso - Personal email addresses for this user'),
122
123
124
              'supannMailPerso', FALSE
            )
          ),
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
          new SetAttribute(
            new CompositeAttribute(
              _('supannMailPrive - Private email addresses for this user'),
              'supannMailPrive',
              [
                new SelectAttribute(
                  '', _('Email address type'),
                  'supannMailPrive_prefix', TRUE,
                  ['PERSO','PARENTS','PRO','SECOURS'],
                  '',
                  [_('Personal'),_('Legal representative'),_('Professional'),_('Secours')]
                ),
                new MailAttribute(
                  '', _('Email address'),
                  'supannMailPrive_email', TRUE
                ),
              ],
              '/^{(.+)}(.*)$/',
              '{%s}%s',
              '',
              _('Private email addresses')
            )
          ),
          new SetAttribute(
            new CompositeAttribute(
              _('supannAdressePostalePrivee - Private addresses for this user'),
              'supannAdressePostalePrivee',
              [
                new SelectAttribute(
                  '', _('Address type'),
                  'supannAdressePostalePrivee_prefix', TRUE,
                  ['PERSO','PARENTS','PRO','TEMP'],
                  '',
                  [_('Personal'),_('Legal representative'),_('Professional'),_('Temporary')]
                ),
                new PostalAddressAttribute(
                  '', _('Address'),
                  'supannAdressePostalePrivee_address', TRUE
                ),
              ],
              '/^{(.+)}(.*)$/',
              '{%s}%s',
              '',
              _('Private addresses')
            )
          ),
171
          new BooleanAttribute(
172
            _('Red list'), _('supannListeRouge - Should this person be on the red list'),
173
174
            'supannListeRouge', TRUE
          )
175
176
177
        ]
      ],
      'affectation' => [
178
        'name'  => _('Assignment'),
179
        'attrs' => [
180
          new SelectAttribute(
181
            _('Primary assignment'), _('supannEntiteAffectationPrincipale - Main assignment of the person'),
182
183
184
185
            'supannEntiteAffectationPrincipale', FALSE
          ),
          new SetAttribute(
            new SelectAttribute(
186
              _('Assignments'), _('supannEntiteAffectation - Represents assignments of the person in an institution, a component, service, etc.'),
187
188
189
190
              'supannEntiteAffectation', FALSE
            )
          ),
          new SetAttribute(
191
            new SupannPrefixedSelectAttribute(
192
              _('Entity types'), _('supannTypeEntiteAffectation - Types of the entities this person is assigned to'),
193
              'supannTypeEntiteAffectation', FALSE, 'entite'
194
195
            )
          ),
196
197
198
        ]
      ],
      'affiliation' => [
199
        'name'  => _('Affiliation'),
200
        'attrs' => [
201
          new SelectAttribute(
202
            _('Primary affiliation'), _('eduPersonPrimaryAffiliation - Main status of the person'),
203
204
205
206
            'eduPersonPrimaryAffiliation', FALSE
          ),
          new SetAttribute(
            new SelectAttribute(
207
              _('Affiliations'), _('eduPersonAffiliation - Status of the person: student, BIATOSS, teacher, contract, retired, hosted staff (CNRS, INSERM, etc.), a former student, etc.'),
208
209
210
211
212
              'eduPersonAffiliation', FALSE
            )
          ),
          new SetAttribute(
            new SelectAttribute(
213
              _('Establishment'), _('supannEtablissement - Institution or unit of administrative attachment of the person'),
214
215
216
              'supannEtablissement', FALSE
            )
          ),
217
218
219
220
221
          new ObjectsAttribute(
            _('Parrain'), _('supannParrainDN - Person or structure responsible for this user'),
            'supannParrainDN', FALSE,
            ['user','entite','ogroup','group']
          )
222
223
224
        ]
      ],
      'student' => [
225
        'name'  => _('Student profile'),
226
        'attrs' => [
227
228
229
230
231
          new StringAttribute(
            _('INE code'), _('supannCodeINE - INE code of this student'),
            'supannCodeINE', FALSE
          ),
          new StringAttribute(
232
            _('Student ID'), _('supannEtuId - Scolarity id'),
233
234
            'supannEtuId', FALSE
          ),
235
236
237
        ]
      ],
      'student2' => [
238
        'name'      => _('Student registrations'),
239
        'class'     => ['fullwidth'],
240
        'template'  => get_template_path('student_subscription.tpl', TRUE, dirname(__FILE__)),
241
        'attrs'     => [
242
          /* These attributes are handled by the SupannEtuInscriptionAttribute */
243
244
245
246
247
248
249
250
251
          new HiddenAttribute('supannEtuAnneeInscription'),
          new HiddenAttribute('supannEtuRegimeInscription'),
          new HiddenAttribute('supannEtuSecteurDisciplinaire'),
          new HiddenAttribute('supannEtuTypeDiplome'),
          new HiddenAttribute('supannEtuCursusAnnee'),
          new HiddenAttribute('supannEtuDiplome'),
          new HiddenAttribute('supannEtuEtape'),
          new HiddenAttribute('supannEtuElementPedagogique'),
          new SupannOrderedArrayAttribute(
252
            new SupannEtuInscriptionAttribute(
253
              _('supannEtuInscription - Registrations for this student'),
254
              'supannEtuInscription'
255
            ),
Mortier Benoit's avatar
Mortier Benoit committed
256
257
            // no order
            FALSE,
258
            [],
Mortier Benoit's avatar
Mortier Benoit committed
259
260
            // no edit button
            FALSE
261
          )
262
263
264
        ]
      ],
      'personnal' => [
265
        'name'  => _('Personal profile'),
266
        'attrs' => [
267
          new StringAttribute(
268
            _('Personal ID'), _('supannEmpId - Employee identifier'),
269
270
            'supannEmpId', FALSE
          ),
Côme Chilliet's avatar
Côme Chilliet committed
271
          new SupannPrefixedSelectAttribute(
272
            _('Personal corps'), _('supannEmpCorps'),
Côme Chilliet's avatar
Côme Chilliet committed
273
            'supannEmpCorps', FALSE, 'corps'
274
275
276
          ),
          new SetAttribute(
            new SupannPrefixedSelectAttribute(
277
              _('Activity'), _('supannActivite - Category of profession'),
278
279
280
              'supannActivite', FALSE, 'activite'
            )
          )
281
282
283
        ]
      ],
      'personnal2' => [
284
        'name'      => _('Roles'),
285
286
        'class'     => ['fullwidth'],
        'attrs'     => [
287
288
289
290
291
292
          /* These attributes are handled by the SupannOrderedArrayAttribute */
          new HiddenAttribute('supannRoleGenerique'),
          new SupannOrderedArrayAttribute(
            new SupannCompositeAttribute(
              _('supannRoleEntite'),
              'supannRoleEntite',
293
              [
294
                new SupannPrefixedSelectAttribute(
295
                  _('Generic role'), _('supannRoleGenerique - Generic role of the person in the facility'),
296
                  'supannRoleGenerique_role', TRUE, 'role'
297
298
                ),
                new SelectAttribute(
299
                  _('Entity type'), _('supannTypeEntiteAffectation - type of the assigned entity'),
300
301
302
                  'supannTypeEntiteAffectation_type', TRUE
                ),
                new SelectAttribute(
303
                  _('Entity'), _('supannEntiteAffectation - Represents assignments of the person in an institution, a component, service, etc.'),
304
305
                  'supannEntiteAffectation_code', FALSE
                ),
306
              ],
307
              '',
Mortier Benoit's avatar
Mortier Benoit committed
308
309
              // no label
              ''
310
            ),
Mortier Benoit's avatar
Mortier Benoit committed
311
312
            // no order
            FALSE,
313
            [],
Mortier Benoit's avatar
Mortier Benoit committed
314
315
            // no edit button
            FALSE
316
          )
317
318
319
        ]
      ],
    ];
320
321
  }

322
  function __construct ($dn = NULL, $object = NULL, $parent = NULL, $mainTab = FALSE)
323
  {
324
    global $config;
325

326
    parent::__construct($dn, $object, $parent, $mainTab);
327

328
329
330
331
    $this->attributesAccess['supannRefId']->setUnique('whole');
    $this->attributesAccess['eduPersonPrincipalName']->setUnique('whole');
    $this->attributesAccess['supannAutreMail']->setUnique('whole');

332
    /* list of entity stored in LDAP tree */
333
334
    $ldap = $config->get_ldap_link();
    $ldap->cd($config->current['BASE']);
335
    $ldap->search('(objectClass=supannEntite)', ['supannCodeEntite','supannTypeEntite','ou','o','supannEtablissement','description']);
336

337
338
339
340
    $code_entites   = [];
    $label_entites  = [];
    $code_etablissements  = [];
    $label_etablissements = [];
341
    $this->entitesCache   = [];
342
343
344
    while ($attrs = $ldap->fetch()) {
      if (isset($attrs['supannCodeEntite'][0])) {
        $code_entites[] = $attrs['supannCodeEntite'][0];
345
346
347
348
349
350
351
352
353
        if (isset($attrs['description'][0])) {
          $description = $attrs['description'][0];
          if (mb_strlen($description, 'UTF-8') > 30) {
            $description = mb_substr($description, 0, 27, 'UTF-8').'...';
          }
          $description = ' ('.$description.')';
        } else {
          $description = '';
        }
354
        if (isset($attrs['ou'][0])) {
355
          $label_entites[] = $attrs['ou'][0].$description;
356
        } else {
357
          $label_entites[] = $attrs['o'][0].$description;
358
        }
359
360
361
362
363
        if (isset($attrs['supannEtablissement'])) {
          for ($i = 0; $i < $attrs['supannEtablissement']['count']; $i++) {
            $code_etablissements[]  = $attrs['supannEtablissement'][$i];
            $label_etablissements[] = $attrs['o'][0].' ('.$attrs['supannEtablissement'][$i].')';
          }
364
        }
365
        $this->entitesCache[$attrs['supannCodeEntite'][0]] = $attrs;
366
367
      }
    }
368
369
    array_multisort($label_entites,         $code_entites);
    array_multisort($label_etablissements,  $code_etablissements);
370
371
372

    $this->attributesAccess['supannEntiteAffectation']->attribute->setChoices($code_entites, $label_entites);
    $this->attributesAccess['supannEtablissement']->attribute->setChoices($code_etablissements, $label_etablissements);
373
    $this->attributesAccess['supannEtuInscription']->attribute->attributes[0]->setChoices($code_etablissements, $label_etablissements);
374
375
376
    $this->supannInit();
  }

377
  protected function supannInit ()
378
  {
379
380
    list ($codes, $labels) = supann::get_choices_for('affiliation');
    $this->attributesAccess['eduPersonAffiliation']->attribute->setChoices($codes, $labels);
381

382
383
384
385
386
387
388
389
390
391
392
393
394
    $this->attributesAccess['supannEtuInscription']->setHeaders([
      _('Establishment'),
      _('Year'),
      _('Registration type'),
      _('Sector'),
      _('Diploma type'),
      _('Curriculum'),
      _('Entity'),
      _('Diploma'),
      _('Step'),
      _('Element'),
      ''
    ]);
395
    $this->attributesAccess['supannEtuInscription']->setLinearRendering(FALSE);
396

397
    $this->updateFieldsChoicesAndValues();
398
399
  }

400
  /* Update choices of fields which depends on other fields values */
401
  protected function updateFieldsChoicesAndValues ()
402
  {
403
    global $config;
404
405
    $code_ent   = $this->attributesAccess['supannEntiteAffectation']->getValue();
    $label_ent  = $this->attributesAccess['supannEntiteAffectation']->getDisplayValues();
406
407

    /* Values */
408
409
410
411
412
413
414
    $supannEtablissement = $this->supannEtablissement;
    foreach ($this->supannEtuInscription as $inscription) {
      $values = $this->attributesAccess['supannEtuInscription']->attribute->readValues($inscription);
      $supannEtablissement[] = $values[0];
    }
    $this->supannEtablissement = array_values(array_unique($supannEtablissement));

415
416
417
418
419
420
421
422
423
    $supannTypeEntiteAffectation = $this->supannTypeEntiteAffectation;
    foreach ($code_ent as $code) {
      if (isset($this->entitesCache[$code]['supannTypeEntite'][0])) {
        $supannTypeEntiteAffectation[] = $this->entitesCache[$code]['supannTypeEntite'][0];
      }
    }
    $this->supannTypeEntiteAffectation = array_values(array_unique($supannTypeEntiteAffectation));

    /* Choices */
424
    $this->attributesAccess['supannEntiteAffectationPrincipale']->setChoices(
425
      $code_ent, $label_ent
426
    );
Mortier Benoit's avatar
Mortier Benoit committed
427
428
429
430
    // supannEntiteAffectation
    $this->attributesAccess['supannEtuInscription']->attribute->attributes[6]->setChoices($code_ent, $label_ent);
    // supannEntiteAffectation
    $this->attributesAccess['supannRoleEntite']->attribute->attributes[2]->setChoices($code_ent, $label_ent);
431
432
    $code_tent   = $this->attributesAccess['supannTypeEntiteAffectation']->getValue();
    $label_tent  = $this->attributesAccess['supannTypeEntiteAffectation']->getDisplayValues();
Mortier Benoit's avatar
Mortier Benoit committed
433
434
    // supannTypeEntiteAffectation
    $this->attributesAccess['supannRoleEntite']->attribute->attributes[1]->setChoices($code_tent, $label_tent);
435

436
437
438
439
    $this->attributesAccess['eduPersonPrimaryAffiliation']->setChoices(
      $this->attributesAccess['eduPersonAffiliation']->getValue(),
      $this->attributesAccess['eduPersonAffiliation']->getDisplayValues()
    );
440
441
  }

442
  function check (): array
443
444
445
446
447
448
449
450
451
452
453
  {
    $message = parent::check();

    $affiliations = $this->eduPersonAffiliation;
    if (in_array('member', $affiliations) && in_array('affiliate', $affiliations)) {
      $message[] = _('"member" and "affiliate" values are incompatible for eduPersonAffiliation. Please remove one of them.');
    }

    return $message;
  }

454
  function save_object ()
455
456
  {
    parent::save_object();
457
    $this->updateFieldsChoicesAndValues();
458
  }
459

460
  function adapt_from_template (array $attrs, array $skip = [])
461
462
  {
    parent::adapt_from_template($attrs, $skip);
463
    $this->updateFieldsChoicesAndValues();
464
465
  }

466
  protected function prepare_save (): array
467
  {
468
    $this->attributesAccess['supannEtuInscription']->supannPrepareSave();
469
    $this->attributesAccess['supannRoleEntite']->supannPrepareSave();
470
    return parent::prepare_save();
471
  }
472
}