2018-11-25 19:39:09 +00:00
|
|
|
"""passbook access helper classes"""
|
2020-07-01 22:18:10 +01:00
|
|
|
from typing import Optional
|
|
|
|
|
2019-02-26 08:10:37 +00:00
|
|
|
from django.contrib import messages
|
2020-07-02 12:48:42 +01:00
|
|
|
from django.contrib.auth.mixins import AccessMixin
|
2020-07-12 21:47:46 +01:00
|
|
|
from django.contrib.auth.views import redirect_to_login
|
2020-07-02 12:48:42 +01:00
|
|
|
from django.http import HttpRequest, HttpResponse
|
|
|
|
from django.shortcuts import redirect
|
2019-02-26 08:10:37 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2019-10-01 09:24:10 +01:00
|
|
|
from structlog import get_logger
|
2018-12-09 20:07:38 +00:00
|
|
|
|
2019-10-04 15:09:35 +01:00
|
|
|
from passbook.core.models import Application, Provider, User
|
2020-07-12 21:47:46 +01:00
|
|
|
from passbook.flows.views import SESSION_KEY_APPLICATION_PRE
|
2019-10-07 15:33:48 +01:00
|
|
|
from passbook.policies.engine import PolicyEngine
|
2020-05-28 20:45:54 +01:00
|
|
|
from passbook.policies.types import PolicyResult
|
2018-12-09 20:07:38 +00:00
|
|
|
|
2019-10-04 09:08:53 +01:00
|
|
|
LOGGER = get_logger()
|
2018-11-25 19:39:09 +00:00
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
|
2020-07-01 22:18:10 +01:00
|
|
|
class BaseMixin:
|
|
|
|
"""Base Mixin class, used to annotate View Member variables"""
|
|
|
|
|
|
|
|
request: HttpRequest
|
|
|
|
|
|
|
|
|
2020-07-02 12:48:42 +01:00
|
|
|
class PolicyAccessMixin(BaseMixin, AccessMixin):
|
2018-11-25 19:39:09 +00:00
|
|
|
"""Mixin class for usage in Authorization views.
|
|
|
|
Provider functions to check application access, etc"""
|
|
|
|
|
2020-07-12 21:47:46 +01:00
|
|
|
def handle_no_permission(self, application: Optional[Application] = None):
|
|
|
|
if application:
|
|
|
|
self.request.session[SESSION_KEY_APPLICATION_PRE] = application
|
|
|
|
return redirect_to_login(
|
|
|
|
self.request.get_full_path(),
|
|
|
|
self.get_login_url(),
|
|
|
|
self.get_redirect_field_name(),
|
|
|
|
)
|
|
|
|
|
2020-07-02 12:48:42 +01:00
|
|
|
def handle_no_permission_authorized(self) -> HttpResponse:
|
|
|
|
"""Function called when user has no permissions but is authorized"""
|
2020-07-08 23:41:14 +01:00
|
|
|
# TODO: Remove this URL and render the view instead
|
2020-07-02 12:48:42 +01:00
|
|
|
return redirect("passbook_flows:denied")
|
|
|
|
|
2019-10-04 15:09:35 +01:00
|
|
|
def provider_to_application(self, provider: Provider) -> Application:
|
2018-11-25 19:39:09 +00:00
|
|
|
"""Lookup application assigned to provider, throw error if no application assigned"""
|
2018-12-09 20:07:38 +00:00
|
|
|
try:
|
|
|
|
return provider.application
|
|
|
|
except Application.DoesNotExist as exc:
|
2019-12-31 11:51:16 +00:00
|
|
|
messages.error(
|
|
|
|
self.request,
|
|
|
|
_(
|
|
|
|
'Provider "%(name)s" has no application assigned'
|
|
|
|
% {"name": provider}
|
|
|
|
),
|
|
|
|
)
|
2019-02-26 08:10:37 +00:00
|
|
|
raise exc
|
2018-11-25 19:39:09 +00:00
|
|
|
|
2020-07-01 22:18:10 +01:00
|
|
|
def user_has_access(
|
|
|
|
self, application: Application, user: Optional[User] = None
|
|
|
|
) -> PolicyResult:
|
2018-11-25 19:39:09 +00:00
|
|
|
"""Check if user has access to application."""
|
2020-07-01 22:18:10 +01:00
|
|
|
user = user or self.request.user
|
|
|
|
policy_engine = PolicyEngine(
|
|
|
|
application, user or self.request.user, self.request
|
|
|
|
)
|
2019-10-15 14:44:59 +01:00
|
|
|
policy_engine.build()
|
2020-07-01 22:18:10 +01:00
|
|
|
result = policy_engine.result
|
|
|
|
LOGGER.debug(
|
|
|
|
"AccessMixin user_has_access", user=user, app=application, result=result,
|
|
|
|
)
|
|
|
|
if not result.passing:
|
|
|
|
for message in result.messages:
|
|
|
|
messages.error(self.request, _(message))
|
|
|
|
return result
|