From f87df5204e8bb4d553a10672917f280fdfe765f6 Mon Sep 17 00:00:00 2001 From: Max Dor Date: Tue, 19 Feb 2019 22:29:55 +0100 Subject: [PATCH] Allow toggle to enable/disable merging 3PIDs in profile --- README.md | 8 +++++++ rest_auth_provider.py | 49 +++++++++++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index d2ac8d1..1abb526 100644 --- a/README.md +++ b/README.md @@ -75,6 +75,14 @@ If you would like to change the behaviour, you can use the following configurati ``` 3PIDs received from the backend are merged with the ones already linked to the account. +If you would like to change this behaviour, you can use the following configuration item: +```yaml + config: + policy: + all: + threepid: + update: false +``` ## Integrate To use this module with your back-end, you will need to implement a single REST endpoint: diff --git a/rest_auth_provider.py b/rest_auth_provider.py index a72dcc4..1d582d9 100644 --- a/rest_auth_provider.py +++ b/rest_auth_provider.py @@ -87,25 +87,28 @@ class RestAuthProvider(object): else: logger.info("Display name was not set because it was not given or policy restricted it") - if "three_pids" in profile: - logger.info("Handling 3PIDs") - for threepid in profile["three_pids"]: - medium = threepid["medium"].lower() - address = threepid["address"].lower() - logger.info("Looking for 3PID %s:%s in user profile", medium, address) + if (self.config.updateThreepid): + if "three_pids" in profile: + logger.info("Handling 3PIDs") + for threepid in profile["three_pids"]: + medium = threepid["medium"].lower() + address = threepid["address"].lower() + logger.info("Looking for 3PID %s:%s in user profile", medium, address) - validated_at = self.account_handler.hs.get_clock().time_msec() - if not (yield store.get_user_id_by_threepid(medium, address)): - logger.info("3PID is not present, adding") - yield store.user_add_threepid( - user_id, - medium, - address, - validated_at, - validated_at - ) - else: - logger.info("3PID is present, skipping") + validated_at = self.account_handler.hs.get_clock().time_msec() + if not (yield store.get_user_id_by_threepid(medium, address)): + logger.info("3PID is not present, adding") + yield store.user_add_threepid( + user_id, + medium, + address, + validated_at, + validated_at + ) + else: + logger.info("3PID is present, skipping") + else: + logger.info("3PIDs were not updated due to policy") else: logger.info("No profile data") @@ -121,6 +124,7 @@ class RestAuthProvider(object): regLower = True setNameOnRegister = True setNameOnLogin = False + updateThreepid = True rest_config = _RestConfig() rest_config.endpoint = config["endpoint"] @@ -152,6 +156,15 @@ class RestAuthProvider(object): # we don't care pass + try: + rest_config.updateThreepid = config['policy']['all']['threepid']['update'] + except TypeError: + # we don't care + pass + except KeyError: + # we don't care + pass + return rest_config def _require_keys(config, required):