From 6fe957fb1642d80c3fe83ebe34e7eaaf93e47041 Mon Sep 17 00:00:00 2001 From: Alisha Mohanty Date: May 11 2019 11:41:13 +0000 Subject: Prevent user from sending a Happiness Packet to themselves --- diff --git a/happinesspackets/messaging/forms.py b/happinesspackets/messaging/forms.py index b9f9636..63f1192 100644 --- a/happinesspackets/messaging/forms.py +++ b/happinesspackets/messaging/forms.py @@ -11,6 +11,7 @@ from django.conf import settings from django.urls import reverse from django.db.models import Q from django.utils import timezone +from email_normalize import normalize from .models import Message, strip_email @@ -61,14 +62,31 @@ class MessageSendForm(forms.ModelForm): Submit('submit', 'Send some happiness', css_class='btn-lg centered'), ) + def is_recipient_email_equals_sender_email(self): + recipient_email = self.cleaned_data.get('recipient_email') + sender_email = self.user.email + sender_username = self.user.username + normalized_sender_email = normalize(sender_email) + normalized_recipient_email = normalize(recipient_email) + #Fedora assigned email to the Sender + sender_fedora_email = sender_username + '@fedoraproject.org' + if normalized_recipient_email in ( + sender_email, sender_fedora_email, normalized_sender_email): + return True + else: + return False + def clean(self): super(MessageSendForm, self).clean() + isREEqualsSE = self.is_recipient_email_equals_sender_email() # if self.cleaned_data.get('hp'): # raise forms.ValidationError('') if self.cleaned_data.get('sender_approved_public_named') and not self.cleaned_data.get('sender_approved_public'): self.add_error('sender_approved_public_named', "If you want us to publish the message including your names, " "you must also check 'I agree to publish this message and" "display it publicly in the Happiness Archive'") + if isREEqualsSE: + raise forms.ValidationError("You cannot send a Fedora Happiness Packet to yourself!") validate_email(self.user.email) class MessageRecipientForm(forms.ModelForm): diff --git a/happinesspackets/messaging/views.py b/happinesspackets/messaging/views.py index 45359f3..0899711 100644 --- a/happinesspackets/messaging/views.py +++ b/happinesspackets/messaging/views.py @@ -227,7 +227,7 @@ class FasidSearchView(): @staticmethod def fasidCheck(request): try: - fas = AccountSystem(username= settings.ADMIN_USERNAME, password= settings.ADMIN_PASSWORD) + fas = AccountSystem(username=settings.ADMIN_USERNAME, password=settings.ADMIN_PASSWORD) fasid = request.GET['fasid'] is_server_error = 'False' type_of_error = ' No Error ' @@ -235,7 +235,7 @@ class FasidSearchView(): u_name = 'No name' u_email = 'No email' if not person: - logger.error("The FAS username doesnot exsist!") + logger.error("The FAS username does not exist!") account_exists = 'No' else: account_exists = 'Yes' diff --git a/requirements/base.txt b/requirements/base.txt index d18128a..a8b57a5 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -45,5 +45,8 @@ django-haystack==2.8.1 #python-fedora for f-a-s API python-fedora==0.10.0 +#Dependency for normalizing emails +email-normalize==0.2.1 + # Dependency for YAML file pyyaml==5.1