Skip to content

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

Move ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL flag to SiteSettings model (#12781)

SzymJede855333041
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 83306e99fc..09da33df19 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -32,6 +32,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Allow setting metadata during invoice creating and updating - #12641 by @IKarbowiak
- Introduce channel permissions - #10423 by @IKarbowiak
- Limit staff users to access only certain channels. Granted permissions only apply to channels that the user has already been given access to.
+- 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
@@ -42,12 +43,12 @@ All notable, unreleased changes to this project will be documented in this file.
### 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
- Add `ORDER_REFUNDED`, `ORDER_FULLY_REFUNDED`, `ORDER_PAID` webhooks - #12533 by @korycins
- 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
+- Remove `ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL` env variable from settings - ##12781 by @SzymJ
# 3.13.0
diff --git a/saleor/demo/settings.py b/saleor/demo/settings.py
index 3cdbdf23ca..8c576acffd 100644
--- a/saleor/demo/settings.py
+++ b/saleor/demo/settings.py
@@ -55,7 +55,6 @@ ROOT_EMAIL = os.environ.get("ROOT_EMAIL")
# (see saleor.demo.management.commands.populatedb).
INSTALLED_APPS.remove("saleor.core") # noqa: F405
INSTALLED_APPS += ["saleor.demo", "saleor.core"] # noqa: F405
-ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL = False
def _get_project_name_from_url(url: str) -> str:
diff --git a/saleor/graphql/account/mutations/account.py b/saleor/graphql/account/mutations/account.py
index fc6842305e..99a7234e68 100644
--- a/saleor/graphql/account/mutations/account.py
+++ b/saleor/graphql/account/mutations/account.py
@@ -27,6 +27,7 @@ from ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
from ...core.types import AccountError, BaseInputObjectType, NonNullList
from ...meta.mutations import MetadataInput
from ...plugins.dataloaders import get_plugin_manager_promise
+from ...site.dataloaders import get_site_promise
from ..enums import AddressTypeEnum
from ..i18n import I18nMixin
from ..types import Address, AddressInput, User
@@ -99,13 +100,18 @@ class AccountRegister(ModelMutation):
@classmethod
def mutate(cls, root, info: ResolveInfo, **data):
+ site = get_site_promise(info.context).get()
response = super().mutate(root, info, **data)
- response.requires_confirmation = settings.ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL
+ response.requires_confirmation = (
+ site.settings.enable_account_confirmation_by_email
+ )
return response
@classmethod
def clean_input(cls, info: ResolveInfo, instance, data, **kwargs):
- if not settings.ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL:
+ site = get_site_promise(info.context).get()
+
+ if not site.settings.enable_account_confirmation_by_email:
return super().clean_input(info, instance, data, **kwargs)
elif not data.get("redirect_url"):
raise ValidationError(
@@ -150,8 +156,10 @@ class AccountRegister(ModelMutation):
user, attach_addresses_data=False
)
manager = get_plugin_manager_promise(info.context).get()
+ site = get_site_promise(info.context).get()
+
with traced_atomic_transaction():
- if settings.ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL:
+ if site.settings.enable_account_confirmation_by_email:
user.is_active = False
user.save()
notifications.send_account_confirmation(
diff --git a/saleor/graphql/account/tests/test_account.py b/saleor/graphql/account/tests/test_account.py
index 1f4514c9ee..d63d7f00a4 100644
--- a/saleor/graphql/account/tests/test_account.py
+++ b/saleor/graphql/account/tests/test_account.py
@@ -1616,14 +1616,22 @@ def test_customer_register(
assert customer_creation_event.user == new_user
-@override_settings(ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL=False)
@patch("saleor.plugins.manager.PluginsManager.notify")
-def test_customer_register_disabled_email_confirmation(mocked_notify, api_client):
+def test_customer_register_disabled_email_confirmation(
+ mocked_notify, api_client, site_settings
+):
+ # given
+ site_settings.enable_account_confirmation_by_email = False
+ site_settings.save(update_fields=["enable_account_confirmation_by_email"])
+
variables = {"email": email, "password": "Password"}
+
+ # when
response = api_client.post_graphql(ACCOUNT_REGISTER_MUTATION, variables)
errors = response.json()["data"]["accountRegister"]["errors"]
+ # then
assert errors == []
created_user = User.objects.get()
expected_payload = get_default_user_payload(created_user)
@@ -1632,19 +1640,29 @@ def test_customer_register_disabled_email_confirmation(mocked_notify, api_client
mocked_notify.assert_not_called()
-@override_settings(ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL=True)
@patch("saleor.plugins.manager.PluginsManager.notify")
-def test_customer_register_no_redirect_url(mocked_notify, api_client):
+def test_customer_register_no_redirect_url(mocked_notify, api_client, site_settings):
+ # given
+ site_settings.enable_account_confirmation_by_email = True
+ site_settings.save(update_fields=["enable_account_confirmation_by_email"])
+
variables = {"email": "[email protected]", "password": "Password"}
+
+ # when
response = api_client.post_graphql(ACCOUNT_REGISTER_MUTATION, variables)
errors = response.json()["data"]["accountRegister"]["errors"]
+
+ # then
assert "redirectUrl" in map(lambda error: error["field"], errors)
mocked_notify.assert_not_called()
@override_settings(ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL=False)
-def test_customer_register_upper_case_email(api_client):
+def test_customer_register_upper_case_email(api_client, site_settings):
# given
+ site_settings.enable_account_confirmation_by_email = False
+ site_settings.save(update_fields=["enable_account_confirmation_by_email"])
+
variables = {"email": email, "password": "Password"}
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index d802136836..59b972d746 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -8941,6 +8941,15 @@ type Shop {
"""
staffNotificationRecipients: [StaffNotificationRecipient!]
+ """
+ Determines if account confirmation by email is enabled.
+
+ Added in Saleor 3.14.
+
+ Requires one of the following permissions: MANAGE_SETTINGS.
+ """
+ enableAccountConfirmationByEmail: Boolean
+
"""
Resource limitations and current usage if any set for a shop
@@ -17922,6 +17931,13 @@ input ShopSettingsInput {
"""
limitQuantityPerCheckout: Int
+ """
+ Enable automatic account confirmation by email.
+
+ Added in Saleor 3.14.
+ """
+ enableAccountConfirmationByEmail: Boolean
+
"""
Include taxes in prices.
diff --git a/saleor/graphql/shop/mutations.py b/saleor/graphql/shop/mutations.py
index f1d28811bc..aeec61114c 100644
--- a/saleor/graphql/shop/mutations.py
+++ b/saleor/graphql/shop/mutations.py
@@ -13,7 +13,7 @@ from ..account.i18n import I18nMixin
from ..account.types import AddressInput, StaffNotificationRecipient
from ..channel.types import OrderSettings
from ..core import ResolveInfo
-from ..core.descriptions import ADDED_IN_31, DEPRECATED_IN_3X_INPUT
+from ..core.descriptions import ADDED_IN_31, ADDED_IN_314, DEPRECATED_IN_3X_INPUT
from ..core.doc_category import (
DOC_CATEGORY_GIFT_CARDS,
DOC_CATEGORY_ORDERS,
@@ -86,6 +86,10 @@ class ShopSettingsInput(graphene.InputObjectType):
)
)
+ enable_account_confirmation_by_email = graphene.Boolean(
+ description="Enable automatic account confirmation by email." + ADDED_IN_314
+ )
+
# deprecated
include_taxes_in_prices = graphene.Boolean(
description=(
diff --git a/saleor/graphql/shop/tests/test_shop.py b/saleor/graphql/shop/tests/test_shop.py
index c1af8adc3f..9eab2c103e 100644
--- a/saleor/graphql/shop/tests/test_shop.py
+++ b/saleor/graphql/shop/tests/test_shop.py
@@ -274,6 +274,7 @@ def test_query_default_mail_sender_settings_not_set(
def test_shop_digital_content_settings_mutation(
staff_api_client, site_settings, permission_manage_settings
):
+ # given
query = """
mutation updateSettings($input: ShopSettingsInput!) {
shopSettingsUpdate(input: $input) {
@@ -301,11 +302,14 @@ def test_shop_digital_content_settings_mutation(
}
assert not site_settings.automatic_fulfillment_digital_products
+
+ # when
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_settings]
)
content = get_graphql_content(response)
+ # then
data = content["data"]["shopSettingsUpdate"]["shop"]
assert data["automaticFulfillmentDigitalProducts"]
assert data["defaultDigitalMaxDownloads"]
@@ -319,6 +323,7 @@ def test_shop_digital_content_settings_mutation(
def test_shop_settings_mutation(
staff_api_client, site_settings, permission_manage_settings
):
+ # given
query = """
mutation updateSettings($input: ShopSettingsInput!) {
shopSettingsUpdate(input: $input) {
@@ -328,6 +333,8 @@ def test_shop_settings_mutation(
chargeTaxesOnShipping,
fulfillmentAutoApprove,
fulfillmentAllowUnpaid
+ enableAccountConfirmationByEmail
+
}
errors {
field,
@@ -336,6 +343,7 @@ def test_shop_settings_mutation(
}
}
"""
+ assert site_settings.enable_account_confirmation_by_email
charge_taxes_on_shipping = site_settings.charge_taxes_on_shipping
new_charge_taxes_on_shipping = not charge_taxes_on_shipping
variables = {
@@ -344,21 +352,29 @@ def test_shop_settings_mutation(
"headerText": "Lorem ipsum",
"chargeTaxesOnShipping": new_charge_taxes_on_shipping,
"fulfillmentAllowUnpaid": False,
+ "enableAccountConfirmationByEmail": False,
}
}
+
+ # when
response = staff_api_client.post_graphql(
query, variables, permissions=[permission_manage_settings]
)
content = get_graphql_content(response)
data = content["data"]["shopSettingsUpdate"]["shop"]
+
+ # then
assert data["headerText"] == "Lorem ipsum"
assert data["includeTaxesInPrices"] is False
assert data["chargeTaxesOnShipping"] == new_charge_taxes_on_shipping
assert data["fulfillmentAutoApprove"] is True
assert data["fulfillmentAllowUnpaid"] is False
+ assert data["enableAccountConfirmationByEmail"] is False
+
site_settings.refresh_from_db()
assert not site_settings.include_taxes_in_prices
assert site_settings.charge_taxes_on_shipping == new_charge_taxes_on_shipping
+ assert site_settings.enable_account_confirmation_by_email is False
def test_shop_reservation_settings_mutation(
diff --git a/saleor/graphql/shop/types.py b/saleor/graphql/shop/types.py
index 8f540ae44c..ff4e718a32 100644
--- a/saleor/graphql/shop/types.py
+++ b/saleor/graphql/shop/types.py
@@ -18,6 +18,7 @@ from ..checkout.types import PaymentGateway
from ..core.descriptions import (
ADDED_IN_31,
ADDED_IN_35,
+ ADDED_IN_314,
DEPRECATED_IN_3X_FIELD,
DEPRECATED_IN_3X_INPUT,
)
@@ -290,6 +291,13 @@ class Shop(graphene.ObjectType):
required=False,
permissions=[SitePermissions.MANAGE_SETTINGS],
)
+ enable_account_confirmation_by_email = PermissionsField(
+ graphene.Boolean,
+ description=(
+ "Determines if account confirmation by email is enabled." + ADDED_IN_314
+ ),
+ permissions=[SitePermissions.MANAGE_SETTINGS],
+ )
limits = PermissionsField(
LimitInfo,
required=True,
@@ -514,6 +522,11 @@ class Shop(graphene.ObjectType):
def resolve_staff_notification_recipients(_, _info):
return account_models.StaffNotificationRecipient.objects.all()
+ @staticmethod
+ @load_site_callback
+ def resolve_enable_account_confirmation_by_email(_, _info, site):
+ return site.settings.enable_account_confirmation_by_email
+
@staticmethod
def resolve_limits(_, _info):
return LimitInfo(current_usage=Limits(), allowed_usage=Limits())
diff --git a/saleor/settings.py b/saleor/settings.py
index 4f4af55d91..0c10ff4d78 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -136,11 +136,6 @@ EMAIL_BACKEND: str = email_config.get("EMAIL_BACKEND", "")
EMAIL_USE_TLS: bool = email_config.get("EMAIL_USE_TLS", False)
EMAIL_USE_SSL: bool = email_config.get("EMAIL_USE_SSL", False)
-# If enabled, make sure you have set proper storefront address in ALLOWED_CLIENT_HOSTS.
-ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL = get_bool_from_env(
- "ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL", True
-)
-
ENABLE_SSL = get_bool_from_env("ENABLE_SSL", False)
if ENABLE_SSL:
@@ -732,16 +727,6 @@ for entry_point in installed_plugins:
PLUGINS = BUILTIN_PLUGINS + EXTERNAL_PLUGINS
-if (
- not DEBUG
- and ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL
- and ALLOWED_CLIENT_HOSTS == get_list(_DEFAULT_CLIENT_HOSTS)
-):
- raise ImproperlyConfigured(
- "Make sure you've added storefront address to ALLOWED_CLIENT_HOSTS "
- "if ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL is enabled."
- )
-
# Timeouts for webhook requests. Sync webhooks (eg. payment webhook) need more time
# for getting response from the server.
WEBHOOK_TIMEOUT = 10
diff --git a/saleor/site/migrations/0037_sitesettings_enable_account_confirmation_by_email.py b/saleor/site/migrations/0037_sitesettings_enable_account_confirmation_by_email.py
new file mode 100644
index 0000000000..bd85331e9c
--- /dev/null
+++ b/saleor/site/migrations/0037_sitesettings_enable_account_confirmation_by_email.py
@@ -0,0 +1,25 @@
+# Generated by Django 3.2.19 on 2023-05-08 09:36
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("site", "0036_remove_order_settings"),
+ ]
+
+ operations = [
+ migrations.AddField(
+ model_name="sitesettings",
+ name="enable_account_confirmation_by_email",
+ field=models.BooleanField(default=True),
+ ),
+ migrations.RunSQL(
+ """
+ ALTER TABLE site_sitesettings
+ ALTER COLUMN enable_account_confirmation_by_email
+ SET DEFAULT true;
+ """,
+ migrations.RunSQL.noop,
+ ),
+ ]
diff --git a/saleor/site/migrations/0038_auto_20230510_1107.py b/saleor/site/migrations/0038_auto_20230510_1107.py
new file mode 100644
index 0000000000..da87b8c07b
--- /dev/null
+++ b/saleor/site/migrations/0038_auto_20230510_1107.py
@@ -0,0 +1,23 @@
+# Generated by Django 3.2.19 on 2023-05-10 11:07
+
+from django.db import migrations
+from ...settings import get_bool_from_env
+
+
+def set_enable_account_confirmation_by_email_flag(apps, schema_editor):
+ confirmation_flag = get_bool_from_env("ENABLE_ACCOUNT_CONFIRMATION_BY_EMAIL", True)
+ SiteSettings = apps.get_model("site", "SiteSettings")
+ SiteSettings.objects.update(enable_account_confirmation_by_email=confirmation_flag)
+
+
+class Migration(migrations.Migration):
+ dependencies = [
+ ("site", "0037_sitesettings_enable_account_confirmation_by_email"),
+ ]
+
+ operations = [
+ migrations.RunPython(
+ set_enable_account_confirmation_by_email_flag,
+ migrations.RunPython.noop,
+ ),
+ ]
diff --git a/saleor/site/models.py b/saleor/site/models.py
index 86369e871b..0807ed3508 100644
--- a/saleor/site/models.py
+++ b/saleor/site/models.py
@@ -64,6 +64,7 @@ class SiteSettings(models.Model):
validators=email_sender_name_validators(),
)
default_mail_sender_address = models.EmailField(blank=True, null=True)
+ enable_account_confirmation_by_email = models.BooleanField(default=True)
customer_set_password_url = models.CharField(max_length=255, blank=True, null=True)
fulfillment_auto_approve = models.BooleanField(default=True)
fulfillment_allow_unpaid = models.BooleanField(default=True)