I'd like to be able to add or remove the "View" grant for anonymous users based on the value of a field on node.

Specific use case - I've got a client who gives grants to organizations. Part of their requirements when giving a grant is to receive annual reports on what the organization has done with the grant. The organization gets to choose whether any given report is to be "public" (anonymous users) or "private" (board members & the user who uploaded the report).

The goal is to make everything "private" by default, but to automatically add "View" access for anonymous users if "public" is selected in the node edit form.

The default "private" status works as expected. All I need to do now is figure out how to programmatically add "View" when "public" is selected.

I've spent a few hours Googling and not making much headway. Is there a simple way to do this that I have somehow just missed? Assuming I need to do this in code, is there a recommended way to do this?

I'm still learning the ins and outs of building modules. This initially seemed like it should be pretty straight forward, but there seem to be a number of different hooks that all seem equally plausible to use for this. I can't make anything I've tried so far work - any help / suggestions would be much appreciated.

Thanks!
Dan

Comments

dang42 created an issue.

StijnStroobants’s picture

Hi, I guess you can add this functionality in a hook_node_grants.

if ($op == 'insert' || $op == 'update') {
    $grants = array();
    $realm = 'nodeaccess_rid';

    if (isset($node->field_yourcheckbox[LANGUAGE_NONE][0]['value'])
      && $node->field_yourcheckbox[LANGUAGE_NONE][0]['value'] == '1') {
      $grant = array(
        'gid' => 2,
        'realm' => $realm,
        'grant_view' => 1,
        'grant_update' => 0,
        'grant_delete' => 0,
      );
      $grants[] = $grant;
    }
}

I'm not 100% sure this will work, but you can give it a try.