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.

157 lines
5.2 KiB

  1. ###################################################################################
  2. #
  3. # Copyright (c) 2017-2019 MuK IT GmbH.
  4. #
  5. # This file is part of MuK Session Store
  6. # (see https://mukit.at).
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU Lesser General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU Lesser General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU Lesser General Public License
  19. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. ###################################################################################
  22. import functools
  23. import json
  24. import logging
  25. from contextlib import contextmanager
  26. from datetime import datetime
  27. import psycopg2
  28. from odoo.sql_db import db_connect
  29. from odoo.tools import config
  30. from werkzeug.contrib.sessions import SessionStore
  31. _logger = logging.getLogger(__name__)
  32. def retry_database(func):
  33. @functools.wraps(func)
  34. def wrapper(self, *args, **kwargs):
  35. for attempts in range(1, 6):
  36. try:
  37. return func(self, *args, **kwargs)
  38. except psycopg2.InterfaceError as error:
  39. _logger.warn("SessionStore connection failed! (%s/5)" % attempts)
  40. if attempts >= 5:
  41. raise error
  42. return wrapper
  43. class PostgresSessionStore(SessionStore):
  44. def __init__(self, *args, **kwargs):
  45. super(PostgresSessionStore, self).__init__(*args, **kwargs)
  46. self.dbname = config.get("session_store_dbname", "session_store")
  47. self._setup_database(raise_exception=False)
  48. def _setup_database(self, raise_exception=True):
  49. try:
  50. with db_connect(self.dbname, allow_uri=True).cursor() as cursor:
  51. cursor.autocommit(True)
  52. self._create_table(cursor)
  53. except:
  54. self._create_database()
  55. self._setup_database()
  56. def _create_database(self):
  57. with db_connect("postgres").cursor() as cursor:
  58. cursor.autocommit(True)
  59. cursor.execute(
  60. """
  61. CREATE DATABASE {dbname}
  62. ENCODING 'unicode'
  63. TEMPLATE 'template0';
  64. """.format(
  65. dbname=self.dbname
  66. )
  67. )
  68. def _create_table(self, cursor):
  69. cursor.execute(
  70. """
  71. CREATE TABLE IF NOT EXISTS sessions (
  72. sid varchar PRIMARY KEY,
  73. write_date timestamp without time zone NOT NULL,
  74. payload text NOT NULL
  75. );
  76. """
  77. )
  78. @contextmanager
  79. def open_cursor(self):
  80. connection = db_connect(self.dbname, allow_uri=True)
  81. cursor = connection.cursor()
  82. cursor.autocommit(True)
  83. yield cursor
  84. cursor.close()
  85. @retry_database
  86. def save(self, session):
  87. with self.open_cursor() as cursor:
  88. cursor.execute(
  89. """
  90. INSERT INTO sessions (sid, write_date, payload)
  91. VALUES (%(sid)s, now() at time zone 'UTC', %(payload)s)
  92. ON CONFLICT (sid)
  93. DO UPDATE SET payload = %(payload)s, write_date = now() at time zone 'UTC';
  94. """,
  95. dict(sid=session.sid, payload=json.dumps(dict(session))),
  96. )
  97. @retry_database
  98. def delete(self, session):
  99. with self.open_cursor() as cursor:
  100. cursor.execute("DELETE FROM sessions WHERE sid=%s;", [session.sid])
  101. @retry_database
  102. def get(self, sid):
  103. if not self.is_valid_key(sid):
  104. return self.new()
  105. with self.open_cursor() as cursor:
  106. cursor.execute(
  107. """
  108. SELECT payload, write_date
  109. FROM sessions WHERE sid=%s;
  110. """,
  111. [sid],
  112. )
  113. try:
  114. payload, write_date = cursor.fetchone()
  115. if write_date.date() != datetime.today().date():
  116. cursor.execute(
  117. """
  118. UPDATE sessions
  119. SET write_date = now() at time zone 'UTC'
  120. WHERE sid=%s;
  121. """,
  122. [sid],
  123. )
  124. return self.session_class(json.loads(payload), sid, False)
  125. except Exception:
  126. return self.session_class({}, sid, False)
  127. @retry_database
  128. def list(self):
  129. with self.open_cursor() as cursor:
  130. cursor.execute("SELECT sid FROM sessions;")
  131. return [record[0] for record in cursor.fetchall()]
  132. @retry_database
  133. def clean(self):
  134. with self.open_cursor() as cursor:
  135. cursor.execute(
  136. """
  137. DELETE FROM sessions
  138. WHERE now() at time zone 'UTC' - write_date > '7 days';
  139. """
  140. )