Use comment access handler instead of hardcoding permissions

Created on 17 May 2017, almost 8 years ago
Updated 20 October 2023, over 1 year ago

Problem/Motivation

The comment module comes with a CommentAccessControlHandler which is not really used on crucial places. Instead of using this handler, there are dozens of places, where the permissions provided by the comment module are checked directly (e.g. post comment permission checks instead of $entity->access('create') or access comments permission checks instead of $entity->access('view')).

For example, the $entity->access('create') access check is only used during comment link generation, but not for the comment form itself.

Due to this, it is currently impossible to create a custom access control handler for comment entities, as it would not be used where necessary. When reading through the code, you can also see there is a lot of access-related duplicate code (because the permission checks duplicate the access checks of the access control handler).

These changes will affect other issues as well, when those deal with comment access/permissions, but they are very important to get the comment entity access handling correct.

Proposed resolution

  • Scan through the code and eliminate all permission checks with proper $entity->access() checks. Search for the following permissions:
    • access comments
    • post comments
    • edit own comments

    The <em>administer comments</em> permission has been split in #3180177: [PP-2] Replaces usages of 'administer comments' permission w/ access handler checks โ†’ in order to limit the scope of this issue.

  • If possible, the commenting status of the parent ID should be taken into account in the access control handler checks. As this status flag affects the entity access for certain operations, its checks should also be centralized in the access control handler
  • On "create access" the commented entity and, on comment replying, the parent comment should be passed as context as they might contain valuable information for a custom comment entity access control handler when making the decision.
  • Comment-related tests should still work as before, because only the access checking behavior is changed, but not the access requirements themselves

Remaining tasks

None.

User interface changes

None.

API changes

The current comments access checks behaviour can be altered by swapping the comment access control handler and implementing a custom access logic.

Data model changes

None.

๐Ÿ“Œ Task
Status

Needs work

Version

11.0 ๐Ÿ”ฅ

Component
Commentย  โ†’

Last updated about 2 hours ago

Created by

๐Ÿ‡ฉ๐Ÿ‡ชGermany hctom

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡ฐ๐Ÿ‡ฌKyrgyzstan dan_metille

    Is the patch still needed with Drupal 10?
    As it is mentioned as 'required' by the https://www.drupal.org/project/group_comment โ†’ module, it would be great to have some update.
    Thanks for any feedback.

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands jaapjan

    Attached a patch for 10.1. Patch is based on 01ed69c2. Slightly adjusted for D10.

  • ๐Ÿ‡ณ๐Ÿ‡ฑNetherlands jaapjan

    It seems I introduced a type error in the last patch when the account is not set. Updated patch. (Only addition
    $account = $account ?: \Drupal::currentUser(); in CommentFieldItemList.php).

  • ๐Ÿ‡ง๐Ÿ‡ชBelgium keszthelyi Brussels

    This patch is identical to #136, only I removed one change introduced by this commit. I'm not sure if that permission check is needed (as view access on the parent comment is checked), and feels like it goes against the idea of the patch itself (to use access checks instead of permission checks). We use this patch together with Group Comment module, and that check would only allow replying to comments if we'd use the 'View comments' global permission, and we'd like to avoid that in favor of group permissions.

  • Pipeline finished with Failed
    about 1 year ago
    Total: 187s
    #126022
  • ๐Ÿ‡ฌ๐Ÿ‡งUnited Kingdom jaydenpearly

    Tested for version 10.3
    #136 and #137 didn't apply...

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States loze Los Angeles

    this is 137 rerolled for 10.3.x

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States loze Los Angeles
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States loze Los Angeles

    My patch in #139 was missing a use statement, this should work for 10.3.x

  • Pipeline finished with Failed
    6 months ago
    Total: 157s
    #298976
  • Pipeline finished with Success
    6 months ago
    Total: 349s
    #298982
  • ๐Ÿ‡จ๐Ÿ‡ฆCanada Liam Morland Ontario, CA ๐Ÿ‡จ๐Ÿ‡ฆ

    liam morland โ†’ made their first commit to this issueโ€™s fork.

  • ๐Ÿ‡จ๐Ÿ‡ฆCanada Liam Morland Ontario, CA ๐Ÿ‡จ๐Ÿ‡ฆ

    I have put the patch in #141 into the merge request. The person who opened the merge request, @claudiu.cristea, will need to update the merge request so that it targets branch 11.x.

  • Pipeline finished with Failed
    6 months ago
    Total: 298s
    #306480
  • Pipeline finished with Skipped
    6 months ago
    #307380
  • Pipeline finished with Success
    6 months ago
    Total: 176s
    #309114
  • Pipeline finished with Success
    6 months ago
    Total: 166s
    #309294
  • Pipeline finished with Failed
    6 months ago
    Total: 44s
    #312728
  • Pipeline finished with Skipped
    6 months ago
    #314147
  • Pipeline finished with Success
    6 months ago
    Total: 271s
    #314163
  • Pipeline finished with Failed
    6 months ago
    Total: 172s
    #318997
  • Pipeline finished with Failed
    6 months ago
    Total: 171s
    #321694
  • Pipeline finished with Failed
    6 months ago
    Total: 200s
    #321734
  • Pipeline finished with Failed
    5 months ago
    Total: 213s
    #321778
  • Pipeline finished with Success
    5 months ago
    Total: 146s
    #321958
  • Pipeline finished with Success
    5 months ago
    Total: 171s
    #322873
  • Pipeline finished with Failed
    5 months ago
    Total: 1829s
    #325080
  • Pipeline finished with Success
    5 months ago
    Total: 162s
    #332858
  • Pipeline finished with Success
    5 months ago
    Total: 144s
    #332931
  • Pipeline finished with Success
    5 months ago
    Total: 100s
    #335749
  • Pipeline finished with Failed
    5 months ago
    Total: 369s
    #338175
  • Pipeline finished with Failed
    5 months ago
    Total: 221s
    #338372
  • Pipeline finished with Failed
    5 months ago
    Total: 219s
    #338416
  • Pipeline finished with Failed
    5 months ago
    Total: 243s
    #338512
  • Pipeline finished with Skipped
    5 months ago
    #338718
  • Pipeline finished with Success
    5 months ago
    Total: 231s
    #342868
  • Pipeline finished with Success
    4 months ago
    Total: 219s
    #358505
  • Pipeline finished with Success
    4 months ago
    Total: 139s
    #359612
  • Pipeline finished with Failed
    4 months ago
    Total: 143s
    #364501
  • Pipeline finished with Success
    4 months ago
    Total: 206s
    #364543
  • Pipeline finished with Failed
    4 months ago
    Total: 238s
    #366704
  • Pipeline finished with Success
    4 months ago
    Total: 371s
    #368654
  • Pipeline finished with Success
    4 months ago
    Total: 145s
    #369615
  • Pipeline finished with Success
    4 months ago
    Total: 324s
    #369639
  • Pipeline finished with Success
    4 months ago
    Total: 190s
    #369655
  • Pipeline finished with Success
    4 months ago
    Total: 172s
    #369820
  • Pipeline finished with Failed
    4 months ago
    Total: 218s
    #373602
  • Pipeline finished with Success
    4 months ago
    Total: 174s
    #376239
  • Pipeline finished with Canceled
    3 months ago
    Total: 387s
    #383836
  • Pipeline finished with Success
    3 months ago
    Total: 390s
    #383841
  • Pipeline finished with Success
    3 months ago
    Total: 542s
    #383872
  • Pipeline finished with Success
    3 months ago
    Total: 124s
    #387949
  • Pipeline finished with Success
    3 months ago
    Total: 135s
    #387958
  • Pipeline finished with Success
    3 months ago
    Total: 142s
    #397884
  • Pipeline finished with Success
    3 months ago
    Total: 372s
    #398416
  • Pipeline finished with Success
    3 months ago
    Total: 526s
    #400397
  • Pipeline finished with Success
    3 months ago
    Total: 229s
    #402072
  • Pipeline finished with Success
    3 months ago
    Total: 142s
    #403867
  • Pipeline finished with Success
    2 months ago
    Total: 163s
    #405392
  • Pipeline finished with Failed
    2 months ago
    Total: 165s
    #409484
  • Pipeline finished with Failed
    2 months ago
    Total: 231s
    #409490
  • Pipeline finished with Failed
    2 months ago
    Total: 235s
    #409522
  • Pipeline finished with Failed
    2 months ago
    Total: 145s
    #409546
  • Pipeline finished with Failed
    2 months ago
    #409608
  • Pipeline finished with Failed
    2 months ago
    Total: 139s
    #409631
  • Pipeline finished with Success
    2 months ago
    Total: 132s
    #409691
  • Pipeline finished with Success
    2 months ago
    Total: 575s
    #411315
  • Pipeline finished with Failed
    2 months ago
    Total: 334s
    #411513
  • Pipeline finished with Failed
    2 months ago
    Total: 276s
    #411517
  • Pipeline finished with Failed
    2 months ago
    Total: 1487s
    #412540
  • Pipeline finished with Failed
    2 months ago
    Total: 269s
    #412727
  • Pipeline finished with Failed
    2 months ago
    Total: 264s
    #412747
  • Pipeline finished with Failed
    2 months ago
    Total: 279s
    #412752
  • Pipeline finished with Success
    2 months ago
    Total: 148s
    #413196
  • Pipeline finished with Canceled
    2 months ago
    Total: 124s
    #413676
  • Pipeline finished with Success
    2 months ago
    Total: 184s
    #413697
  • Pipeline finished with Failed
    2 months ago
    Total: 245s
    #413745
  • Pipeline finished with Success
    2 months ago
    Total: 154s
    #413975
  • Pipeline finished with Success
    2 months ago
    Total: 152s
    #413981
  • Pipeline finished with Canceled
    2 months ago
    Total: 137s
    #413996
  • Pipeline finished with Success
    2 months ago
    Total: 157s
    #414006
  • Pipeline finished with Success
    2 months ago
    Total: 228s
    #414040
  • Pipeline finished with Failed
    2 months ago
    Total: 150s
    #415965
  • Pipeline finished with Success
    2 months ago
    Total: 374s
    #418301
  • ๐Ÿ‡ท๐Ÿ‡ดRomania claudiu.cristea Arad ๐Ÿ‡ท๐Ÿ‡ด

    Done

  • ๐Ÿ‡ท๐Ÿ‡ดRomania claudiu.cristea Arad ๐Ÿ‡ท๐Ÿ‡ด

    But why did you squash? We lost the commit history :(

  • ๐Ÿ‡ฆ๐Ÿ‡บAustralia dpi Perth, Australia

    @claudiu.cristea if you want to rebuild you can try looking at the fork, and looking at the tags with `previous-` prefix. @ https://git.drupalcode.org/issue/drupal-2879087/-/network/9.2.x?ref_type... / https://git.drupalcode.org/issue/drupal-2879087/-/tags

  • Pipeline finished with Success
    about 2 months ago
    Total: 252s
    #423198
  • Pipeline finished with Success
    about 2 months ago
    Total: 135s
    #424282
  • Pipeline finished with Success
    about 2 months ago
    Total: 152s
    #424285
  • Pipeline finished with Failed
    about 2 months ago
    Total: 154s
    #424561
  • Pipeline finished with Success
    about 2 months ago
    Total: 167s
    #424674
  • Pipeline finished with Failed
    about 2 months ago
    Total: 331s
    #426674
  • First commit to issue fork.
  • Pipeline finished with Success
    about 2 months ago
    Total: 215s
    #428362
  • Pipeline finished with Success
    about 2 months ago
    Total: 177s
    #428367
  • Pipeline finished with Failed
    about 2 months ago
    Total: 138s
    #428567
  • Pipeline finished with Failed
    about 2 months ago
    Total: 131s
    #428619
  • Pipeline finished with Failed
    about 2 months ago
    Total: 1760s
    #428684
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium herved

    Update: I recreated the history from previous/2879087-comment-access-handler/2022-01-31 branch
    https://git.drupalcode.org/issue/drupal-2879087/-/tags/previous%2F287908...
    Merged 11.x, fixed conflicts, phpcs, phpstan and the tests touched by the patch.

    5 tests are failing at the moment, among those these are concerning to me and relate to the changes in CommentController::replyFormAccess:
    - core/modules/comment/tests/src/Functional/CommentNonNodeTest.php
    - core/modules/comment/tests/src/Functional/CommentAnonymousTest.php

    Also #137 may be right, I'll try to investigate those issues.

  • Pipeline finished with Failed
    about 2 months ago
    Total: 434s
    #429827
  • Pipeline finished with Failed
    about 2 months ago
    Total: 1310s
    #429915
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium herved

    For now here are patches of the current MR23 commit f2ac23e6.

  • Pipeline finished with Failed
    about 2 months ago
    Total: 1247s
    #430006
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium herved

    Important fix for cacheability:
    The CommentDefaultFormatterCacheTagsTest was failing because the CommentDefaultFormatter was not applying cacheability to the render array.

    ---

    On that note, we have a need in our project where the create comment permission will have a very high cache variance (per user).
    This means that currently, it would kill the Dynamic Page Cache when CommentDefaultFormatter checks for the create permission and adds the lazy_builder and cacheability to the render array.
    I propose to move the access check inside the lazy_builder callback, in CommentLazyBuilders::renderForm.

    I'd like to keep the scope to a minimum but this touches on the same lines and lots of projects may be in a similar scenario so we should try to preserve the page cache as much as possible. Viewing comment is less likely to have such requirements.
    The change is very small, but this could also be reverted and a separate issue or follow up instead if anyone objects.

  • Pipeline finished with Failed
    about 2 months ago
    Total: 426s
    #430047
  • Pipeline finished with Failed
    about 2 months ago
    Total: 1491s
    #430071
  • Pipeline finished with Failed
    about 2 months ago
    Total: 695s
    #430096
  • Pipeline finished with Failed
    about 2 months ago
    Total: 115s
    #430977
  • Pipeline finished with Failed
    about 2 months ago
    Total: 499s
    #430981
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium herved

    I pushed an attempt now to address #118 to #125 (reply to {$pid}).
    By calling $parent_comment->access('reply') in CommentController::replyFormAccess and doing the actual check in CommentAccessControlHandler::checkAccess

    Also, I had a look at the other issue โœจ [PP-1] Add thread depth configuration to comments Postponed which requires this but it seems to rely only on CommentAccessControlHandler::checkCreateAccess at the moment. Since we now have the reply access operation, I'm not even sure we need all the changes to $context (parent_comment, commented_entity).
    I'll sync with @claudiu.cristea on Monday.

    ---

    PS: still the same 2 (less concerning) test failures which I left out for now since I don't quite understand them yet.

  • Pipeline finished with Success
    about 2 months ago
    Total: 380s
    #431584
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium herved
  • Pipeline finished with Failed
    about 1 month ago
    Total: 411s
    #432620
  • Pipeline finished with Success
    about 1 month ago
    Total: 1088s
    #432724
  • Pipeline finished with Success
    about 1 month ago
    Total: 513s
    #433414
  • ๐Ÿ‡ง๐Ÿ‡ชBelgium herved
  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    smustgrave โ†’ changed the visibility of the branch 9.2.x to hidden.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    smustgrave โ†’ changed the visibility of the branch 2879087-use-comment-access to hidden.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    smustgrave โ†’ changed the visibility of the branch 2879087-comment-access-handler-9.4.x to hidden.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    smustgrave โ†’ changed the visibility of the branch 2879087-comment-access-handler-9.2.x to hidden.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    smustgrave โ†’ changed the visibility of the branch 9.4.x to hidden.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    smustgrave โ†’ changed the visibility of the branch 11.x to hidden.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    smustgrave โ†’ changed the visibility of the branch 10.3.x to hidden.

  • ๐Ÿ‡บ๐Ÿ‡ธUnited States smustgrave

    Issue summary needs to be flushed out.

    1. This is adding a new trait but not sure I see that mentioned.
    2. All the changes outside the comment module, to me seem out of scope.
    3. Adding a test EntityCreateAccessCustomStaticCidTest outside of comment not sure I see the why.

    Hiding all branches.

  • Pipeline finished with Success
    about 1 month ago
    Total: 164s
    #433936
  • Pipeline finished with Success
    about 1 month ago
    Total: 155s
    #434767
  • Pipeline finished with Success
    about 1 month ago
    Total: 264s
    #444565
  • Pipeline finished with Success
    28 days ago
    Total: 462s
    #446550
  • Pipeline finished with Success
    21 days ago
    Total: 206s
    #452143
  • Pipeline finished with Failed
    15 days ago
    Total: 237s
    #457393
  • Pipeline finished with Failed
    15 days ago
    Total: 191s
    #457395
  • Pipeline finished with Success
    15 days ago
    Total: 390s
    #457468
  • Pipeline finished with Success
    13 days ago
    Total: 152s
    #459018
  • Pipeline finished with Success
    13 days ago
    Total: 198s
    #459023
  • Pipeline finished with Success
    13 days ago
    Total: 154s
    #459037
  • Pipeline finished with Success
    12 days ago
    Total: 191s
    #459965
  • Pipeline finished with Failed
    11 days ago
    Total: 820s
    #460641
  • Pipeline finished with Success
    8 days ago
    Total: 160s
    #462580
  • Pipeline finished with Success
    3 days ago
    Total: 190s
    #467180
  • Pipeline finished with Success
    2 days ago
    Total: 319s
    #467195
  • Pipeline finished with Failed
    2 days ago
    Total: 356s
    #467196
  • Pipeline finished with Success
    1 day ago
    Total: 143s
    #468336
Production build 0.71.5 2024