You are here

function flag_flag_access in Flag 6.2

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

Implementation of hook_flag_access().

File

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

Code

function flag_flag_access($flag, $content_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->content_type == 'node') {

    // For non-existent nodes (such as on the node add form), assume that the
    // current user is creating the content.
    if (empty($content_id) || !($node = node_load($content_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->content_type == 'comment') {

    // For non-existent comments (such as on the comment add form), assume that
    // the current user is creating the content.
    if (empty($content_id) || !($comment = $flag
      ->fetch_content($content_id))) {
      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;
    }
  }
}