You are here

function checklistapi_checklist_access in Checklist API 8

Same name and namespace in other branches
  1. 7 checklistapi.module \checklistapi_checklist_access()

Access callback: Checks the current user's access to a given checklist.

Parameters

string $id: The checklist ID.

string $operation: (optional) The operation to test access for. Accepted values are "view", "edit", and "any". Defaults to "any".

Return value

bool Returns TRUE if the current user has access to perform a given operation on the specified checklist, or FALSE if not.

Throws

InvalidArgumentException Throws an exception if an unsupported operation is supplied.

4 calls to checklistapi_checklist_access()
ChecklistapiAccessCheck::access in src/Access/ChecklistapiAccessCheck.php
Checks routing access for the checklist.
ChecklistapiChecklist::userHasAccess in src/ChecklistapiChecklist.php
Determines whether the current user has access to the checklist.
ChecklistapiModuleTest::testChecklistapiChecklistAccessInvalidMode in tests/src/Unit/ChecklistapiModuleTest.php
Tests that checklistapi_checklist_access() rejects an invalid mode.
checklistapi_help in ./checklistapi.module
Implements hook_help().

File

./checklistapi.module, line 32
An API for creating fillable, persistent checklists.

Code

function checklistapi_checklist_access($id, $operation = 'any') {
  $all_operations = [
    'view',
    'edit',
    'any',
  ];
  if (!in_array($operation, $all_operations)) {
    throw new \InvalidArgumentException(sprintf('No such operation "%s"', $operation));
  }
  $current_user = \Drupal::currentUser();
  $access['view'] = $current_user
    ->hasPermission('view any checklistapi checklist') || $current_user
    ->hasPermission("view {$id} checklistapi checklist");
  $access['edit'] = $current_user
    ->hasPermission('edit any checklistapi checklist') || $current_user
    ->hasPermission("edit {$id} checklistapi checklist");
  $access['any'] = $access['view'] || $access['edit'];
  return $access[$operation];
}