Skip to content

Commit

Permalink
Add permission to check email
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin Etienne committed Apr 16, 2015
1 parent 351ff9e commit 89362be
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 3 deletions.
11 changes: 11 additions & 0 deletions user_management/api/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,14 @@ def has_permission(self, request, view):
return True

return request.user.is_staff


class IsAnonymousOrOwner(BasePermission):
"""Check if user is anonymous or if email belongs to user."""
def has_permission(self, request, view):
if request.user.is_anonymous():
return True
email = request.DATA.get('email')
if not email:
return True
return request.user.email.lower() == email.lower()
20 changes: 20 additions & 0 deletions user_management/api/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,3 +1028,23 @@ def test_send_email_authenticated(self):

expected = 'example.com account validate'
self.assertEqual(email.subject, expected)

def test_send_email_other_user(self):
"""Assert a user can not request a confirmation email for another user."""
user, other_user = UserFactory.create_batch(2)
data = {'email': other_user.email}
request = self.create_request('post', user=user, data=data)
view = self.view_class.as_view()
response = view(request)

self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)

def test_send_email_empty(self):
"""Assert we delegate the error to the serializer if no email data was sent."""
data = {}
request = self.create_request('post', data=data)
view = self.view_class.as_view()
response = view(request)

self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn('This field is required.', response.data['email'])
6 changes: 3 additions & 3 deletions user_management/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,10 @@ class ResendConfirmationEmail(generics.GenericAPIView):
"""
Resend a confirmation email.
`POST` request to resend a confirmation email for existing user. Useful when
the token has expired.
`POST` request to resend a confirmation email for existing user. If user is
authenticated the email sent should match.
"""
permission_classes = [AllowAny]
permission_classes = [permissions.IsAnonymousOrOwner]
serializer_class = serializers.ResendConfirmationEmailSerializer
throttle_classes = [throttling.ResendConfirmationEmailRateThrottle]
throttle_scope = 'confirmations'
Expand Down

0 comments on commit 89362be

Please sign in to comment.