Prevent deletion of data lists that are in use.

Deletion of data lists which are used in icinga objects (through custom variables) or in sync rules is prevented.
This commit is contained in:
raviks789 2022-02-01 18:27:28 +01:00 committed by Thomas Gelf
parent 95730fb0aa
commit 4f38557374

View File

@ -2,6 +2,7 @@
namespace Icinga\Module\Director\Objects;
use Exception;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\DirectorObject\Automation\ExportInterface;
@ -106,6 +107,55 @@ class DirectorDatalist extends DbObject implements ExportInterface
return $this;
}
protected function beforeDelete()
{
if ($this->hasBeenUsed()) {
throw new Exception(
sprintf(
"Cannot delete '%s', as the datalist '%s' is currently being used.",
$this->get('list_name'),
$this->get('list_name')
)
);
}
}
protected function hasBeenUsed()
{
$datalistType = 'Icinga\\Module\\Director\\DataType\\DataTypeDatalist';
$db = $this->getDb();
$dataFieldsCheck = $db->select()
->from(['df' =>'director_datafield'], ['varname'])
->join(
['dfs' => 'director_datafield_setting'],
'dfs.datafield_id = df.id AND dfs.setting_name = \'datalist_id\'',
[]
)
->join(
['l' => 'director_datalist'],
'l.id = dfs.setting_value',
[]
)
->where('datatype = ?', $datalistType)
->where('setting_value = ?', $this->get('id'));
if ($db->fetchOne($dataFieldsCheck)) {
return true;
}
$syncCheck = $db->select()
->from(['sp' =>'sync_property'], ['source_expression'])
->where('sp.destination_field = ?', 'list_id')
->where('sp.source_expression = ?', $this->get('id'));
if ($db->fetchOne($syncCheck)) {
return true;
}
return false;
}
/**
* @throws DuplicateKeyException
*/