2020-05-14 17:49:02 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Group entity class.
|
|
|
|
*
|
|
|
|
* @category Class
|
|
|
|
* @package Pandora FMS
|
|
|
|
* @subpackage OpenSource
|
|
|
|
* @version 1.0.0
|
|
|
|
* @license See below
|
|
|
|
*
|
|
|
|
* ______ ___ _______ _______ ________
|
|
|
|
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
|
|
|
|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
|
|
|
|
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
|
|
|
|
*
|
|
|
|
* ============================================================================
|
2020-11-27 13:52:35 +01:00
|
|
|
* Copyright (c) 2005-2021 Artica Soluciones Tecnologicas
|
2020-05-14 17:49:02 +02:00
|
|
|
* Please see http://pandorafms.org for full contribution list
|
|
|
|
* 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 for version 2.
|
|
|
|
* 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.
|
|
|
|
* ============================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Begin.
|
|
|
|
namespace PandoraFMS;
|
|
|
|
|
2020-07-03 17:52:44 +02:00
|
|
|
global $config;
|
|
|
|
require_once $config['homedir'].'/include/functions_groups.php';
|
|
|
|
|
2020-05-14 17:49:02 +02:00
|
|
|
/**
|
|
|
|
* PandoraFMS Group entity.
|
|
|
|
*/
|
|
|
|
class Group extends Entity
|
|
|
|
{
|
|
|
|
|
2020-09-11 14:34:41 +02:00
|
|
|
/**
|
|
|
|
* List of available ajax methods.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private static $ajaxMethods = ['getGroupsForSelect'];
|
|
|
|
|
2020-05-14 17:49:02 +02:00
|
|
|
|
2021-04-21 10:26:33 +02:00
|
|
|
/**
|
|
|
|
* Instances a new object using array definition.
|
|
|
|
*
|
|
|
|
* @param array $data Fields data.
|
|
|
|
* @param string $class_str Class name.
|
|
|
|
*
|
|
|
|
* @return object With current definition.
|
|
|
|
*/
|
|
|
|
public static function build(array $data=[], string $class_str=__CLASS__)
|
|
|
|
{
|
|
|
|
return parent::build($data, $class_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-14 17:49:02 +02:00
|
|
|
/**
|
|
|
|
* Builds a PandoraFMS\Group object from a group id.
|
|
|
|
*
|
|
|
|
* @param integer $id_group Group Id.
|
|
|
|
* @param boolean $recursive Create parents as objects.
|
|
|
|
*/
|
|
|
|
public function __construct(?int $id_group=null, bool $recursive=false)
|
|
|
|
{
|
|
|
|
if ($id_group === 0) {
|
|
|
|
parent::__construct('tgrupo');
|
|
|
|
|
|
|
|
$this->fields['id'] = 0;
|
|
|
|
$this->fields['nombre'] = 'All';
|
|
|
|
} else if (is_numeric($id_group) === true) {
|
|
|
|
parent::__construct('tgrupo', ['id_grupo' => $id_group]);
|
|
|
|
if ($recursive === true) {
|
|
|
|
// Customize certain fields.
|
|
|
|
$this->fields['parent'] = new Group($this->fields['parent']);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Empty skel.
|
|
|
|
parent::__construct('tgrupo');
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-03 17:52:44 +02:00
|
|
|
/**
|
|
|
|
* Return an array of ids with all children
|
|
|
|
*
|
|
|
|
* @param boolean $ids_only Return an array of id_groups or
|
|
|
|
* entire rows.
|
|
|
|
* @param boolean $ignorePropagationState Search all children ignoring or
|
|
|
|
* depending on propagate_acl flag.
|
|
|
|
*
|
|
|
|
* @return array With all children.
|
|
|
|
*/
|
|
|
|
public function getChildren(
|
|
|
|
bool $ids_only=false,
|
|
|
|
bool $ignorePropagationState=false
|
2020-09-11 14:34:41 +02:00
|
|
|
):array {
|
2020-07-03 17:52:44 +02:00
|
|
|
$available_groups = \groups_get_children(
|
|
|
|
$this->id_grupo(),
|
|
|
|
$ignorePropagationState
|
|
|
|
);
|
|
|
|
|
|
|
|
if (is_array($available_groups) === false) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($ids_only === true) {
|
|
|
|
return array_keys($available_groups);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $available_groups;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-07 18:24:51 +01:00
|
|
|
/**
|
|
|
|
* Alias of 'nombre'.
|
|
|
|
*
|
|
|
|
* @param string|null $name Name of group.
|
|
|
|
*
|
|
|
|
* @return string|void Name assigned or void if set operation.
|
|
|
|
*/
|
|
|
|
public function name(?string $name=null)
|
|
|
|
{
|
|
|
|
if ($name === null) {
|
|
|
|
return $this->nombre();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->nombre($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-11 14:34:41 +02:00
|
|
|
/**
|
|
|
|
* Retrieves a list of groups fitered.
|
|
|
|
*
|
|
|
|
* @param array $filter Filters to be applied.
|
|
|
|
*
|
|
|
|
* @return array With all results or false if error.
|
|
|
|
* @throws Exception On error.
|
|
|
|
*/
|
|
|
|
private static function search(array $filter):array
|
|
|
|
{
|
|
|
|
// Default values.
|
|
|
|
if (empty($filter['id_user']) === true) {
|
|
|
|
// By default query current user groups.
|
|
|
|
$filter['id_user'] = false;
|
2021-01-07 18:25:59 +01:00
|
|
|
} else if ((bool) \users_is_admin() === false) {
|
2020-09-11 14:34:41 +02:00
|
|
|
// Override user queried if user is not an admin.
|
|
|
|
$filter['id_user'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($filter['id_user']) === true) {
|
|
|
|
$filter['id_user'] = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($filter['keys_field']) === true) {
|
|
|
|
$filter['keys_field'] = 'id_grupo';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($filter['returnAllColumns']) === false) {
|
|
|
|
$filter['returnAllColumns'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$groups = \users_get_groups(
|
|
|
|
$filter['id_user'],
|
|
|
|
$filter['privilege'],
|
|
|
|
$filter['returnAllGroup'],
|
|
|
|
// Return all columns.
|
|
|
|
$filter['returnAllColumns'],
|
|
|
|
// Field id_groups is not being used anymore.
|
|
|
|
null,
|
|
|
|
$filter['keys_field'],
|
|
|
|
// Cache.
|
|
|
|
true,
|
|
|
|
// Search term.
|
|
|
|
$filter['search']
|
|
|
|
);
|
|
|
|
|
|
|
|
if (is_array($groups) === false) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $groups;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an hierarchical ordered array.
|
|
|
|
*
|
|
|
|
* @param array $groups All groups available.
|
|
|
|
*
|
|
|
|
* @return array Groups ordered.
|
|
|
|
*/
|
|
|
|
private static function prepareGroups(array $groups):array
|
|
|
|
{
|
|
|
|
$return = [];
|
2020-10-08 16:41:26 +02:00
|
|
|
$tree_groups = \groups_get_groups_tree_recursive($groups);
|
|
|
|
foreach ($tree_groups as $k => $v) {
|
2020-09-11 14:34:41 +02:00
|
|
|
$return[] = [
|
|
|
|
'id' => $k,
|
|
|
|
'text' => \io_safe_output(
|
|
|
|
\ui_print_truncate_text(
|
|
|
|
$v['nombre'],
|
|
|
|
GENERIC_SIZE_TEXT,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'level' => $v['deep'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-10-08 16:41:26 +02:00
|
|
|
$unassigned = [];
|
|
|
|
$processed = array_keys($tree_groups);
|
|
|
|
foreach ($groups as $k => $v) {
|
|
|
|
if (in_array($k, $processed) === true) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$unassigned[] = [
|
|
|
|
'id' => $k,
|
|
|
|
'text' => \io_safe_output(
|
|
|
|
\ui_print_truncate_text(
|
|
|
|
$v,
|
|
|
|
GENERIC_SIZE_TEXT,
|
|
|
|
false,
|
|
|
|
true,
|
|
|
|
false
|
|
|
|
)
|
|
|
|
),
|
|
|
|
'level' => 0,
|
|
|
|
];
|
|
|
|
}
|
2020-09-11 14:34:41 +02:00
|
|
|
|
2020-10-08 16:41:26 +02:00
|
|
|
return array_merge($unassigned, $return);
|
2020-09-11 14:34:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-14 17:49:02 +02:00
|
|
|
/**
|
|
|
|
* Saves current group definition to database.
|
|
|
|
*
|
|
|
|
* @return mixed Affected rows of false in case of error.
|
2020-09-11 14:34:41 +02:00
|
|
|
* @throws \Exception On error.
|
2020-05-14 17:49:02 +02:00
|
|
|
*/
|
|
|
|
public function save()
|
|
|
|
{
|
2021-04-20 11:09:07 +02:00
|
|
|
if (\is_management_allowed() !== true) {
|
2021-01-07 18:25:59 +01:00
|
|
|
$msg = 'cannot be modified in a centralized management environment';
|
2020-05-14 17:49:02 +02:00
|
|
|
throw new \Exception(
|
2021-01-07 18:25:59 +01:00
|
|
|
get_class($this).' error, '.$msg
|
2020-05-14 17:49:02 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-20 11:09:07 +02:00
|
|
|
$updates = $this->fields;
|
|
|
|
|
|
|
|
if (is_numeric($updates['parent']) === false) {
|
|
|
|
$updates['parent'] = $this->parent()->id_grupo();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean null fields.
|
|
|
|
foreach ($updates as $k => $v) {
|
|
|
|
if ($v === null) {
|
|
|
|
unset($updates[$k]);
|
2020-05-14 17:49:02 +02:00
|
|
|
}
|
2021-04-20 11:09:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($updates['propagate']) === false) {
|
|
|
|
$updates['propagate'] = 0;
|
|
|
|
}
|
2020-05-14 17:49:02 +02:00
|
|
|
|
2021-04-20 11:09:07 +02:00
|
|
|
if (isset($updates['disabled']) === false) {
|
|
|
|
$updates['disabled'] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->fields['id_grupo'] > 0) {
|
|
|
|
return \db_process_sql_update(
|
2020-05-14 17:49:02 +02:00
|
|
|
'tgrupo',
|
2021-04-20 11:09:07 +02:00
|
|
|
$updates,
|
2020-05-14 17:49:02 +02:00
|
|
|
['id_grupo' => $this->fields['id_grupo']]
|
|
|
|
);
|
2021-04-20 11:09:07 +02:00
|
|
|
} else {
|
|
|
|
// Create new group.
|
|
|
|
$this->fields['id_grupo'] = \db_process_sql_insert(
|
|
|
|
'\tgrupo',
|
2021-04-20 14:08:27 +02:00
|
|
|
$updates
|
2021-04-20 11:09:07 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
if ($this->fields['id_grupo'] === false) {
|
|
|
|
global $config;
|
|
|
|
$msg = __(
|
|
|
|
'Failed to save group %s',
|
|
|
|
$config['dbconnection']->error
|
|
|
|
);
|
|
|
|
throw new \Exception(
|
|
|
|
get_class($this).' error, '.$msg
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return true;
|
|
|
|
}
|
2020-05-14 17:49:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-20 11:09:07 +02:00
|
|
|
/**
|
|
|
|
* Delete this group.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function delete()
|
|
|
|
{
|
|
|
|
// Propagate parents.
|
|
|
|
\db_process_sql_update(
|
|
|
|
'tgrupo',
|
|
|
|
['parent' => $this->parent()->id_grupo()],
|
|
|
|
['parent' => $this->id_grupo()]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Remove stats.
|
|
|
|
\db_process_sql_delete(
|
|
|
|
'tgroup_stat',
|
|
|
|
['id_group' => $this->id_grupo()]
|
|
|
|
);
|
|
|
|
|
|
|
|
// Remove group.
|
|
|
|
\db_process_sql_delete(
|
|
|
|
'tgrupo',
|
|
|
|
['id_grupo' => $this->id_grupo()]
|
|
|
|
);
|
|
|
|
|
|
|
|
unset($this->fields['id_grupo']);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-11 14:34:41 +02:00
|
|
|
/**
|
|
|
|
* Return error message to target.
|
|
|
|
*
|
|
|
|
* @param string $msg Error message.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public static function error(string $msg)
|
|
|
|
{
|
|
|
|
echo json_encode(['error' => $msg]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verifies target method is allowed to be called using AJAX call.
|
|
|
|
*
|
|
|
|
* @param string $method Method to be invoked via AJAX.
|
|
|
|
*
|
|
|
|
* @return boolean Available (true), or not (false).
|
|
|
|
*/
|
|
|
|
public static function ajaxMethod(string $method):bool
|
|
|
|
{
|
|
|
|
return in_array($method, self::$ajaxMethods) === true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This method is being invoked by select2 to improve performance while
|
|
|
|
* installation has a lot of groups (more than 5k).
|
|
|
|
*
|
|
|
|
* Security applied in controller include/ajax/group.php.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
* @throws \Exception On error.
|
|
|
|
*/
|
|
|
|
public static function getGroupsForSelect()
|
|
|
|
{
|
|
|
|
$id_user = get_parameter('id_user', false);
|
|
|
|
$privilege = get_parameter('privilege', 'AR');
|
|
|
|
$returnAllGroup = get_parameter('returnAllGroup', false);
|
|
|
|
$id_group = get_parameter('id_group', false);
|
|
|
|
$keys_field = get_parameter('keys_field', 'id_grupo');
|
|
|
|
$search = get_parameter('search', '');
|
2020-09-11 14:53:29 +02:00
|
|
|
$step = get_parameter('step', 1);
|
2020-09-11 14:34:41 +02:00
|
|
|
$limit = get_parameter('limit', false);
|
2021-08-26 12:42:16 +02:00
|
|
|
$not_condition = get_parameter('not_condition', false);
|
2020-09-11 14:34:41 +02:00
|
|
|
$exclusions = get_parameter('exclusions', '[]');
|
|
|
|
$inclusions = get_parameter('inclusions', '[]');
|
|
|
|
|
2020-09-30 17:47:44 +02:00
|
|
|
if (empty($id_user) === true) {
|
|
|
|
$id_user = false;
|
|
|
|
}
|
|
|
|
|
2020-09-11 14:34:41 +02:00
|
|
|
$groups = self::search(
|
|
|
|
[
|
|
|
|
'id_user' => $id_user,
|
|
|
|
'privilege' => $privilege,
|
|
|
|
'returnAllGroup' => $returnAllGroup,
|
|
|
|
'returnAllColumns' => true,
|
|
|
|
'id_group' => $id_group,
|
|
|
|
'keys_field' => $keys_field,
|
|
|
|
'search' => $search,
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2020-10-08 16:41:26 +02:00
|
|
|
$exclusions = json_decode(\io_safe_output($exclusions), true);
|
2020-09-11 14:34:41 +02:00
|
|
|
if (empty($exclusions) === false) {
|
|
|
|
foreach ($exclusions as $ex) {
|
|
|
|
unset($groups[$ex]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 16:41:26 +02:00
|
|
|
$inclusions = json_decode(\io_safe_output($inclusions), true);
|
2020-09-11 14:34:41 +02:00
|
|
|
if (empty($inclusions) === false) {
|
2020-10-08 16:41:26 +02:00
|
|
|
foreach ($inclusions as $k => $g) {
|
|
|
|
if (empty($groups[$k]) === true) {
|
|
|
|
if (is_numeric($g) === true) {
|
|
|
|
$groups[$k] = \groups_get_name($k);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($groups[$k]) === true) {
|
|
|
|
// Group does not exist, direct value assigned.
|
|
|
|
$groups[$k] = $g;
|
|
|
|
}
|
2020-09-11 14:34:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$return = self::prepareGroups($groups);
|
|
|
|
|
2021-08-23 09:52:42 +02:00
|
|
|
// When not_condition is select firts option text change All to None.
|
2021-08-26 12:42:16 +02:00
|
|
|
if ($not_condition === 'true') {
|
2021-08-23 09:52:42 +02:00
|
|
|
$return[0]['text'] = 'None';
|
|
|
|
}
|
|
|
|
|
2020-09-11 14:34:41 +02:00
|
|
|
if (is_array($return) === false) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use global block size configuration.
|
|
|
|
global $config;
|
|
|
|
$limit = $config['block_size'];
|
2020-09-11 14:53:29 +02:00
|
|
|
$offset = (($step - 1) * $limit);
|
2020-09-11 14:34:41 +02:00
|
|
|
|
|
|
|
// Pagination over effective groups retrieved.
|
|
|
|
// Calculation is faster than transference.
|
|
|
|
$count = count($return);
|
2020-09-11 14:56:32 +02:00
|
|
|
if (is_numeric($offset) === true && $offset >= 0) {
|
2020-09-11 14:34:41 +02:00
|
|
|
if (is_numeric($limit) === true && $limit > 0) {
|
|
|
|
$return = array_splice($return, $offset, $limit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-11 15:09:52 +02:00
|
|
|
if ($step > 2) {
|
|
|
|
$processed = (($step - 2) * $limit);
|
|
|
|
} else {
|
|
|
|
$processed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$current_ammount = (count($return) + $processed);
|
|
|
|
|
2020-09-11 14:34:41 +02:00
|
|
|
echo json_encode(
|
|
|
|
[
|
|
|
|
'results' => $return,
|
2020-09-11 15:03:40 +02:00
|
|
|
'pagination' => [
|
2020-09-11 15:09:52 +02:00
|
|
|
'more' => $current_ammount < $count,
|
2020-09-11 15:03:40 +02:00
|
|
|
],
|
2020-09-11 14:34:41 +02:00
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-14 17:49:02 +02:00
|
|
|
}
|