You are here

function trash_flag_flag_access in Trash Flag 7

Implements hook_flag_access().

File

./trash_flag.module, line 36

Code

function trash_flag_flag_access($flag, $content_id, $action, $account) {

  // Do nothing if there is no restriction by authorship.
  if ($flag->name == 'trash') {

    // Node must exists to be trashed.
    if (empty($content_id) || !($node = node_load($content_id))) {
      return FALSE;
    }
    $op = $action == 'flag' ? 'trash' : 'untrash';
    $type = $node->type;
    $access = FALSE;

    // Allow per group trash settings.
    if (module_exists('og')) {
      $access = og_user_access_entity('administer group', 'node', $node, $account);

      // we don't actually use administer group permission, just use above check
      // to see if node is part of group.
      if (!is_null($access)) {
        $access = og_user_access_entity("{$op} any content", 'node', $node, $account) || og_user_access_entity("{$op} any {$type} content", 'node', $node, $account) || $account->uid == $node->uid && og_user_access_entity("{$op} own {$type} content", 'node', $node, $account);
      }
      else {
        $access = FALSE;
      }
    }

    // Fallback to normal permission checks.
    return $access || user_access("{$op} any content", $account) || user_access("{$op} any {$type} content", $account) || $account->uid == $node->uid && user_access("{$op} own {$type} content", $account);
  }
}