Skip to content
This repository was archived by the owner on Nov 28, 2023. It is now read-only.

Commit cac1d75

Browse files
committed
完成编辑whitelist功能
1 parent 31005fe commit cac1d75

File tree

5 files changed

+223
-5
lines changed

5 files changed

+223
-5
lines changed

app/controller/RulesAdmin.py

+49-2
Original file line numberDiff line numberDiff line change
@@ -384,5 +384,52 @@ def del_whitelist():
384384

385385
# edit the special white list
386386
@web.route(ADMIN_URL + '/edit_whitelist/<int:whitelist_id>', methods=['GET', 'POST'])
387-
def edit_whitelist():
388-
pass
387+
def edit_whitelist(whitelist_id):
388+
if request.method == 'POST':
389+
whitelist_id = request.form.get('whitelist_id')
390+
project_id = request.form.get('project')
391+
rule_id = request.form.get('rule')
392+
path = request.form.get('path')
393+
reason = request.form.get('reason')
394+
status = request.form.get('status')
395+
396+
if not whitelist_id or whitelist_id == "":
397+
return jsonify(tag='danger', msg='wrong whitelist')
398+
if not project_id or project_id == "":
399+
return jsonify(tag='danger', msg='project can not be empty')
400+
if not rule_id or rule_id == "":
401+
return jsonify(tag='danger', msg='rule can not be empty')
402+
if not path or path == "":
403+
return jsonify(tag='danger', msg='path can not be empty')
404+
if not reason or reason == "":
405+
return jsonify(tag='danger', msg='reason can not be empty')
406+
if not status or status == "":
407+
return jsonify(tag='danger', msg='status can not be empty')
408+
409+
whitelist = CobraWhiteList.query.filter_by(id=whitelist_id).first()
410+
if not whitelist:
411+
return jsonify(tag='danger', msg='wrong whitelist')
412+
413+
whitelist.project_id = project_id
414+
whitelist.rule_id = rule_id
415+
whitelist.path = path
416+
whitelist.reason = reason
417+
whitelist.status = status
418+
419+
try:
420+
db.session.add(whitelist)
421+
db.session.commit()
422+
return jsonify(tag='success', msg='update success.')
423+
except:
424+
return jsonify(tag='danger', msg='unknown error.')
425+
else:
426+
rules = CobraRules.query.all()
427+
projects = CobraProjects.query.all()
428+
whitelist = CobraWhiteList.query.filter_by(id=whitelist_id).first()
429+
data = {
430+
'rules': rules,
431+
'projects': projects,
432+
'whitelist': whitelist,
433+
}
434+
435+
return render_template('rulesadmin/edit_whitelist.html', data=data)

app/templates/asset/js/admin.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -352,21 +352,44 @@ $("#show_all_projects").click(function () {
352352

353353
// show all white lists click
354354
$("#show_all_whitelists").click(function () {
355-
console.log('show all white list');
356355
$.get('whitelists', function (data) {
357356
$("#main-div").html(data);
358357

359358
// edit the special white list
360359
$("[id^=edit-whitelist]").click(function () {
361360
var cur_id = $(this).attr('id').split('-')[2];
362361
console.log("edit the " + cur_id);
362+
363+
$.get('edit_whitelist/'+cur_id, function (data) {
364+
$("#main-div").html(data);
365+
366+
$("#edit-whitelist-button").click(function () {
367+
var project = $("#project").val();
368+
var rule = $("#rule").val();
369+
var path = $("#path").val();
370+
var reason = $("#reason").val();
371+
var status = $("#status:checked").val();
372+
373+
data = {
374+
'whitelist_id': cur_id,
375+
'project': project,
376+
'rule': rule,
377+
'path': path,
378+
'reason': reason,
379+
'status': status
380+
};
381+
382+
$.post("edit_whitelist/"+cur_id, data, function (result) {
383+
showAlert(result.tag, result.msg, '#edit-whitelist-result');
384+
});
385+
});
386+
});
363387
});
364388

365389

366390
// delete the special white list
367391
$("[id^=del-whitelist]").click(function () {
368392
var cur_id = $(this).attr('id').split('-')[2];
369-
console.log("delete the " + cur_id);
370393
$.post('del_whitelist', {'whitelist_id': cur_id}, function (data) {
371394
showAlert(data.tag, data.msg, "#operate_result");
372395
$("#show_all_whitelists").click();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<div class="row clearfix">
2+
<div class="col-md-12 column">
3+
<div class="page-header">
4+
<h3>
5+
Edit White List
6+
<small></small>
7+
</h3>
8+
</div>
9+
</div>
10+
</div>
11+
<form role="form">
12+
<div class="form-group">
13+
<label for="project">Project</label>
14+
<select id="project" class="form-control">
15+
{% for project in data.projects %}
16+
<option value="{{ project.id }}" {% if data.whitelist.project_id == project.id %}selected{% endif %}>
17+
{{ project.id }}-{{ project.name }}-{{ project.repository }}-{{ project.author }}
18+
</option>
19+
{% endfor %}
20+
</select>
21+
</div>
22+
<div class="form-group">
23+
<label for="rule">Rule</label>
24+
<select id="rule" class="form-control">
25+
{% for rule in data.rules %}
26+
<option value="{{ rule.id }}" {% if data.whitelist.rule_id == rule.id %}selected{% endif %}>
27+
{{ rule.id }}-{{ rule.description }}
28+
</option>
29+
{% endfor %}
30+
</select>
31+
</div>
32+
<div class="form-group">
33+
<label for="path">Path</label>
34+
<input type="text" class="form-control" id="path" value="{{ data.whitelist.path }}"/>
35+
</div>
36+
<div class="form-group">
37+
<label for="reason">Reason</label>
38+
<textarea class="form-control" id="reason">{{ data.whitelist.reason }}</textarea>
39+
</div>
40+
<div class="form-group">
41+
<label for="status">Status</label>&nbsp;&nbsp;&nbsp;&nbsp;
42+
<label class="radio-inline">
43+
<input type="radio" name="status" id="status" value="1"
44+
{% if data.whitelist.status == 1 %}checked{% endif %}> On
45+
</label>
46+
<label class="radio-inline">
47+
<input type="radio" name="status" id="status" value="2"
48+
{% if data.whitelist.status == 2 %}checked{% endif %}> Off
49+
</label>
50+
</div>
51+
<div id="edit-whitelist-result" hidden></div>
52+
<button type="button" class="btn btn-success" id="edit-whitelist-button">Save</button>
53+
</form>

app/templates/rulesadmin/whitelists.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
</thead>
1313
<tbody id="main-table">
1414
{% for whitelist in data.whitelists %}
15-
<tr>
15+
<tr {% if whitelist.status == 1 %}class="success"{% else %}class="danger"{% endif %}>
1616
<td>{{ whitelist.id }}</td>
1717
<td>{{ whitelist.project_id }}</td>
1818
<td>{{ whitelist.rule_id }}</td>

migrations/versions/d9ce546dbcac_.py

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
"""empty message
2+
3+
Revision ID: d9ce546dbcac
4+
Revises: 9ffa798c4399
5+
Create Date: 2016-06-03 09:38:12.089267
6+
7+
"""
8+
9+
# revision identifiers, used by Alembic.
10+
revision = 'd9ce546dbcac'
11+
down_revision = '9ffa798c4399'
12+
13+
from alembic import op
14+
import sqlalchemy as sa
15+
from sqlalchemy.dialects import mysql
16+
17+
def upgrade():
18+
### commands auto generated by Alembic - please adjust! ###
19+
op.create_table('results',
20+
sa.Column('id', mysql.INTEGER(unsigned=True), nullable=False),
21+
sa.Column('scan_id', mysql.INTEGER(display_width=11), nullable=True),
22+
sa.Column('rule_id', mysql.INTEGER(display_width=11), nullable=True),
23+
sa.Column('file', sa.String(length=512), nullable=True),
24+
sa.Column('line', mysql.INTEGER(display_width=11), nullable=True),
25+
sa.Column('code', sa.String(length=512), nullable=True),
26+
sa.Column('created_at', sa.DateTime(), nullable=True),
27+
sa.Column('updated_at', sa.DateTime(), nullable=True),
28+
sa.PrimaryKeyConstraint('id')
29+
)
30+
op.create_table('tasks',
31+
sa.Column('id', mysql.INTEGER(unsigned=True), nullable=False),
32+
sa.Column('task_type', sa.SmallInteger(), nullable=False),
33+
sa.Column('filename', sa.String(length=255), nullable=True),
34+
sa.Column('url', sa.String(length=255), nullable=True),
35+
sa.Column('branch', sa.String(length=64), nullable=True),
36+
sa.Column('scan_way', sa.SmallInteger(), nullable=False),
37+
sa.Column('old_version', sa.String(length=40), nullable=True),
38+
sa.Column('new_version', sa.String(length=40), nullable=True),
39+
sa.Column('created_at', sa.DATETIME(), nullable=False),
40+
sa.Column('updated_at', sa.DATETIME(), nullable=False),
41+
sa.PrimaryKeyConstraint('id')
42+
)
43+
op.drop_table('cobra_task_info')
44+
op.add_column(u'languages', sa.Column('extensions', sa.String(length=128), nullable=True))
45+
op.drop_column(u'languages', 'suffix')
46+
op.add_column(u'projects', sa.Column('author', sa.String(length=50), nullable=True))
47+
op.add_column(u'projects', sa.Column('last_scan', sa.DateTime(), nullable=True))
48+
op.add_column(u'projects', sa.Column('remark', sa.String(length=50), nullable=True))
49+
op.drop_column(u'projects', 'username')
50+
op.drop_column(u'projects', 'repo_type')
51+
op.drop_column(u'projects', 'password')
52+
op.drop_column(u'projects', 'scan_at')
53+
op.drop_column(u'projects', 'branch')
54+
op.add_column(u'whitelist', sa.Column('path', sa.String(length=512), nullable=True))
55+
op.add_column(u'whitelist', sa.Column('status', mysql.TINYINT(), nullable=True))
56+
op.drop_column(u'whitelist', 'file')
57+
### end Alembic commands ###
58+
59+
60+
def downgrade():
61+
### commands auto generated by Alembic - please adjust! ###
62+
op.add_column(u'whitelist', sa.Column('file', mysql.VARCHAR(length=512), nullable=True))
63+
op.drop_column(u'whitelist', 'status')
64+
op.drop_column(u'whitelist', 'path')
65+
op.add_column(u'projects', sa.Column('branch', mysql.VARCHAR(length=128), nullable=True))
66+
op.add_column(u'projects', sa.Column('scan_at', mysql.DATETIME(), nullable=True))
67+
op.add_column(u'projects', sa.Column('password', mysql.VARCHAR(length=128), nullable=True))
68+
op.add_column(u'projects', sa.Column('repo_type', mysql.TINYINT(display_width=2), autoincrement=False, nullable=False))
69+
op.add_column(u'projects', sa.Column('username', mysql.VARCHAR(length=128), nullable=True))
70+
op.drop_column(u'projects', 'remark')
71+
op.drop_column(u'projects', 'last_scan')
72+
op.drop_column(u'projects', 'author')
73+
op.add_column(u'languages', sa.Column('suffix', mysql.VARCHAR(length=256), nullable=False))
74+
op.drop_column(u'languages', 'extensions')
75+
op.create_table('cobra_task_info',
76+
sa.Column('id', mysql.INTEGER(display_width=11), nullable=False),
77+
sa.Column('task_type', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False),
78+
sa.Column('create_time', mysql.INTEGER(display_width=11), autoincrement=False, nullable=False),
79+
sa.Column('filename', mysql.VARCHAR(length=255), nullable=True),
80+
sa.Column('level', mysql.SMALLINT(display_width=6), autoincrement=False, nullable=False),
81+
sa.Column('new_version', mysql.VARCHAR(length=40), nullable=True),
82+
sa.Column('old_version', mysql.VARCHAR(length=40), nullable=True),
83+
sa.Column('password', mysql.VARCHAR(length=255), nullable=True),
84+
sa.Column('scan_type', mysql.SMALLINT(display_width=6), autoincrement=False, nullable=False),
85+
sa.Column('scan_way', mysql.SMALLINT(display_width=6), autoincrement=False, nullable=False),
86+
sa.Column('url', mysql.VARCHAR(length=255), nullable=True),
87+
sa.Column('username', mysql.VARCHAR(length=255), nullable=True),
88+
sa.Column('branch', mysql.VARCHAR(length=64), nullable=True),
89+
sa.PrimaryKeyConstraint('id'),
90+
mysql_default_charset=u'utf8',
91+
mysql_engine=u'InnoDB'
92+
)
93+
op.drop_table('tasks')
94+
op.drop_table('results')
95+
### end Alembic commands ###

0 commit comments

Comments
 (0)