Hi all,

Maybe I'm trying to accomplish something awfully trivial for you experts but here is the case I'm stuck with:

* I have created a CCK-content type that contains a 'Relevant to' field, which lists all users to which the node should be readable. It lists a number of users that have a certain role ('board members'). The NAMES of the users are stored, as - unless I'm mistaken - it is not possible to pass both keys and labels to a field when they need to be retrieved from the db

* The node should only be accessible to those users. That is, the board members for which the node is not relevant, should not be able to access the node

* Imho this means that I have to dynamically grant access rights to these users when they click the 'submit' button to save the node . However, I haven't got a clue how I should do this:

1. How can I extract the user id's based on the names selected in the 'relevant to' field?

2. How can I assign access rights to the selected users by means of PHP?

I was thinking that I should override a save node function, but I'm not sure which function and what functinos to call to change the access rights.

Can someone help me, if I've explained my problem clearly enough?

Thanks in advance for the effort!

Comments

jastraat’s picture

Mark,
Just to clarify, are you using user reference fields? (If not, that might be the first thing to do; the user id is the actual stored value in that case.)

While I haven't done this myself, I'd suggest using a combination of the following modules to get the functionality that you're looking for:
http://drupal.org/project/acl
http://drupal.org/project/content_access
http://drupal.org/project/rules

You can set up a rule to trigger on node save. And if acl and content access don't exactly work the way you need them you, you can use a rule to execute custom PHP assigning access rights.

infojunkie’s picture

I just finished implementing similar functionality in my project. Here's how I did it:

<?php
function yourmodule_node_access_records($node) {
  // Handle privacy for <content-type> on the node side.
  // Define 2 realms, one for node owners and the other for viewers.
  // Owners have full control over their own nodes, referenced viewers can only view them.
  if ($node->type == '<content-type>') {
    $grants = array();
    $grants[] = array(
      'realm' => 'yourmodule_owner',
      'gid' => $node->uid,
      'grant_view' => TRUE,
      'grant_update' => TRUE,
      'grant_delete' => TRUE,
    );
    foreach ($node->field_relevant as $relevant) {
      $grants[] = array(
        'realm' => 'yourmodule_viewer',
        'gid' => $relevant['uid'],
        'grant_view' => TRUE,
        'grant_update' => FALSE,
        'grant_delete' => FALSE,
      );
    }
    return $grants;
  }
}

function yourmodule_node_grants($account, $op) {
  // Handle privacy for <content-type> on the user side.
  // The 2 grant IDs are the user ID.
  $grants['yourmodule_owner'] = array($account->uid);
  $grants['yourmodule_viewer'] = array($account->uid);
  return $grants;
}
?>

You need to rebuild node permissions at admin/content/node-settings/rebuild to get this working.

Hope this helps!