You are here

public function UserFlagType::actionAccess in Flag 8.4

Checks whether a user has permission to flag/unflag or not.

Parameters

string $action: The action for which to check permissions, either 'flag' or 'unflag'.

\Drupal\flag\FlagInterface $flag: The flag object.

\Drupal\Core\Session\AccountInterface $account: An AccountInterface object.

\Drupal\Core\Entity\EntityInterface $flaggable: (optional) The flaggable entity.

Return value

\Drupal\Core\Access\AccessResult An AccessResult object.

Overrides EntityFlagType::actionAccess

File

src/Plugin/Flag/UserFlagType.php, line 144

Class

UserFlagType
Provides a flag type for user entities.

Namespace

Drupal\flag\Plugin\Flag

Code

public function actionAccess($action, FlagInterface $flag, AccountInterface $account, EntityInterface $flaggable = NULL) {
  $access = parent::actionAccess($action, $flag, $account, $flaggable);
  if ($flaggable && $this
    ->hasExtraPermission('owner')) {

    // Permit selfies.
    $permission = $action . ' ' . $flag
      ->id() . ' own user account';
    $selfies_permission_access = AccessResult::allowedIfHasPermission($account, $permission)
      ->addCacheContexts([
      'user',
    ]);
    $account_match_access = AccessResult::allowedIf($account
      ->id() == $flaggable
      ->id());
    $own_access = $selfies_permission_access
      ->andIf($account_match_access);
    $access = $access
      ->orIf($own_access);

    // Act on others' profiles.
    $permission = $action . ' ' . $flag
      ->id() . ' other user accounts';
    $others_permission_access = AccessResult::allowedIfHasPermission($account, $permission)
      ->addCacheContexts([
      'user',
    ]);
    $account_mismatch_access = AccessResult::allowedIf($account
      ->id() != $flaggable
      ->id());
    $others_access = $others_permission_access
      ->andIf($account_mismatch_access);
    $access = $access
      ->orIf($others_access);
  }
  return $access;
}