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.

54 lines
1.8 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. super()._register_hook()
  15. original_fn = wsgi_server.application_unproxied
  16. def _patch(environ, start_response):
  17. current_thread().environ = environ
  18. return original_fn(environ, start_response)
  19. wsgi_server.application_unproxied = _patch
  20. @classmethod
  21. def _auth_check_remote(cls, login, method):
  22. """Force a method to raise an AccessDenied on falsey return."""
  23. with cls.pool.cursor() as cr:
  24. env = api.Environment(cr, SUPERUSER_ID, {})
  25. remote = env["res.users"].remote
  26. remote.ensure_one()
  27. result = method()
  28. if not result:
  29. # Force exception to record auth failure
  30. raise AccessDenied()
  31. return result
  32. # Override all auth-related core methods
  33. @classmethod
  34. def _login(cls, db, login, password):
  35. return cls._auth_check_remote(
  36. login,
  37. lambda: super(ResUsers, cls)._login(db, login, password),
  38. )
  39. @classmethod
  40. def authenticate(cls, db, login, password, user_agent_env):
  41. return cls._auth_check_remote(
  42. login,
  43. lambda: super(ResUsers, cls).authenticate(
  44. db, login, password, user_agent_env),
  45. )