accounts.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2019 The Matrix.org Foundation C.I.C.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from __future__ import absolute_import
  16. from sydent.users.accounts import Account
  17. class AccountStore(object):
  18. def __init__(self, sydent):
  19. self.sydent = sydent
  20. def getAccountByToken(self, token):
  21. """
  22. Select the account matching the given token, if any.
  23. :param token: The token to identify the account, if any.
  24. :type token: unicode
  25. :return: The account matching the token, or None if no account matched.
  26. :rtype: Account or None
  27. """
  28. cur = self.sydent.db.cursor()
  29. res = cur.execute("select a.user_id, a.created_ts, a.consent_version from accounts a, tokens t "
  30. "where t.user_id = a.user_id and t.token = ?", (token,))
  31. row = res.fetchone()
  32. if row is None:
  33. return None
  34. return Account(*row)
  35. def storeAccount(self, user_id, creation_ts, consent_version):
  36. """
  37. Stores an account for the given user ID.
  38. :param user_id: The Matrix user ID to create an account for.
  39. :type user_id: unicode
  40. :param creation_ts: The timestamp in milliseconds.
  41. :type creation_ts: int
  42. :param consent_version: The version of the terms of services that the user last
  43. accepted.
  44. :type consent_version: str or None
  45. """
  46. cur = self.sydent.db.cursor()
  47. res = cur.execute(
  48. "insert or ignore into accounts (user_id, created_ts, consent_version) "
  49. "values (?, ?, ?)",
  50. (user_id, creation_ts, consent_version),
  51. )
  52. self.sydent.db.commit()
  53. def setConsentVersion(self, user_id, consent_version):
  54. """
  55. Saves that the given user has agreed to all of the terms in the document of the
  56. given version.
  57. :param user_id: The Matrix ID of the user that has agreed to the terms.
  58. :type user_id: str
  59. :param consent_version: The version of the document the user has agreed to.
  60. :type consent_version: unicode or None
  61. """
  62. cur = self.sydent.db.cursor()
  63. res = cur.execute(
  64. "update accounts set consent_version = ? where user_id = ?",
  65. (consent_version, user_id),
  66. )
  67. self.sydent.db.commit()
  68. def addToken(self, user_id, token):
  69. """
  70. Stores the authentication token for a given user.
  71. :param user_id: The Matrix user ID to save the given token for.
  72. :type user_id: unicode
  73. :param token: The token to store for that user ID.
  74. :type token: unicode
  75. """
  76. cur = self.sydent.db.cursor()
  77. res = cur.execute(
  78. "insert into tokens (user_id, token) values (?, ?)",
  79. (user_id, token),
  80. )
  81. self.sydent.db.commit()
  82. def delToken(self, token):
  83. """
  84. Deletes an authentication token from the database.
  85. :param token: The token to delete from the database.
  86. :type token: unicode
  87. """
  88. cur = self.sydent.db.cursor()
  89. res = cur.execute(
  90. "delete from tokens where token = ?",
  91. (token,),
  92. )
  93. deleted = cur.rowcount
  94. self.sydent.db.commit()
  95. return deleted