You are here

function forum_access_init in Forum Access 6

Same name and namespace in other branches
  1. 5 forum_access.module \forum_access_init()

Implementation of hook_init().

Enable moderator access on node/%, node/%/edit, comment/edit/%, and comment/delete/% where needed.

File

./forum_access.module, line 81
forum_access.module

Code

function forum_access_init() {
  global $user;
  if ($user->uid == 1) {
    return;
  }
  switch (arg(0)) {
    case 'comment':
      if (variable_get('forum_access_D5_legacy_mode', FALSE)) {
        return;

        // disable comment access control
      }
      if ((arg(1) == 'edit' || arg(1) == 'delete') && !user_access('administer comments')) {
        if (is_numeric($cid = arg(2))) {

          // comment/edit/%, comment/delete/%
          $access[] = arg(1) == 'edit' ? 'update' : 'delete';
          $comment = _comment_load($cid);
          $nid = $comment->nid;

          // If the node turns out to be in a forum where we have update/delete
          // access, then we need Moderator permissions now, so we can moderate
          // this comment.
          // We won't provide full Administrator access, though: we'll remove
          // author and timestamp, for example.
          $grant_normal_access = TRUE;
        }
      }
      break;
    case 'node':
      if (is_numeric(arg(1))) {
        if (arg(2) == 'edit' && !user_access('administer nodes')) {

          // node/%/edit
          $access[] = 'update';
          $nid = arg(1);

          // If the node turns out to be in a forum where we have update/delete
          // access, then we already get limited edit capabilities from NA, but
          // we need some more, e.g. publish/unpublish and comment status.
          // In order to get these controls on the form, we need Moderator
          // permissions now.
          // We won't provide full Administrator access, though: we'll remove
          // author and timestamp, for example.
        }
        if (arg(2) == 'delete' && !user_access('administer nodes')) {

          // node/%/delete
          $access = array();
          $nid = arg(1);

          // This is the delete confirmation page. We don't need any
          // additional permissions, but we'll assert 'view' access below.
        }
        if (arg(2) == NULL && !user_access('administer comments')) {

          // node/%
          $access[] = 'update';
          $nid = arg(1);

          // If the node turns out to be in a forum where we have update/delete
          // access, then we'll get the 'Edit' link automatically from NA, but
          // we'll need Moderator permissions, so that we can add the edit/delete
          // comment links (*after* we've identified the other comment links).
        }
      }
      break;
  }
  if (isset($nid)) {
    $node = node_load($nid);
    if ($tid = _forum_access_get_tid($node)) {
      if (!forum_access_access($tid, 'view')) {
        drupal_access_denied();
        module_invoke_all('exit');
        exit;
      }
      foreach ($access as $a) {
        $faa = forum_access_access($tid, $a);
        $grant_moderator_access = $faa > 1;
        $grant_normal_access = !empty($grant_normal_access) && $faa > 0;
        if ($grant_normal_access || $grant_moderator_access) {
          $user->_forum_access_moderator = $grant_moderator_access;
          if (arg(0) == 'comment' || arg(0) == 'node' && arg(2) == 'edit') {
            module_load_include('node.inc', 'forum_access');
            _forum_access_enable_moderator();
            break;
          }
        }
      }
    }
  }
}