class_Action.inc 7.7 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 (string $name, $label, $icon, string $targets, $callable, array $acls = [], bool $inmenu = TRUE, bool $inline = TRUE, array $validTypes = [])
53
54
55
56
57
  {
    if ($targets == '0') {
      $inline = FALSE;
    }

58
59
60
61
62
63
64
    $this->name       = $name;
    $this->label      = $label;
    $this->icon       = $icon;
    $this->targets    = $targets;
    $this->callable   = $callable;
    $this->inmenu     = $inmenu;
    $this->inline     = $inline;
65
    $this->validTypes = array_map('strtoupper', $validTypes);
66
    $this->acl        = [];
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
    /*
     * 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);
      }
86
      $this->acl[] = [
87
88
89
90
        'category'  => $category,
        'class'     => $class,
        'attribute' => $attribute,
        'acl'       => str_split($acl),
91
      ];
92
    }
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
  function setParent (management $parent)
121
122
123
124
  {
    $this->parent = $parent;
  }

125
  function getName (): string
126
127
128
129
  {
    return $this->name;
  }

130
  function getLabel ()
131
132
133
134
  {
    return $this->label;
  }

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

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

145
  function setInMenu (bool $inmenu)
146
147
148
149
  {
    $this->inmenu = $inmenu;
  }

150
  function listActions (): array
151
  {
152
    return [$this->name];
153
154
  }

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

173
  function fillMenuItems (array &$actions)
174
175
  {
    if (!$this->inmenu) {
176
      return;
177
    }
178

179
180
181
182
    if (!$this->hasPermission($this->parent->listing->getBase())) {
      return;
    }

183
    $actions[] = [
184
185
186
      'name'      => $this->name,
      'icon'      => $this->icon,
      'label'     => $this->label,
187
      'enabled'   => $this->isEnabledFor(),
188
      'separator' => $this->separator,
189
    ];
190
  }
191

192
  function fillRowClasses (array &$classes, ListingEntry $entry)
193
194
195
  {
  }

196
  function renderColumnIcons (ListingEntry $entry): string
197
  {
198
199
200
201
    if (!$this->inline) {
      return '';
    }

202
    if (!empty($this->validTypes) && !($entry->isTemplate() && in_array('TEMPLATE', $this->validTypes)) && !in_array($entry->type, $this->validTypes)) {
203
204
205
      return '';
    }

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

211
212
213
214
    if (!$this->isEnabledFor($entry)) {
      return '<img src="'.htmlentities($this->icon.'&disabled=1', ENT_COMPAT, 'UTF-8').'"'.
              ' title="'.$this->label.'" alt="'.$this->label.'"/>';
    }
215
216

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

221
  function isEnabledFor (ListingEntry $entry = NULL): bool
222
223
224
225
226
227
228
  {
    if (isset($this->enabledCallable)) {
      return call_user_func($this->enabledCallable, $this->name, $entry);
    }
    return TRUE;
  }

229
  function hasPermission (string $dn, string $type = NULL, bool $template = FALSE): bool
230
231
232
  {
    global $ui;

233
234
235
    if ($type === NULL) {
      $types = $this->parent->objectTypes;
    } else {
236
      $types = [$type];
237
238
239
240
241
242
    }
    /*
     * 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
     */
243
    foreach ($this->acl as $acl) {
244
245
246
247
248
249
250
251
      $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']);
        }
252
      } else {
253
254
255
256
257
258
259
260
261
262
263
        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']);
264
        }
265
266
      }

267
268
      // Check rights
      foreach ($acl['acl'] as $part) {
269
270
271
272
273
274
275
276
        if (strpos($checkAcl, $part) === FALSE) {
          return FALSE;
        }
      }
    }

    return TRUE;
  }
277
}