uid != $user->uid)) { return; } if( ((is_null($form['type']) || $form['type']['#value'] .'_node_form' != $form_id) && ($form_id != 'node_type_form') ) || !count($types = user_relationships_types_load()) ) { return; } // Node form if ($form_id != 'node_type_form') { $form['user_relationship_node_access'] = array( '#type' => 'fieldset', '#title' => t('User Relationships Node Access'), '#description' => t('Node access based on your relationships to other users'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#tree' => TRUE, '#theme' => 'user_relationship_node_access_form', ); foreach($types as $rtid => $type) { unset($types[$rtid]); if ($type->is_oneway) { $types["{$rtid}_yt"] = t('@type (you to them)', array('@type' => $type->plural_name)); $types["{$rtid}_ty"] = t('@type (them to you)', array('@type' => $type->plural_name)); } else { $types[$rtid] = $type->plural_name; } } asort($types); // reverse the optimization made after saving $permissions = array(); if (is_array($form['#node']->user_relationship_node_access)) { foreach ($form['#node']->user_relationship_node_access as $rtid => $permission) { foreach ($permission as $action => $trash) { $permissions[$action][$rtid] = $rtid; } } } $form['user_relationship_node_access'] = array( '#type' => 'fieldset', '#title' => t('User Relationships Node Access'), '#description' => t('Node access based on your relationships to other users'), '#collapsible' => TRUE, '#collapsed' => TRUE, '#tree' => TRUE, ); foreach (array('view', 'update', 'delete') as $action) { $form['user_relationship_node_access'][$action] = array( '#type' => 'checkboxes', '#multiple' => TRUE, '#options' => $types, '#title' => t(ucfirst($action)), '#default_value' => $permissions[$action], '#description' => t('If no box is ticked, then anyone can @action.', array('@action' => $action)) ); } } } /** * hook_nodeapi() */ function user_relationship_node_access_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { global $user; if ($node->uid != $user->uid) { return; } switch($op) { case 'insert': case 'update': if (isset($node->user_relationship_node_access)) { // reformat the array and optimize $user_relationship_node_access = array(); foreach($node->user_relationship_node_access as $action => $permissions) { foreach ($permissions as $key => $permission) { if($permission) { $user_relationship_node_access[$key][$action] = TRUE; } } } db_query( "DELETE FROM {user_relationship_node_access} WHERE nid = %d", $node->nid ); db_query( "INSERT INTO {user_relationship_node_access} (nid, permissions) VALUES (%d, '%s')", $node->nid, serialize($user_relationship_node_access) ); $node->user_relationship_node_access = $user_relationship_node_access; } break; case 'load': $node->user_relationship_node_access = unserialize(db_result(db_query( "SELECT permissions FROM {user_relationship_node_access} WHERE nid = %d", $node->nid ))); break; case 'delete': db_query( "DELETE FROM {user_relationship_node_access} WHERE nid = %d", $node->nid ); break; } } /** * hook_node_grants() */ function user_relationship_node_access_node_grants($user, $op) { // get this user's relationships $relationships = user_relationships_load(array( 'user' => $user->uid, 'approved' => TRUE )); foreach($relationships as $relationship) { $author_uid = $relationship->requestee_id == $user->uid ? $relationship->requester_id : $relationship->requestee_id; if ($relationship->is_oneway) { $grants["user_relationship_node_access_{$relationship->rtid}_yt"][] = $relationship->requester_id; $grants["user_relationship_node_access_{$relationship->rtid}_ty"][] = $relationship->requestee_id; } else { $grants["user_relationship_node_access_{$relationship->rtid}"][] = $author_uid; } } $grants['user_relationship_node_access_author'] = array($user->uid); return $grants; } /** * hook_access_records() */ function user_relationship_node_access_node_access_records($node) { if(_user_relationship_node_access_disabling()) { return; } if(is_array($node->user_relationship_node_access)) { foreach($node->user_relationship_node_access as $rtid => $permissions) { $grants[] = array( 'realm' => "user_relationship_node_access_{$rtid}", 'gid' => $node->uid, 'grant_view' => $permissions['view'], 'grant_update' => $permissions['update'], 'grant_delete' => $permissions['delete'] ); } } if(count($grants)) { $grants[] = array( 'realm' => 'user_relationship_node_access_author', 'gid' => $node->uid, 'grant_view' => TRUE, 'grant_update' => TRUE, 'grant_delete' => TRUE ); } return $grants; }