class_supannAccountStatus.inc 9.93 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org)

  Copyright (C) 2018-2019 FusionDirectory

  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.

  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.

  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
class supannAccountStatus extends simplePlugin implements UserTabLockingAction
23
24
25
26
27
28
29
30
31
32
33
34
{
  protected $displayHeader = TRUE;

  protected $substatus;

  static function plInfo (): array
  {
    return [
      'plShortName'   => _('SupAnn status'),
      'plDescription' => _('SupAnn status management'),
      'plFilter'      => '(supannRessourceEtatDate=*)',
      'plIcon'        => 'geticon.php?context=applications&icon=supann&size=48',
35
      'plSmallIcon'   => 'geticon.php?context=applications&icon=supann-status&size=16',
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
      'plSelfModify'  => FALSE,
      'plPriority'    => 15,
      'plObjectClass' => [],
      'plObjectType'  => ['user'],
      'plDepends'     => ['supannAccount'],

      'plProvidedAcls'  => parent::generatePlProvidedAcls(static::getAttributesInfo())
    ];
  }

  // The main function : information about attributes
  static function getAttributesInfo (): array
  {
    return [
      'status' => [
        'name'  => _('Status'),
52
        'class' => ['fullwidth'],
53
54
        'attrs' => [
          new OrderedArrayAttribute(
55
            new CompositeAttribute(
56
57
58
59
60
61
              _('State of the account and associated resources'),
              'supannRessourceEtatDate',
              [
                new SelectAttribute(
                  _('Resource'), _('Which resource this state concerns'),
                  'supannRessourceEtatDate_label', TRUE,
62
                  ['COMPTE','MAIL'], '',
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
                  [_('Account'), _('Mail')]
                ),
                new SelectAttribute(
                  _('Status'), _('Active status'),
                  'supannRessourceEtatDate_status', TRUE,
                  ['A','I','S'], 'A',
                  [_('Active'), _('Inactive'), _('Suspended')]
                ),
                new SelectAttribute(
                  _('Substatus'), _('Substatus'),
                  'supannRessourceEtatDate_substatus', FALSE,
                  []
                ),
                new DateAttribute(
                  _('Start date'), _('Date this status started'),
                  'supannRessourceEtatDate_start', FALSE,
                  'Ymd', '',
80
                  NULL, 'today'
81
82
83
84
85
                ),
                new DateAttribute(
                  _('End date'), _('Date this status will end'),
                  'supannRessourceEtatDate_end', FALSE,
                  'Ymd', '',
86
                  'today', NULL
87
88
                ),
              ],
89
90
              '/^\{([^}]+)\}([^:]+):([^:]*):([^:]*):([^:]*)$/',
              '{%s}%s:%s:%s:%s',
91
92
93
94
95
96
              '',
              ''
            ),
            FALSE,
            [],
            TRUE
97
98
          ),
          new HiddenArrayAttribute('supannRessourceEtat', FALSE, []),
99
100
101
102
103
104
105
106
107
108
109
        ]
      ],
    ];
  }

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

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

110
    $this->substatus = static::getConfiguredSubstates();
111

112
113
114
115
116
117
118
119
120
121
    $resources = ['COMPTE' => _('Account'),'MAIL' => _('Mail')];
    foreach ($config->get_cfg_value('SupannRessourceLabels', []) as $line) {
      list($resource,$label) = explode(':', $line, 2);
      $resources[$resource] = $label;
    }
    $this->attributesAccess['supannRessourceEtatDate']->attribute->attributes[0]->setChoices(
      array_keys($resources),
      array_values($resources)
    );

122
123
124
125
126
127
128
129
130
131
132
133
    $this->attributesAccess['supannRessourceEtatDate']->setLinearRendering(FALSE);
    $this->attributesAccess['supannRessourceEtatDate']->setRequired(TRUE);
    $this->attributesAccess['supannRessourceEtatDate']->setHeaders([
      _('Resource'),
      _('Status'),
      _('Substatus'),
      _('Begin'),
      _('End'),
      ''
    ]);
    $this->attributesAccess['supannRessourceEtatDate']->attribute->attributes[1]->setSubmitForm('updateFieldsChoices');
    $this->updateFieldsChoices();
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

    /* Import from supannRessourceEtat if needed */
    if (!empty($this->supannRessourceEtat) && empty($this->supannRessourceEtatDate)) {
      $this->supannRessourceEtatDate = array_map(
        function ($supannRessourceEtat)
        {
          if (strpos($supannRessourceEtat, ':') === FALSE) {
            return $supannRessourceEtat.':::';
          } else {
            return $supannRessourceEtat.'::';
          }
        },
        $this->supannRessourceEtat
      );
    }
149
150
151
152
153
  }

  /* Update choices of fields which depends on other fields values */
  function updateFieldsChoices ()
  {
154
    $substatusChoices = $this->substatus[$this->attributesAccess['supannRessourceEtatDate']->attribute->attributes[1]->getValue()] ?? [];
155
    $this->attributesAccess['supannRessourceEtatDate']->attribute->attributes[2]->setChoices(
156
157
      array_keys($substatusChoices),
      array_values($substatusChoices)
158
159
    );
  }
160
161
162
163
164
165
166

  protected function prepare_save (): array
  {
    /* Copy active states over to supannRessourceEtat */
    $this->supannRessourceEtat = array_map(
      function ($supannRessourceEtatDate)
      {
167
        list($labelstate,$substate) = explode(':', $supannRessourceEtatDate);
168
        if (empty($substate)) {
169
          return $labelstate;
170
        } else {
171
          return $labelstate.':'.$substate;
172
173
174
175
176
177
        }
      },
      $this->supannRessourceEtatDate
    );
    return parent::prepare_save();
  }
178
179
180
181
182

  function check (): array
  {
    $errors = parent::check();

183
    $resources = [];
184
    foreach ($this->supannRessourceEtatDate as $line) {
185
186
187
      $resource = preg_replace('/^\{([^}]+)\}.*$/', '\1',  $line);
      if (in_array($resource, $resources)) {
        $errors[] = sprintf(_('Resource "%s" is present more than once in supannRessourceEtatDate'), $resource);
188
      } else {
189
        $resources[] = $resource;
190
191
192
193
194
      }
    }

    return $errors;
  }
195

196
197
198
  protected function post_save ()
  {
    foreach ($this->supannRessourceEtatDate as $line) {
199
      list ($label, $state, $substate, $start, $end) = $this->attributesAccess['supannRessourceEtatDate']->attribute->readValues($line);
200
201
202
203
      if ($label == 'COMPTE') {
        if ($state == 'A') {
          /* Unlock account */
          userManagement::lockUser('unlock', $this->parent->getBaseObject()->userPassword, $this->dn);
204
        } elseif (($state == 'S') || ($state == 'I')) {
205
206
207
208
209
210
211
212
213
          /* Lock account */
          userManagement::lockUser('lock', $this->parent->getBaseObject()->userPassword, $this->dn);
        }
        break;
      }
    }
    parent::post_save();
  }

214
215
216
217
218
219
220
221
  public function fillLockingLDAPAttrs (string $mode, array &$modify)
  {
    if (empty($this->supannRessourceEtatDate)) {
      return;
    }
    $modify['supannRessourceEtatDate']  = [];
    $modify['supannRessourceEtat']      = [];
    foreach ($this->supannRessourceEtatDate as $line) {
222
      list ($label, $state, $substate, $start, $end) = $this->attributesAccess['supannRessourceEtatDate']->attribute->readValues($line);
223
224
225
226
      if (($label == 'COMPTE') && ($state == 'A') && ($mode == 'LOCK')) {
        /* Lock account */
        $modify['supannRessourceEtatDate'][]  = '{'.$label.'}S:SupannVerrouille:'.date('Ymd').':';
        $modify['supannRessourceEtat'][]      = '{'.$label.'}S:SupannVerrouille';
227
      } elseif (($label == 'COMPTE') && (($state == 'S') || ($state == 'I')) && ($mode == 'UNLOCK')) {
228
229
230
231
232
233
234
235
236
237
238
        /* Unlock account */
        $modify['supannRessourceEtatDate'][]  = '{'.$label.'}A:SupannActif:'.date('Ymd').':';
        $modify['supannRessourceEtat'][]      = '{'.$label.'}A:SupannActif';
      } else {
        /* Do not touch other fields */
        $modify['supannRessourceEtatDate'][]  = $line;
        $modify['supannRessourceEtat'][]      = '{'.$label.'}'.$state.(empty($substate) ? '' : ':'.$substate);
      }
    }
  }

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
  static function getOfficialSubstates (): array
  {
    return [
      'A' => [
        'SupannAnticipe'  => _('Anticipated'),
        'SupannActif'     => _('Active'),
        'SupannSursis'    => _('Extension'),
      ],
      'I' => [
        'SupannPrecree'       => _('Precreated'),
        'SupannCree'          => _('Created'),
        'SupannExpire'        => _('Expired'),
        'SupannInactif'       => _('Inactive'),
        'SupannSupprDonnees'  => _('Data deletion'),
        'SupannSupprCompte'   => _('Account deletion'),
      ],
      'S' => [
        'SupannVerrouille'          => _('Locked'),
        'SupannVerrouAdministratif' => _('Administrative lock'),
        'SupannVerrouTechnique'     => _('Technical lock'),
      ],
    ];
  }

  static function getConfiguredSubstates (): array
  {
    global $config;

    $officialSubstates = static::getOfficialSubstates();

    $substates = [
      'A' => [],
      'I' => [],
      'S' => [],
    ];

    $labels = [];

    foreach ($config->get_cfg_value('SupannRessourceSubStatesLabels', []) as $line) {
      list($substate,$label) = explode(':', $line, 2);
      $labels[$substate] = $label;
    }

    foreach ($config->get_cfg_value('SupannRessourceSubStates', []) as $line) {
      list(,$state,$substate) = explode(':', $line, 3);
      if (isset($officialSubstates[$state][$substate])) {
        $substates[$state][$substate] = $officialSubstates[$state][$substate];
      } elseif (isset($labels[$substate])) {
        $substates[$state][$substate] = $labels[$substate];
      } else {
        $substates[$state][$substate] = $substate;
      }
    }

    return $substates;
  }
295
}