I have a project in which I wanted to give node preview view permissions to field (so anonymous users can preview their nodes) but not give anonymous users actual access to the fields. I was about to work on this on the theme, while I thought that extending field_permissions was not going to be that difficult.

I then thought of submitting a patch for this permissions, but as being too specific, I thought it was hard for it to be accepted.

Instead I modified field_permissions to allow other modules to add permissions and also grant access depending on those permissions, this can make use of all of the module UI which is nice while not altering too much the functionality.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

hanoii’s picture

Category: bug » feature
hanoii’s picture

Status: Active » Needs review
FileSize
4.22 KB

Attached is a patch that adds one alterer and one hook. The alterer allows other modules to alter the permissions list and the hook allow the modules to grant access to them.

I also added an api php file with documentation, and I use my module for granting view on node preview as an example for it.

Status: Needs review » Needs work

The last submitted patch, 1448154-2-field_permissions-allow_module_extensions.patch, failed testing.

hanoii’s picture

Status: Needs work » Needs review
FileSize
3.8 KB

Properly formatted patch.

Matthew Davidson’s picture

Status: Needs review » Reviewed & tested by the community

Works like a charm for me. And kudos for updating the documentation; I never think to do that.

drewmacphee’s picture

Worked for me too, can we get a commit?

Bohus Ulrych’s picture

Issue summary: View changes

This new hook functionality works great! It will be nice to have it implemented in the module directly.

ndf’s picture

Status: Reviewed & tested by the community » Needs work

You can grant permission with this patch for new/custom field-permissions. Though hook_field_access works inverted: if not any module denies, then grant. field_access() in field.module will deny access for the first module that returns FALSE.

Off-course it is nice to grant behaviour, but you cannot trust on it.

So my 2ct is to reverse the behavior, and also give the alterer the option to

  1. deny access for custom field-permissions
  2. un-deny (not grant) access for existing permissions

We could wait until field_permissions_field_access() is almost finished, and then invoke the hook where you can change the field_permission before returning it.

  • mariacha1 committed 10ed068 on 7.x-1.x authored by hanoii
    Issue #1448154 by hanoii: Allow other modules to extend this module via...
mariacha1’s picture

Status: Needs work » Fixed
FileSize
72.55 KB
89.9 KB

I've committed this with some changes based on #8:

As @ndf mentions, the concept of returning TRUE and short-circuiting out of the permissions check isn't in keeping with the D7 field access model. The model only cares about when you return FALSE.

I've changed the api function to allow you to further restrict access to permissions, but not to attempt to grant new ones.

This means in a scenario like the one originally described, where I wanted to grant a role permission to view a field, but only on the "teaser" view mode, would need to be implemented in the following way:

  1. First, figure out how to pass the view mode into this function. (It's not, from what I can tell, part of any of the data that's being passed in right now.)
  2. Second, give the ability to view the field to anyone on the at the generic level
  3. Third, remove the ability to view the field at the more specific level (view mode, in this example)

Here's an illustration:

Good:

Permissions flow downward from generic to specific

Bad:

Permissions do not flow upwards from specific to generic

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.