Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/CommonDBTM.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
use Glpi\DBAL\QueryParam;
use Glpi\Debug\Profiler;
use Glpi\Event;
use Glpi\Exception\Crud\AddException;
use Glpi\Exception\Crud\CloneException;
use Glpi\Exception\Crud\DeleteException;
use Glpi\Exception\Crud\PurgeException;
use Glpi\Exception\Crud\UpdateException;
use Glpi\Exception\Database\StatementException;
use Glpi\Exception\Http\AccessDeniedHttpException;
use Glpi\Exception\Http\NotFoundHttpException;
Expand Down Expand Up @@ -1281,7 +1286,7 @@ public function add(array $input, $options = [], $history = true)
global $CFG_GLPI, $DB;

if ($DB->isReplica()) {
return false;
throw new AddException('Cannot add object in slave database');
}

// This means we are not adding a cloned object
Expand All @@ -1297,7 +1302,10 @@ public function add(array $input, $options = [], $history = true)
if (isset($input['id'])) {
$id_to_clone = $input['id'];
}
if (isset($id_to_clone) && $this->getFromDB($id_to_clone)) {
if (isset($id_to_clone)) {
if (!$this->getFromDB($id_to_clone)) {
throw new CloneException('Unable to load item to clone #' . $id_to_clone);
}
if ($clone_id = $this->clone($input, $history)) {
$this->getFromDB($clone_id); // Load created items fields
}
Expand Down Expand Up @@ -1434,7 +1442,7 @@ public function add(array $input, $options = [], $history = true)
}
}

return false;
throw new AddException('Cannot add item to database');
}

/**
Expand Down Expand Up @@ -1810,7 +1818,7 @@ public function update(array $input, $history = true, $options = [])
}
}

return false;
throw new UpdateException('Cannot update item in database');
}


Expand Down Expand Up @@ -2212,7 +2220,12 @@ public function delete(array $input, $force = false, $history = true)
return true;
}
}
return false;

if ($force) {
throw new PurgeException('Cannot purge item from database');
} else {
throw new DeleteException('Cannot delete item from database');
}
}


Expand Down
37 changes: 37 additions & 0 deletions src/Glpi/Exception/Crud/AddException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2026 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Exception\Crud;

class AddException extends CrudException {}
37 changes: 37 additions & 0 deletions src/Glpi/Exception/Crud/CloneException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2026 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Exception\Crud;

class CloneException extends CrudException {}
39 changes: 39 additions & 0 deletions src/Glpi/Exception/Crud/CrudException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2026 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Exception\Crud;

use Exception;

class CrudException extends Exception {}
37 changes: 37 additions & 0 deletions src/Glpi/Exception/Crud/DeleteException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2026 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Exception\Crud;

class DeleteException extends CrudException {}
37 changes: 37 additions & 0 deletions src/Glpi/Exception/Crud/PurgeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2026 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Exception\Crud;

class PurgeException extends DeleteException {}
37 changes: 37 additions & 0 deletions src/Glpi/Exception/Crud/UpdateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* ---------------------------------------------------------------------
*
* GLPI - Gestionnaire Libre de Parc Informatique
*
* http://glpi-project.org
*
* @copyright 2015-2026 Teclib' and contributors.
* @licence https://www.gnu.org/licenses/gpl-3.0.html
*
* ---------------------------------------------------------------------
*
* LICENSE
*
* This file is part of GLPI.
*
* 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 3 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, see <https://www.gnu.org/licenses/>.
*
* ---------------------------------------------------------------------
*/

namespace Glpi\Exception\Crud;

class UpdateException extends CrudException {}
3 changes: 2 additions & 1 deletion src/ProfileRight.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use Glpi\DBAL\QueryExpression;
use Glpi\DBAL\QueryParam;
use Glpi\DBAL\QuerySubQuery;
use Glpi\Exception\Crud\CloneException;

/**
* Profile class
Expand All @@ -61,7 +62,7 @@ public function clone(array $override_input = [], bool $history = true, bool $cl
global $DB;

if ($DB->isReplica()) {
return false;
throw new CloneException('Cannot clone item on a DB replica.');
}
$new_item = new static();
$input = $this->fields;
Expand Down
46 changes: 33 additions & 13 deletions tests/functional/CommonDBTMTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
use Document_Item;
use Entity;
use Glpi\Event;
use Glpi\Exception\Crud\AddException;
use Glpi\Exception\Crud\UpdateException;
use Glpi\Exception\Http\AccessDeniedHttpException;
use Glpi\Exception\Http\NotFoundHttpException;
use Glpi\Tests\DbTestCase;
Expand Down Expand Up @@ -1358,20 +1360,33 @@ public function testCheckUnicity()
])
);

$this->assertFalse($computer->update([
'id' => $computers_id2,
'uuid' => '76873749-0813-482f-ac20-eb7102ed3367',
]));
$exception_thrown = false;
try {
$computer->update([
'id' => $computers_id2,
'uuid' => '76873749-0813-482f-ac20-eb7102ed3367',
]);
} catch (UpdateException $e) {
$exception_thrown = true;
$this->assertSame('Cannot update item in database', $e->getMessage());
}
$this->assertTrue($exception_thrown);

$err_msg = 'Impossible record for UUID = 76873749-0813-482f-ac20-eb7102ed3367<br>Other item exist<br>[<a href="/front/computer.form.php?id=' . $computers_id1 . '" data-bs-toggle="tooltip" data-bs-placement="bottom" title="testCheckUnicity01">testCheckUnicity01</a> - ID: ' . $computers_id1 . ' - Serial number: - Entity: Root entity &gt; _test_root_entity]';
$this->hasSessionMessages(ERROR, [$err_msg]);

$this->assertFalse($computer->add([
'name' => __FUNCTION__ . '03',
'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true),
'uuid' => '76873749-0813-482f-ac20-eb7102ed3367',
]));

$exception_thrown = false;
try {
$computer->add([
'name' => __FUNCTION__ . '03',
'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true),
'uuid' => '76873749-0813-482f-ac20-eb7102ed3367',
]);
} catch (AddException $e) {
$exception_thrown = true;
$this->assertSame('Cannot add item to database', $e->getMessage());
}
$this->assertTrue($exception_thrown);
$this->hasSessionMessages(ERROR, [$err_msg]);
}

Expand Down Expand Up @@ -1434,12 +1449,17 @@ public function testSkipCheckUnicityWithTemplate()
$this->hasNoSessionMessages([ERROR]);

//create computer with same name should not be possible (because of first computer)
$this->assertFalse(
$exception_thrown = false;
try {
$computer->add([
'name' => __FUNCTION__ . '01',
'entities_id' => getItemByTypeName('Entity', '_test_root_entity', true),
])
);
]);
} catch (AddException $e) {
$exception_thrown = true;
$this->assertSame('Cannot add item to database', $e->getMessage());
}
$this->assertTrue($exception_thrown);
$err_msg = 'Impossible record for Name = ' . __FUNCTION__ . '01<br>Other item exist<br>[<a href="/front/computer.form.php?id=' . $computers_id . '" data-bs-toggle="tooltip" data-bs-placement="bottom" title="' . __FUNCTION__ . '01">' . __FUNCTION__ . '01</a> - ID: ' . $computers_id . ' - Serial number: - Entity: Root entity &gt; _test_root_entity]';
$this->hasSessionMessages(ERROR, [$err_msg]);

Expand Down
Loading
Loading