You are here

function organigrams_user_access in Organigrams 7

Get a value indicating whether the user perform the operation.

Parameters

string $operation: An operation to perform on the organigram. Valid values are view, edit and delete.

mixed $object: An organigram ID, machine name or object on which the operation will be performed.

mixed $account: The account to check, if not given use currently logged in user.

Return value

bool TRUE if the user has permission to perform the operation, otherwise FALSE.

2 calls to organigrams_user_access()
organigrams_entity_access in ./organigrams.module
Access callback for the organigrams entities.
organigrams_form_overview_organigrams_items in ./organigrams_item.admin.inc
Generates a form overview of all organigrams items.
2 string references to 'organigrams_user_access'
organigrams_entity_info in ./organigrams.module
Implements hook_entity_info().
organigrams_menu in ./organigrams.module
Implements hook_menu().

File

./organigrams.module, line 999
Defines the organigrams functions and entity types.

Code

function organigrams_user_access($operation, $object, $account = NULL) {
  static $valid_operations = array(
    'view',
    'edit',
    'delete',
  );

  // Check if the user is not an administrator and validate the operation.
  if (!($granted = user_access('administer organigrams', $account)) && in_array($operation, $valid_operations)) {

    // If the $object argument is a string.
    if (is_string($object)) {

      // Then assume its an organigram machine name and try to load the
      // organigram.
      $object = organigrams_machine_name_load($object);
    }
    elseif (is_numeric($object)) {

      // Then assume its an organigram ID and try to load the organigram.
      $object = organigrams_load($object);
    }

    // Check if the operation describes a view operation.
    if ($operation == 'view' && $object->status) {

      // Check for the access content access right.
      $granted = user_access('access content', $account);
    }
    else {

      // The view operation is denied so check if the user has edit access.
      if ($operation == 'view') {
        $operation = 'edit';
      }

      // Get the machine name.
      $machine_name = isset($object->machine_name) ? $object->machine_name : $object->organigrams_machine_name;

      // A user which has not the correct permission will be evaluated for a
      // specific organigram permission. Retrieve a value indicating whether the
      // current user has permission to perform the specified operation on the
      // organigram.
      $granted = user_access("{$operation} organigram {$machine_name}", $account);
    }
  }
  return $granted;
}