Problem/Motivation
Users that can create media but not edit them are able to change authoring information.
This is not ideal if you want to allow anonymous users to create media.
Steps to reproduce
I've given "anonymous" and "authenticated" the following permissions:
- 'create news content' so that they can submit news articles (nodes) without logging in
- 'create image media' so that they can submit images to attach to their news articles
At /node/add/news, an anonymous user does not get access to change the authoring information.
At /media/add/image, an anonymous user does get access to change the authoring information, and can change the "authored by" from anonymous to anyone they want, and the same with "authored on" (see attached image).
Proposed resolution
We should allow editing these fields only the user has the permission to edit media.
Remaining tasks
User interface changes
Introduced terminology
API changes
Data model changes
Release notes snippet
| Comment | File | Size | Author |
|---|
Issue fork drupal-2973447
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:
- 2973447-
changes, plain diff MR !15167
- 2973447-anonymous-users-can
compare
Comments
Comment #2
George Bills commentedThe following patch seems to work for me - it's a one liner to hide the authoring information behind the "administer media" permission.
Comment #3
cilefen commentedComment #5
dhirendra.mishra commentedI am attaching patch below.kindly test and review it.
Comment #7
George Bills commentedMaybe I'm missing something dhirendra.mishra, but how does MediaForm get $currentUser set?
MediaForm extends ContentEntityForm,ContentEntityForm extends EntityForm,EntityForm extends FormBase, FormBase has a protectedfunction currentUser()that just does areturn \Drupal::currentUser();. So my first patch just calls to currentUser() and checks perms that way.NodeForm uses a currentUser property, but that gets injected into its constructor, MediaForm doesn't have that property. So I don't think your patch can work, since your $this->currentUser will always be null.
The "proper" thing to do is probably to inject the current user in the constructor but I was trying to keep the diff as small as possible.
Comment #8
George Bills commentedReattaching the patch without the trailing whitespace on the last (not applied) line, since your autotester thingy refuses to apply with that whitespace there.
This is the "old" way calling to
$this->currentUser()function, coding standards might require using an injected$this->currentUservariable but as above I wanted to keep the diff small. ContentEntityForm that we inherit from calls out to$this->currentUser()a few times already anyway.Comment #9
George Bills commentedComment #10
George Bills commentedTake 3...
Comment #11
George Bills commentedComment #12
chr.fritschI think that makes sense. Could we get a test for that?
Comment #13
George Bills commentedNew patch with a test included. @chr.fritsch please give feedback on if the test meets standards - I tried to match what looked like the convention the original tests all use. Maybe it'd be nice to merge the "test an admin user" section with my new "test a normal user with the 'administer media' permission" section but I didn't want to change things too much.
Comment #14
George Bills commentedComment #15
chr.fritschThank you @George Bills. This looks great.
I only found one nitpick:
This line is not needed.
Could you also please upload a fail patch (a patch that only contains the new test)? That will prove the fix.
Comment #16
George Bills commentedThanks @chr.fritsch, new patches attached.
Comment #17
chr.fritschPatches should end on *.patch. *.txt will not be picked up by the testbot.
@George Bills, could you please re-upload your files with the correct file extensions.
Comment #18
nitebreedI re-uploaded the patches from #16 so the testbot can pick them up
Comment #20
chr.fritschThank you @Nitebreed.
This looks good to me now.
Comment #21
alexpottI'm not sure that this is the best fix. Looking at the node entity I think we should improve the field access to determine access and not only do this on the form level. See \Drupal\node\NodeAccessControlHandler::checkFieldAccess().
Comment #22
chr.fritschHere is a new patch that moves the access check to the field level.
Comment #23
seanbDo we really need users to have 'administer media' for them to publish/unpublish media? I know node is doing that, but just not sure if we should copy it.
Berdir pointed out that the default status could be 'Unpublished' and there could be some kind of workflow where you don't want users to publish the nodes, but I'm not sure how valid that argument is for media.
Removing the author and created field is probably fine for most sites, but removing the published checkbox for some users on existing sites is probably not.
Comment #24
phenaproximaTagging and escalating.
Comment #25
seanbBack to NW for the status field. I think we are also missing an interface test where users unpublish media via the interface. I would have at least expected some tests to fail after making a change like this.
Comment #26
wim leersI'd also question this.
Comment #27
webchickLooking at https://api.drupal.org/api/drupal/core%21modules%21media%21media.permiss... it looks like there's an "Update own media" permission, so maybe attach published/unpublished to that, if we're indeed talking only about personal media here? (You definitely don't want random weirdos unpublishing other peoples' media; though there's also an "Update any media" for that.)
Comment #28
seanbLinking the publishing permissions to edit sounds very reasonable. If you can change the value of the source field and the name, most of the times you would expect to be able to publish / unpublish as well. As long as we don't have more granular permissions in core, this is probably the best we can do.
Comment #29
wim leersThis sounds reasonable to me.
Comment #33
vsujeetkumar commented@seanB I have added test according to #25, Can you please check and advise these tests are up to the mark.
Comment #34
samiullah commented@vsujeetkumar, Looks good
@seanB This needs code review as well for tests before we move this one to RTBC
Comment #35
seanbThe suggestion from #25 to use
update media/update any mediainstead ofadminister mediastill needs to be implemented. Setting to needs for for that.Also
This line is longer than 80 chars.
Comment #36
vsujeetkumar commented@seanB I have done with the changes mentioned in #35, Please have a look and advise.
Comment #38
samiullah commented@vsjujeetkumar tests are failing, please recheck
Comment #39
seanbThe
update mediapermission is only for media owners/authors, so we should also check if the user is the owner.The
update any mediapermission is still missing.BTW, in #2862422: Add per-media type creation permissions for media we added per media type permissions and the generic permissions will be deprecated in #2925459: Deprecate generic media permissions.
You should probably take a look at
MediaAccessControlHandler::checkAccess().And then specifically this part:
Another thing is also that the caching is probably not correct for the code we currently have, so you also need to take a look at #2998824: MediaAccessControlHandler update/delete access caching is not correct which adds some changes to fix the caching.
Comment #40
phenaproximaComment #41
samiullah commentedNeeds updated patch for retesting
Needs fix
Comment #44
sakthivel m commented#44 Fixed whitespace errors and re created patch
Comment #53
prudloff commentedInstead of re-implementing half the logic from MediaAccessControlHandler::checkAccess(), I think we can simply call this method.
Comment #54
smustgrave commentedSince this is altering the access (all be it seems correct) think it needs a CR as it is altering behavior.
Comment #55
smustgrave commentedFelt bad so I went ahead did the CR, super simple one
Comment #56
godotislateSince the author field is hidden from the form to users who don't have edit access, I think there should be test assertions that the UID is still set to that creator on save of new media entity.