You are here

function commons_activity_streams_message_access_alter in Drupal Commons 7.3

Implements hook_message_access_alter().

File

modules/commons/commons_activity_streams/commons_activity_streams.module, line 93

Code

function commons_activity_streams_message_access_alter(&$access, $context) {

  // We're only interested in the 'view' operation.
  if ($context['op'] != 'view') {
    return;
  }
  $message = $context['entity'];

  // Verify view access to nodes referenced in the message.
  if (isset($message->field_target_nodes)) {
    foreach ($message->field_target_nodes[LANGUAGE_NONE] as $key => $value) {
      $node = node_load($value['target_id']);
      if (!node_access('view', $node, $context['account'])) {

        // If the user cannot view any nodes in the message,
        // deny access to the entire message;
        $access = FALSE;
        return;
      }
    }
  }

  // Verify view access to comments referenced in the message.
  if (isset($message->field_target_comments)) {
    foreach ($message->field_target_comments[LANGUAGE_NONE] as $key => $value) {
      $comment = comment_load($value['target_id']);
      if (!entity_access('view', 'comment', $comment, $context['account'])) {

        // If the user cannot view any comments in the message,
        // deny access to the entire message;
        $access = FALSE;
        return;
      }
    }
  }
}