(20250123) Made a common API to disable all integrations.

This commit is contained in:
2025-01-23 12:29:03 +05:30
parent 816789fce5
commit fe48b79c09
20 changed files with 663 additions and 36 deletions
+56 -6
View File
@@ -48,7 +48,7 @@ from controllers_v2.core.base import CoreBaseModel
from models.core.auth_token import CoreAuthTokenModel
# To work with datatypes:
from typing import List
from typing import List, Literal
# To make API calls:
import httpx
@@ -210,6 +210,8 @@ class CoreAuthTokenController(CoreBaseModel):
"authType": auth_token.authType,
"user": auth_token.user.model_dump(),
"clientUserId": auth_token.clientUserId,
"clientDisplayName": auth_token.clientDisplayName,
"clientDisplayPicture": auth_token.clientDisplayPicture,
"auth": auth_token.auth,
"token": auth_token.token,
"firstRefreshTs": auth_token.firstRefreshTs,
@@ -258,6 +260,7 @@ class CoreAuthTokenController(CoreBaseModel):
token_notes: dict,
display_name: str = None,
display_picture: str = None,
last_action: Literal["Auth Requested", "Auth Granted", "Auth Revoked"] = "Auth Granted",
session_token: str = None
) -> bool:
@@ -272,6 +275,7 @@ class CoreAuthTokenController(CoreBaseModel):
:param token_notes: Any notes to feed into MariaDB with the token identifier.
:param display_name: The name of the user to user as their display name.
:param display_picture: The URL at which you will find a display picture of the user.
:param last_action: A string to show what action was taken last.
:param session_token: The session token of the user who requested this service.
:return: True if saved, False if failed.
"""
@@ -301,6 +305,8 @@ class CoreAuthTokenController(CoreBaseModel):
"auth": auth_token.auth,
"token": auth_token.token,
"status": auth_token.status,
"clientDisplayName": auth_token.clientDisplayName,
"clientDisplayPicture": auth_token.clientDisplayPicture,
"lastRefreshTs": request_ts,
"firstRefreshTs": {
"$cond": {
@@ -331,7 +337,7 @@ class CoreAuthTokenController(CoreBaseModel):
mongo_json["user"]["billingAccountId"], # ............................... 'p_billing_account_id'
mongo_json["client"], # ................................................. 'p_provider'
auth_token.status, # .................................................... 'p_current_status'
"Auth Granted", # ....................................................... 'p_last_action'
last_action or "Auth Granted", # ........................................ 'p_last_action'
display_name, # ......................................................... 'p_display_name'
display_picture, # ...................................................... 'p_display_picture'
token_key, # ............................................................ 'p_token_id'
@@ -398,14 +404,55 @@ class CoreAuthTokenController(CoreBaseModel):
# Done here:
return success
async def modify_status(
async def modify_status_by_token_key(
self,
sql_conn: AsyncMySQL,
mongo_data_conn: AsyncMongo,
auth_token: CoreAuthTokenModel,
):
token_key: str | ObjectId,
new_status: Literal["pending", "active", "disabled"],
additional_filter: dict = None,
session_token: str = None
) -> bool:
pass
"""
This is to simply modify the status of an account to efficiently activate/deactivate it.
NOTE: The operation succeeds ONLY IF the new status of the account is different from the existing status.
:param sql_conn: The database connection (MariaDB) to use to perform the action.
:param mongo_data_conn: The database connection (MongoDB) to use to perform the action.
:param token_key: The identifier granted by the 'generate_token_key' method.
:param new_status: Whatever you want the new status to be.
:param additional_filter: Any addition filters to use.
:param session_token: The session token of the user who requested this service.
:return: True if successful, else False.
"""
# Fetch the token from the key:
additional_filter = additional_filter or {}
# additional_filter["status"] = {"$ne": new_status}
if self._service_type is not None: additional_filter["serviceType"] = self._service_type
if self._client is not None: additional_filter["client"] = self._client
auth_token = await self.get_token_from_key(
mongo_data_conn = mongo_data_conn,
token_key = token_key,
additional_filter = additional_filter
)
# If there is not matching auth-token:
if not auth_token: return False
# Now we modify the status and return the result:
auth_token.status = new_status
return await self.set_token(
sql_conn = sql_conn,
mongo_data_conn = mongo_data_conn,
token_key = token_key,
auth_token = auth_token,
token_notes = {},
display_name = auth_token.clientDisplayName,
display_picture = auth_token.clientDisplayPicture,
last_action = "Auth Revoked",
session_token = session_token
)
# ┏┓┳┓┳┳┳┓ ┳┓ •
# ┃ ┣┫┃┃┃┃ ━━ ┣┫┏┓╋┏┓┓┏┓┓┏┏┓
@@ -597,6 +644,9 @@ class CoreAuthTokenController(CoreBaseModel):
batch_timeout_ts = now - datetime.timedelta(seconds = int(batch_timeout_seconds))
filter_json = {
"$and": [
{
"status": "active", # ... The account must be active.
},
{
"$or": [
{"syncAfterTs": None}, # ............ The time after which sync'ing is allowed is null.