You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB

  1. # -*- coding: utf-8 -*-
  2. # Copyright 2018 Tecnativa - Jairo Llopis
  3. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  4. from psycopg2 import IntegrityError
  5. def migrate(cr, version):
  6. if not version:
  7. return
  8. # Fix typo across DB
  9. cr.execute(
  10. """ UPDATE res_authentication_attempt
  11. SET result = 'successful'
  12. WHERE result = 'successfull'""",
  13. )
  14. # Store whitelist IPs in new format
  15. cr.execute(
  16. """ SELECT remote
  17. FROM res_banned_remote
  18. WHERE active IS FALSE""",
  19. )
  20. remotes = {record[0] for record in cr.fetchall()}
  21. try:
  22. with cr.savepoint():
  23. cr.execute(
  24. "INSERT INTO ir_config_parameter (key, value) VALUES (%s, %s)",
  25. (
  26. "auth_brute_force.whitelist_remotes",
  27. ",".join(remotes),
  28. ),
  29. )
  30. except IntegrityError:
  31. # Parameter already exists
  32. cr.execute(
  33. "SELECT value FROM ir_config_parameter WHERE key = %s",
  34. ("auth_brute_force.whitelist_remotes",)
  35. )
  36. current = set(cr.fetchall()[0][0].split(","))
  37. cr.execute(
  38. "UPDATE ir_config_parameter SET value = %s WHERE key = %s",
  39. (",".join(current | remotes),
  40. "auth_brute_force.whitelist_remotes"),
  41. )
  42. # Update the configured IP limit parameter
  43. cr.execute(
  44. "UPDATE ir_config_parameter SET key = %s WHERE key = %s",
  45. (
  46. "auth_brute_force.whitelist_remotes",
  47. "auth_brute_force.max_by_ip",
  48. )
  49. )