Skip to content

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

Add redirect url to staff create mutation

fowczarekd732fd2d447b
diff --git a/saleor/dashboard/emails.py b/saleor/dashboard/emails.py
index d8d0448e80..be32840ad3 100644
--- a/saleor/dashboard/emails.py
+++ b/saleor/dashboard/emails.py
@@ -1,3 +1,5 @@
+from urllib.parse import urlencode
+
from django.conf import settings
from django.contrib.auth.tokens import default_token_generator
from django.urls import reverse
@@ -11,33 +13,73 @@ from ..core.emails import get_email_base_context
from ..core.utils import build_absolute_uri
-def _send_set_password_email(pk, template_name):
- user = User.objects.get(pk=pk)
- uid = urlsafe_base64_encode(force_bytes(user.pk))
- token = default_token_generator.make_token(user)
+def send_set_password_staff_email_with_url(redirect_url, staff):
+ """Trigger sending a set password email for the given staff."""
+ token = default_token_generator.make_token(staff)
+ _send_set_password_staff_email_with_url.delay(staff.email, redirect_url, token)
+
+
+def send_set_password_staff_email(staff):
+ """Trigger sending a set password email for the given staff."""
+ token = default_token_generator.make_token(staff)
+ _send_set_password_staff_email.delay(staff.email, staff.pk, token)
+
+
+def _send_set_password_staff_email_with_url(recipient_email, redirect_url, token):
+ params = urlencode({"email": recipient_email, "token": token})
+ password_set_url = f"{redirect_url}?%{params}"
+ _send_set_password_email(
+ recipient_email, password_set_url, "dashboard/staff/set_password"
+ )
+
+
+def _send_set_password_staff_email(recipient_email, staff_pk, token):
+ uid = urlsafe_base64_encode(force_bytes(staff_pk))
password_set_url = build_absolute_uri(
reverse(
"account:reset-password-confirm", kwargs={"token": token, "uidb64": uid}
)
)
+ _send_set_password_email(
+ recipient_email, password_set_url, "dashboard/staff/set_password"
+ )
+
+
+def _send_set_password_email(recipient_email, password_set_url, template_name):
ctx = get_email_base_context()
ctx["password_set_url"] = password_set_url
send_templated_mail(
template_name=template_name,
from_email=settings.DEFAULT_FROM_EMAIL,
- recipient_list=[user.email],
+ recipient_list=[recipient_email],
context=ctx,
)
@app.task
-def send_set_password_staff_email(staff_pk):
- _send_set_password_email(staff_pk, "dashboard/staff/set_password")
+def send_set_password_customer_email(pk):
+ _old_send_set_password_email(pk, "dashboard/customer/set_password")
-def send_set_password_customer_email(pk):
- _send_set_password_email(pk, "dashboard/customer/set_password")
+def _old_send_set_password_email(pk, template_name):
+ user = User.objects.get(pk=pk)
+ uid = urlsafe_base64_encode(force_bytes(user.pk))
+ token = default_token_generator.make_token(user)
+ password_set_url = build_absolute_uri(
+ reverse(
+ "account:reset-password-confirm", kwargs={"token": token, "uidb64": uid}
+ )
+ )
+ ctx = get_email_base_context()
+ ctx["password_set_url"] = password_set_url
+ send_templated_mail(
+ template_name=template_name,
+ from_email=settings.DEFAULT_FROM_EMAIL,
+ recipient_list=[user.email],
+ context=ctx,
+ )
@app.task
diff --git a/saleor/dashboard/staff/views.py b/saleor/dashboard/staff/views.py
index 32c8e13cf1..25ff30e84f 100644
--- a/saleor/dashboard/staff/views.py
+++ b/saleor/dashboard/staff/views.py
@@ -71,7 +71,7 @@ def staff_create(request):
msg = pgettext_lazy("Dashboard message", "Added staff member %s") % (staff,)
messages.success(request, msg)
if created:
- send_set_password_staff_email.delay(staff.pk)
+ send_set_password_staff_email(staff)
else:
send_promote_customer_to_staff_email.delay(staff.pk)
return redirect("dashboard:staff-list")
diff --git a/saleor/graphql/account/mutations/staff.py b/saleor/graphql/account/mutations/staff.py
index 2e69ebc07d..8ace56714e 100644
--- a/saleor/graphql/account/mutations/staff.py
+++ b/saleor/graphql/account/mutations/staff.py
@@ -10,7 +10,8 @@ from ....account.thumbnails import create_user_avatar_thumbnails
from ....account.utils import get_random_avatar
from ....checkout import AddressType
from ....core.permissions import get_permissions
-from ....dashboard.emails import send_set_password_staff_email
+from ....core.utils.url import validate_storefront_url
+from ....dashboard.emails import send_set_password_staff_email_with_url
from ....dashboard.staff.utils import remove_staff_member
from ...account.enums import AddressTypeEnum
from ...account.types import Address, AddressInput, User
@@ -45,6 +46,12 @@ class StaffCreateInput(StaffInput):
send_password_email = graphene.Boolean(
description="Send an email with a link to set the password"
)
+ redirect_url = graphene.String(
+ description=(
+ "URL of a view where users should be redirected to "
+ "set the password. URL in RFC 1808 format.",
+ )
+ )
class CustomerCreate(BaseCustomerCreate):
@@ -155,6 +162,13 @@ class StaffCreate(ModelMutation):
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
+ if cleaned_input.get("send_password_email"):
+ if not cleaned_input.get("redirect_url"):
+ raise ValidationError(
+ {"redirect_url": "Redirect url is required to send a password."}
+ )
+ validate_storefront_url(cleaned_input.get("redirect_url"))
+
# set is_staff to True to create a staff user
cleaned_input["is_staff"] = True
@@ -173,7 +187,9 @@ class StaffCreate(ModelMutation):
if create_avatar:
create_user_avatar_thumbnails.delay(user_id=user.pk)
if cleaned_input.get("send_password_email"):
- send_set_password_staff_email.delay(user.pk)
+ send_set_password_staff_email_with_url(
+ cleaned_input.get("redirect_url"), user
+ )
class StaffUpdate(StaffCreate):
diff --git a/tests/api/test_account.py b/tests/api/test_account.py
index 91b78b5bec..f88b06790d 100644
--- a/tests/api/test_account.py
+++ b/tests/api/test_account.py
@@ -1207,19 +1207,12 @@ def test_customer_delete_errors(customer_user, admin_user, staff_user):
CustomerDelete.clean_instance(info, customer_user)
-@patch("saleor.dashboard.emails.send_set_password_staff_email.delay")
-def test_staff_create(
- send_set_password_staff_email_mock,
- staff_api_client,
- media_root,
- permission_manage_staff,
-):
- query = """
+STAFF_CREATE_MUTATION = """
mutation CreateStaff(
$email: String, $permissions: [PermissionEnum],
- $send_mail: Boolean) {
+ $send_mail: Boolean, $redirect_url: String) {
staffCreate(input: {email: $email, permissions: $permissions,
- sendPasswordEmail: $send_mail}) {
+ sendPasswordEmail: $send_mail, redirectUrl: $redirect_url}) {
errors {
field
message
@@ -1238,17 +1231,23 @@ def test_staff_create(
}
}
}
- """
+"""
+
+@patch("saleor.dashboard.emails._send_set_password_email")
+def test_staff_create(
+ _send_set_password_email_mock, staff_api_client, media_root, permission_manage_staff
+):
variables = {
"email": email,
"permissions": [PermissionEnum.MANAGE_PRODUCTS.name],
"send_mail": True,
+ "redirect_url": "https://www.example.com",
}
response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_staff]
+ STAFF_CREATE_MUTATION, variables, permissions=[permission_manage_staff]
)
content = get_graphql_content(response)
data = content["data"]["staffCreate"]
@@ -1268,10 +1267,73 @@ def test_staff_create(
assert staff_user.is_staff
- assert send_set_password_staff_email_mock.call_count == 1
- args, kwargs = send_set_password_staff_email_mock.call_args
- call_pk = args[0]
- assert call_pk == staff_user.pk
+ _send_set_password_email_mock.assert_called_once_with(
+ staff_user.email, ANY, "dashboard/staff/set_password"
+ )
+
+
+def test_staff_create_without_send_password(
+ staff_api_client, media_root, permission_manage_staff
+):
+ email = "[email protected]"
+ variables = {"email": email}
+ response = staff_api_client.post_graphql(
+ STAFF_CREATE_MUTATION, variables, permissions=[permission_manage_staff]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["staffCreate"]
+ assert not data["errors"]
+ staff_user = User.objects.get(email=email)
+ assert staff_user
+
+
+def test_staff_create_without_redirect_url(
+ staff_api_client, media_root, permission_manage_staff
+):
+ email = "[email protected]"
+ variables = {"email": email, "send_mail": True}
+ response = staff_api_client.post_graphql(
+ STAFF_CREATE_MUTATION, variables, permissions=[permission_manage_staff]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["staffCreate"]
+ assert data["errors"][0]["field"] == "redirectUrl"
+ staff_user = User.objects.filter(email=email)
+ assert not staff_user
+
+
+def test_staff_create_without_invalid_url(
+ staff_api_client, media_root, permission_manage_staff
+):
+ email = "[email protected]"
+ variables = {"email": email, "send_mail": True, "redirect_url": "invalid"}
+ response = staff_api_client.post_graphql(
+ STAFF_CREATE_MUTATION, variables, permissions=[permission_manage_staff]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["staffCreate"]
+ assert data["errors"][0]["field"] == "redirectUrl"
+ staff_user = User.objects.filter(email=email)
+ assert not staff_user
+
+
+def test_staff_create_without_url_not_allowed(
+ staff_api_client, media_root, permission_manage_staff
+):
+ email = "[email protected]"
+ variables = {
+ "email": email,
+ "send_mail": True,
+ "redirectUrl": "https://www.fake.com",
+ }
+ response = staff_api_client.post_graphql(
+ STAFF_CREATE_MUTATION, variables, permissions=[permission_manage_staff]
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["staffCreate"]
+ assert data["errors"][0]["field"] == "redirectUrl"
+ staff_user = User.objects.filter(email=email)
+ assert not staff_user
def test_staff_update(staff_api_client, permission_manage_staff, media_root):