Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MSC3823 - Account Suspension #17051

Merged
merged 8 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion synapse/storage/databases/main/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,8 @@ def get_user_by_id_txn(txn: LoggingTransaction) -> Optional[UserInfo]:
consent_server_notice_sent, appservice_id, creation_ts, user_type,
deactivated, COALESCE(shadow_banned, FALSE) AS shadow_banned,
COALESCE(approved, TRUE) AS approved,
COALESCE(locked, FALSE) AS locked
COALESCE(locked, FALSE) AS locked,
COALESCE(suspended, FALSE) AS suspended
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
FROM users
WHERE name = ?
""",
Expand All @@ -261,6 +262,7 @@ def get_user_by_id_txn(txn: LoggingTransaction) -> Optional[UserInfo]:
shadow_banned,
approved,
locked,
suspended,
) = row

return UserInfo(
Expand All @@ -277,6 +279,7 @@ def get_user_by_id_txn(txn: LoggingTransaction) -> Optional[UserInfo]:
user_type=user_type,
approved=bool(approved),
locked=bool(locked),
suspended=bool(suspended),
)

return await self.db_pool.runInteraction(
Expand Down Expand Up @@ -1180,6 +1183,26 @@ async def get_user_locked_status(self, user_id: str) -> bool:
# Convert the potential integer into a boolean.
return bool(res)

@cached()
async def get_user_suspended_status(self, user_id: str) -> bool:
"""
Determine whether the user's account is suspended.
Args:
user_id: The user ID of the user in question
Returns:
True if the user's account is suspended, false if not.
H-Shay marked this conversation as resolved.
Show resolved Hide resolved
"""

res = await self.db_pool.simple_select_one_onecol(
table="users",
keyvalues={"name": user_id},
retcol="suspended",
allow_none=True,
desc="get_user_suspended",
)

return bool(res)

async def get_threepid_validation_session(
self,
medium: Optional[str],
Expand Down Expand Up @@ -2206,6 +2229,35 @@ def set_user_deactivated_status_txn(
self._invalidate_cache_and_stream(txn, self.get_user_by_id, (user_id,))
txn.call_after(self.is_guest.invalidate, (user_id,))

async def set_user_suspended_status(self, user_id: str, suspended: bool) -> None:
"""
Set whether the user's account is suspended in the `users` table.

Args:
user_id: The user ID of the user in question
suspended: True if the user is suspended, false if not
"""
await self.db_pool.runInteraction(
"set_user_suspended_status",
self.set_user_suspended_status_txn,
user_id,
suspended,
)

def set_user_suspended_status_txn(
self, txn: LoggingTransaction, user_id: str, suspended: bool
) -> None:
self.db_pool.simple_update_one_txn(
txn=txn,
table="users",
keyvalues={"name": user_id},
updatevalues={"suspended": 1 if suspended else 0},
)
self._invalidate_cache_and_stream(
txn, self.get_user_suspended_status, (user_id,)
)
self._invalidate_cache_and_stream(txn, self.get_user_by_id, (user_id,))

async def set_user_locked_status(self, user_id: str, locked: bool) -> None:
"""Set the `locked` property for the provided user to the provided value.

Expand Down
2 changes: 1 addition & 1 deletion tests/storage/test_registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ def test_register(self) -> None:

self.assertEqual(
UserInfo(
# TODO(paul): Surely this field should be 'user_id', not 'name'
user_id=UserID.from_string(self.user_id),
is_admin=False,
is_guest=False,
Expand All @@ -57,6 +56,7 @@ def test_register(self) -> None:
locked=False,
is_shadow_banned=False,
approved=True,
suspended=False,
),
(self.get_success(self.store.get_user_by_id(self.user_id))),
)
Expand Down