class_groupManagement.inc 12.7 KB
Newer Older
1
2
3
4
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 2003  Cajus Pollmeier
5
  Copyright (C) 2011-2016  FusionDirectory
6
7
8
9
10
11
12
13
14
15
16
17
18

  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
19
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20
21
*/

22
class groupManagement extends simpleManagement
23
{
24
  protected $objectTypes  = array('ogroup', 'role', 'group');
25
26

  protected $autoFilter = FALSE;
27

28
29
  public static $skipTemplates = TRUE;

30
31
  protected $siActive = FALSE;

32
33
34
  static function plInfo()
  {
    return array(
35
      'plShortName'   => _('Groups and roles'),
36
37
      'plTitle'       => _('Manage groups and roles'),
      'plDescription' => _('Allows you to manage object groups, POSIX groups and roles'),
38
      'plIcon'        => 'geticon.php?context=types&icon=resource-group&size=48',
39
      'plSection'     => 'accounts',
40
      'plManages'     => array('ogroup', 'group', 'role'),
41
      'plPriority'    => 20,
42

43
      'plProvidedAcls' => array()
44
45
46
    );
  }

47
  function __construct()
48
  {
49
50
    $this->filterXMLPath  = get_template_path('group-filter.xml', TRUE, dirname(__FILE__));
    $this->listXMLPath    = get_template_path('group-list.xml', TRUE, dirname(__FILE__));
51

52
53
54
55
56
57
    // Check if we are able to communicate with Argonaut server
    if (class_available('supportDaemon') && class_available('argonautAction')) {
      $o = new supportDaemon();
      $this->siActive = $o->is_available();
    }

58
    parent::__construct();
59

60
    $this->headpage->registerElementFilter('filterProperties', 'groupManagement::filterProperties');
61
62
  }

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
  function configureFilter ()
  {
    parent::configureFilter();

    if (!class_available('posixGroup')) {
      /* If posix plugin is not installed, ignore posix filter */
      $this->filter->elements['PRIMARY']['unset']  = '';
      $this->filter->elements['PRIMARY']['set']    = '';
    }
    if (!class_available('mailAccount') || !class_available('posixGroup')) {
      $this->filter->elements['MAIL']['unset']  = '';
      $this->filter->elements['MAIL']['set']    = '';
    }
    if (!class_available('sambaAccount') || !class_available('posixGroup')) {
      $this->filter->elements['SAMBA']['unset']  = '';
      $this->filter->elements['SAMBA']['set']    = '';
    }
80
81
82
83
    if (static::$skipTemplates) {
      $this->filter->elements['FILTERTEMPLATE']['unset']  = '';
      $this->filter->elements['FILTERTEMPLATE']['set']    = '';
    }
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
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
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
  function configureHeadpage ()
  {
    parent::configureHeadpage();

    // Register Daemon Events
    if ($this->siActive) {
      $this->headpage->xmlData['actionmenu']['action'][4]['action'] = array();
      $this->headpage->xmlData['actionmenu']['action'][5]['action'] = array();
      $events = argonautEventTypes::get_event_types();
      foreach ($events as $name => $data) {
        $this->registerAction('T_'.$name, 'handleEvent');
        $this->headpage->xmlData['actionmenu']['action'][4]['action'][] = array(
        'name' => 'T_'.$name,
        'type' => 'entry',
        'image' => $data['img'],
        'label' => $data['name'],
        );
        $this->registerAction('S_'.$name, 'handleEvent');
        $this->headpage->xmlData['actionmenu']['action'][5]['action'][] = array(
        'name' => 'S_'.$name,
        'type' => 'entry',
        'image' => $data['img'],
        'label' => $data['name'],
        );
      }
    } else {
      unset($this->headpage->xmlData['actionmenu']['action'][3]);
      unset($this->headpage->xmlData['actionmenu']['action'][4]);
      unset($this->headpage->xmlData['actionmenu']['action'][5]);
      $this->headpage->xmlData['actionmenu']['action'] = array_values($this->headpage->xmlData['actionmenu']['action']);
    }
    $this->registerAction('saveEvent', 'saveEventDialog');
  }

  /*! \brief    Handle Argonaut events
   *            All schedules and triggered events are handled here.
   */
  function handleEvent($action, array $target)
  {
    global $config;

    if (!$this->siActive) {
      return;
    }

    // Detect whether this event is scheduled or triggered.
    $triggered = TRUE;
    if (preg_match('/^S_/', $action)) {
      $triggered = FALSE;
    }

    // Detect triggered or scheduled actions
    $headpage = $this->getHeadpage();
    $event = preg_replace('/^[TS]_/', '', $action);

    // Now send FAI/Argonaut events here.
    $mac = array();

    // Collect target mac addresses
    $ldap = $config->get_ldap_link();
    foreach ($target as $dn) {
      $obj = $headpage->getEntry($dn);
      if (isset($obj['member']['count'])) {
        for ($i = 0; $i < $obj['member']['count']; $i++) {
          $ldap->cat($obj['member'][$i]);
          if ($attrs = $ldap->fetch()) {
            if (isset($attrs['macAddress'][0])) {
              $mac[] = $attrs['macAddress'][0];
            } else {
              msg_dialog::display(_('Action canceled'), sprintf(_('System %s has no mac address defined, cannot trigger action'), $obj['member'][$i]), ERROR_DIALOG);
            }
          } else {
            msg_dialog::display(_('Action canceled'), sprintf(_('Could not find system %s, cannot trigger action'), $obj['member'][$i]), ERROR_DIALOG);
          }
        }
      }
    }

    if ((count($mac) == 0) && $triggered) {
      return;
    }

    $o_queue  = new supportDaemon();

    /* Skip installation or update trigerred events,
     *  if this entry is currently processing.
     */
    if ($triggered && in_array($event, array('reinstall','update'))) {
      foreach ($mac as $key => $mac_address) {
        if ($o_queue->is_currently_installing($mac_address)) {
          msg_dialog::display(_('Action canceled'), sprintf(_('System %s is currently installing'), $dn), ERROR_DIALOG);
          unset($mac[$key]);
          logging::log('security', 'systems/'.get_class($this), '', array(), 'Skip adding "argonautAction::'.$event.'" for mac "'.$mac_address.'", there is already a job in progress.');
        }
      }
    }

    if ((count($mac) == 0) && $triggered) {
      return;
    }

    // Prepare event to be added
    $events   = argonautEventTypes::get_event_types();
    if (isset($events[$event])) {
      $this->dialogObject = new argonautAction($event, $mac);

      if ($triggered) {
        $this->dialogObject->scheduled = FALSE;
        $res = $o_queue->append($this->dialogObject);
        if ($o_queue->is_error()) {
          msg_dialog::display(_('Infrastructure service'), msgPool::siError($o_queue->get_error()), ERROR_DIALOG);
        } else {
          msg_dialog::display(_('Action triggered'), sprintf(_('Action called without error (result was "%s")'), $res), INFO_DIALOG);
        }
        $this->closeDialogs();
      } else {
        $this->dialogObject->scheduled = TRUE;
      }
    }
  }

  /*! \brief  Save event dialogs.
   *          And append the new Argonaut event.
   */
  function saveEventDialog()
  {
212
    $this->dialogObject->save_object();
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
    $msgs = $this->dialogObject->check();
    if (count($msgs)) {
      msg_dialog::displayChecks($msgs);
      return;
    }
    if ($this->siActive) {
      $o_queue = new supportDaemon();
      $o_queue->append($this->dialogObject);
      if ($o_queue->is_error()) {
        msg_dialog::display(_('Infrastructure service'), msgPool::siError($o_queue->get_error()), ERROR_DIALOG);
      }
      $this->closeDialogs();
    }
  }

  /*! \brief  Detects actions/events send by the ui
   *           and the corresponding targets.
   */
  function detectPostActions()
  {
    $action = parent::detectPostActions();
    if (isset($_POST['save_event_dialog'])) {
      $action['action'] = 'saveEvent';
    } elseif (isset($_POST['abort_event_dialog'])) {
      $action['action'] = 'cancel';
    }
    return $action;
  }

242
  static function filterProperties($row, $dn, $objectType, $attrs, $gosaGroupObjects = NULL)
243
  {
244
245
246
247
    global $config;
    static $grouptabs = array();

    // Load information if needed
248
    $result = '&nbsp;';
249
    if (!empty($attrs)) {
250
      if (strtolower($objectType) == 'ogroup') {
251
252
253
        if (empty($gosaGroupObjects)) {
          return $result;
        } else {
254
          return static::filterGroupObjects($row, $gosaGroupObjects);
255
256
        }
      }
257
      if (strtolower($objectType) == 'role') {
258
        return '<input class="center" type="image" src="geticon.php?context=types&amp;icon=role&amp;size=16" '.
259
260
                 'alt="'._('Role').'" title="'._('Edit role properties').'" '.
                 'name="listing_edit_'.$row.'" style="padding:1px"/>';
261
      }
262
263
      if (empty($grouptabs)) {
        foreach ($config->data['TABS']['GROUPTABS'] as $plug) {
264
          if ($plug['CLASS'] == 'posixGroup') {
265
266
267
268
269
            continue;
          }
          if (class_available($plug['CLASS'])) {
            $name = $plug['CLASS'];

270
            $grouptabs[$name] = new $name($dn);
271
272
273
274
275
276
          }
        }
      }
      $result = '<input class="center" type="image" src="geticon.php?context=types&amp;icon=user-group&amp;size=16" '.
                       'alt="'._('Posix').'" title="'._('Edit posix properties').'" '.
                       'name="listing_edit_tab_group_'.$row.'" style="padding:1px"/>';
277
278
279
280
      foreach ($grouptabs as $class => $grouptab) {
        if ($grouptab->is_this_account($attrs)) {
          $infos = pluglist::pluginInfos($class);
          if (isset($infos['plSmallIcon'])) {
281
282
283
            $result .= '<input class="center" type="image" src="'.htmlentities($infos['plSmallIcon'], ENT_COMPAT, 'UTF-8').'" '.
                       'alt="'.$infos['plTitle'].'" title="'.$infos['plTitle'].'" '.
                       'name="listing_edit_tab_'.$class.'_'.$row.'" style="padding:1px"/>';
284
          } else {
285
            @DEBUG (DEBUG_TRACE, __LINE__, __FUNCTION__, __FILE__, $infos['plShortName']." ($class)", 'No icon for');
286
287
          }
        } else {
288
          $result .= '<img src="images/empty.png" alt=" " class="center optional '.$class.'" style="padding:1px"/>';
289
290
291
292
        }
      }
    }

293
294
    return $result;
  }
295

296
297
  static function filterGroupObjects($row, $gosaGroupObjects)
  {
298
299
    $types  = preg_replace('/[^a-z]/i', '', $gosaGroupObjects[0]);
    $result = '';
300
301
302
303
304
    for ($i = 0; $i < strlen($types); $i++) {
      if ($types[$i] == 'I') {
        continue;
      }
      $info   = objects::infos(ogroup::$objectTypes[$types[$i]]);
305
      $result .= '<img class="center" src="'.htmlentities($info['icon'], ENT_COMPAT, 'UTF-8').'" '.
306
307
        'alt="'.$info['name'].'" title="'.$info['name'].'" style="padding:1px"/>';
    }
308
    if ($result == '') {
309
310
311
312
313
      return '&nbsp;';
    }
    return $result;
  }

314
315
316
317
  function renderList()
  {
    $smarty = get_smarty();
    $filters = array(
318
319
320
      array('id'  => 'USER',        'label'  => _('Show user groups')),
      array('id'  => 'ROLE',        'label'  => _('Show organizational roles')),
      array('id'  => 'APPLICATION', 'label'  => _('Show application groups')),
321
      array('id'  => 'UNKNOWN',     'label'  => _('Show unidentified groups')),
322
    );
323
324
325
326
327
328
329
330
    if (class_available('posixGroup')) {
      $filters[] = array('id'  => 'PRIMARY',  'label'  => _('Show primary groups'));
      if (class_available('mailAccount')) {
        $filters[] = array('id'  => 'MAIL',  'label'  => _('Show mail groups'));
      }
      if (class_available('sambaAccount')) {
        $filters[] = array('id'  => 'SAMBA', 'label'  => _('Show samba groups'));
      }
331
    }
332
    if (class_available('simpleSecurityObject')) {
333
      $filters[] = array('id'  => 'SIMPLESECURITYOBJECT',  'label'  => _('Show DSA entries'));
334
    }
335
336
337
338
339
340
341
342
343
344
345
346
347
    if (class_available('systemManagement')) {
      $filters = array_merge(
        $filters,
        array(
          array('id'  => 'SERVER',      'label'  => _('Show server groups')),
          array('id'  => 'WORKSTATION', 'label'  => _('Show workstation groups')),
          array('id'  => 'WINDOWS',     'label'  => _('Show windows groups')),
          array('id'  => 'TERMINAL',    'label'  => _('Show terminal groups')),
          array('id'  => 'PRINTER',     'label'  => _('Show printer groups')),
          array('id'  => 'PHONE',       'label'  => _('Show phone groups')),
        )
      );
    }
348
349
350
351
352
353
    if (!static::$skipTemplates) {
      $filters[] = array(
        'id'    => 'FILTERTEMPLATE',
        'label' => sprintf(_('Show %s'), _('Template'))
      );
    }
354
    $smarty->assign('objectFilters', $filters);
355
356
357
    $this->headpage->update();
    $display = $this->headpage->render();
    return $this->getHeader().$display;
358
  }
359
}
360
?>