Display comment box to the author of the node being referenced using panels
Database query would help you.

function my_module_get_author_by_nid($node_nid) {
  $author = '';
  if (is_numeric($node_nid) &amp;&amp; $node_nid != 0) {
    $uid = db_select('node', 'n')
        -&gt;fields('n', array('uid'))
        -&gt;condition('nid', $node_nid, '=')
        -&gt;execute()
        -&gt;fetchField();
    return $uid;
  }
}


// And then a slight change in your code

    global $user;
    if (arg(0) == 'node' &amp;&amp; is_numeric(arg(1))) {
      // Get the nid
      $nid = arg(1);

      // Load the node if you need to
      $node = node_load($nid);
    }

     // Get Reference Node Nid
    $reference_nid = $node-&gt;your_reference_field_name[$node-&gt;language][0]['target_id'];
    // Get Author for reference Node
   $reference_author =  my_module_get_author_by_nid($reference_nid);
    if($node-&gt;uid == $user-&gt;uid || $user-&gt;uid == $reference_author){
      return TRUE;
    } else {
      return FALSE;
    }