Commit 3778463e authored by BAIRE Anthony's avatar BAIRE Anthony
Browse files

replace User.provider_addresses with User.email_addresses

email.addresses lists all email addresses belonging to the user
thus we can make more generic queries

also adds EmailAddress.is_provider
parent 0f544778
......@@ -3,7 +3,6 @@ from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse, JsonResponse
from django.shortcuts import redirect
from allauth.account.models import EmailAddress
from .models import Job
from .helpers import get_request_user
......@@ -34,7 +33,8 @@ class AllgoValidAccountMixin:
if isinstance(self, ProviderAccessMixin):
# user visiting 'ProviderAccess' pages must have at least one
# of their provider addresses validated
email_addresses = user.provider_addresses
email_addresses = [addr for addr in user.email_addresses
if addr.is_provider]
if not email_addresses:
# user does not have Provider access
......@@ -43,7 +43,7 @@ class AllgoValidAccountMixin:
else:
# user visiting 'UserAccess' pages must have any of their
# addresses validated
email_addresses = EmailAddress.objects.filter(user=user)
email_addresses = user.email_addresses
if not any(addr.verified for addr in email_addresses):
# user has no verified address
......
......@@ -631,44 +631,43 @@ def save_user_profile(sender, instance, **kwargs):
instance.allgouser.save()
@property
def provider_addresses(user):
"""Get a tuple of EmailAddress in the allowed developer domains
WARNING: the returned EmailAddress items may not be verified
def is_provider(email_addr: EmailAddress) -> bool:
"""Return true if the email address is in the list of domains allowed to provide applications
This function lists all allauth EmailAddress of the user that match any
domain listed in settings.ALLOWED_DEVELOPER_DOMAINS
WARNING: the address may not be verified
"""
If an empty tuple is returned, then the user is not allowed to create a
webapp.
try:
_, domain = email_addr.email.split("@")
return domain in settings.ALLOWED_DEVELOPER_DOMAINS
except ValueError:
# malformatted email
return False
When the function returns a non-empty tuple this does not automatically
imply that the user can create a webapp, because we still have to ensure
that the email is validated (however this is sufficient for displaying the
"create webapp"/"import webapp" buttons).
EmailAddress.add_to_class('is_provider', is_provider)
@property
def email_addresses(user: User):
"""Get the email addresses associated to a user account
returns a db query of EmailAddress objects
"""
def get_domain(email_addr):
try:
_, domain = email_addr.email.split("@")
return domain
except ValueError:
# malformatted email
return None
return EmailAddress.objects.filter(user=user)
return tuple(addr for addr in EmailAddress.objects.filter(user=user)
if get_domain(addr) in settings.ALLOWED_DEVELOPER_DOMAINS)
auth.models.User.add_to_class('email_addresses', email_addresses)
def is_provider(user):
@property
def is_provider(user: User):
"""Return true if the user has at least one email address in the allowed
developer domains
Warning: this function returns True even if the email address is not
verified
"""
return bool(user.provider_addresses)
return any(addr.is_provider for addr in user.email_addresses)
# Add the `provider_addresses` and `is_provider` methods to the `User` model
auth.models.User.add_to_class('provider_addresses', provider_addresses)
# Add the `is_provider` method to the `User` model
auth.models.User.add_to_class('is_provider', is_provider)
# NOTE: because there is a circular dependency between models.py and
......
......@@ -56,7 +56,6 @@ from django.views.generic import (
)
from django.views.generic.detail import SingleObjectMixin
from taggit.models import Tag
from allauth.account.models import EmailAddress
from .forms import (
UserForm,
......@@ -407,8 +406,8 @@ class WebappImport(ProviderAccessMixin, SuccessMessageMixin, FormView):
current_user = self.request.user
if not current_user.is_superuser:
# get the user EmailAddress that matches the owner of the imported app
email_addr = EmailAddress.objects.filter(
user=current_user, email=js["user"]).first()
email_addr = current_user.email_addresses.filter(
email=js["user"]).first()
# ensure this app has the same owner
if email_addr is None:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment