BanIpManager.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Drupal\ban;
  3. use Drupal\Core\Database\Connection;
  4. /**
  5. * Ban IP manager.
  6. */
  7. class BanIpManager implements BanIpManagerInterface {
  8. /**
  9. * The database connection used to check the IP against.
  10. *
  11. * @var \Drupal\Core\Database\Connection
  12. */
  13. protected $connection;
  14. /**
  15. * Construct the BanSubscriber.
  16. *
  17. * @param \Drupal\Core\Database\Connection $connection
  18. * The database connection which will be used to check the IP against.
  19. */
  20. public function __construct(Connection $connection) {
  21. $this->connection = $connection;
  22. }
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function isBanned($ip) {
  27. return (bool) $this->connection->query("SELECT * FROM {ban_ip} WHERE ip = :ip", [':ip' => $ip])->fetchField();
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function findAll() {
  33. return $this->connection->query('SELECT * FROM {ban_ip}');
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function banIp($ip) {
  39. $this->connection->merge('ban_ip')
  40. ->key(['ip' => $ip])
  41. ->fields(['ip' => $ip])
  42. ->execute();
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function unbanIp($id) {
  48. $this->connection->delete('ban_ip')
  49. ->condition('ip', $id)
  50. ->execute();
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function findById($ban_id) {
  56. return $this->connection->query("SELECT ip FROM {ban_ip} WHERE iid = :iid", [':iid' => $ban_id])->fetchField();
  57. }
  58. }