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.

72 lines
2.9 KiB

  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # OpenERP, Open Source Management Solution
  5. # Copyright (C) 2015 ABF OSIELL (<http://osiell.com>).
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. ##############################################################################
  21. from openerp import models, fields, api
  22. from openerp.http import request
  23. class AuditlogtHTTPSession(models.Model):
  24. _name = 'auditlog.http.session'
  25. _description = u"Auditlog - HTTP User session log"
  26. _order = "create_date DESC"
  27. _rec_name = 'display_name'
  28. display_name = fields.Char(u"Name", compute="_display_name")
  29. name = fields.Char(u"Session ID")
  30. user_id = fields.Many2one(
  31. 'res.users', string=u"User")
  32. http_request_ids = fields.One2many(
  33. 'auditlog.http.request', 'http_session_id', string=u"HTTP Requests")
  34. @api.multi
  35. def _display_name(self):
  36. for httpsession in self:
  37. create_date = fields.Datetime.from_string(httpsession.create_date)
  38. tz_create_date = fields.Datetime.context_timestamp(
  39. httpsession, create_date)
  40. httpsession.display_name = u"%s (%s)" % (
  41. httpsession.user_id and httpsession.user_id.name or '?',
  42. fields.Datetime.to_string(tz_create_date))
  43. @api.model
  44. def current_http_session(self):
  45. """Create a log corresponding to the current HTTP user session, and
  46. returns its ID. This method can be called several times during the
  47. HTTP query/response cycle, it will only log the user session on the
  48. first call.
  49. If no HTTP user session is available, returns `False`.
  50. """
  51. if not request:
  52. return False
  53. httpsession = request.httpsession
  54. if httpsession:
  55. existing_session = self.search(
  56. [('name', '=', httpsession.sid),
  57. ('user_id', '=', request.uid)])
  58. if existing_session:
  59. return existing_session.id
  60. vals = {
  61. 'name': httpsession.sid,
  62. 'user_id': request.uid,
  63. }
  64. httpsession.auditlog_http_session_id = self.create(vals).id
  65. return httpsession.auditlog_http_session_id
  66. return False