jumpserver/apps/perms/views.py

191 lines
8.3 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
2016-09-13 17:08:26 +00:00
import functools
2016-09-10 13:08:10 +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
2016-09-11 01:50:42 +00:00
from django.db.models import Q
from django.views.generic import TemplateView, ListView
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
from django.urls import reverse_lazy
from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic.detail import DetailView, SingleObjectMixin
2016-09-10 13:08:10 +00:00
2016-09-13 17:08:26 +00:00
from common.utils import search_object_attr
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
2016-09-10 13:08:10 +00:00
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
2016-09-11 14:45:24 +00:00
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'),
'keyword': self.keyword,
2016-09-10 13:08:10 +00:00
}
kwargs.update(context)
2016-09-11 15:20:14 +00:00
return super(AssetPermissionListView, self).get_context_data(**kwargs)
2016-09-10 13:08:10 +00:00
def get_queryset(self):
# Todo: Default order by lose asset connection num
2016-09-11 15:20:14 +00:00
self.queryset = super(AssetPermissionListView, self).get_queryset()
2016-09-10 13:08:10 +00:00
self.keyword = keyword = self.request.GET.get('keyword', '')
2016-09-11 14:45:24 +00:00
self.sort = sort = self.request.GET.get('sort', '-date_created')
2016-09-10 13:08:10 +00:00
if keyword:
self.queryset = self.queryset.filter(Q(users__name__contains=keyword) |
Q(users__username__contains=keyword) |
Q(user_groups__name__contains=keyword) |
Q(assets__ip__contains=keyword) |
Q(assets__hostname__contains=keyword) |
Q(system_users__username__icontains=keyword) |
Q(system_users__name__icontains=keyword) |
2016-09-11 14:45:24 +00:00
Q(asset_groups__name__icontains=keyword) |
Q(comment__icontains=keyword) |
Q(name__icontains=keyword)).distinct()
2016-09-10 13:08:10 +00:00
if sort:
self.queryset = self.queryset.order_by(sort)
return self.queryset
2016-09-11 15:20:14 +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')
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)
2016-09-11 15:20:14 +00:00
return super(AssetPermissionCreateView, self).get_context_data(**kwargs)
def get_success_message(self, cleaned_data):
2016-09-15 04:20:53 +00:00
success_message = _('Create asset permission <a href="%s"> %s </a> successfully.' %
(
reverse_lazy('perms:asset-permission-detail', kwargs={'pk': self.object.pk}),
self.object.name,
))
return success_message
2016-09-11 15:20:14 +00:00
class AssetPermissionUpdateView(AdminUserRequiredMixin, UpdateView):
model = AssetPermission
form_class = AssetPermissionForm
template_name = 'perms/asset_permission_create_update.html'
success_message = _('Update asset permission <a href="%s"> %s </a> successfully.')
def get_context_data(self, **kwargs):
context = {
'app': _('Perms'),
'action': _('Update asset permission')
}
kwargs.update(context)
return super(AssetPermissionUpdateView, self).get_context_data(**kwargs)
def get_success_url(self):
success_url = reverse_lazy('perms:asset-permission-detail', kwargs={'pk': self.object.pk})
return success_url
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(AssetPermissionDetailView, self).get_context_data(**kwargs)
class AssetPermissionDeleteView(AdminUserRequiredMixin, DeleteView):
model = AssetPermission
template_name = 'perms/delete_confirm.html'
success_url = reverse_lazy('perms:asset-permission-list')
2016-09-16 12:53:10 +00:00
class AssetPermissionUserView(AdminUserRequiredMixin, SingleObjectMixin, ListView):
template_name = 'perms/asset_permission_user.html'
context_object_name = 'asset_permission'
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=AssetPermission.objects.all())
self.keyword = self.request.GET.get('keyword', '')
2016-09-16 12:53:10 +00:00
return super(AssetPermissionUserView, self).get(request, *args, **kwargs)
def get_queryset(self):
2016-09-13 17:08:26 +00:00
queryset = self.object.get_granted_users()
if self.keyword:
search_func = functools.partial(search_object_attr, value=self.keyword,
attr_list=['username', 'name', 'email'],
2016-09-13 17:08:26 +00:00
ignore_case=True)
queryset = filter(search_func, queryset)
2016-09-13 17:08:26 +00:00
return queryset
def get_context_data(self, **kwargs):
users_granted = self.get_queryset()
user_groups_granted = self.object.user_groups.all()
context = {
'app': _('Perms'),
'action': _('Asset permission user list'),
'users_remain': [user for user in User.objects.all() if user not in users_granted],
'user_groups': self.object.user_groups.all(),
'user_groups_remain': [user_group for user_group in UserGroup.objects.all()
if user_group not in user_groups_granted],
'keyword': self.keyword,
}
kwargs.update(context)
2016-09-16 12:53:10 +00:00
return super(AssetPermissionUserView, self).get_context_data(**kwargs)
2016-09-16 12:53:10 +00:00
class AssetPermissionAssetView(AdminUserRequiredMixin, SingleObjectMixin, ListView):
template_name = 'perms/asset_permission_asset.html'
context_object_name = 'asset_permission'
paginate_by = settings.CONFIG.DISPLAY_PER_PAGE
def get(self, request, *args, **kwargs):
self.object = self.get_object(queryset=AssetPermission.objects.all())
self.keyword = self.request.GET.get('keyword', '')
2016-09-16 12:53:10 +00:00
return super(AssetPermissionAssetView, self).get(request, *args, **kwargs)
def get_queryset(self):
queryset = self.object.get_granted_assets()
if self.keyword:
search_func = functools.partial(search_object_attr, value=self.keyword,
attr_list=['hostname', 'ip'],
ignore_case=True)
queryset = filter(search_func, queryset)
return queryset
def get_context_data(self, **kwargs):
assets_granted = self.get_queryset()
asset_groups_granted = self.object.user_groups.all()
context = {
'app': _('Perms'),
'action': _('Asset permission asset list'),
'assets_remain': (asset for asset in Asset.objects.all() if asset not in assets_granted),
'asset_groups': self.object.asset_groups.all(),
'asset_groups_remain': [asset_group for asset_group in AssetGroup.objects.all()
if asset_group not in asset_groups_granted],
'keyword': self.keyword,
}
kwargs.update(context)
2016-09-16 12:53:10 +00:00
return super(AssetPermissionAssetView, self).get_context_data(**kwargs)