When saving a node that has more than one nodereference field, a slew of SQL errors occurs.

I tracked it down to the following code, which appears to insert several copies of the same grant (one for each nodereference field), but I'm not sure what it's actually supposed to be doing:

     if ($field['module'] == 'nodereference') {
		            $nid = $node->{$field_name}[0]['nid'];
		            $uid = $node->uid;
		            $type = $field['type_name'];
		            $permission = $edit_via_ref_content;
		            $edit_perm = in_array($permission, $default_permissions[$rid])  ? 1 : 0;
		            $permission = $delete_via_ref_content;
		            $delete_perm = in_array($permission, $default_permissions[$rid])  ? 1 : 0;
		            $view_any_content = $view_via_ref_content;
		            $view_perm = (($edit_perm || $delete_perm) ? 1 : in_array($view_any_content, $default_permissions[$rid]));
		            $grants[] = array(
			            'realm' => 'view_own_owner',
			            'gid' => $uid,
			            'grant_view' => $view_perm,
			            'grant_update' => $edit_perm,
			            'grant_delete' => $delete_perm,
			            'priority' => 0,
		            );
		        }

Also, the code just below that for userreferences appears to assume that no two userreference fields on the node will reference the same user, if they do, I think it will give the same errors:

		            $grants[] = array(
			            'realm' => 'view_own_owner',
			            'gid' => $uid,
			            'grant_view' => $view_perm,
			            'grant_update' => $edit_perm,
			            'grant_delete' => $delete_perm,
			            'priority' => 0,
		            );
CommentFileSizeAuthor
#3 view_own_module_duplicate_entry_fix.diff814 bytesthesleuth

Comments

restyler’s picture

I got the same problem.
There is no sense in multiple grants[] entries, for the same node, for same realm and with same gid.
I think I've fixed that, just replace

return $grants;

in view_own_node_access_records()

with

  // only 1 entry per gid per realm makes sense.
  // so, we combine this big grants array to grants with maximum rights
  $combined_grants = array();
  foreach ($grants as $g) {
    $key = $node -> nid . '_' . $g['gid'] . '_' . $g['realm'];    
    if (!isset($combined_grants[$key])) {
      $combined_grants[$key] = $g;
    } else {
      foreach (array('grant_view', 'grant_update', 'grant_delete') as $grant) {
        $combined_grants[$key][$grant] = ($combined_grants[$key][$grant] > $g[$grant] ? $combined_grants[$key][$grant] : $g[$grant]) ;     
      }
    }
  }
  
 
  return $combined_grants;
asak’s picture

+1 for the fix by restyler above

Didn't go over it - just tried it and it seems to solve this problem.

thesleuth’s picture

StatusFileSize
new814 bytes

Was having the same issue. The fix above works for us. Patch?

kenorb’s picture

Status: Active » Needs review
kenorb’s picture

This (#3) look like workaround, any reason why does it happen?

kenorb’s picture

Priority: Normal » Minor