class_Action.inc 7.17 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
  protected $acl;

35
36
37
38
39
  /* Booleans */
  protected $inmenu;
  protected $inline;

  protected $callable;
40
  protected $enabledCallable;
41
42
43
44

  protected $minTargets;
  protected $maxTargets;

45
46
  protected $separator = FALSE;

47
48
49
  protected $parent;

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

    $this->name     = $name;
    $this->label    = $label;
    $this->icon     = $icon;
    $this->targets  = $targets;
    $this->callable = $callable;
    $this->inmenu   = $inmenu;
    $this->inline   = $inline;
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
    $this->acl      = array();
    /*
     * 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),
      );
    }
89

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

116
117
118
119
120
  function setParent(management $parent)
  {
    $this->parent = $parent;
  }

121
122
123
124
125
126
127
128
129
130
  function getName()
  {
    return $this->name;
  }

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

131
132
133
134
135
  function setSeparator($bool)
  {
    $this->separator = $bool;
  }

136
137
138
139
140
  function setEnableFunction(callable $callable)
  {
    $this->enabledCallable = $callable;
  }

141
142
143
144
145
146
147
148
149
150
151
  function listActions()
  {
    return array($this->name);
  }

  function execute($management, $action)
  {
    if ($this->callable === FALSE) {
      return;
    }
    if (count($action['targets']) < $this->minTargets) {
152
      throw new FusionDirectoryException('Not enough targets ('.count($action['targets']).') passed for action '.$this->name);
153
154
    }
    if (($this->maxTargets !== FALSE) && (count($action['targets']) > $this->maxTargets)) {
155
      throw new FusionDirectoryException('Too many targets ('.count($action['targets']).') passed for action '.$this->name);
156
157
158
159
160
161
162
163
    }
    $func = $this->callable;
    if (!is_array($func)) {
      $func = array($management, $func);
    }
    return call_user_func($func, $action);
  }

164
  function fillMenuItems(&$actions)
165
166
  {
    if (!$this->inmenu) {
167
      return;
168
    }
169

170
171
172
173
    if (!$this->hasPermission($this->parent->listing->getBase())) {
      return;
    }

174
    $actions[] = array(
175
176
177
      'name'      => $this->name,
      'icon'      => $this->icon,
      'label'     => $this->label,
178
      'enabled'   => $this->isEnabledFor(),
179
      'separator' => $this->separator,
180
    );
181
  }
182

183
  function fillRowClasses(&$classes, ListingEntry $entry)
184
185
186
  {
  }

187
  function renderColumnIcons(ListingEntry $entry)
188
  {
189
190
191
192
    if (!$this->inline) {
      return '';
    }

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

198
199
200
201
    if (!$this->isEnabledFor($entry)) {
      return '<img src="'.htmlentities($this->icon.'&disabled=1', ENT_COMPAT, 'UTF-8').'"'.
              ' title="'.$this->label.'" alt="'.$this->label.'"/>';
    }
202
203

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

208
209
210
211
212
213
214
215
  function isEnabledFor(ListingEntry $entry = NULL)
  {
    if (isset($this->enabledCallable)) {
      return call_user_func($this->enabledCallable, $this->name, $entry);
    }
    return TRUE;
  }

216
  function hasPermission($dn, $type = NULL, $template = FALSE)
217
218
219
  {
    global $ui;

220
221
222
223
224
225
226
227
228
229
    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
     */
230
    foreach ($this->acl as $acl) {
231
232
233
234
235
236
237
238
      $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']);
        }
239
      } else {
240
241
242
243
244
245
246
247
248
249
250
        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']);
251
        }
252
253
      }

254
255
      // Check rights
      foreach ($acl['acl'] as $part) {
256
257
258
259
260
261
262
263
        if (strpos($checkAcl, $part) === FALSE) {
          return FALSE;
        }
      }
    }

    return TRUE;
  }
264
}