The short version: _upload_form() should be using a form #access property to set its requirement for upload files permission, and not hard-code it in the module logic. Because it does not do this, the only way to extend this form from a contributed module is to hack core.
The long version: I was asked by a client how to extend comment_upload.module so that it provided a permission "upload files to comments" that could be given out to certain low-level users, who don't have "upload files" permissions, since that's reserved for a more powerful role. Sure, you're thinking, throw in a hook_perm(), and modify the permission check in comment_upload.module. Bing, bang, done, right? WRONG. ;)
Why? Because comment_upload.module is calling _upload_form() to place the upload form in the comment. This is because it would be silly to duplicate all of that function's code in comment_upload.module, both because it's annoying, and also because if there were ever important security fixes to this part of code, comment_upload.module's version might fall out of date. Not good.
Unfortunately, _upload_form() has this dandy piece of code:
if (user_access('upload files')) {
// Make the form here.
}
which results in absolutely nada being returned back to comment_upload.module's form.
If, however, we did $form['#access'] = user_access('upload files'), /then/ comment_upload.module could use hook_form_alter() to have it check "upload files to comments" instead of the more generic "upload files."
Whew!
Anyway, patch forthcoming.
| Comment | File | Size | Author |
|---|---|---|---|
| #11 | upload_226853.patch | 8.95 KB | drewish |
| #8 | upload-fix-access-check-226853-8-6.x-7.x.patch | 3.55 KB | webchick |
| #5 | 226853.test_.txt | 1.45 KB | vladimir.dolgopolov |
| #2 | upload-fix-access-check-226853-2-5.x.patch | 1.78 KB | webchick |
| #1 | upload-fix-access-check-226853-1-6.x-7.x.patch | 2.49 KB | webchick |
Comments
Comment #1
webchickPatch for 6.x and 7.x.
Comment #2
webchickAnd here's one for 5.x.
Comment #3
webchickOopsie.
Comment #4
keith.smith commentedI know your just moving this line around, but what's with:
Doesn't "Images are larger than %resolution will be resized." have an extra "are" in it or something?
Comment #5
vladimir.dolgopolov commentedHere is the test for Simpletest module for the issue.
My first attempt was create a test module for this which called _upload_form() and put its own perm for uploads.
But it's better to isolate a test as far as possible.
There are 2 checks: for 'upload files' permission and for 'upload files to comments' (as an example).
Comment #6
gábor hojtsy@webchick: patch looks good on short review.
@keith.smith: yeah, you are right
@vladimir: you'd sill need a custom form alter hook to alter the form and its #access check to the permission you'd like to check
Comment #7
vladimir.dolgopolov commented@Gábor Hojtsy: It seems I have to generate a module in the test. Simpletest module can't do that itself, but I can.
Comment #8
webchickHere's a patch for 6.x/7.x that fixes the string bug found by Keith Smith. Thanks, and thanks a LOT for the test-writing, Vladimir!! :) You rock!
5.x doesn't have the sloppy string so old patch still applies.
Comment #9
vladimir.dolgopolov commentedHere is improved test for the issue.
The main idea: create simple module with hook_perm() and hook_form_alter() and alter a form according permissions.
After drupal_prepare_form() check for permissions the form has.
Test is too big because of a module creation stuff here.
Comment #10
drewish commentedwanted to point to a similar issue #247095: Upload.module hard-codes 'view uploaded files' permission check, i'll try to test this out this afternoon.
Comment #11
drewish commentedhere's a re-roll with vladimir.dolgopolov's test included. i will say that this test freaks me the flip out. we now got the ability to have hidden modules so we don't need to be creating them dynamically but i'm a bit unsure where the module should live.
Comment #12
webchickI think I would rather fix this in 7.x with #563000: Provide upgrade path to file. So, assuming that patch makes it in, this is a 6.x-only bug.