This is part of the #2028027: [META] Missing access control for base fields meta issue. Please read the meta issue for the reasoning.
This issue is for implementing default access for all base fields of a comment. We need to go through them and look for existing access restrictions and make sure they are covered as part of the default Access of their field's item classes.
Example: We've got a permission for editing user names, that needs to be covered.
What to do: For each field that has a default access being not TRUE, we
- have to add a custom FieldItem class extending the field type's one
- implement defaultAccess() for it
- write a unit test case to prove it works as it should
Comments
Comment #1
fagoComment #2
xjmComment #3
larowlanhaving a crack
Comment #4
larowlanSomething like so?
Comment #6
larowlanvalid fails, access controls++
Comment #7
wim leersPartial review, but for the majority of the patch.
Are these changes really necessary/in scope?
Double quotes around the permission name would increase legibility.
s/read only/read-only/
Why this change?
Please also tag with
@group Access.Missing
@inheritdoc.Unnecessary leading newline.
Two periods, should be one.
You use the shorthand array syntax for the first entry, but not for the second. Let's make that consistent? :)
This does not test an unpublished comment.
This is rather tricky to follow. You look at comments 1, 2, 3, 1, 2, 1 respectively. Wouldn't it be simpler to look at all combinations of all three comments and all three users, and set expectations for those combinations in advance, in an array?
This feels like a regression…?
Comment #8
larowlanYep unrelated hunks - here it is without them.
Will address other reviews tomorrow.
Fixes 7.12, 7.4, 7.1
Thanks for the review.
Comment #9
berdirI started to do this as well in #2227503: Apply formatters and widgets to Comment base fields.
Note that the access logic needs to be in sync with the current #access logic on the form elements to be able to actually rely on this, I don't think this is currently the case, because it means that it depends on field settings and is different for editing and adding comments.
If we can't or don't want to do that, we need to find a different solution there (separate form modes? Not sure yet..)
Comment #10
larowlanFixes #7
Using generatePermutations to cover all combinations actually found some missing parts - so great idea!
Not sure if I need to do anything here because of #9?
Comment #12
larowlanTipping those fails pertain to #9
Comment #13
berdirOne effect/goal of this is to make the #access in form elements of the comment form unecessary, because switching to widgets also means that field access will automatically be respected.
However, if you look at the #access checks there, they are much more complicated. They depend on the comment field settings (if anon users can not, can or must fill out the fields, and it also depends on whether the comment is new or not (admins are not "allowed" to set the name for a new comment, but they can edit the name of a comment by an anomyous user).
So if we want to make the custom #access checks there unnecessary (note that something like quickedit will not be able to respect custom #access logic in the form, although not really problematic here I think), we need to implement that logic.
In this case, editing the entity isn't possible right?
You don't need to check this. This is additionally to view/edit permission of the whole entity. Callers of field access() are expected to check that first if they are not already in a context where they can safely assume that this is possible (like an edit form).
Comment #14
larowlanNot sure what to do with the 'date' field - there is no 'date' field on Comment entity, so for now it stays as is?
Conversion to something else part of using a widget?
Comment #15
berdirYeah, date is changed to 'created' in my issue, just like we did for the node form.
Comment #16
berdirOh, you either need to keep the #access logic for now or check it yourself using $comment->field->access('edit'). Automatic checks only work for actual widgets. As I am converting that in my issue, I'd suggest to just leave it.
Comment #17
andypostI think "date" should go away, maybe file a critical to fix it?
homepage and subject could be changed by user, so homepage is not admin-field
Comment #18
larowlan#16 fixed
#17 fixed
Re 'date' » see #15
Comment #19
larowlanAnything more to do here?
Comment #20
andypostNow looks good
Comment #21
chx commentedSame mistake as znerol's patch a few days ago just even harder to see.
you wanted
Comment #22
larowlanGood catch, opened #2337227: NodeAccessControlHandler performs non-strict in_array check too
Comment #23
chx commentedThanks!
Comment #24
wim leersCould you please delay committing this patch until #2287071: Add cacheability metadata to access checks is committed? That ~300K patch is very, very painful to reroll and this one will conflict. Sorry, and thanks for your consideration. I will help with rerolling this patch if you like.
Comment #26
larowlanre-roll
Comment #27
larowlantrivial re-roll, back to rtbc - #2287071: Add cacheability metadata to access checks still to go in
Comment #28
alexpott#2287071: Add cacheability metadata to access checks is in
Comment #29
larowlanfixed for #2287071: Add cacheability metadata to access checks and took out references to non-existent domains in test urls/email addresses
Comment #30
jibranBack to RTBC
Comment #31
larowlanthanks @jibran
@Wim Leers can you eyeball my usage of the new api?
Comment #32
wim leersThe comment lists "name" and "mail", the code lists "name", "mail" and "homepage".
The combination of
allowedIf()andcachePerRole()has a short hand:allowedIfHasPermission().This works, but it feels a little bit strange. You say "first access result OR second access result", which is good. But then you apply all these cache metadata manipulations to the OR result. But they actually belong *on* the second access result.
So I'd change this code block to:
That makes it easier to follow :) And reduces risk of introducing mistakes in the future!
No comment for this one, even though there's a comment for everything else?
Analogously here.
The way this code falls back to the parent method is a bit problematic. It isn't in and of itself, but it is in combination with the above if-tests for the "view" and "edit" operations.
Let's take a look at the "view" case.
The "view" operation if-test above always takes permissions into account. That means that this is actually the "else" case for that same if-test, and therefore whatever the parent method returns should also be cached per role (
->cachePerRole()), because it means the user *does* have a certain permission, which has affected why this access result (the one from the parent method) was chosen!As a general rule, it's better to write access checking logic as follows:
rather than:
Because then it's much more obvious why the call to the parent method for a certain operation should associate certain cacheability metadata and not in another case.
I hope that was sufficiently clear.
Comment #33
larowlanComment #34
larowlan32.1 fixed
32.2 fixed
32.3 fixed
32.4 fixed
32.5 fixed
32.6 fixed in part need advise - see attached patch
Comment #35
wim leersLooks great, with one exception:
Isn't this a functional change?
Before, when the if-test evaluated to FALSE, the parent's
checkFieldAcces()method was called. That's no longer the case.Comment #36
larowlanSame patch as at #34 discussed with @Wim Leers on irc
Comment #37
larowlanQuestion: should we be blocking access to mail and host name to other than admim
Comment #38
wim leers#35 was answered; it's not a functional change, it was a bug in earlier patches. From an
AccessResultPOV, this is solid & RTBC.#37: Do you mean "homepage" instead of "hostname"? Nobody can edit the hostname. I'm also not sure what you mean, because anon users can edit their own mail and homepage in the current patch. Who else should be able to edit it?
Comment #39
larowlanOh yes you are right w.r.t host-name, no issue there√
With mail, I'm talking about 'view' access, not edit access.
Pretty sure we say 'private' on the email field for anonymous users.
So I think from an access point of view, we should alter the 'view' logic to make that field only visible to admins.
Thoughts?
Comment #40
wim leersThat sounds right to me, but I'm no Comment module expert.
Comment #41
larowlanOk, fixes that.
Comment #43
larowlandon't forget to merge
Comment #44
wim leersComment #45
catchThis is an oddity of comment module, but anonymous users can't really edit name/homepage/mail - they get to fill in those fields on creation. Could we make the comment a bit clearer?
Also can admin users really edit those fields even if the field isn't configured to allow them? That seems OK but also pointless.
I don't think we should allow admins to view hostname by default. Currently we dump that in the database (and there's an issue to stop doing that at all), then never display it anywhere.
Comment #46
wim leers#45.2: I think this only makes it so that if there were a formatter enabled for it by default, it would only be visible for admin users. But since there isn't, there's no actual change in the UI. @larowlan, please confirm.
Comment #47
larowlan45.1 Fixed comment
45.2 Fixed
Comment #48
wim leersThis also needs
->cachePerRole().Comment #49
larowlanfixed - thanks
Comment #50
wim leersComment #51
alexpottCommitted ee083e0 and pushed to 8.0.x. Thanks!
Fixed on commit - this class no longer exists and is not used :)