deactivate_account.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2017, 2018 New Vector Ltd
  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 twisted.internet import defer, reactor
  16. from ._base import BaseHandler
  17. from synapse.types import UserID, create_requester
  18. from synapse.util.logcontext import run_in_background
  19. import logging
  20. logger = logging.getLogger(__name__)
  21. class DeactivateAccountHandler(BaseHandler):
  22. """Handler which deals with deactivating user accounts."""
  23. def __init__(self, hs):
  24. super(DeactivateAccountHandler, self).__init__(hs)
  25. self._auth_handler = hs.get_auth_handler()
  26. self._device_handler = hs.get_device_handler()
  27. self._room_member_handler = hs.get_room_member_handler()
  28. self.user_directory_handler = hs.get_user_directory_handler()
  29. # Flag that indicates whether the process to part users from rooms is running
  30. self._user_parter_running = False
  31. # Start the user parter loop so it can resume parting users from rooms where
  32. # it left off (if it has work left to do).
  33. reactor.callWhenRunning(self._start_user_parting)
  34. @defer.inlineCallbacks
  35. def deactivate_account(self, user_id):
  36. """Deactivate a user's account
  37. Args:
  38. user_id (str): ID of user to be deactivated
  39. Returns:
  40. Deferred
  41. """
  42. # FIXME: Theoretically there is a race here wherein user resets
  43. # password using threepid.
  44. # first delete any devices belonging to the user, which will also
  45. # delete corresponding access tokens.
  46. yield self._device_handler.delete_all_devices_for_user(user_id)
  47. # then delete any remaining access tokens which weren't associated with
  48. # a device.
  49. yield self._auth_handler.delete_access_tokens_for_user(user_id)
  50. yield self.store.user_delete_threepids(user_id)
  51. yield self.store.user_set_password_hash(user_id, None)
  52. # Add the user to a table of users pending deactivation (ie.
  53. # removal from all the rooms they're a member of)
  54. yield self.store.add_user_pending_deactivation(user_id)
  55. # delete from user directory
  56. yield self.user_directory_handler.handle_user_deactivated(user_id)
  57. # Now start the process that goes through that list and
  58. # parts users from rooms (if it isn't already running)
  59. self._start_user_parting()
  60. def _start_user_parting(self):
  61. """
  62. Start the process that goes through the table of users
  63. pending deactivation, if it isn't already running.
  64. Returns:
  65. None
  66. """
  67. if not self._user_parter_running:
  68. run_in_background(self._user_parter_loop)
  69. @defer.inlineCallbacks
  70. def _user_parter_loop(self):
  71. """Loop that parts deactivated users from rooms
  72. Returns:
  73. None
  74. """
  75. self._user_parter_running = True
  76. logger.info("Starting user parter")
  77. try:
  78. while True:
  79. user_id = yield self.store.get_user_pending_deactivation()
  80. if user_id is None:
  81. break
  82. logger.info("User parter parting %r", user_id)
  83. yield self._part_user(user_id)
  84. yield self.store.del_user_pending_deactivation(user_id)
  85. logger.info("User parter finished parting %r", user_id)
  86. logger.info("User parter finished: stopping")
  87. finally:
  88. self._user_parter_running = False
  89. @defer.inlineCallbacks
  90. def _part_user(self, user_id):
  91. """Causes the given user_id to leave all the rooms they're joined to
  92. Returns:
  93. None
  94. """
  95. user = UserID.from_string(user_id)
  96. rooms_for_user = yield self.store.get_rooms_for_user(user_id)
  97. for room_id in rooms_for_user:
  98. logger.info("User parter parting %r from %r", user_id, room_id)
  99. try:
  100. yield self._room_member_handler.update_membership(
  101. create_requester(user),
  102. user,
  103. room_id,
  104. "leave",
  105. ratelimit=False,
  106. )
  107. except Exception:
  108. logger.exception(
  109. "Failed to part user %r from room %r: ignoring and continuing",
  110. user_id, room_id,
  111. )