Merge branch 'ent-8022-12445-duplicacion-de-politicas-con-mergeador-de-metaconsola' into 'develop'

Entity dynamic method calling improved

See merge request artica/pandorafms!4415
This commit is contained in:
Daniel Rodriguez 2021-09-28 12:56:00 +00:00
commit ddd550ce42
2 changed files with 97 additions and 27 deletions

View File

@ -153,6 +153,17 @@ abstract class Entity
/**
* Dynamically call methods in this object.
*
* To dynamically switch between community methods and prioritize
* enterprise ones, define method visibility as 'protected' in both
* classes.
*
* For instance, in following situation:
* protected PandoraFMS\Agent::test()
* protected PandoraFMS\Enterprise\Agent::test()
*
* If enterprise is available, then PandoraFMS\Enterprise\Agent::test()
* will be executed, community method otherwise.
*
* @param string $methodName Name of target method or attribute.
* @param array $params Arguments for target method.
*
@ -161,41 +172,38 @@ abstract class Entity
*/
public function __call(string $methodName, ?array $params=null)
{
// Prioritize written methods over dynamic ones.
if (method_exists($this, $methodName) === true) {
return call_user_func_array(
$this->{$methodName},
$params
);
}
// Enterprise capabilities.
// Prioritize enterprise written methods over dynamic fields.
if (\enterprise_installed() === true
&& $this->enterprise !== null
&& method_exists($this->enterprise, $methodName) === true
) {
return call_user_func_array(
[
$this->enterprise,
$methodName,
],
$params
return $this->enterprise->$methodName(...$params);
}
if (method_exists($this, $methodName) === false) {
if (array_key_exists($methodName, $this->fields) === true) {
if (empty($params) === true) {
return $this->fields[$methodName];
} else {
$this->fields[$methodName] = $params[0];
}
return null;
}
throw new \Exception(
get_class($this).' error, method '.$methodName.' does not exist'
);
}
if (array_key_exists($methodName, $this->fields) === true) {
if (empty($params) === true) {
return $this->fields[$methodName];
} else {
$this->fields[$methodName] = $params[0];
}
return null;
}
throw new \Exception(
get_class($this).' error, method '.$methodName.' does not exist'
);
// Do not return nor throw exceptions after this point, allow php
// default __call behaviour to continue working with object method
// defined.
// If you're receiving NULL as result of the method invocation, ensure
// it is not private, take in mind this method will mask any access
// level error or notification since it is public and has limited access
// to the object (public|protected).
}

View File

@ -0,0 +1,62 @@
<?php
require_once __DIR__.'/../include/config.php';
require_once __DIR__.'/../vendor/autoload.php';
if (file_exists(__DIR__.'/../'.ENTERPRISE_DIR.'/load_enterprise.php') === true) {
include_once __DIR__.'/../'.ENTERPRISE_DIR.'/load_enterprise.php';
}
if (isset($_SERVER['argc']) === false) {
exit(1);
}
global $config;
use PandoraFMS\Agent;
$ids = \db_get_all_rows_filter('tagente', [], ['id_agente']);
if ($ids === false) {
echo "Unable to find agents\n";
$ids = [];
}
$policies = \db_get_all_rows_filter('tpolicies', [], 'id,name');
$policies = array_reduce(
$policies,
function ($carry, $item) {
$carry[$item['name']] = $item['id'];
return $carry;
},
[]
);
foreach ($ids as $a) {
try {
$agent = new Agent($a['id_agente']);
if ($agent->hasRemoteCapabilities() === true) {
$agent_policies = $agent->getConfPolicies();
$oldIds = [];
$newIds = [];
foreach ($agent_policies as $oldId => $name) {
$oldIds[] = $oldId;
$newIds[] = $policies[io_safe_input($name)];
}
$res_update_con_policy = $agent->updatePolicyIds(
$oldIds,
$newIds
);
if ($res_update_con_policy === false) {
echo 'Failed ['.$agent->name()."]\n";
} else {
echo 'Agent '.io_safe_output($agent->alias())." updated successfully\n";
}
} else {
echo 'Agent '.io_safe_output($agent->alias())." skipped\n";
}
} catch (Exception $e) {
echo $e->getMessage()."\n";
}
}