Hi,

I love this module and its UI, but I also need to be able to set individual node permissions programmatically.

Could you please provide a code snippet to set given node's view and edit permissions in PHP?

Thanks!

Comments

good_man’s picture

tterranigma’s picture

Issue summary: View changes

Here is a function for Drupal 7 that will grant view access to a given user:

function mymodule_grant_access($nid, $applicant_uid) { 
  // applicant is the user that we will grant access to
  global $user;
  $node = node_load($nid);

  if ($user->uid == $node->uid) { // or your checks here

    //dummy object to pass as argument to content_access_action_user() below
    $op = new stdClass();
    $op->uid = $applicant_uid;

    $params['content_access_user_view'] = $op;
    $params['content_access_user_update'] = 0;
    $params['content_access_user_delete'] = 0; 
    //set to 0 those that you don't need to avoid php warnings

    $params['node'] = $node;
    
    // from the content_access_rules module
    content_access_action_user($params, 'grant');

    drupal_set_message(t("Granted access to $user->uid."));
    drupal_goto("node/$nid");
  }
  else { 
    drupal_access_denied();
  }
}
gisle’s picture

Status: Active » Closed (outdated)