diff --git a/apps/ops/hands.py b/apps/ops/hands.py new file mode 100644 index 000000000..1cdbd13a2 --- /dev/null +++ b/apps/ops/hands.py @@ -0,0 +1,14 @@ +""" + jumpserver.__app__.hands.py + ~~~~~~~~~~~~~~~~~ + + This app depends other apps api, function .. should be import or write mack here. + + Other module of this app shouldn't connect with other app. + + :copyright: (c) 2014-2016 by Jumpserver Team. + :license: GPL v2, see LICENSE for more details. +""" + + +from users.utils import AdminUserRequiredMixin \ No newline at end of file diff --git a/apps/ops/models.py b/apps/ops/models.py index 3f0f0205d..70fe0cc7f 100644 --- a/apps/ops/models.py +++ b/apps/ops/models.py @@ -334,7 +334,7 @@ class Sudo(models.Model): "Host_Alias": self.hosts, "Runas_Alias": self.runas, "Extra_Lines": self.extras, - "privileges": self.privileges} + "Privileges": self.privileges} return template.render(context) @property @@ -392,7 +392,7 @@ root ALL=(ALL:ALL) ALL # JumpServer Generate User privilege is here. # Note privileges is a tuple list like [(user, host, runas, command, nopassword),] {% if privileges -%} -{% for User_Flag, Host_Flag, Runas_Flag, Command_Flag, NopassWord in privileges -%} +{% for User_Flag, Host_Flag, Runas_Flag, Command_Flag, NopassWord in Privileges -%} {% if NopassWord -%} {{ User_Flag }} {{ Host_Flag }}=({{ Runas_Flag }}) NOPASSWD: {{ Command_Flag }} {%- else -%} diff --git a/apps/ops/utils.py b/apps/ops/utils.py index c84951fd7..7307f91ca 100644 --- a/apps/ops/utils.py +++ b/apps/ops/utils.py @@ -1,2 +1,41 @@ # ~*~ coding: utf-8 ~*~ -# + + +class CreateHostAliasMinxin(object): + pass + + +class CreateUserAliasMinxin(object): + pass + + +class CreateCmdAliasMinxin(object): + pass + + +class CreateRunasAliasMinxin(object): + pass + + +class CreateExtralineAliasMinxin(object): + pass + + +class UpdateHostAliasMinxin(object): + pass + + +class UpdateUserAliasMinxin(object): + pass + + +class UpdateCmdAliasMinxin(object): + pass + + +class UpdateRunasAliasMinxin(object): + pass + + +class UpdateExtralineAliasMinxin(object): + pass \ No newline at end of file diff --git a/apps/ops/views.py b/apps/ops/views.py index 91ea44a21..b161cd0e5 100644 --- a/apps/ops/views.py +++ b/apps/ops/views.py @@ -1,3 +1,121 @@ -from django.shortcuts import render +# ~*~ coding: utf-8 ~*~ +from __future__ import unicode_literals -# Create your views here. +from django.conf import settings +from django.views.generic.list import ListView, MultipleObjectMixin +from django.views.generic.edit import CreateView, DeleteView, UpdateView +from django.views.generic.detail import DetailView, SingleObjectMixin + +from .hands import AdminUserRequiredMixin +from .utils import * + + +class SudoListView(AdminUserRequiredMixin, ListView): + paginate_by = settings.CONFIG.DISPLAY_PER_PAGE + model = Asset + context_object_name = 'asset_list' + template_name = 'assets/asset_list.html' + + def get_queryset(self): + queryset = super(AssetListView, self).get_queryset() + queryset = sorted(queryset, key=self.sorted_by_valid_and_ip) + return queryset + + @staticmethod + def sorted_by_valid_and_ip(asset): + ip_list = int_seq(asset.ip.split('.')) + ip_list.insert(0, asset.is_valid()[0]) + return ip_list + + def get_context_data(self, **kwargs): + context = { + 'app': 'Assets', + 'action': 'asset list', + 'tag_list': [(i.id,i.name,i.asset_set.all().count())for i in Tag.objects.all().order_by('name')] + + } + kwargs.update(context) + return super(AssetListView, self).get_context_data(**kwargs) + + +class SudoCreateView(AdminUserRequiredMixin, + CreateHostAliasMinxin, + CreateUserAliasMinxin, + CreateCmdAliasMinxin, + CreateRunasAliasMinxin, + CreateExtralineAliasMinxin, + CreateView): + model = Asset + tag_type = 'asset' + form_class = AssetCreateForm + template_name = 'assets/asset_create.html' + success_url = reverse_lazy('assets:asset-list') + + def form_valid(self, form): + asset = form.save() + asset.created_by = self.request.user.username or 'Admin' + asset.save() + return super(AssetCreateView, self).form_valid(form) + + def form_invalid(self, form): + print(form.errors) + return super(AssetCreateView, self).form_invalid(form) + + + def get_context_data(self, **kwargs): + context = { + 'app': 'Assets', + 'action': 'Create asset', + } + kwargs.update(context) + + return super(AssetCreateView, self).get_context_data(**kwargs) + + +class SudoUpdateView(AdminUserRequiredMixin, + UpdateHostAliasMinxin, + UpdateUserAliasMinxin, + UpdateCmdAliasMinxin, + UpdateRunasAliasMinxin, + UpdateExtralineAliasMinxin, + UpdateView): + model = Asset + form_class = AssetCreateForm + template_name = 'assets/asset_update.html' + success_url = reverse_lazy('assets:asset-list') + + def get_context_data(self, **kwargs): + context = { + 'app': 'Assets', + 'action': 'Update asset', + } + kwargs.update(context) + return super(AssetUpdateView, self).get_context_data(**kwargs) + + def form_invalid(self, form): + print(form.errors) + return super(AssetUpdateView, self).form_invalid(form) + + +class SudoDeleteView(DeleteView): + model = Asset + template_name = 'assets/delete_confirm.html' + success_url = reverse_lazy('assets:asset-list') + + +class SudoDetailView(DetailView): + model = Asset + context_object_name = 'asset' + template_name = 'assets/asset_detail.html' + + def get_context_data(self, **kwargs): + asset_groups = self.object.groups.all() + context = { + 'app': 'Assets', + 'action': 'Asset detail', + 'asset_groups_remain': [asset_group for asset_group in AssetGroup.objects.all() + if asset_group not in asset_groups], + 'asset_groups': asset_groups, + } + kwargs.update(context) + return super(AssetDetailView, self).get_context_data(**kwargs)