2020-05-16 17:55:59 +00:00
|
|
|
"""passbook PolicyBinding administration"""
|
|
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django.contrib.auth.mixins import (
|
|
|
|
PermissionRequiredMixin as DjangoPermissionRequiredMixin,
|
|
|
|
)
|
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
2020-07-03 22:16:46 +00:00
|
|
|
from django.db.models import QuerySet
|
2020-05-16 17:55:59 +00:00
|
|
|
from django.urls import reverse_lazy
|
2020-09-11 21:21:11 +00:00
|
|
|
from django.utils.translation import gettext as _
|
2020-07-01 22:13:33 +00:00
|
|
|
from django.views.generic import ListView, UpdateView
|
2020-05-16 17:55:59 +00:00
|
|
|
from guardian.mixins import PermissionListMixin, PermissionRequiredMixin
|
2020-09-19 20:54:49 +00:00
|
|
|
from guardian.shortcuts import get_objects_for_user
|
2020-05-16 17:55:59 +00:00
|
|
|
|
2020-09-25 23:51:42 +00:00
|
|
|
from passbook.admin.views.utils import DeleteMessageView, UserPaginateListMixin
|
2020-05-16 17:55:59 +00:00
|
|
|
from passbook.lib.views import CreateAssignPermView
|
|
|
|
from passbook.policies.forms import PolicyBindingForm
|
2020-09-19 20:54:49 +00:00
|
|
|
from passbook.policies.models import PolicyBinding
|
2020-05-16 17:55:59 +00:00
|
|
|
|
|
|
|
|
2020-09-25 23:51:42 +00:00
|
|
|
class PolicyBindingListView(LoginRequiredMixin, PermissionListMixin, UserPaginateListMixin, ListView):
|
2020-05-16 17:55:59 +00:00
|
|
|
"""Show list of all policies"""
|
|
|
|
|
|
|
|
model = PolicyBinding
|
|
|
|
permission_required = "passbook_policies.view_policybinding"
|
2020-05-19 18:29:02 +00:00
|
|
|
ordering = ["order", "target"]
|
2020-07-03 22:16:46 +00:00
|
|
|
template_name = "administration/policy_binding/list.html"
|
|
|
|
|
|
|
|
def get_queryset(self) -> QuerySet:
|
|
|
|
# Since `select_subclasses` does not work with a foreign key, we have to do two queries here
|
|
|
|
# First, get all pbm objects that have bindings attached
|
|
|
|
objects = (
|
2020-09-19 20:54:49 +00:00
|
|
|
get_objects_for_user(
|
|
|
|
self.request.user, "passbook_policies.view_policybindingmodel"
|
|
|
|
)
|
|
|
|
.filter(policies__isnull=False)
|
2020-07-03 22:16:46 +00:00
|
|
|
.select_subclasses()
|
|
|
|
.select_related()
|
|
|
|
.order_by("pk")
|
|
|
|
)
|
|
|
|
for pbm in objects:
|
2020-09-19 20:54:49 +00:00
|
|
|
pbm.bindings = get_objects_for_user(
|
|
|
|
self.request.user, self.permission_required
|
|
|
|
).filter(target__pk=pbm.pbm_uuid)
|
2020-07-03 22:16:46 +00:00
|
|
|
return objects
|
2020-05-16 17:55:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PolicyBindingCreateView(
|
|
|
|
SuccessMessageMixin,
|
|
|
|
LoginRequiredMixin,
|
|
|
|
DjangoPermissionRequiredMixin,
|
|
|
|
CreateAssignPermView,
|
|
|
|
):
|
|
|
|
"""Create new PolicyBinding"""
|
|
|
|
|
|
|
|
model = PolicyBinding
|
|
|
|
permission_required = "passbook_policies.add_policybinding"
|
|
|
|
form_class = PolicyBindingForm
|
|
|
|
|
|
|
|
template_name = "generic/create.html"
|
2020-05-20 07:17:06 +00:00
|
|
|
success_url = reverse_lazy("passbook_admin:policies-bindings")
|
2020-05-16 17:55:59 +00:00
|
|
|
success_message = _("Successfully created PolicyBinding")
|
|
|
|
|
|
|
|
|
|
|
|
class PolicyBindingUpdateView(
|
|
|
|
SuccessMessageMixin, LoginRequiredMixin, PermissionRequiredMixin, UpdateView
|
|
|
|
):
|
|
|
|
"""Update policybinding"""
|
|
|
|
|
|
|
|
model = PolicyBinding
|
|
|
|
permission_required = "passbook_policies.change_policybinding"
|
|
|
|
form_class = PolicyBindingForm
|
|
|
|
|
|
|
|
template_name = "generic/update.html"
|
2020-05-20 07:17:06 +00:00
|
|
|
success_url = reverse_lazy("passbook_admin:policies-bindings")
|
2020-05-16 17:55:59 +00:00
|
|
|
success_message = _("Successfully updated PolicyBinding")
|
|
|
|
|
|
|
|
|
|
|
|
class PolicyBindingDeleteView(
|
2020-07-01 22:13:33 +00:00
|
|
|
LoginRequiredMixin, PermissionRequiredMixin, DeleteMessageView
|
2020-05-16 17:55:59 +00:00
|
|
|
):
|
|
|
|
"""Delete policybinding"""
|
|
|
|
|
|
|
|
model = PolicyBinding
|
|
|
|
permission_required = "passbook_policies.delete_policybinding"
|
|
|
|
|
|
|
|
template_name = "generic/delete.html"
|
2020-05-20 07:17:06 +00:00
|
|
|
success_url = reverse_lazy("passbook_admin:policies-bindings")
|
2020-05-16 17:55:59 +00:00
|
|
|
success_message = _("Successfully deleted PolicyBinding")
|