You are here

public function CurrentAuthenticatedUser::validateArgument in Search API Saved Searches 8

Performs validation for a given argument.

Overrides ArgumentValidatorPluginBase::validateArgument

File

src/Plugin/views/argument_validator/CurrentAuthenticatedUser.php, line 104

Class

CurrentAuthenticatedUser
Validates whether the argument matches the current authenticated user.

Namespace

Drupal\search_api_saved_searches\Plugin\views\argument_validator

Code

public function validateArgument($argument) {

  // A non-numeric argument can't be a valid UID.
  if (!is_numeric($argument)) {
    return FALSE;
  }
  $admin_permission = SavedSearchAccessControlHandler::ADMIN_PERMISSION;
  $is_admin = $this
    ->getCurrentUser()
    ->hasPermission($admin_permission);

  // Only admins are allowed to view the list of anonymous users' searches.
  if ($argument == 0) {
    return $is_admin;
  }
  try {
    $user_storage = $this
      ->getEntityTypeManager()
      ->getStorage('user');
    $user = $user_storage
      ->load($argument);
  } catch (InvalidPluginDefinitionException $e) {
  } catch (PluginNotFoundException $e) {
  }
  if (empty($user)) {
    return FALSE;
  }
  return $is_admin || $user
    ->id() == $this
    ->getCurrentUser()
    ->id();
}