Problem/Motivation
Using content moderation module on a site where there is some modules which implements hook_node_grants() generate a fatal error when viewing node moderated which have not been yet published.
This issue is a follow up of #2821599: Coexistence of PBF and content_moderation, where i provided a temporary workaround.
I can reproduce this bug with any modules which implements hook_node_grants(). Tested with Permissions by fields, Content Access and a tiny custom My module which implements only an empty hook_node_granst() function.
Steps to reproduce :
- configure a new clean install of drupal 8.2.1
- enable the module content_moderation and configure it to be used on the content type "Article"
- create a new role named "Editor" and some users. Enable all the content_moderation permissions related to "Article" content type for Editor.
- Create two node Article 1 and Article2. Publish the Article 1. Let the Article 2 in draft state.
- Now the users belonging to the "Editor" role can see and edit the two articles written by each other.
- Enable pbf module (or any module which implements hook_node_grants()), or generate a custom module with Drupal Console and implements hook_node_grants() in this module.
- Editor users can see and edit Article 1
- When Editor users try to see Article 2, a fatal error occurs
Fatal error: Call to a member function isRevisionTranslationAffected() on null in core/modules/content_moderation/src/ParamConverter/EntityRevisionConverter.php on line 101
The acquireGrants method in NodeAccessControlHandler provide a default grants[] only if a node is published.
if (empty($grants) && $node->isPublished()) {
$grants[] = array('realm' => 'all', 'gid' => 0, 'grant_view' => 1, 'grant_update' => 0, 'grant_delete' => 0);
}
Then for nodes which have never been published, the getLatestRevisionId() method in the Class ModerationInformation returns always an empty value, and then the fatal error occurs.
Proposed resolution
Check if the latest_revision is an instance of EntityInterface, in the convert method of EntityRevisionConverter Class. See patch attached.
Remaining tasks
Review the patch and/or
Is there an another root cause of this issue ?
User interface changes
No changes
API changes
I believe not
Data model changes
I don't think
Comment | File | Size | Author |
---|---|---|---|
#46 | interdiff.txt | 3.79 KB | amateescu |
#46 | 2821716-46.patch | 4.6 KB | amateescu |
#41 | interdiff-38-41.txt | 1.44 KB | flocondetoile |
#41 | 2821716-41.patch | 5.89 KB | flocondetoile |
#38 | content_moderation_fatal_error_node_grants-2821716-38.patch | 5.98 KB | anish.a |
Comments
Comment #2
flocondetoileThe patch
Comment #3
flocondetoileComment #5
flocondetoileOups. Wrong root directory
Comment #6
gabesulliceI have replicated this issue and can confirm that the #5 resolves the issue for me.
Comment #7
alexpottNice bug finding and the fix looks correct. We need an automated test for this that pretty much does what the excellent steps to reproduce in the issue summary outlines.
Comment #8
alexpottComment #9
flocondetoileWorking on this
Comment #10
flocondetoileAttached a test which reprodruce this issue. It should failed, because the fix is not included.
Comment #12
flocondetoileThe patch with the fix. Should pass
Comment #14
flocondetoileFixing the two exceptions caused by the getLastestRevision method which return NULL if node has never been published
Comment #16
flocondetoileIs it a fail from the testbot ? The test pass well locally. Let's retry.
Comment #17
flocondetoileComment #18
flocondetoileComment #19
flocondetoileComment #20
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedNits ahoy! Ye have been warned.
Can you upload a test-only patch to demo the fail?
Style thing, but can't both account properties be next to each other?
nit, trailing comma
You can use \Drupal\simpletest\NodeCreationTrait::getNodeByTitle
Can we just assert the success message you get from the UI when you create content?
A method would also mean we wouldn't need extra variables for the paths.
extra newlines
This block is used 3 times, maybe we should create a method called assertPathResponses with three arguments, one for each status code?
trailing newline
Comment #21
flocondetoileAttached the patch updated with your feedback.
The patch in #10 do it.
Comment #22
flocondetoileComment #23
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedI didn't realise this test was actually pretty much what \Drupal\content_moderation\Tests\NodeAccessTest. Maybe some others can chime in here, but why not keep "content_moderation_test_grants" and just enable that during that test. Saves us from duplicating a fair bit test code.
Comment #24
flocondetoileSorry for the delay. Here a simplier patch (NodeAccessTest modified only with the test module) which demonstrate the fatal error
Comment #26
flocondetoileAn another test only patch. Because I need the admin user has the "bypass node access" in order to be able to retrieve the node created. And because I need to test access from normal user before the admin user publish the test node.
Comment #28
flocondetoileThe last patch with the fix now
Comment #29
timmillwoodComment #30
timmillwoodLooks good to me.
I hate that we need to add new modules for tests like this, but I guess that's the price we pay for awesome test coverage.
Also going to add a test against 8.3.x, a lot has changed in Content Moderation, so need to make sure it works.
Comment #31
timmillwoodLooks like this needs re-rolling for 8.3.x
Comment #32
anish.a CreditAttribution: anish.a at Axelerant commentedRerolled to 8.3.x
Done for demonstration at Kerala Ruby Users Group meetup on patch rerolls.
Comment #34
Thew CreditAttribution: Thew at Google Code-In commentedComment #35
Thew CreditAttribution: Thew at Google Code-In commentedTesting reroll.
Comment #37
Thew CreditAttribution: Thew at Google Code-In commentedComment #38
anish.a CreditAttribution: anish.a at Axelerant commentedMessed up previous reroll
Comment #39
timmillwoodIt'd be nice to get an interdiff to see what's changed between the patches. I still don't think #38 will pass against 8.3.x because of the workflows module.
Comment #41
flocondetoilePermissions on the patch fixed. Should pass.
Comment #43
flocondetoileFails seems unrelated to the patch. Test relaunched.
Comment #44
Sam152 CreditAttribution: Sam152 as a volunteer and at PreviousNext commentedThe fail looks genuine.
Comment #46
amateescu CreditAttribution: amateescu for Pfizer, Inc. commentedI hit this problem a lot while working on the tests for #2858431: Book storage and UI is not revision aware, changes to drafts leak into live site, so here's a cleaned up patch which should be good to go for 8.4.x.
This is the reason for the recent test fails. Accessing the field item list object is not the same thing as accessing one of its properties :)
Also, we don't need a new module that implements
hook_node_grants()
, we havenode_access_test_empty.module
for that.Comment #47
timmillwood#46 looks good.
Comment #48
alexpottCommitted and pushed 2dad9ec to 8.4.x and 670c1a8 to 8.3.x. Thanks!
@timmillwood, @Sam152, @gabesullice - thanks for reviewing the patch. There were no comments that resulted in patch changes or provided test evidence that would result in commit credit. And of course thanks @flocondetoile, @anish.a, @Thew and @amateescu for working on the patch.
Comment #51
timmillwoodSeems like this issue also fixed #2838452: Permissions: View any unpublished content not working.
Comment #53
eyilmazShouldn't $original_entity->moderation_state be $original_entity->moderation_state->value here ??
Comment #54
timmillwood@eyilmaz - this was committed nearly 2 months ago, if you are seeing issues, please open a new issue.
Comment #55
amateescu CreditAttribution: amateescu for Pfizer, Inc. commented@eyilmaz, that does look a bit weird indeed. Do you mind opening a new issue to investigate the change you proposed and link it back here so everyone who worked on this can follow along?
Thanks!
Comment #56
eyilmazUpdated the patch here:
https://www.drupal.org/node/2848775
since it was more or less related.