jumpserver/apps/perms/views.py

149 lines
5.4 KiB
Python
Raw Normal View History

2016-09-10 13:08:10 +00:00
# ~*~ coding: utf-8 ~*~
2016-08-08 16:43:11 +00:00
2016-09-10 13:08:10 +00:00
from __future__ import unicode_literals, absolute_import
2017-03-09 06:55:33 +00:00
2016-09-11 01:50:42 +00:00
from django.utils.translation import ugettext as _
2016-09-10 13:08:10 +00:00
from django.conf import settings
from django.views.generic import ListView, CreateView, UpdateView
from django.views.generic.edit import DeleteView, FormView
2016-09-11 01:50:42 +00:00
from django.urls import reverse_lazy
from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic.detail import DetailView, SingleObjectMixin
from django.contrib import messages
2016-09-10 13:08:10 +00:00
from common.const import create_success_msg, update_success_msg
from .hands import AdminUserRequiredMixin, User, UserGroup, SystemUser, \
Asset, AssetGroup
2016-09-11 14:45:24 +00:00
from .models import AssetPermission
from .forms import AssetPermissionForm
2016-09-10 13:08:10 +00:00
2016-09-11 15:20:14 +00:00
class AssetPermissionListView(AdminUserRequiredMixin, ListView):
2016-09-11 14:45:24 +00:00
model = AssetPermission
context_object_name = 'asset_permission_list'
template_name = 'perms/asset_permission_list.html'
2016-09-10 13:08:10 +00:00
def get_context_data(self, **kwargs):
context = {
2016-09-11 01:50:42 +00:00
'app': _('Perms'),
2016-09-11 14:45:24 +00:00
'action': _('Asset permission list'),
2016-09-10 13:08:10 +00:00
}
kwargs.update(context)
return super().get_context_data(**kwargs)
2017-12-21 03:31:13 +00:00
class AssetPermissionCreateView(AdminUserRequiredMixin, SuccessMessageMixin, CreateView):
2016-09-11 14:45:24 +00:00
model = AssetPermission
form_class = AssetPermissionForm
2016-09-11 15:20:14 +00:00
template_name = 'perms/asset_permission_create_update.html'
2016-09-11 14:45:24 +00:00
success_url = reverse_lazy('perms:asset-permission-list')
success_message = create_success_msg
2017-03-09 06:55:33 +00:00
2016-09-11 08:59:19 +00:00
def get_context_data(self, **kwargs):
context = {
'app': _('Perms'),
2016-09-11 14:45:24 +00:00
'action': _('Create asset permission'),
2016-09-11 08:59:19 +00:00
}
kwargs.update(context)
return super().get_context_data(**kwargs)
2016-09-11 15:20:14 +00:00
2017-12-21 03:31:13 +00:00
class AssetPermissionUpdateView(AdminUserRequiredMixin, SuccessMessageMixin, UpdateView):
2016-09-11 15:20:14 +00:00
model = AssetPermission
form_class = AssetPermissionForm
template_name = 'perms/asset_permission_create_update.html'
2017-05-15 15:39:54 +00:00
success_url = reverse_lazy("perms:asset-permission-list")
success_message = update_success_msg
2016-09-11 15:20:14 +00:00
def get_context_data(self, **kwargs):
context = {
'app': _('Perms'),
'action': _('Update asset permission')
}
kwargs.update(context)
return super().get_context_data(**kwargs)
2016-09-11 15:20:14 +00:00
class AssetPermissionDetailView(AdminUserRequiredMixin, DetailView):
template_name = 'perms/asset_permission_detail.html'
context_object_name = 'asset_permission'
2016-09-11 15:20:14 +00:00
model = AssetPermission
def get_context_data(self, **kwargs):
context = {
'app': _('Perms'),
'action': _('Asset permission detail'),
'system_users_remain': [
system_user for system_user in SystemUser.objects.all()
if system_user not in self.object.system_users.all()],
'system_users': self.object.system_users.all(),
2016-09-11 15:20:14 +00:00
}
kwargs.update(context)
return super().get_context_data(**kwargs)
2016-09-11 15:20:14 +00:00
class AssetPermissionDeleteView(AdminUserRequiredMixin, DeleteView):
model = AssetPermission
template_name = 'delete_confirm.html'
2016-09-11 15:20:14 +00:00
success_url = reverse_lazy('perms:asset-permission-list')
class AssetPermissionUserView(AdminUserRequiredMixin,
SingleObjectMixin,
ListView):
2016-09-16 12:53:10 +00:00
template_name = 'perms/asset_permission_user.html'
context_object_name = 'asset_permission'
2018-01-12 07:43:26 +00:00
paginate_by = settings.DISPLAY_PER_PAGE
object = None
def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=AssetPermission.objects.all())
return super().get(request, *args, **kwargs)
def get_queryset(self):
2016-09-13 17:08:26 +00:00
queryset = self.object.get_granted_users()
return queryset
def get_context_data(self, **kwargs):
users_granted = self.get_queryset()
groups_granted = self.object.user_groups.all()
context = {
'app': _('Perms'),
'action': _('Asset permission user list'),
'users_remain': User.objects.exclude(id__in=[user.id for user in users_granted]),
'user_groups': self.object.user_groups.all(),
'user_groups_remain': UserGroup.objects.exclude(id__in=[group.id for group in groups_granted])
}
kwargs.update(context)
return super().get_context_data(**kwargs)
class AssetPermissionAssetView(AdminUserRequiredMixin,
SingleObjectMixin,
ListView):
2016-09-16 12:53:10 +00:00
template_name = 'perms/asset_permission_asset.html'
context_object_name = 'asset_permission'
2018-01-12 07:43:26 +00:00
paginate_by = settings.DISPLAY_PER_PAGE
object = None
def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=AssetPermission.objects.all())
return super().get(request, *args, **kwargs)
def get_queryset(self):
queryset = self.object.get_granted_assets()
return queryset
def get_context_data(self, **kwargs):
assets_granted = self.get_queryset()
groups_granted = self.object.asset_groups.all()
context = {
'app': _('Perms'),
'action': _('Asset permission asset list'),
'assets_remain': Asset.objects.exclude(id__in=[asset.id for asset in assets_granted]),
'asset_groups': self.object.asset_groups.all(),
'asset_groups_remain': AssetGroup.objects.exclude(id__in=[group.id for group in groups_granted])
}
kwargs.update(context)
return super().get_context_data(**kwargs)