Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Match orders to user when creating new user or changing email with OIDC plugin (#12863)

Krzysztof Waliczekce3f535366e3
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 284aa43aa3..f54749a86d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -35,6 +35,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Add `enable_account_confirmation_by_email` to `SiteSettings` model and allow to update it via `shopSettingsUpdate` mutation - #12781 by @SzymJ
### Saleor Apps
+
- Introduce channel permissions - #10423 by @IKarbowiak
- Extend the OpenID connect configuration with `Staff user domains` and `Default permission group name for new staff users`.
- When the OpenID plugin is active, the default staff permission group is created and all staff users are assigned to it.
@@ -42,6 +43,7 @@ All notable, unreleased changes to this project will be documented in this file.
`Default permission group name for new staff users` is set to a permission group with no channel restrictions.
### Other changes
+
- Fix saving `description_plaintext` for product - #12586 by @SzymJ
- Remove default `EMAIL_URL` value pointing to console output; from now on EMAIL_URL has to be set explicitly - #12580 by @maarcingebala
- Fix sending `product_created` event in `ProductBulkCreate` mutation - #12605 by @SzymJ
@@ -50,7 +52,8 @@ All notable, unreleased changes to this project will be documented in this file.
- Handle error raised by 0Auth when fetching token - #12672 by @IKarbowiakg
- Fix adding new lines to draft order when existing line has deleted product - #12711 by @SzymJ
- Upgrade checkout `complete_checkout` to assign guest checkout to account if exists - #12758 by @FremahA
-- Remove `ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL` env variable from settings - ##12781 by @Szym
+- Remove `ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL` env variable from settings - ##12781 by @SzymJ
+- Match `Orders` to `User` when creating user using OIDC plugin. - #12863 by @kadewu
# 3.13.0
diff --git a/saleor/plugins/openid_connect/tests/test_utils.py b/saleor/plugins/openid_connect/tests/test_utils.py
index 4692bbe477..ea514d4187 100644
--- a/saleor/plugins/openid_connect/tests/test_utils.py
+++ b/saleor/plugins/openid_connect/tests/test_utils.py
@@ -280,6 +280,54 @@ def test_get_or_create_user_from_payload_creates_user_with_sub():
assert not user_from_payload.has_usable_password()
+def test_get_or_create_user_from_payload_match_orders_for_new_user(order):
+ # given
+ oauth_url = "https://saleor.io/oauth"
+ sub_id = "oauth|1234"
+ customer_email = "[email protected]"
+
+ order.user = None
+ order.user_email = customer_email
+ order.save()
+
+ # when
+ user_from_payload = get_or_create_user_from_payload(
+ payload={"sub": sub_id, "email": customer_email},
+ oauth_url=oauth_url,
+ )
+
+ # then
+ order.refresh_from_db()
+ assert order.user == user_from_payload
+
+
+def test_get_or_create_user_from_payload_match_orders_when_changing_email(
+ customer_user, order
+):
+ # given
+ oauth_url = "https://saleor.io/oauth"
+ sub_id = "oauth|1234"
+ new_customer_email = "[email protected]"
+
+ customer_user.private_metadata = {f"oidc-{oauth_url}": sub_id}
+ customer_user.save()
+
+ order.user_email = new_customer_email
+ order.user = None
+ order.save()
+
+ # when
+ user_from_payload = get_or_create_user_from_payload(
+ payload={"sub": sub_id, "email": new_customer_email},
+ oauth_url=oauth_url,
+ )
+
+ # then
+ customer_user.refresh_from_db()
+ order.refresh_from_db()
+ assert order.user == user_from_payload
+
+
def test_get_or_create_user_from_payload_multiple_subs(customer_user, admin_user):
# given
oauth_url = "https://saleor.io/oauth"
diff --git a/saleor/plugins/openid_connect/utils.py b/saleor/plugins/openid_connect/utils.py
index ce97cdeead..5ef18d0b43 100644
--- a/saleor/plugins/openid_connect/utils.py
+++ b/saleor/plugins/openid_connect/utils.py
@@ -30,6 +30,7 @@ from ...graphql.account.mutations.authentication import (
_does_token_match,
_get_new_csrf_token,
)
+from ...order.utils import match_orders_with_new_user
from ...permission.enums import get_permission_names, get_permissions_from_codenames
from ...permission.models import Permission
from ..error_codes import PluginErrorCode
@@ -403,6 +404,7 @@ def get_or_create_user_from_payload(
email=user_email,
defaults=defaults_create,
)
+ match_orders_with_new_user(user)
except User.MultipleObjectsReturned:
logger.warning("Multiple users returned for single OIDC sub ID")
user, _ = User.objects.get_or_create(
@@ -451,6 +453,7 @@ def _update_user_details(
)
return
user.email = user_email
+ match_orders_with_new_user(user)
fields_to_save.append("email")
if last_login:
if not user.last_login or user.last_login.timestamp() < last_login: