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

  Copyright (C) 2017-2019  FusionDirectory
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

  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.
*/

class sinapsUser extends simplePlugin
{
  static function plInfo()
  {
    return array(
      'plShortName'     => _('Sinaps'),
      'plTitle'         => _('Sinaps'),
      'plDescription'   => _('Used to send acquisition request for users'),
      'plSelfModify'    => FALSE,
      'plObjectClass'   => array(),
      'plObjectType'    => array('user'),
33
34
      'plIcon'        => 'geticon.php?context=applications&icon=sinaps&size=48',
      'plSmallIcon'   => 'geticon.php?context=applications&icon=sinaps&size=16',
35

36
      'plProvidedAcls'  => parent::generatePlProvidedAcls(static::getAttributesInfo())
37
38
39
40
41
    );
  }

  static function getAttributesInfo ()
  {
Côme Chilliet's avatar
Côme Chilliet committed
42
43
44
45
46
47
48
49
50
51
    return array(
      'main' => array(
        'name'  => _('Sinaps'),
        'class' => array('fullwidth'),
        'attrs' => array(
          new DisplayAttribute (
            '', '',
            'sinaps_explanation', FALSE,
            _('This tab takes care of sending Acquisition event to Sinaps whenever a user is modified.')
          ),
52
53
54
55
          new BooleanAttribute(
            _('Force Sinaps Acquisition'), _('Send Acquisition data even if data was not modified in FusionDirectory'),
            'force_acquisition', FALSE
          ),
Côme Chilliet's avatar
Côme Chilliet committed
56
57
58
        )
      )
    );
59
60
  }

61
62
63
64
65
66
67
68
  function __construct ($dn = NULL, $object = NULL, $parent = NULL, $mainTab = FALSE, $attributesInfo = NULL)
  {
    parent::__construct($dn, $object, $parent, $mainTab, $attributesInfo);

    $this->attributesAccess['force_acquisition']->setInLdap(FALSE);
  }

  function codeEntiteToldapUuidCallback ($codeEntite)
69
70
71
72
73
74
75
  {
    global $config;

    /* Translate code entite to UUID refs
     * This is done here to make sure sinapsRequestAcquisition does not perform LDAP searches by itself
     * */
    $entites = objects::ls(
76
77
      array('entite','etablissement'),
      array('supannRefId' => '*', 'dn' => 'raw'),
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
      NULL,
      '(supannCodeEntite='.$codeEntite.')'
    );
    if (empty($entites)) {
      $error = 'Could not find entity '.$codeEntite;
    } elseif (count($entites) > 1) {
      $error = 'Multiple entite matches codeEntite '.$codeEntite;
    } else {
      $entite = reset($entites);
      foreach ($entite['supannRefId'] as $supannRefId) {
        if (preg_match('/^{'.preg_quote($config->get_cfg_value('SinapsUuidPrefix', 'LDAPUUID'), '/').'}(.+)$/', $supannRefId, $m)) {
          return $m[1];
        }
      }
      $error = 'Could not find any UUID for '.$entite['dn'];
    }

    throw new FusionDirectoryException($error);
  }

  protected function prepare_save ()
  {
    global $config;

    /* We have to work in prepare_save as the rest won’t get called since there is nothing to save */

    $errors = parent::prepare_save();
    if (!empty($errors)) {
      return $errors;
    }

    if ($config->get_cfg_value('SinapsEnabled', 'FALSE') == 'FALSE') {
      return array();
    }
112

113
114
115
116
    if ($config->get_cfg_value('SinapsAcquisitionURL', '') == '') {
      return array('Sinaps acquisition URL is not filled in FusionDirectory configuration');
    }

117
    $contractEnded = FALSE;
Côme Chilliet's avatar
Côme Chilliet committed
118
    if (isset($this->parent->by_object['personalInfo']) && ($this->parent->by_object['personalInfo']->attributesAccess['fdContractEndDate']->getValue() != '')) {
119
120
121
122
123
124
125
126
127
128
129
130
      $fdContractEndDate  = $this->parent->by_object['personalInfo']->attributesAccess['fdContractEndDate']->getDateValue();
      $tomorrow           = new DateTime('tomorrow');
      if ($fdContractEndDate < $tomorrow) {
        if ($this->parent->by_object['personalInfo']->attributesAccess['fdContractEndDate']->hasChanged()) {
          $contractEnded = TRUE;
        } else {
          /* Do not sync already deleted users */
          return;
        }
      }
    }

131
    $attributes = sinapsRequestAcquisition::$attributes;
132
133
134
135
136
137
138
139
140

    $mapping = array();
    foreach ($config->get_cfg_value('SinapsAcquisitionContactMethodMap', array()) as $field) {
      list($ldapField, $sinapsType) = explode('|', $field, 2);

      $mapping[$ldapField]    = $sinapsType;
      $attributes[$ldapField] = 1;
    }

141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
    if (!$this->force_acquisition) {
      /* Skip sync if no field changed */
      $sync = FALSE;
      foreach (array_keys($attributes) as $ldapField) {
        foreach ($this->parent->by_object as $class => $tab) {
          if (isset($tab->attributesAccess[$ldapField])) {
            if ($tab->attributesAccess[$ldapField]->hasChanged()) {
              $sync = TRUE;
              break 2;
            }
            break;
          }
        }
      }
      if (!$sync) {
        return array();
      }
    }

160
161
    $attributes['dn'] = 'raw';

162
163
164
    $users  = objects::ls('user', $attributes, $this->dn, '', FALSE, 'base');
    $user   = reset($users);

165
166
167
168
169
    if (!is_array($user)) {
      return;
    }

    try {
170
      $request = new sinapsRequestAcquisition();
171
      $request->fill($user, $config->get_cfg_value('SinapsUuidPrefix', 'LDAPUUID'), $config->get_cfg_value('SinapsAcquisitionTypeExterne', 'FD'), $mapping, array($this, 'codeEntiteToldapUuidCallback'), $contractEnded);
172
173
174
      $xml = $request->getXml();
      $date = date('Y-m-d_H-i-s');
      sinapsDiffusionHandlerJob::dumpFile($date.'_acquittement.xml', $xml);
175

176
177
178
179
180
      if ($config->get_cfg_value('SinapsDryRun') != 'TRUE') {
        $answer = sinapsDiffusionHandlerJob::sendPostRequest($config->get_cfg_value('SinapsAcquisitionURL'), $config->get_cfg_value('SinapsLogin', 'fusiondirectory'), $config->get_cfg_value('SinapsPassword'), $xml);
        sinapsDiffusionHandlerJob::dumpFile($date.'_acquittement-answer.xml', $answer);
      }
    } catch (FusionDirectoryException $e) {
181
182
183
    }
  }
}