Add account expiration feature

This commit is contained in:
Brendan Abolivier 2019-04-08 17:10:55 +01:00
parent 35442efb75
commit 747aa9f8ca
10 changed files with 144 additions and 4 deletions

View file

@ -86,6 +86,26 @@ class RegistrationWorkerStore(SQLBaseStore):
token
)
@cachedInlineCallbacks()
def get_expiration_ts_for_user(self, user):
"""Get the expiration timestamp for the account bearing a given user ID.
Args:
user (str): The ID of the user.
Returns:
defer.Deferred: None, if the account has no expiration timestamp,
otherwise int representation of the timestamp (as a number of
milliseconds since epoch).
"""
res = yield self._simple_select_one_onecol(
table="account_validity",
keyvalues={"user_id": user.to_string()},
retcol="expiration_ts_ms",
allow_none=True,
desc="get_expiration_date_for_user",
)
defer.returnValue(res)
@defer.inlineCallbacks
def is_server_admin(self, user):
res = yield self._simple_select_one_onecol(
@ -351,6 +371,8 @@ class RegistrationStore(RegistrationWorkerStore,
columns=["creation_ts"],
)
self._account_validity = hs.config.account_validity
# we no longer use refresh tokens, but it's possible that some people
# might have a background update queued to build this index. Just
# clear the background update.
@ -485,6 +507,18 @@ class RegistrationStore(RegistrationWorkerStore,
"user_type": user_type,
}
)
if self._account_validity.enabled:
now_ms = self.clock.time_msec()
expiration_ts = now_ms + self._account_validity.period
self._simple_insert_txn(
txn,
"account_validity",
values={
"user_id": user_id,
"expiration_ts_ms": expiration_ts,
}
)
except self.database_engine.module.IntegrityError:
raise StoreError(
400, "User ID already taken.", errcode=Codes.USER_IN_USE