Fix setup for new hook

This commit is contained in:
yllen 2017-08-02 13:33:32 +02:00 committed by Johan Cwiklinski
parent e8acb472eb
commit a16d0fb850
2 changed files with 22 additions and 12 deletions

View File

@ -54,18 +54,27 @@ class PluginExampleComputer extends CommonDBTM {
echo '</table>';
}
// implement "item_can" hook (9.2) for Computer
static function restrict(Computer $comp) {
// no right to see computer from group 1
if (isset($comp->right)) {
// call from ConnDBTM::can method, filter for current item
if ($comp->getField('groups_id') == 1) {
$comp->right = false;
}
} else {
// called from Search::addDefaultWhere method, return additional condition
$comp->add_where = "glpi_computers.groups_id != 1";
}
static function item_can($item) {
if (($item-getType() == 'Computer')
&& ($item->right == READ)
&& ($item->fields['groups_id'] > 0)
&& !in_array($item->fields['groups_id'], $_SESSION["glpigroups"])) {
$item->right = 0; // unknown, so denied.
}
}
static function add_default_where($in) {
list($itemtype, $condition) = $in;
if ($itemtype == 'Computer') {
$table = getTableForItemType($itemtype);
$condition .= " (".$table.".groups_id NOT IN (".implode(',', $_SESSION["glpigroups"])."))";
}
return [$itemtype, $condition];
}
}

View File

@ -110,7 +110,8 @@ function plugin_init_example() {
$PLUGIN_HOOKS['item_empty']['example'] = array('Computer' => 'plugin_item_empty_example');
// Restrict right
$PLUGIN_HOOKS['item_can']['example'] = ['Computer' => ['PluginExampleComputer', 'restrict']];
$PLUGIN_HOOKS['item_can']['example'] = ['Computer' => ['PluginExampleComputer', 'item_can']];
$PLUGIN_HOOKS['add_default_where']['example'] = ['Computer' => ['PluginExampleComputer', 'add_default_where']];
// Example using a method in class
$PLUGIN_HOOKS['pre_item_add']['example'] = array('Computer' => array('PluginExampleExample',