Skip to content

Fix can update item calls#23923

Open
SebSept wants to merge 8 commits intoglpi-project:mainfrom
SebSept:fix_canUpdateItem_calls
Open

Fix can update item calls#23923
SebSept wants to merge 8 commits intoglpi-project:mainfrom
SebSept:fix_canUpdateItem_calls

Conversation

@SebSept
Copy link
Copy Markdown
Contributor

@SebSept SebSept commented Apr 15, 2026

Checklist before requesting a review

Please delete options that are not relevant.

  • I have read the CONTRIBUTING document.
  • I have performed a self-review of my code.

This comment was marked as low quality.

Copy link
Copy Markdown
Member

@AdrienClairembault AdrienClairembault left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me 👍

Comment thread src/Glpi/Knowbase/SidePanel/HistoryRenderer.php Outdated
@SebSept SebSept self-assigned this Apr 16, 2026
Copy link
Copy Markdown
Member

@cedric-anne cedric-anne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need to take time for a precise review, to see if it will not cause issues in some specific case, for instance with a profile assigned to the simplified interface.

Anyway, if you used rector for this, then please propose a PR in our own rector extension (https://github.com/glpi-project/rector-glpi), with relevant tests. This will permit to detect unexpected uses of these methods in future PRs, and will also permit to plugins developers to use it in their plugins.

@SebSept
Copy link
Copy Markdown
Contributor Author

SebSept commented Apr 16, 2026

I need to take time for a precise review, to see if it will not cause issues in some specific case, for instance with a profile assigned to the simplified interface.

Anyway, if you used rector for this, then please propose a PR in our own rector extension (https://github.com/glpi-project/rector-glpi), with relevant tests. This will permit to detect unexpected uses of these methods in future PRs, and will also permit to plugins developers to use it in their plugins.

I'm not really sure about the custom rule. The changes made by the rule need to be reviewed before submitting, however, I'll submit the rule 👍🏼

@SebSept SebSept force-pushed the fix_canUpdateItem_calls branch from b4bafec to 5961014 Compare April 16, 2026 12:39
@cedric-anne
Copy link
Copy Markdown
Member

I need to take time for a precise review, to see if it will not cause issues in some specific case, for instance with a profile assigned to the simplified interface.
Anyway, if you used rector for this, then please propose a PR in our own rector extension (https://github.com/glpi-project/rector-glpi), with relevant tests. This will permit to detect unexpected uses of these methods in future PRs, and will also permit to plugins developers to use it in their plugins.

I'm not really sure about the custom rule. The changes made by the rule need to be reviewed before submitting, however, I'll submit the rule 👍🏼

Are some replacement invalid ? Is there a condition that can be used to identify them ?

@SebSept SebSept force-pushed the fix_canUpdateItem_calls branch from 5961014 to 898c83e Compare April 23, 2026 13:46
SebSept added 8 commits April 24, 2026 13:52
Doesn't deserve to be in rector.php because it sometimes generate redundant code (e.g. !ProjectTask::canUpdate() || !$projecttask->can($projecttask->getID(), UPDATE) )  .

Reapply "rector rule to replace canUpdateItem()"

This reverts commit b4bafec.
# Conflicts:
#	ajax/webhook.php

# Conflicts:
#	src/KnowbaseItemTranslation.php
something like
`!ProjectTask::canUpdate() || !$projecttask->canUpdateItem();`

becomes `!ProjectTask::canUpdate() || !$projecttask->can($projecttask->getID(), UPDATE);`

which is redundant and should be just `!$projecttask->can($projecttask->getID(), UPDATE)`

Maybe I can fix the rector rule ...
@SebSept SebSept force-pushed the fix_canUpdateItem_calls branch from 28e24ae to a0eda74 Compare April 24, 2026 12:48
@SebSept
Copy link
Copy Markdown
Contributor Author

SebSept commented Apr 28, 2026

Are some replacement invalid ? Is there a condition that can be used to identify them ?

we'll discuss about that in the PR (not yet issued) with the rector rule.

I can apply the same for sibbling methods (canViewItem()) in another PR.

Comment thread src/Glpi/Controller/Knowbase/KnowbaseItemController.php
Comment thread src/Glpi/Form/ServiceCatalog/Provider/KnowbaseItemProvider.php
Comment thread src/CommonITILTask.php
foreach ($iterator as $data) {
$item->getFromResultSet($data);
if ($item->canViewItem()) {
if ($item->can($item->getID(), READ)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, we are looping on a single itemtype so it will be more efficient to call canView before the loop and canViewItem inside.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

canViewItem() doesn't check is item is private.
by using can($item->getID(), READ), the code change add the check for isPrivate() which is a good thing.

Comment thread src/Group.php
Comment thread src/Link.php
Comment thread src/MassiveAction.php

// Check rights
if (!$item->canUpdateItem()) {
if (!$item->can($item->getID(), UPDATE)) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another loop here.

Comment thread src/Notepad.php

$item = $document_obj->fields;
$item['_can_edit'] = Document::canUpdate() && $document_obj->canUpdateItem();
$item['_can_edit'] = $document_obj->can($document_obj->getID(), UPDATE);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another loop here.

Comment thread src/Problem_Ticket.php
Comment thread src/Project.php

$project->fields = $subproject;
$item['_readonly'] = !Project::canUpdate() || !$project->canUpdateItem();
$item['_readonly'] = !$project->can($project->getID(), UPDATE);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another loop here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants