diff --git a/passbook/core/models.py b/passbook/core/models.py index 2fc1aac86..ca7e980fc 100644 --- a/passbook/core/models.py +++ b/passbook/core/models.py @@ -64,7 +64,8 @@ class Application(RuleModel): def user_is_authorized(self, user: User) -> bool: """Check if user is authorized to use this application""" - raise NotImplementedError() + from passbook.core.rules import RuleEngine + return RuleEngine(self).for_user(user).result def __str__(self): return self.name diff --git a/passbook/core/views/access.py b/passbook/core/views/access.py index c136650ef..dc42c25d3 100644 --- a/passbook/core/views/access.py +++ b/passbook/core/views/access.py @@ -1,6 +1,10 @@ """passbook access helper classes""" from logging import getLogger +from django.http import Http404 + +from passbook.core.models import Application + LOGGER = getLogger(__name__) class AccessMixin: @@ -9,7 +13,12 @@ class AccessMixin: def provider_to_application(self, provider): """Lookup application assigned to provider, throw error if no application assigned""" - return provider.application + try: + return provider.application + except Application.DoesNotExist as exc: + # TODO: Log that no provider has no application assigned + LOGGER.warning('Provider "%s" has no application assigned...', provider) + raise Http404 from exc def user_has_access(self, application, user): """Check if user has access to application.""" diff --git a/passbook/core/views/authentication.py b/passbook/core/views/authentication.py index 76d23dc4f..83bc01603 100644 --- a/passbook/core/views/authentication.py +++ b/passbook/core/views/authentication.py @@ -26,7 +26,17 @@ class LoginView(UserPassesTestMixin, FormView): # Allow only not authenticated users to login def test_func(self): - return not self.request.user.is_authenticated + return self.request.user.is_authenticated is False + + def handle_no_permission(self): + return self.logged_in_redirect() + + def logged_in_redirect(self): + """User failed check so user is authenticated already. + Either redirect to ?next param or home.""" + if 'next' in self.request.GET: + return redirect(self.request.GET.get('next')) + return redirect(reverse('passbook_core:overview')) def get_context_data(self, **kwargs): kwargs['config'] = CONFIG.get('passbook') @@ -80,11 +90,7 @@ class LoginView(UserPassesTestMixin, FormView): request.session.set_expiry(0) # Expires when browser is closed messages.success(request, _("Successfully logged in!")) LOGGER.debug("Successfully logged in %s", user.username) - # Check if there is a next GET parameter and redirect to that - if 'next' in request.GET: - return redirect(request.GET.get('next')) - # Otherwise just index - return redirect(reverse('passbook_core:overview')) + return self.logged_in_redirect() def invalid_login(self, request: HttpRequest, disabled_user: User = None) -> HttpResponse: """Handle login for disabled users/invalid login attempts""" diff --git a/passbook/oauth_client/backends.py b/passbook/oauth_client/backends.py index 22c26009c..647fc048f 100644 --- a/passbook/oauth_client/backends.py +++ b/passbook/oauth_client/backends.py @@ -19,8 +19,6 @@ class AuthorizedServiceBackend(ModelBackend): source_q, identifier=identifier ).select_related('user')[0] except IndexError: - print('hmm') return None else: - print('a') return access.user diff --git a/passbook/oauth_client/views/core.py b/passbook/oauth_client/views/core.py index 6b607766b..d0892c4c6 100644 --- a/passbook/oauth_client/views/core.py +++ b/passbook/oauth_client/views/core.py @@ -1,17 +1,14 @@ """Core OAauth Views""" -import base64 -import hashlib from logging import getLogger from django.conf import settings from django.contrib import messages -from django.contrib.auth import authenticate, get_user_model, login +from django.contrib.auth import authenticate, login from django.contrib.auth.mixins import LoginRequiredMixin from django.http import Http404 from django.shortcuts import get_object_or_404, redirect, render from django.urls import reverse -from django.utils.encoding import force_text, smart_bytes from django.utils.translation import ugettext as _ from django.views.generic import RedirectView, View diff --git a/setup.cfg b/setup.cfg new file mode 100644 index 000000000..1bcc2fecc --- /dev/null +++ b/setup.cfg @@ -0,0 +1,3 @@ +[pycodestyle] +ignore = E731,E121 +max-line-length = 100