Commit 70fa43ad authored by ibuler's avatar ibuler

[Update] 修改一些逻辑

parent 44bf01d4
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# #
from django import forms
from orgs.mixins import OrgModelForm from orgs.mixins import OrgModelForm
from ..models import CommandFilter, CommandFilterRule from ..models import CommandFilter, CommandFilterRule
...@@ -18,3 +20,8 @@ class CommandFilterRuleForm(OrgModelForm): ...@@ -18,3 +20,8 @@ class CommandFilterRuleForm(OrgModelForm):
fields = [ fields = [
'filter', 'type', 'content', 'priority', 'action', 'comment' 'filter', 'type', 'content', 'priority', 'action', 'comment'
] ]
widgets = {
'content': forms.Textarea(attrs={
'placeholder': 'eg:\r\nreboot\r\nrm -rf'
}),
}
...@@ -35,11 +35,10 @@ class CommandFilterRule(OrgModelMixin): ...@@ -35,11 +35,10 @@ class CommandFilterRule(OrgModelMixin):
(TYPE_COMMAND, _('Command')), (TYPE_COMMAND, _('Command')),
) )
ACTION_DENY = 'deny' ACTION_DENY, ACTION_ALLOW = range(2)
ACTION_ACCEPT = 'accept'
ACTION_CHOICES = ( ACTION_CHOICES = (
(ACTION_DENY, _('Deny')), (ACTION_DENY, _('Deny')),
(ACTION_ACCEPT, _('Accept')) (ACTION_ALLOW, _('Allow')),
) )
id = models.UUIDField(default=uuid.uuid4, primary_key=True) id = models.UUIDField(default=uuid.uuid4, primary_key=True)
...@@ -47,11 +46,14 @@ class CommandFilterRule(OrgModelMixin): ...@@ -47,11 +46,14 @@ class CommandFilterRule(OrgModelMixin):
type = models.CharField(max_length=16, default=TYPE_COMMAND, choices=TYPE_CHOICES, verbose_name=_("Type")) type = models.CharField(max_length=16, default=TYPE_COMMAND, choices=TYPE_CHOICES, verbose_name=_("Type"))
priority = models.IntegerField(default=50, verbose_name=_("Priority"), validators=[MinValueValidator(1), MaxValueValidator(100)]) priority = models.IntegerField(default=50, verbose_name=_("Priority"), validators=[MinValueValidator(1), MaxValueValidator(100)])
content = models.TextField(max_length=1024, verbose_name=_("Content"), help_text=_("One line one command")) content = models.TextField(max_length=1024, verbose_name=_("Content"), help_text=_("One line one command"))
action = models.CharField(max_length=16, default=ACTION_DENY, choices=ACTION_CHOICES, verbose_name=_("Action")) action = models.IntegerField(default=ACTION_DENY, choices=ACTION_CHOICES, verbose_name=_("Action"))
comment = models.CharField(max_length=64, blank=True, default='', verbose_name=_("Comment")) comment = models.CharField(max_length=64, blank=True, default='', verbose_name=_("Comment"))
date_created = models.DateTimeField(auto_now_add=True) date_created = models.DateTimeField(auto_now_add=True)
date_updated = models.DateTimeField(auto_now=True) date_updated = models.DateTimeField(auto_now=True)
created_by = models.CharField(max_length=128, blank=True, default='', verbose_name=_('Created by')) created_by = models.CharField(max_length=128, blank=True, default='', verbose_name=_('Created by'))
class Meta:
ordering = ('priority', 'action')
def __str__(self): def __str__(self):
return '{} % {}'.format(self.type, self.content) return '{} % {}'.format(self.type, self.content)
...@@ -38,12 +38,10 @@ class Node(OrgModelMixin): ...@@ -38,12 +38,10 @@ class Node(OrgModelMixin):
return True return True
self_key = [int(k) for k in self.key.split(':')] self_key = [int(k) for k in self.key.split(':')]
other_key = [int(k) for k in other.key.split(':')] other_key = [int(k) for k in other.key.split(':')]
if len(self_key) < len(other_key): return self_key.__lt__(other_key)
return True
elif len(self_key) > len(other_key): def __lt__(self, other):
return False return not self.__gt__(other)
else:
return self_key[-1] < other_key[-1]
@property @property
def name(self): def name(self):
......
...@@ -117,7 +117,7 @@ class SystemUser(AssetUser): ...@@ -117,7 +117,7 @@ class SystemUser(AssetUser):
sudo = models.TextField(default='/bin/whoami', verbose_name=_('Sudo')) sudo = models.TextField(default='/bin/whoami', verbose_name=_('Sudo'))
shell = models.CharField(max_length=64, default='/bin/bash', verbose_name=_('Shell')) shell = models.CharField(max_length=64, default='/bin/bash', verbose_name=_('Shell'))
login_mode = models.CharField(choices=LOGIN_MODE_CHOICES, default=AUTO_LOGIN, max_length=10, verbose_name=_('Login mode')) login_mode = models.CharField(choices=LOGIN_MODE_CHOICES, default=AUTO_LOGIN, max_length=10, verbose_name=_('Login mode'))
cmd_filters = models.ManyToManyField('CommandFilter', related_name='system_users', verbose_name=_("Command filter")) cmd_filters = models.ManyToManyField('CommandFilter', related_name='system_users', verbose_name=_("Command filter"), blank=True)
cache_key = "__SYSTEM_USER_CACHED_{}" cache_key = "__SYSTEM_USER_CACHED_{}"
......
...@@ -66,7 +66,7 @@ $(document).ready(function(){ ...@@ -66,7 +66,7 @@ $(document).ready(function(){
var $data_table = $('#cmd_filter_list_table').DataTable(); var $data_table = $('#cmd_filter_list_table').DataTable();
var name = $(this).closest("tr").find(":nth-child(2)").children('a').html(); var name = $(this).closest("tr").find(":nth-child(2)").children('a').html();
var uid = $this.data('uid'); var uid = $this.data('uid');
var the_url = '{% url "api-assets:label-detail" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', uid); var the_url = '{% url "api-assets:cmd-filter-detail" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', uid);
objectDelete($this, name, the_url); objectDelete($this, name, the_url);
setTimeout( function () { setTimeout( function () {
$data_table.ajax.reload(); $data_table.ajax.reload();
......
...@@ -51,7 +51,24 @@ ...@@ -51,7 +51,24 @@
{% block custom_foot_js %} {% block custom_foot_js %}
<script> <script>
var content_origin_placeholder = '';
var content_origin_help_text = '';
var content_ref = '';
var content_help_ref = '';
$(document).ready(function(){ $(document).ready(function(){
content_ref = $('#id_content');
content_help_ref = content_ref.next();
content_origin_placeholder = content_ref.attr('placeholder');
content_origin_help_text = content_help_ref.html();
}).on('change', '#id_type', function () {
if ($('#id_type :selected').val() === 'regex') {
content_ref.attr('placeholder', 'rm.*|reboot|shutdown');
content_help_ref.html("");
} else {
content_ref.attr('placeholder', content_origin_placeholder);
content_help_ref.html(content_origin_help_text);
}
}) })
</script> </script>
{% endblock %} {% endblock %}
\ No newline at end of file
{% extends 'base.html' %} {% extends 'base.html' %}
{% load common_tags %}
{% load static %} {% load static %}
{% load i18n %} {% load i18n %}
...@@ -113,7 +114,7 @@ ...@@ -113,7 +114,7 @@
</tr> </tr>
</form> </form>
{% for node in system_user.nodes.all %} {% for node in system_user.nodes.all|sort %}
<tr> <tr>
<td ><b class="bdg_node" data-gid={{ node.id }}>{{ node }}</b></td> <td ><b class="bdg_node" data-gid={{ node.id }}>{{ node }}</b></td>
<td> <td>
......
...@@ -91,10 +91,11 @@ class SystemUserAssetView(AdminUserRequiredMixin, DetailView): ...@@ -91,10 +91,11 @@ class SystemUserAssetView(AdminUserRequiredMixin, DetailView):
context_object_name = 'system_user' context_object_name = 'system_user'
def get_context_data(self, **kwargs): def get_context_data(self, **kwargs):
nodes_remain = sorted(Node.objects.exclude(systemuser=self.object), reverse=True)
context = { context = {
'app': _('assets'), 'app': _('assets'),
'action': _('System user asset'), 'action': _('System user asset'),
'nodes_remain': Node.objects.exclude(systemuser=self.object) 'nodes_remain': nodes_remain
} }
kwargs.update(context) kwargs.update(context)
return super().get_context_data(**kwargs) return super().get_context_data(**kwargs)
...@@ -100,3 +100,9 @@ def is_bool_field(field): ...@@ -100,3 +100,9 @@ def is_bool_field(field):
@register.filter @register.filter
def to_dict(data): def to_dict(data):
return dict(data) return dict(data)
@register.filter
def sort(data):
print(data)
return sorted(data)
...@@ -1295,7 +1295,7 @@ msgstr "创建规则" ...@@ -1295,7 +1295,7 @@ msgstr "创建规则"
#: assets/templates/assets/cmd_filter_rule_list.html:61 #: assets/templates/assets/cmd_filter_rule_list.html:61
msgid "Strategy" msgid "Strategy"
msgstr "分类" msgstr "策略"
#: assets/templates/assets/delete_confirm.html:6 #: assets/templates/assets/delete_confirm.html:6
#: perms/templates/perms/delete_confirm.html:6 templates/delete_confirm.html:6 #: perms/templates/perms/delete_confirm.html:6 templates/delete_confirm.html:6
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment