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.

53 lines
1.7 KiB

  1. # Copyright 2018 Creu Blanca
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
  3. from threading import current_thread
  4. from odoo import api, models, SUPERUSER_ID
  5. from odoo.exceptions import AccessDenied
  6. from odoo.service import wsgi_server
  7. class ResUsers(models.Model):
  8. _inherit = "res.users"
  9. # HACK https://github.com/odoo/odoo/issues/24183
  10. # TODO Remove in v12, and use normal odoo.http.request to get details
  11. @api.model_cr
  12. def _register_hook(self):
  13. """🐒-patch XML-RPC controller to know remote address."""
  14. original_fn = wsgi_server.application_unproxied
  15. def _patch(environ, start_response):
  16. current_thread().environ = environ
  17. return original_fn(environ, start_response)
  18. wsgi_server.application_unproxied = _patch
  19. @classmethod
  20. def _auth_check_remote(cls, login, method):
  21. """Force a method to raise an AccessDenied on falsey return."""
  22. with cls.pool.cursor() as cr:
  23. env = api.Environment(cr, SUPERUSER_ID, {})
  24. remote = env["res.users"].remote
  25. remote.ensure_one()
  26. result = method()
  27. if not result:
  28. # Force exception to record auth failure
  29. raise AccessDenied()
  30. return result
  31. # Override all auth-related core methods
  32. @classmethod
  33. def _login(cls, db, login, password):
  34. return cls._auth_check_remote(
  35. login,
  36. lambda: super(ResUsers, cls)._login(db, login, password),
  37. )
  38. @classmethod
  39. def authenticate(cls, db, login, password, user_agent_env):
  40. return cls._auth_check_remote(
  41. login,
  42. lambda: super(ResUsers, cls).authenticate(
  43. db, login, password, user_agent_env),
  44. )