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.

110 lines
3.4 KiB

  1. # -*- coding: utf-8 -*-
  2. # (c) 2015 ACSONE SA/NV, Dhinesh D
  3. # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
  4. import logging
  5. from openerp import models
  6. from openerp.http import root
  7. from openerp.http import request
  8. from os import utime
  9. from os.path import getmtime
  10. from time import time
  11. _logger = logging.getLogger(__name__)
  12. class ResUsers(models.Model):
  13. _inherit = 'res.users'
  14. def _auth_timeout_ignoredurls_get(self):
  15. """Pluggable method for calculating ignored urls
  16. Defaults to stored config param
  17. """
  18. param_model = self.pool['ir.config_parameter']
  19. return param_model._auth_timeout_get_parameter_ignoredurls()
  20. def _auth_timeout_deadline_calculate(self):
  21. """Pluggable method for calculating timeout deadline
  22. Defaults to current time minus delay using delay stored as config param
  23. """
  24. param_model = self.pool['ir.config_parameter']
  25. delay = param_model._auth_timeout_get_parameter_delay()
  26. if delay is False or delay <= 0:
  27. return False
  28. return time() - delay
  29. def _auth_timeout_session_terminate(self, session):
  30. """Pluggable method for terminating a timed-out session
  31. This is a late stage where a session timeout can be aborted.
  32. Useful if you want to do some heavy checking, as it won't be
  33. called unless the session inactivity deadline has been reached.
  34. Return:
  35. True: session terminated
  36. False: session timeout cancelled
  37. """
  38. if session.db and session.uid:
  39. session.logout(keep_db=True)
  40. return True
  41. def _auth_timeout_check(self):
  42. if not request:
  43. return
  44. session = request.session
  45. # Calculate deadline
  46. deadline = self._auth_timeout_deadline_calculate()
  47. # Check if past deadline
  48. expired = False
  49. if deadline is not False:
  50. path = root.session_store.get_session_filename(session.sid)
  51. try:
  52. expired = getmtime(path) < deadline
  53. except OSError as e:
  54. _logger.warning(
  55. 'Exception reading session file modified time: %s'
  56. % e
  57. )
  58. pass
  59. # Try to terminate the session
  60. terminated = False
  61. if expired:
  62. terminated = self._auth_timeout_session_terminate(session)
  63. # If session terminated, all done
  64. if terminated:
  65. return
  66. # Else, conditionally update session modified and access times
  67. ignoredurls = self._auth_timeout_ignoredurls_get()
  68. if request.httprequest.path not in ignoredurls:
  69. if 'path' not in locals():
  70. path = root.session_store.get_session_filename(session.sid)
  71. try:
  72. utime(path, None)
  73. except OSError as e:
  74. _logger.warning(
  75. 'Exception updating session file access/modified times: %s'
  76. % e
  77. )
  78. pass
  79. return
  80. def _check_session_validity(self, db, uid, passwd):
  81. """Adaptor method for backward compatibility"""
  82. return self._auth_timeout_check()
  83. def check(self, db, uid, passwd):
  84. res = super(ResUsers, self).check(db, uid, passwd)
  85. self._check_session_validity(db, uid, passwd)
  86. return res