You are here

protected function ProjectAccessControlHandler::checkOwn in Drupal PM (Project Management) 4.x

Test for given 'own' permission.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

string $operation: The entity operation that needs to be performed.

\Drupal\Core\Session\AccountInterface $account: The user account.

Return value

string|null The permission string indicating it's allowed.

1 call to ProjectAccessControlHandler::checkOwn()
ProjectAccessControlHandler::checkAccess in modules/pm_project/src/ProjectAccessControlHandler.php
Performs access checks.

File

modules/pm_project/src/ProjectAccessControlHandler.php, line 84

Class

ProjectAccessControlHandler
Access controller for the Project entity.

Namespace

Drupal\pm_project

Code

protected function checkOwn(EntityInterface $entity, string $operation, AccountInterface $account) {
  $status = $entity
    ->isPublished();
  $uid = $entity
    ->getOwnerId();
  $is_own = $account
    ->isAuthenticated() && $account
    ->id() == $uid;
  if (!$is_own) {
    return;
  }
  $bundle = $entity
    ->bundle();
  $ops = [
    'create' => '%bundle add own %bundle entities',
    'view unpublished' => '%bundle view own unpublished %bundle entities',
    'view' => '%bundle view own entities',
    'update' => '%bundle edit own entities',
    'delete' => '%bundle delete own entities',
  ];
  $permission = strtr($ops[$operation], [
    '%bundle' => $bundle,
  ]);
  if ($operation === 'view unpublished') {
    if (!$status && $account
      ->hasPermission($permission)) {
      return $permission;
    }
    else {
      return NULL;
    }
  }
  if ($account
    ->hasPermission($permission)) {
    return $permission;
  }
  return NULL;
}