Programmatically set node access rules

Public

EXAMPLE: Only node author can view node

Get raw version
php
  1. function mumodule_node_access($node, $op, $account) {
  2. global $user;
  3. $clients = array();
  4.  
  5. // you have to do this or it will throw errors on all non node pages.
  6. if(is_string($node)) {
  7. return NODE_ACCESS_IGNORE; // a simply pass through. Does nothing.
  8. }
  9.  
  10. if($node->type == '<my-node-type>') { // I assume you don't want it for page nodes etc.
  11. if($node->nid == $user->uid) {
  12. return NODE_ACCESS_ALLOW;
  13. } else {
  14. return NODE_ACCESS_DENY;
  15. }
  16. } else {
  17. return NODE_ACCESS_DENY;
  18. }
  19. }

Only related users may view nodes

Get raw version
php
  1. function mumodule_node_access($node, $op, $account) {
  2. global $user;
  3. $clients = array();
  4.  
  5. if(is_string($node)) {
  6. return NODE_ACCESS_IGNORE;
  7. }
  8.  
  9. if($node->type == 'project') {
  10. if(!empty($node->field_related_clients)) {
  11. $client_uid = $node->field_related_clients['und'][0]['target_id'];
  12. if($client_uid == $user->uid) {
  13.  
  14. } else {
  15. return NODE_ACCESS_DENY;
  16. }
  17. } else {
  18. return NODE_ACCESS_DENY;
  19. }
  20. } else {
  21. return NODE_ACCESS_DENY;
  22. }
  23. }