You are here

function node_registration_access in Node registration 7

All Registration access callbacks. Just like node.module has node_access.

13 calls to node_registration_access()
NodeRegistrationEntityClass::buildContent in includes/node_registration.entity.inc
Override buildContent() to add registration properties.
node_registration_access_proxy in includes/node_registration.entity.inc
Access callback proxy for entity_access(), which has a different argument order.
node_registration_access_toggle_with_token in ./node_registration.module
Access callback for registration/%node_registration/toggle/ajax/%/%.
node_registration_entity_view in ./node_registration.module
Implements hook_entity_view().
node_registration_form in includes/node_registration.forms.inc
The registration form. It takes a (mandatory) existing or empty registration object.

... See full list

2 string references to 'node_registration_access'
node_registration_menu in ./node_registration.module
Implements hook_menu().
node_registration_views_data in includes/node_registration.views.inc
Implements hook_views_data().

File

includes/node_registration.api.inc, line 398
Registration API functions.

Code

function node_registration_access($registration, $op, $account = NULL, &$reason = NULL) {
  global $user;
  $account or $account = $user;

  // Rules can get here with a 'view' op without a Registration object.
  // @see entity_rules_integration_event_access()
  if (!$registration || !is_object($registration) || empty($registration->nid)) {
    return $op == 'view' && user_access('administer node registration', $account);
  }
  $reasons =& drupal_static(__FUNCTION__ . ':reasons');
  $cache_key = implode(':', array(
    __FUNCTION__,
    $account->uid,
    $registration->nid,
    $registration->registration_id,
    $op,
  ));
  $access = _node_registration_cache($cache_key, function () use ($cache_key, $registration, $op, $account, &$reason) {

    // There's definitely a node involved.
    $node = $registration->node ? $registration->node : ($registration->node = node_load($registration->nid));
    $type = $node->type;
    $settings = $node->registration;
    $enabled = $settings
      ->enabled();
    $debug = isset($_GET['debug']);
    $reason = '';
    $is_admin = user_access('administer node registration', $account);
    switch ($op) {
      case 'delete':

        // Must be cancelled.
        if (!$registration->cancelled) {
          return FALSE;
        }

        // Admins: always.
        if ($is_admin) {
          return TRUE;
        }

        // Others, only with the right permission.
        if (user_access('delete own ' . $type . ' node registration', $account)) {
          return TRUE;
        }
        break;
      case 'view':

        // Admins: always.
        if ($is_admin) {
          return TRUE;
        }

        // Never if: not enabled, cancelled or not verified.
        if (!$enabled || $registration->cancelled) {
          return FALSE;
        }

        // Semi view admins: no matter the owner.
        if (user_access('view ' . $type . ' node registration', $account)) {
          return TRUE;
        }

        // The rest: only if they're the owner.
        if (user_access('view own ' . $type . ' node registration', $account)) {
          return _node_registration_secret_access($registration, $account);
        }
        break;
      case 'edit':
      case 'update':

        // Admins: always.
        if ($is_admin) {
          return TRUE;
        }

        // If enabled, cancellable and access: only if they're the owner.
        $cancellable = $settings
          ->max_cancel_time_passed();
        if ($enabled && !$cancellable && user_access('edit own ' . $type . ' node registration', $account)) {
          return _node_registration_secret_access($registration, $account);
        }
        break;
      case 'cancel':

        // Must not be cancelled.
        if ($registration->cancelled) {
          return FALSE;
        }

        // Admins: always.
        if ($is_admin) {
          return TRUE;
        }

        // Not if past cancel time.
        if ($settings
          ->max_cancel_time_passed()) {
          return FALSE;
        }

        // If enabled and access: only if they're owner.
        if ($enabled && user_access('cancel own ' . $type . ' node registration', $account)) {
          return _node_registration_secret_access($registration, $account);
        }
        break;
      case 'add':
      case 'create':

        // Check: enabled.
        if ($enabled) {

          // Registration min date.
          $allow_min_date = $settings
            ->min_registration_time_passed($disallow_min_date_reason);

          // Registration max date.
          $allow_max_date = !$settings
            ->max_registration_time_passed($disallow_max_date_reason);

          // Check: start and end date.
          if ($allow_min_date && $allow_max_date) {

            // Check: capacity.
            if (_node_registration_event_has_room($node) || $settings->allow_exceeding_capacity) {

              // Fetch existing registration for current user.
              $registration = _node_registration_user_registered($node, $account);

              // Check: already registered.
              if (!$registration || node_registration_node_access($node, 'register others', $account)) {

                // The following are indirect settings from registration type and node.
                $user_access = user_access('add ' . $type . ' node registration', $account);
                $registration_access = $account->uid ? $settings->allow_authenticated : $settings->allow_anonymous;

                // Check: user access.
                if ($is_admin || $user_access && $registration_access) {
                  return TRUE;
                }
                else {

                  // Fail: user access.
                  $debug && watchdog('node_registration', 'user access insufficient');
                  $reason = 'access';
                }
              }
              else {

                // Fail: already registered.
                $debug && watchdog('node_registration', 'user already registered');
                $reason = 'registered';
              }
            }
            else {

              // Fail: capacity.
              $debug && watchdog('node_registration', 'no room left and no exceeding capacity');
              $reason = 'capacity';
            }
          }
          else {

            // Fail: invalid start or end date.
            $debug && watchdog('node_registration', 'registration date invalid: start date (@min_date) or end date (@max_date)', array(
              '@min_date' => $disallow_min_date_reason,
              '@max_date' => $disallow_max_date_reason,
            ));
            $reason = 'date';
          }
        }
        else {

          // Fail: registration not enabled.
          $debug && watchdog('node_registration', 'event registration not enabled');
          $reason = 'disabled';
        }
        break;
    }
    return FALSE;
  }, function ($result, $cache_key) use ($registration, $op, $account, &$reason) {

    // Allow other modules to override and change this result.
    $reason or $reason = $result ? '' : '?';
    $alt_access = module_invoke_all('node_registration_access', $registration, $op, $account, $reason);
    if (in_array(!$result, $alt_access, TRUE)) {
      return !$result;
    }
    return $result;
  });
  if ($reason) {
    $reasons[$cache_key] = $reason;
  }
  else {
    $reason = @$reasons[$cache_key];
  }
  return $access;
}