Problem/Motivation

By default content moderation module gives View any unpublished content permission to view the all the unpublished content in the site. But we shouldn't able to restrict the unpublished content view access of particular content type. So this will be too permissive when complex workflows enanled.

Proposed resolution

Per bundle unpublished permission
Add view any unpublished [ENTITY_TYPE:BUNDLE] content permissions.

Remaining tasks

  • Make the code change
  • Add tests

User interface changes

API changes

Data model changes

CommentFileSizeAuthor
#56 2875867-56.patch19.69 KBkevin.dutra
#49 Screenshot from 2020-01-03 10-04-46.png14.91 KBiyyappan.govind
#46 interdiff-42-46.txt1.39 KBiyyappan.govind
#46 2875867-46.patch19.67 KBiyyappan.govind
#42 interdiff-40-42.txt1.52 KBiyyappan.govind
#42 2875867-42.patch19.66 KBiyyappan.govind
#40 content-permissions-2875867-40.patch19.62 KBMadhura BK
#40 interdiff-2875867-39-40.txt2.3 KBMadhura BK
#33 2875867-33.patch19.25 KBjhedstrom
#33 interdiff-2875867-29-33.txt2.55 KBjhedstrom
#29 2875867-29.patch19.58 KBjhedstrom
#26 2875867-26.patch19.58 KBjhedstrom
#26 interdiff-2875867-24-26.txt781 bytesjhedstrom
#24 2875867-24.patch19.44 KBjhedstrom
#24 interdiff-2875867-18-24.txt1.71 KBjhedstrom
#18 2875867-18.patch18.98 KBjhedstrom
#18 interdiff-2875867-14-18.txt3.63 KBjhedstrom
#14 2875867-14.patch15.35 KBjofitz
#11 2875867-11.patch15.3 KBjhedstrom
#11 interdiff-2875867-08-11.txt2.47 KBjhedstrom
#8 2875867-08.patch14.18 KBjhedstrom
#8 interdiff-2875867-06-08.txt6.47 KBjhedstrom
#6 2875867-06.patch12.21 KBjhedstrom
#6 interdiff-2875867-04-06.txt952 bytesjhedstrom
#4 2875867-04.patch11.28 KBjhedstrom

Issue fork drupal-2875867

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

jhedstrom created an issue. See original summary.

timmillwood’s picture

There's also talk about moving the view any unpublished content to core rather than content_moderation. #273595: Move permission "view any unpublished content" from Content Moderation to Node

jhedstrom’s picture

jhedstrom’s picture

Status: Active » Needs review
FileSize
11.28 KB

Here's a first attempt at this.

Status: Needs review » Needs work

The last submitted patch, 4: 2875867-04.patch, failed testing. View results

jhedstrom’s picture

Status: Needs work » Needs review
FileSize
952 bytes
12.21 KB

This updates the previously failing test class to utilize the class resolver service instead of directly instantiating the permissions class.

Sam152’s picture

Status: Needs review » Needs work

Review as follows. I know there has been some friction in other issues that introduce a lot of permissions. I think it makes sense though because node allows you to customise everything per-bundle.

  1. +++ b/core/modules/content_moderation/src/Permissions.php
    @@ -2,17 +2,69 @@
    +      $container->get('entity_type.manager'),
    

    We could just inject the result of getDefinitions() here if we aren't using all of entityTypeManager.

  2. +++ b/core/modules/content_moderation/src/Permissions.php
    @@ -35,4 +87,28 @@ public function transitionPermissions() {
    +        foreach ($this->bundleInfo->getBundleInfo($entity_type->id()) as $bundle_id => $bundle_information) {
    

    It would be good to have a test for an entity without bundles and ensure we don't get two permissions that do that same thing (wouldn't want a permission for "entity_test" and "entity_test:entity_test").

  3. +++ b/core/modules/content_moderation/src/Permissions.php
    @@ -35,4 +87,28 @@ public function transitionPermissions() {
    +          if ($this->moderationInfo->shouldModerateEntitiesOfBundle($entity_type, $bundle_id))
    +          $permissions["view any unpublished {$entity_type->id()}:{$bundle_id} content"] = [
    +            'title' => $this->t('%entity_type: View any unpublished %bundle content', [
    +              '%entity_type' => $entity_type->getLabel(),
    +              '%bundle' => $bundle_information['label'],
    +            ]),
    +          ];
    

    Missing braces and indent.

jhedstrom’s picture

Status: Needs work » Needs review
FileSize
6.47 KB
14.18 KB

I think this addresses #7.

I was going to add additional entity/bundle tests to ContentModerationPermissionsTest, but wanted to use the test entity types, and discovered #2886203: Clarify scope of each entity_test entity type. Rather than block this on that issue, we could use node to test here...

timmillwood’s picture

I think this looks pretty good, very tempted to RTBC, but two very minor comments first, so leaving as Needs Review:

  1. +++ b/core/modules/content_moderation/src/Access/LatestRevisionCheck.php
    @@ -57,6 +57,12 @@ public function access(Route $route, RouteMatchInterface $route_match, AccountIn
    +      if (!$access_result->isAllowed()) {
    ...
           if (!$access_result->isAllowed()) {
    

    Something does feel right about doing this same check twice. I guess it makes sense, if access is not allowed check this, if access is still not allowed check this, etc. Although I wonder if there is a better solution.

  2. +++ b/core/modules/content_moderation/src/Permissions.php
    @@ -35,4 +87,29 @@ public function transitionPermissions() {
    +    foreach ($this->entityDefinitions as $entity_type) {
    +      if ($this->moderationInfo->canModerateEntitiesOfEntityType($entity_type)) {
    +        foreach ($this->bundleInfo->getBundleInfo($entity_type->id()) as $bundle_id => $bundle_information) {
    +          if ($this->moderationInfo->shouldModerateEntitiesOfBundle($entity_type, $bundle_id)) {
    

    I'm surprised we don't have one or more get methods in ModerationInformation that would do this. Like getBundlesThatShouldModerate() or something.

jhedstrom’s picture

Something does feel right about doing this same check twice. I guess it makes sense, if access is not allowed check this, if access is still not allowed check this, etc. Although I wonder if there is a better solution.

This level of verbosity for access-related things was initially introduced in this case in #2865498: Latest revision tab should respect 'view own unpublished content' permission, so it seemed to make sense to continue that pattern.

jhedstrom’s picture

FileSize
2.47 KB
15.3 KB

Since the test entity with bundle isn't currently revisionable, this adds node types to the kernel test so that both bundle and non-bundle entity types are tested.

Like getBundlesThatShouldModerate() or something

I didn't see anything like that... we could add it in a follow-up?

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.0-alpha1 will be released the week of July 31, 2017, which means new developments and disruptive changes should now be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

timmillwood’s picture

Status: Needs review » Needs work
Issue tags: +Needs reroll
jofitz’s picture

Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
15.35 KB

Re-rolled.

vijaycs85’s picture

jhedstrom’s picture

Status: Needs review » Needs work

Patch no longer applies due to some coding standards fixes.

Also, it needs to be updated to include the new permission in content_moderation_entity_access():

function content_moderation_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
  /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
  $moderation_info = Drupal::service('content_moderation.moderation_information');

  $access_result = NULL;
  if ($operation === 'view') {
    $access_result = (($entity instanceof EntityPublishedInterface) && !$entity->isPublished())
      ? AccessResult::allowedIfHasPermission($account, 'view any unpublished content')
      : AccessResult::neutral();
jhedstrom’s picture

Assigned: Unassigned » jhedstrom

Going to work on #16.

jhedstrom’s picture

Assigned: jhedstrom » Unassigned
Status: Needs work » Needs review
FileSize
3.63 KB
18.98 KB

This updates the entity access hook to add the new permission, and adds test coverage of that code path.

dawehner’s picture

Just a general comment. I would expect \Drupal\node\Plugin\views\filter\Status::query to be extended in some way or another here.

jhedstrom’s picture

Good point @dawehner! I hadn't even thought of that. Since it doesn't currently factor in the content moderation view any unpublished content permission either, perhaps this could be done in a follow-up issue?

timmillwood’s picture

  • I agree that #19 could be handled in a follow up.
  • It might be worth adding a test with \Drupal\entity_test\Entity\EntityTestNoBundle just to make sure when the $bundle is added to a permission name / title it still works ok when there is no bundle entity key on the entity.
  • Also, as I noted in #2, there is an effort to move permissions to core (#273595: Move permission "view any unpublished content" from Content Moderation to Node), I guess we'd do the same with the per-bundle permissions too.
oakulm’s picture

Status: Needs review » Reviewed & tested by the community

Hi,

I applied patch in #18 to clean Drupal 8.5.x

Made 3 roles, 2 content types and 2 users. As far as I can see the patch does the job.

Few observations:
Users can comment on unpublished nodes if they have "view any unpublished x content". This probably is as it should?
You can create permission set where user can create nodes (unpublished) but can't see them after creating them. This is also fine, I think.

There was one small glitch while patching:

patching file core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php
Hunk #1 succeeded at 184 with fuzz 1 (offset 33 lines).
timmillwood’s picture

Status: Reviewed & tested by the community » Needs work

I still think we need the extra test, as stated in #21.

jhedstrom’s picture

Status: Needs work » Needs review
FileSize
1.71 KB
19.44 KB

Thanks for the review @timmillwood.

This adds the no-bundle test entity. I've also added #2935732: The node views status filter is not aware of content moderation's unpublished permissions for the views issue @dawehner mentioned in #19.

For #273595: Move permission "view any unpublished content" from Content Moderation to Node, I'd say if this goes in first, it can be updated to move these as well (and the other way around should that go in sooner).

Status: Needs review » Needs work

The last submitted patch, 24: 2875867-24.patch, failed testing. View results

jhedstrom’s picture

Status: Needs work » Needs review
FileSize
781 bytes
19.58 KB

The fails in #24 were due to the change to this test in #2932154: ModerationInformation::getLatestRevisionId returns access-specific results which turned on node_access_test which was making the nodes private in the failing test. Since this test is about per-bundle permissions, the fix was to disable private nodes for the new test.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.0-alpha1 will be released the week of January 17, 2018, which means new developments and disruptive changes should now be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Georgii’s picture

I have just applied the patch in #26. Worked for me in my system with 4 content type. Tried different combinations. Viewed node/%nid/latest having different permissions selected. Changed permissions and tried again. All works as expected.

Thank you @jhedstrom !!!

jhedstrom’s picture

FileSize
19.58 KB
timmillwood’s picture

Status: Needs review » Reviewed & tested by the community

Looks good, think it's ready for committer review.

tstoeckler’s picture

Status: Reviewed & tested by the community » Needs review

Should we check whether permission_granularity is set to bundle for a given entity type before adding this to all entity types?

jhedstrom’s picture

Assigned: Unassigned » jhedstrom

Should we check whether permission_granularity is set to bundle for a given entity type before adding this to all entity types?

I had forgotten this existed at all. That makes a ton of sense. Working on that now.

jhedstrom’s picture

Assigned: jhedstrom » Unassigned
FileSize
2.55 KB
19.25 KB

This adds a check for permission granularity.

Since we don't have any test entity types that specify bundle as the permission granularity, this change also changes the test expectations, but I think that's ok.

Version: 8.6.x-dev » 8.7.x-dev

Drupal 8.6.0-alpha1 will be released the week of July 16, 2018, which means new developments and disruptive changes should now be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Manuel Garcia’s picture

Version: 8.7.x-dev » 8.8.x-dev

Drupal 8.7.0-alpha1 will be released the week of March 11, 2019, which means new developments and disruptive changes should now be targeted against the 8.8.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

alison’s picture

Hmm, I'm not sure #2809177: Introduce entity permission providers will cover this functionality...? It doesn't look like it -- the closest I'm seeing so far is "view own unpublished ENTITY_TYPE" -- not "view own unpublished (BUNDLE) ENTITY_TYPE," or "view unpublished ENTITY_TYPE" or "view unpublished (BUNDLE) ENTITY_TYPE".

I'll post a comment over there, too, just to see.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.0-alpha1 will be released the week of October 14th, 2019, which means new developments and disruptive changes should now be targeted against the 8.9.x-dev branch. (Any changes to 8.9.x will also be committed to 9.0.x in preparation for Drupal 9’s release, but some changes like significant feature additions will be deferred to 9.1.x.). For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Wim Leers’s picture

Status: Needs review » Needs work

LGTM, just nits 😅

  1. +++ b/core/modules/content_moderation/src/Permissions.php
    @@ -2,18 +2,69 @@
    +  protected $entityDefinitions;
    ...
    +   * @param \Drupal\Core\Entity\EntityTypeInterface[] $entity_definitions
    

    🤓 Nit: $entity_type_definitions would be more accurate.

  2. +++ b/core/modules/content_moderation/src/Permissions.php
    @@ -37,4 +88,29 @@ public function transitionPermissions() {
    +   * Returns an array of per-entity and bundle unpublished permissions.
    ...
    +   *   The per-entity-bundle permissions for viewing unpublished content.
    

    🤓 Nit: "per entity type", not "per entity"

  3. +++ b/core/modules/content_moderation/tests/src/Functional/NodeAccessTest.php
    @@ -184,4 +184,77 @@ public function testPageAccess() {
    +    // Access that the status field is no longer visible.
    

    🤓 s/Access/Assess/ or perhaps "assert".

Madhura BK’s picture

Status: Needs work » Needs review
FileSize
2.3 KB
19.62 KB

Have made changes suggested in #39.

Status: Needs review » Needs work

The last submitted patch, 40: content-permissions-2875867-40.patch, failed testing. View results

iyyappan.govind’s picture

Hi Folks, I have updated the patch to fix the coding standard and using the deprecated class usage. Please review it. Thanks

iyyappan.govind’s picture

iyyappan.govind’s picture

Status: Needs work » Needs review
Wim Leers’s picture

Status: Needs review » Needs work

Great! Just one detail was forgotten 😅

+++ b/core/modules/content_moderation/src/Permissions.php
@@ -2,18 +2,69 @@
+  protected $entityDefinitions;

This should be named $entityTypeDefinitions.

iyyappan.govind’s picture

Status: Needs work » Needs review
FileSize
19.67 KB
1.39 KB

Hi Wim,

I have updated patch with your suggestions mentioned in #45. Please review the patch. Thanks

Wim Leers’s picture

LGTM!

To reach RTBC, we need some meta work to be done too.

iyyappan.govind’s picture

Issue summary: View changes
iyyappan.govind’s picture

Issue summary: View changes
FileSize
14.91 KB
iyyappan.govind’s picture

Hi All, I have added the change record for this issue. Please review. Change record URL - https://www.drupal.org/node/3104146

Sam152’s picture

Status: Needs review » Needs work

Reviewing this with the benefit of some foresight, two things stand out to me as potential issues or caveats:

  • The node permission "view own unpublished content" doesn't have the same per-bundle support.
  • The existing permission is supported via \Drupal\node\Plugin\views\filter\Status, I don't know how these new permissions will interact with this existing feature.
+++ b/core/modules/content_moderation/tests/src/Unit/LatestRevisionCheckTest.php
@@ -106,27 +111,31 @@ public function testLatestAccessPermissions($entity_class, $entity_type, $has_pe
   public function accessSituationProvider() {
     return [
       // Node with global permissions and latest version.

I also think it's cleaner if all these test cases are additive instead of modified.

Version: 8.9.x-dev » 9.1.x-dev

Drupal 8.9.0-beta1 was released on March 20, 2020. 8.9.x is the final, long-term support (LTS) minor release of Drupal 8, which means new developments and disruptive changes should now be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

ressa’s picture

Adding related view_unpublished issue.

Version: 9.1.x-dev » 9.2.x-dev

Drupal 9.1.0-alpha1 will be released the week of October 19, 2020, which means new developments and disruptive changes should now be targeted for the 9.2.x-dev branch. For more information see the Drupal 9 minor version schedule and the Allowed changes during the Drupal 9 release cycle.

Version: 9.2.x-dev » 9.3.x-dev

Drupal 9.2.0-alpha1 will be released the week of May 3, 2021, which means new developments and disruptive changes should now be targeted for the 9.3.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

kevin.dutra’s picture

ressa’s picture

Thanks @kevin.dutra! Perhaps an interdiff can be added and Status changed to "Needs review", to get it tested?

kevin.dutra’s picture

@ressa, that was just a re-roll of the patch in #46, so it doesn't address the comments in #51 and as a result it's not ready for review. There's no interdiff because there are no code changes from the #46 patch, just adjustments to the code context so that Git can find the correct places to apply the changes.

ressa’s picture

I see, thanks for clarifying. I originally just did a quick comparison between the two patches, and saw a bigger change, which I thought was the removal of a service. Looking closer, I now see that that change is in the current 9.3.x-dev version.

TMWagner made their first commit to this issue’s fork.

TMWagner’s picture

@kevin.dutra For what it's worth, #56 applies cleanly to core 9.2.5

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.0-rc1 was released on November 26, 2021, which means new developments and disruptive changes should now be targeted for the 9.4.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.0-alpha1 was released on May 6, 2022, which means new developments and disruptive changes should now be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 10.1.x-dev

Drupal 9.5.0-beta2 and Drupal 10.0.0-beta2 were released on September 29, 2022, which means new developments and disruptive changes should now be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 10.1.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch, which currently accepts only minor-version allowed changes. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.