-
-
Couldn't load subscription status.
- Fork 1.5k
Glpi key use exceptions not errors #21623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -52,10 +52,23 @@ public static function getIcon() | |||||||
|
|
||||||||
| public static function displayTabContentForItem(CommonGLPI $item, $tabnum = 1, $withtemplate = 0) | ||||||||
| { | ||||||||
| if ($item::class === Config::class) { | ||||||||
| self::showForConfig(); | ||||||||
| try { | ||||||||
| if ($item->getType() == 'Config') { | ||||||||
| $glpiNetwork = new self(); | ||||||||
| $glpiNetwork->showForConfig(); | ||||||||
|
Comment on lines
+57
to
+58
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I intented to have the less possible changes, but I can change it if it hurt your eyes 😆 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's only just because it is a static method and should be called statically. |
||||||||
| } | ||||||||
| return true; | ||||||||
| } catch (Exception $e) { | ||||||||
| TemplateRenderer::getInstance()->display( | ||||||||
| '/central/messages.html.twig', | ||||||||
| [ | ||||||||
| 'messages' => [ | ||||||||
| 'errors' => [$e->getMessage()], | ||||||||
| ], | ||||||||
| ] | ||||||||
| ); | ||||||||
| return false; | ||||||||
| } | ||||||||
| return true; | ||||||||
| } | ||||||||
|
|
||||||||
| public static function showForConfig() | ||||||||
|
|
||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| <?php | ||
|
|
||
| /** | ||
| * --------------------------------------------------------------------- | ||
| * | ||
| * GLPI - Gestionnaire Libre de Parc Informatique | ||
| * | ||
| * http://glpi-project.org | ||
| * | ||
| * @copyright 2015-2025 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; | ||
|
|
||
| /** | ||
| * Denotes a problem while using a GLPI API key. | ||
| * | ||
| * This can be an invalid key (unexpected length), a missing key file, an unreadable key file. | ||
| * Fact that content was not decrypted can also mean the content was not created with the current key. | ||
| */ | ||
| class InvalidGlpiKeyException extends \Exception {} |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -97,7 +97,7 @@ public function setUp(): void | |||||
|
|
||||||
| // Make sure the tester plugin is never deactived by a test as it would | ||||||
| // impact others tests that depend on it. | ||||||
| $this->assertTrue(Plugin::isPluginActive('tester')); | ||||||
| // $this->assertTrue(Plugin::isPluginActive('tester')); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, should not be committed. I'll fix it |
||||||
| } | ||||||
|
|
||||||
| public function tearDown(): void | ||||||
|
|
@@ -108,7 +108,7 @@ public function tearDown(): void | |||||
|
|
||||||
| // Make sure the tester plugin is never deactived by a test as it would | ||||||
| // impact others tests that depend on it. | ||||||
| $this->assertTrue(Plugin::isPluginActive('tester')); | ||||||
| // $this->assertTrue(Plugin::isPluginActive('tester')); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do not spent too much time reviewing, it's not ready yet. |
||||||
|
|
||||||
| if (isset($_SESSION['MESSAGE_AFTER_REDIRECT']) && !$this->has_failed) { | ||||||
| unset($_SESSION['MESSAGE_AFTER_REDIRECT'][INFO]); | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This makes it read the key file twice every time it needs to perform an encrypt/decrypt. Should be avoided.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's the downside of having 2 methods. The footprint of reading this local file et probably close to null. So I choose this approach (2 methods vs 1), it looks really better than having a single method that does both (checking key & show the message) and violate SRP principle.
Maybe we can have a local variable to keep the errors and avoid 2 calls to filegetcontents.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you are taking textbook concepts a bit too far here. The single purpose of the get method is to get the key. A part of that happens to be ensuring the key is valid, and that validation is only done here.
Indeed though, it probably doesn't make that much difference in this specific case.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Looks like my answer was lost)
That's not too far, that's the basis. Experience learnt me that's a very important point. Writing, reading, testing, maintaining, everything is easier respecting separation of concerns.
Only very strong reasons can justify not to respect it. I don't see any reason to do it here.
And we can use ensure method elsewhere in the future.