I wrote an install profile implementing the API that laid out permissions in the code likeso:
$perms = array('
access site-wide contact form,
access content,
search content,
access comments,
post comments,
');
install_add_permissions(1, $perms);
To my surprise, though a site created with this profile showed the correct permissions marked in the checkboxes under admin/user/permissions, the permissions weren't actually in effect (anonymous role couldn't see the contact form, couldn't leave a comment etc). And yes comments were enabled for the specific node type, and set to default read/write, per the rest of my profile.
See attached screenshot to see the permissions are indeed checked on a fresh install from code per the array above.
The next surprise was, if I made no changes on the admin/user/permissions page at this point, but submitted the form, magically the perms started to take effect!
While reproducing, I noticed that when the array of perms was laid out in a more readable format as above, the permissions table's values looked the same. That is:
mysql> select perm from permission where rid = 1;
+-----------------------------------------------------------------------+
| perm |
+-----------------------------------------------------------------------+
| access site-wide contact form,
access content,
search content,
access comments,
post comments,
, access content |
+-----------------------------------------------------------------------+
1 row in set (0.00 sec)
When that permissions form is submitted, the data is re-assembled in the database to look like this:
mysql> select perm from permission where rid = 1;
+-----------------------------------------------------------------------+
| perm |
+-----------------------------------------------------------------------+
| access site-wide contact form, access content, search content, access comments, post comments, access content |
+-----------------------------------------------------------------------+
1 row in set (0.00 sec)
and this lack of whitespace appears to make all the difference. (and notice the duplicate 'access content' ?! I definitely didn't define that permission twice in my profile.)
Editing my code to remove the 'readability' that I prefer, and making it match likeso:
$perms = array('access site-wide contact form, access content, search content, access comments, post comments');
fixes it, and permissions work straight off the bat on future builds.
I tried to leave my code the way I wanted it and throw in a node_access_rebuild() or something later in my install profile in the hope I could automate that re-assembling of permissions, but no luck.
Now, not really sure this is a bug, I'm not much of a coder so maybe my way of writing that array is considered poor form. But I'm wondering if the install_add_permissions array merge should trim the whitespace before inserting into the db?
I mean, Drupal's clearly smart enough to read each permission despite the whitespace from the table, which surprised me in itself, because I wouldn't have expected the checkboxes to be ticked for each permission on viewing the permission table. So it seems it's other modules such as Comment, trying to read back from the permission table to see if (in this case) the anonymous user can comment, and being unsuccessful until the format of the data in that column is re-shuffled. As if you notice that extra comma before the duplicate 'access content' permission, it is almost as though the whitespace means the other permissions are treated as one long item in the array or something?
Let me know if I can provide any more info, I just thought I'd share this since others might run into this sort of problem if they're like me and don't like seeing that long one-liner array of permissions!
| Comment | File | Size | Author |
|---|---|---|---|
| screenshot_permissions.png | 56.04 KB | mig5 |
Comments
Comment #1
James Andres commentedEach permission must be each a separate array item. On permission per array key.
Try this:
Good luck!
-- James
Comment #2
James Andres commentedComment #3
Anonymous (not verified) commentedOops, my bad. Sorry for the noise! Thanks a lot.