class_Action.inc 7.52 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
/*
  This code is part of FusionDirectory (http://www.fusiondirectory.org/)
  Copyright (C) 2017-2018  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.
*/

/*!
 * \brief Action base class
 */
class Action
{
  protected $name;
  protected $label;
  protected $icon;

30
  /* 0, 1, ?, + or * */
31
32
  protected $targets;

33
34
35
  /* Object types this action is present on */
  protected $validTypes;

36
37
  protected $acl;

38
39
40
41
42
  /* Booleans */
  protected $inmenu;
  protected $inline;

  protected $callable;
43
  protected $enabledCallable;
44
45
46
47

  protected $minTargets;
  protected $maxTargets;

48
49
  protected $separator = FALSE;

50
51
  protected $parent;

52
  function __construct($name, $label, $icon, $targets, $callable, array $acls = array(), $inmenu = TRUE, $inline = TRUE, array $validTypes = array())
53
54
55
56
57
  {
    if ($targets == '0') {
      $inline = FALSE;
    }

58
59
60
61
62
63
64
65
66
    $this->name       = $name;
    $this->label      = $label;
    $this->icon       = $icon;
    $this->targets    = $targets;
    $this->callable   = $callable;
    $this->inmenu     = $inmenu;
    $this->inline     = $inline;
    $this->validTypes = $validTypes;
    $this->acl        = array();
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
    /*
     * acl may be of the form:
     * acl (ex: 'd')
     * attribute:acl (ex: 'userPassword:r')
     * category/class/acl (ex: 'user/template/r')
     * category/class/attribute:acl (ex: 'user/user/userPassword:r')
     * /class/acl (ex: '/snapshot/c')
     * /class/attribute:acl (ex: '/template/template_cn:w')
     * */
    foreach ($acls as $acl) {
      $category   = NULL;
      $class      = NULL;
      $attribute  = '0';
      if (strpos($acl, '/') !== FALSE) {
        list($category, $class, $acl) = explode('/', $acl, 3);
      }
      if (strpos($acl, ':') !== FALSE) {
        list($attribute, $acl) = explode(':', $acl, 2);
      }
      $this->acl[] = array(
        'category'  => $category,
        'class'     => $class,
        'attribute' => $attribute,
        'acl'       => str_split($acl),
      );
    }
93

94
    switch ($this->targets) {
95
96
97
98
99
100
101
102
      case '0':
        $this->minTargets = 0;
        $this->maxTargets = 0;
        break;
      case '1':
        $this->minTargets = 1;
        $this->maxTargets = 1;
        break;
103
104
105
106
      case '?':
        $this->minTargets = 0;
        $this->maxTargets = 1;
        break;
107
108
109
110
111
112
113
114
115
      case '+':
        $this->minTargets = 1;
        $this->maxTargets = FALSE;
        break;
      case '*':
        $this->minTargets = 0;
        $this->maxTargets = FALSE;
        break;
      default:
116
        throw new FusionDirectoryException('Invalid targets value for action '.$this->name.': '.$this->targets);
117
118
119
    }
  }

120
121
122
123
124
  function setParent(management $parent)
  {
    $this->parent = $parent;
  }

125
126
127
128
129
130
131
132
133
134
  function getName()
  {
    return $this->name;
  }

  function getLabel()
  {
    return $this->label;
  }

135
136
137
138
139
  function setSeparator($bool)
  {
    $this->separator = $bool;
  }

140
141
142
143
144
  function setEnableFunction(callable $callable)
  {
    $this->enabledCallable = $callable;
  }

145
146
147
148
149
150
151
152
153
154
155
  function listActions()
  {
    return array($this->name);
  }

  function execute($management, $action)
  {
    if ($this->callable === FALSE) {
      return;
    }
    if (count($action['targets']) < $this->minTargets) {
156
      throw new FusionDirectoryException(sprintf(_('Not enough targets (%d) passed for action "%s"'), count($action['targets']), $this->name));
157
158
    }
    if (($this->maxTargets !== FALSE) && (count($action['targets']) > $this->maxTargets)) {
159
      throw new FusionDirectoryException(sprintf(_('Too many targets (%d) passed for action "%s"'), count($action['targets']), $this->name));
160
161
162
163
164
165
166
167
    }
    $func = $this->callable;
    if (!is_array($func)) {
      $func = array($management, $func);
    }
    return call_user_func($func, $action);
  }

168
  function fillMenuItems(&$actions)
169
170
  {
    if (!$this->inmenu) {
171
      return;
172
    }
173

174
175
176
177
    if (!$this->hasPermission($this->parent->listing->getBase())) {
      return;
    }

178
    $actions[] = array(
179
180
181
      'name'      => $this->name,
      'icon'      => $this->icon,
      'label'     => $this->label,
182
      'enabled'   => $this->isEnabledFor(),
183
      'separator' => $this->separator,
184
    );
185
  }
186

187
  function fillRowClasses(&$classes, ListingEntry $entry)
188
189
190
  {
  }

191
  function renderColumnIcons(ListingEntry $entry)
192
  {
193
194
195
196
    if (!$this->inline) {
      return '';
    }

197
198
199
200
    if (!empty($this->validTypes) && !($entry->isTemplate() && in_array('template', $this->validTypes)) && !in_array($entry->type, $this->validTypes)) {
      return '';
    }

201
    // Skip the entry completely if there's no permission to execute it
202
    if (!$this->hasPermission($entry->dn, $entry->getTemplatedType(), $entry->isTemplate())) {
203
      return '<img src="images/empty.png" alt=" " class="optional"/>';
204
    }
205

206
207
208
209
    if (!$this->isEnabledFor($entry)) {
      return '<img src="'.htmlentities($this->icon.'&disabled=1', ENT_COMPAT, 'UTF-8').'"'.
              ' title="'.$this->label.'" alt="'.$this->label.'"/>';
    }
210
211

    // Render
212
    return '<input type="image" src="'.htmlentities($this->icon, ENT_COMPAT, 'UTF-8').'"'.
213
            ' title="'.$this->label.'" alt="'.$this->label.'" name="listing_'.$this->name.'_'.$entry->row.'"/>';
214
  }
215

216
217
218
219
220
221
222
223
  function isEnabledFor(ListingEntry $entry = NULL)
  {
    if (isset($this->enabledCallable)) {
      return call_user_func($this->enabledCallable, $this->name, $entry);
    }
    return TRUE;
  }

224
  function hasPermission($dn, $type = NULL, $template = FALSE)
225
226
227
  {
    global $ui;

228
229
230
231
232
233
234
235
236
237
    if ($type === NULL) {
      $types = $this->parent->objectTypes;
    } else {
      $types = array($type);
    }
    /*
     * if category is missing it’s deducted from type (all types are tested for menu actions)
     * if class is missing it’s deducted from attribute if present, otherwise it’s type mainTab
     * if attribute is missing 0 is used
     */
238
    foreach ($this->acl as $acl) {
239
240
241
242
243
244
245
246
      $checkAcl = '';
      if (!empty($acl['category'])) {
        $checkAcl = $ui->get_permissions($dn, $acl['category'].'/'.$acl['class'], $acl['attribute']);
      } elseif (empty($acl['class']) && ($acl['attribute'] != '0')) {
        foreach ($types as $type) {
          $module   = $ui->getAttributeCategory($type, $acl['attribute']);
          $checkAcl .= $ui->get_permissions($dn, $module, $acl['attribute']);
        }
247
      } else {
248
249
250
251
252
253
254
255
256
257
258
        foreach ($types as $type) {
          $infos = objects::infos($type);
          if (!empty($acl['class'])) {
            /* Class with empty category may be used in special cases like '/snapshot/c'*/
            $module = $infos['aclCategory'].'/'.$acl['class'];
          } elseif ($template) {
            $module = $infos['aclCategory'].'/template';
          } else {
            $module = $infos['aclCategory'].'/'.$infos['mainTab'];
          }
          $checkAcl .= $ui->get_permissions($dn, $module, $acl['attribute']);
259
        }
260
261
      }

262
263
      // Check rights
      foreach ($acl['acl'] as $part) {
264
265
266
267
268
269
270
271
        if (strpos($checkAcl, $part) === FALSE) {
          return FALSE;
        }
      }
    }

    return TRUE;
  }
272
}