You are here

function farm_plan_access in farmOS 7

Access callback for plan entities.

Parameters

string $op: The operation being performed. One of 'view', 'update', 'create', 'delete'.

FarmPlan|string $farm_plan: Optionally a specific plan entity to check.

object $account: The user to check for. Leave it to NULL to check for the global user.

Return value

bool Whether access is allowed or not.

4 calls to farm_plan_access()
farm_plan_add_access in modules/farm/farm_plan/farm_plan.module
Access callback: Checks whether the user has permission to add a plan.
farm_plan_add_types_page in modules/farm/farm_plan/farm_plan.pages.inc
Page to select plan type to add new plan.
farm_plan_form in modules/farm/farm_plan/farm_plan.pages.inc
Plan form.
farm_plan_properties_access in modules/farm/farm_plan/farm_plan.module
Access callback for farm_plan properties.
3 string references to 'farm_plan_access'
farm_plan_consideration_menu in modules/farm/farm_plan/farm_plan_consideration/farm_plan_consideration.module
Implements hook_menu().
farm_plan_entity_info in modules/farm/farm_plan/farm_plan.module
Implements hook_entity_info().
farm_plan_menu in modules/farm/farm_plan/farm_plan.module
Implements hook_menu().

File

modules/farm/farm_plan/farm_plan.module, line 416
Farm plan - A farm plan entity type.

Code

function farm_plan_access($op, $farm_plan = NULL, $account = NULL) {
  $rights =& drupal_static(__FUNCTION__, array());

  // If $op is not one of the supported ones, deny access.
  if (!in_array($op, array(
    'create',
    'view',
    'update',
    'delete',
  ), TRUE)) {
    return FALSE;
  }

  // If no user object is supplied, the access check is for the current user.
  if (empty($account)) {
    global $user;
    $account = $user;
  }

  // If no plan is provided, check for access to all plans.
  if (empty($farm_plan)) {
    return user_access('view farm plans', $account);
  }

  // $farm_plan may be either an object or a plan type. Since plan types
  // cannot be an integer, use either id or type as the static cache id.
  $cid = is_object($farm_plan) ? $farm_plan->id : $farm_plan;

  // If we've already checked access for this plan, user and op, return from
  // cache.
  if (isset($rights[$account->uid][$cid][$op])) {
    return $rights[$account->uid][$cid][$op];
  }

  // If the user has 'administer farm_plan module' permission, grant them
  // access.
  if (user_access('administer farm_plan module', $account)) {
    $rights[$account->uid][$cid][$op] = TRUE;
    return TRUE;
  }

  // Check access to the plan based on it's type.
  $type = is_string($farm_plan) ? $farm_plan : $farm_plan->type;
  $farm_plan_types = farm_plan_types();
  $type_names = array();
  foreach ($farm_plan_types as $name => $farm_plan_type) {
    $type_names[] = $name;
  }
  if (in_array($type, $type_names)) {
    if ($op == 'create' && user_access('create ' . $type . ' farm plans', $account)) {
      $rights[$account->uid][$cid][$op] = TRUE;
      return TRUE;
    }
    if ($op == 'view') {
      if (user_access('view any ' . $type . ' farm plans', $account) || user_access('view own ' . $type . ' farm plans', $account) && $account->uid == $farm_plan->uid) {
        $rights[$account->uid][$cid][$op] = TRUE;
        return TRUE;
      }
    }
    if ($op == 'update') {
      if (user_access('edit any ' . $type . ' farm plans', $account) || user_access('edit own ' . $type . ' farm plans', $account) && $account->uid == $farm_plan->uid) {
        $rights[$account->uid][$cid][$op] = TRUE;
        return TRUE;
      }
    }
    if ($op == 'delete') {
      if (user_access('delete any ' . $type . ' farm plans', $account) || user_access('delete own ' . $type . ' farm plans', $account) && $account->uid == $farm_plan->uid) {
        $rights[$account->uid][$cid][$op] = TRUE;
        return TRUE;
      }
    }
  }

  // If all else fails, deny access.
  return FALSE;
}