Skip to content

Commit c7411a1

Browse files
committed
feat #5053 Use a form and POST for more security
1 parent 747cc37 commit c7411a1

File tree

5 files changed

+52
-20
lines changed

5 files changed

+52
-20
lines changed

src/submission/forms.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,3 +569,15 @@ def save(self, commit=True):
569569
if commit:
570570
affiliation.save()
571571
return affiliation
572+
573+
574+
class FrozenAuthorAccountForm(forms.ModelForm):
575+
"""
576+
A form to power the view that lets users link an existing
577+
author record to an existing account record.
578+
Not intended for use entering a new author record.
579+
"""
580+
581+
class Meta:
582+
model = models.FrozenAuthor
583+
fields = ("author",)

src/submission/logic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,8 @@ def get_current_authors(article, request):
634634
if author.email and not author.author:
635635
try:
636636
unlinked_account = core_models.Account.objects.get(
637-
email__iexact=author.email
637+
email__iexact=author.email,
638+
accountrole__role__slug="author",
638639
)
639640
except (
640641
core_models.Account.DoesNotExist,

src/submission/tests/test_views.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,15 @@ def test_edit_author_save_author(self):
354354
def test_link_author_to_account(self):
355355
self.client.force_login(self.kathleen)
356356

357-
# Create unlinked author record with same email as existing user
357+
358+
# Create an unlinked author record
358359
frozen_author, _created = models.FrozenAuthor.objects.get_or_create(
359360
article=self.article,
360-
first_name="T.",
361-
middle_name="S.",
362-
last_name="Eliot",
363-
frozen_email=self.eliot.email,
361+
frozen_email=email,
364362
)
365-
self.client.get(
363+
# Create an account with the same email
364+
account = helpers.create_user(email, ["author"], self.journal_one)
365+
self.client.post(
366366
reverse(
367367
"submission_link_author_to_account",
368368
kwargs={
@@ -375,5 +375,5 @@ def test_link_author_to_account(self):
375375
frozen_author.refresh_from_db()
376376
self.assertEqual(
377377
frozen_author.author,
378-
self.eliot,
378+
account,
379379
)

src/submission/views.py

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,21 +1024,32 @@ def order_authors(request, article_id):
10241024
return HttpResponse("Thanks")
10251025

10261026

1027-
@login_required
1027+
@require_POST
10281028
@user_can_edit_article
10291029
def link_author_to_account(request, article_id, author_id):
10301030
next_url = request.GET.get("next", "")
1031+
10311032
article = get_object_or_404(models.Article, pk=article_id, journal=request.journal)
10321033
author = get_object_or_404(models.FrozenAuthor, pk=author_id, article=article)
1033-
account = get_object_or_404(core_models.Account, email__iexact=author.email)
1034-
author.author = account
1035-
author.save()
1036-
messages.add_message(
1037-
request,
1038-
messages.SUCCESS,
1039-
"%(author_name)s (%(email)s) is now linked to a user account."
1040-
% {"author_name": author.full_name(), "email": author.email},
1034+
account = get_object_or_404(
1035+
core_models.Account,
1036+
email__iexact=author.email,
1037+
accountrole__role__slug="author",
10411038
)
1039+
1040+
author_account_form = forms.FrozenAuthorAccountForm(
1041+
{"author": account.pk},
1042+
instance=author,
1043+
)
1044+
if author_account_form.is_valid():
1045+
author_account_form.save()
1046+
messages.add_message(
1047+
request,
1048+
messages.SUCCESS,
1049+
"%(author_name)s (%(email)s) is now linked to a user account."
1050+
% {"author_name": author.full_name(), "email": author.email},
1051+
)
1052+
10421053
if next_url:
10431054
return redirect(next_url)
10441055
else:

src/templates/admin/elements/current_authors_inner.html

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,17 @@ <h2>{% trans "Current authors" %}</h2>
4343
this author ({{ unlinked_account.email }}), but they are not linked yet.
4444
Would you like to link the author to the account?
4545
</p>
46-
{% url_with_return "submission_link_author_to_account" article.pk author.pk as href %}
47-
{% trans "Link Account" as link_account %}
48-
{% include "elements/a_create.html" with href=href label=link_account %}
46+
<form
47+
method="POST"
48+
action="{% url_with_return "submission_link_author_to_account" article.pk author.pk %}">
49+
{% csrf_token %}
50+
<div class="button-group no-bottom-margin">
51+
<button class="button hollow secondary">
52+
<span class="fa fa-plus"></span>
53+
{% trans "Link Account" %}
54+
</button>
55+
</div>
56+
</form>
4957
</div>
5058
{% endif %}
5159
<div class="flex gap-2 direction-column-small">

0 commit comments

Comments
 (0)