You are here

function flag_flag_access in Flag 7.3

Same name and namespace in other branches
  1. 6.2 flag.module \flag_flag_access()
  2. 7.2 flag.module \flag_flag_access()

Implements hook_flag_access().

File

./flag.module, line 1388
The Flag module.

Code

function flag_flag_access($flag, $entity_id, $action, $account) {

  // Do nothing if there is no restriction by authorship.
  if (empty($flag->access_author)) {
    return;
  }

  // Restrict access by authorship. It's important that TRUE is never returned
  // here, otherwise we'd grant permission even if other modules denied access.
  if ($flag->entity_type == 'node') {

    // For non-existent nodes (such as on the node add form), assume that the
    // current user is creating the content.
    if (empty($entity_id) || !($node = $flag
      ->fetch_entity($entity_id))) {
      return $flag->access_author == 'others' ? FALSE : NULL;
    }
    if ($flag->access_author == 'own' && $node->uid != $account->uid) {
      return FALSE;
    }
    elseif ($flag->access_author == 'others' && $node->uid == $account->uid) {
      return FALSE;
    }
  }

  // Restrict access by comment authorship.
  if ($flag->entity_type == 'comment') {

    // For non-existent comments (such as on the comment add form), assume that
    // the current user is creating the content.
    if (empty($entity_id) || !($comment = $flag
      ->fetch_entity($entity_id)) || $entity_id == 'new') {
      return $flag->access_author == 'comment_others' ? FALSE : NULL;
    }
    $node = node_load($comment->nid);
    if ($flag->access_author == 'node_own' && $node->uid != $account->uid) {
      return FALSE;
    }
    elseif ($flag->access_author == 'node_others' && $node->uid == $account->uid) {
      return FALSE;
    }
    elseif ($flag->access_author == 'comment_own' && $comment->uid != $account->uid) {
      return FALSE;
    }
    elseif ($flag->access_author == 'comment_others' && $comment->uid == $account->uid) {
      return FALSE;
    }
  }
}