Fix - Avoid SQL warning when no user session is active during plugin init#1179
Fix - Avoid SQL warning when no user session is active during plugin init#1179
Conversation
| $entity_restrict = isCommandLine() ? [] : getEntitiesRestrictCriteria(PluginFieldsContainer::getTable(), '', '', true); | ||
|
|
||
| // If entity restriction contains an empty string value, it means no valid session | ||
| // is active. Running the query would produce a MySQL | ||
| // warning (1292: Truncated incorrect DECIMAL value) | ||
| foreach ($entity_restrict as $criterion) { | ||
| if (is_array($criterion) && in_array('', $criterion, true)) { | ||
| return $available_blocks; | ||
| } | ||
| } | ||
|
|
There was a problem hiding this comment.
Maybe something like this:
| $entity_restrict = isCommandLine() ? [] : getEntitiesRestrictCriteria(PluginFieldsContainer::getTable(), '', '', true); | |
| // If entity restriction contains an empty string value, it means no valid session | |
| // is active. Running the query would produce a MySQL | |
| // warning (1292: Truncated incorrect DECIMAL value) | |
| foreach ($entity_restrict as $criterion) { | |
| if (is_array($criterion) && in_array('', $criterion, true)) { | |
| return $available_blocks; | |
| } | |
| } | |
| $activeentities = getEntitiesRestrictCriteria(PluginFieldsContainer::getTable(), '', '', true); | |
| if ($activeentities === []) { | |
| return []; | |
| } | |
| $entity_restrict = isCommandLine() ? [] : $activeentities; |
There was a problem hiding this comment.
getEntitiesRestrictCriteria returns an array of criteria. The problem here isn't that getEntitiesRestrictCriteria returns an empty array; rather, it returns the criterion glpi_plugin_fields_containers.entities_id = "", so the suggestion won't fix the problem.
There was a problem hiding this comment.
That's true, but ultimately I think the problem lies in the getEntitiesRestrictCriteria() function, which returns an invalid value. Instead of returning ...entities_id = '' (which can never work), it should return [new QueryExpression(‘false’)];
For your information, it also checks isCommandLine(), so the one here will likely be redundant.
There was a problem hiding this comment.
You're right, the root cause is actually in the GLPI core — DbUtils::getEntitiesRestrictCriteria() in DbUtils.php.
When called with no active session, no CLI context, and no cron context, the function falls through all conditions without an else branch, leaving $value as an empty string ''. This produces an invalid SQL criterion entities_id = '' on an integer column, which triggers MySQL warning.
I've updated this with a cleaner guard:
if (!Session::getLoginUserID() && !isCommandLine()) {
return $available_blocks;
}This is semantically explicit (no session = no available blocks), consistent with the existing guard already in place in setup.php l.138 — it doesn't rely on inspecting the internal structure of the criteria returned by core.
The real fix should live in the core: adding an else { return [new QueryExpression('false')]; } branch to getEntitiesRestrictCriteria() so all callers are protected. I'll open a separate PR on the GLPI core for that.
There was a problem hiding this comment.
here is the fix for the core : glpi-project/glpi#23934
Checklist before requesting a review
Please delete options that are not relevant.
Description
When initializing the plugin during boot (before any user session),
getEntitiesRestrictCriteria()was called without an active session, producingentities_id = ''in the SQL query and triggering a mass MySQL warning (1292: Truncated incorrect DECIMAL value).Fix by skipping entity restriction when no user is logged in (
!Session::getLoginUserID()), consistent with the existing check already in place insetup.php.Error :
Close #1172