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

Commit 26a4b1a

Browse files
committed
Merge pull request #4 from LiGhT1EsS/master
improves whitelist
2 parents 6074edb + cac1d75 commit 26a4b1a

File tree

7 files changed

+250
-67
lines changed

7 files changed

+250
-67
lines changed

app/controller/RulesAdmin.py

+58-20
Original file line numberDiff line numberDiff line change
@@ -282,37 +282,28 @@ def edit_project(project_id):
282282
# get data from request
283283
project_id = request.form.get('project_id')
284284
name = request.form.get('name')
285-
repo_type = request.form.get('repo_type')
286285
repository = request.form.get('repository')
287-
branch = request.form.get('branch')
288-
username = request.form.get('username')
289-
password = request.form.get('password')
286+
author = request.form.get('author')
287+
remark = request.form.get('remark')
290288

291289
# check data
292290
if not project_id or project_id == "":
293291
return jsonify(tag='danger', msg='wrong project id.')
294292
if not name or name == "":
295293
return jsonify(tag='danger', msg='name cannot be empty')
296-
if not repo_type or repo_type == "":
297-
return jsonify(tag='danger', msg='repo type cannot be empty')
298294
if not repository or repository == "":
299295
return jsonify(tag='danger', msg='repository can not be empty')
300-
if not branch or branch == "":
301-
return jsonify(tag='danger', msg="branch can not be empty")
302296

303297
current_time = time.strftime('%Y-%m-%d %X', time.localtime())
304-
repo_type = 1 if repo_type == "git" else 2
305298
project = CobraProjects.query.filter_by(id=project_id).first()
306299
if not project:
307300
return jsonify(tag='danger', msg='wrong project id.')
308301

309302
# update project data
310303
project.name = name
311-
project.repo_type = 1 if repo_type == 'git' else 2
304+
project.author = author
305+
project.remark = remark
312306
project.repository = repository
313-
project.branch = branch
314-
project.username = username if username and username != "" else None
315-
project.password = password if password and password != "" else None
316307
project.updated_at = current_time
317308
try:
318309
db.session.add(project)
@@ -343,22 +334,22 @@ def add_whitelist():
343334
if request.method == 'POST':
344335
project_id = request.form.get('project_id')
345336
rule_id = request.form.get('rule_id')
346-
file = request.form.get('file')
337+
path = request.form.get('path')
347338
reason = request.form.get('reason')
348339

349340
if not project_id or project_id == "":
350341
return jsonify(tag='danger', msg='project id error.')
351342
if not rule_id or rule_id == "":
352343
return jsonify(tag='danger', msg='rule id error.')
353-
if not file or file == "":
344+
if not path or path == "":
354345
return jsonify(tag='danger', msg='file error.')
355346
if not reason or reason == "":
356347
return jsonify(tag='danger', msg='reason error.')
357348

358349
current_time = time.strftime('%Y-%m-%d %X', time.localtime())
359-
if file[0] != '/':
360-
file = '/' + file
361-
whitelist = CobraWhiteList(project_id, rule_id, file, reason, 1, current_time, current_time)
350+
if path[0] != '/':
351+
path = '/' + path
352+
whitelist = CobraWhiteList(project_id, rule_id, path, reason, 1, current_time, current_time)
362353
try:
363354
db.session.add(whitelist)
364355
db.session.commit()
@@ -393,5 +384,52 @@ def del_whitelist():
393384

394385
# edit the special white list
395386
@web.route(ADMIN_URL + '/edit_whitelist/<int:whitelist_id>', methods=['GET', 'POST'])
396-
def edit_whitelist():
397-
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

+40-42
Original file line numberDiff line numberDiff line change
@@ -289,63 +289,38 @@ $("#show_all_projects").click(function () {
289289

290290
$("#edit-project-button").click(function () {
291291
var name = $("#name").val();
292-
var repo_type = $("input[name=repo_type]:checked").val();
293292
var repository = $("#repository").val();
294-
var branch = $("#branch").val();
295-
var username = $("#username").val();
296-
var password = $("#password").val();
293+
var author = $("#author").val();
294+
var remark = $("#remark").val();
297295

298296
if (!name || name == "") {
299-
var tres = '<div class="alert alert-danger alert-dismissible" role="alert">';
300-
tres += '<button type="button" class="close" data-dismiss="alert" aria-label="Close">';
301-
tres += '<span aria-hidden="true">&times;</span></button>';
302-
tres += '<strong>name cannot be empty!</strong></div>';
303-
$("#edit-project-result").html(tres).fadeIn(1000);
297+
showAlert('danger', 'name can not be empty!', 'edit-project-result');
304298
return false;
305299
}
306300

307-
if (!repo_type || repo_type == "") {
308-
var tres = '<div class="alert alert-danger alert-dismissible" role="alert">';
309-
tres += '<button type="button" class="close" data-dismiss="alert" aria-label="Close">';
310-
tres += '<span aria-hidden="true">&times;</span></button>';
311-
tres += '<strong>repo type error.</strong></div>';
312-
$("#edit-project-result").html(tres).fadeIn(1000);
301+
if (!repository || repository == "") {
302+
showAlert('danger', 'repository can not be empty!', '#edit-project-result');
313303
return false;
314304
}
315-
316-
if (!repository || repository == "") {
317-
var tres = '<div class="alert alert-danger alert-dismissible" role="alert">';
318-
tres += '<button type="button" class="close" data-dismiss="alert" aria-label="Close">';
319-
tres += '<span aria-hidden="true">&times;</span></button>';
320-
tres += '<strong>repository cannot be empty!</strong></div>';
321-
$("#edit-project-result").html(tres).fadeIn(1000);
305+
if (!remark || remark == "") {
306+
showAlert('danger', 'remark can not be empty!', '#edit-project-result');
322307
return false;
323308
}
324309

325-
if (!branch || branch == "") {
326-
var tres = '<div class="alert alert-danger alert-dismissible" role="alert">';
327-
tres += '<button type="button" class="close" data-dismiss="alert" aria-label="Close">';
328-
tres += '<span aria-hidden="true">&times;</span></button>';
329-
tres += '<strong>branch cannot be empty!</strong></div>';
330-
$("#edit-project-result").html(tres).fadeIn(1000);
310+
if (!author || author == "") {
311+
showAlert('danger', 'author cannot be empty!', '#edit-project-result');
331312
return false;
332313
}
333314

334315
data = {
335316
'project_id': cur_project_id,
336317
'name': name,
337-
'repo_type': repo_type,
338318
'repository' : repository,
339-
'branch' : branch,
340-
'username': username,
341-
'password': password
319+
'author': author,
320+
'remark': remark
342321
};
343322
$.post('edit_project/'+cur_project_id, data, function (res) {
344-
var tres = '<div class="alert alert-' + res.tag + ' alert-dismissible" role="alert">';
345-
tres += '<button type="button" class="close" data-dismiss="alert" aria-label="Close">';
346-
tres += '<span aria-hidden="true">&times;</span></button>';
347-
tres += '<strong>' + res.msg + '</strong></div>';
348-
$("#edit-project-result").html(tres).fadeIn(1000);
323+
showAlert(res.tag, res.msg, '#edit-project-result');
349324
});
350325
});
351326
});
@@ -377,21 +352,44 @@ $("#show_all_projects").click(function () {
377352

378353
// show all white lists click
379354
$("#show_all_whitelists").click(function () {
380-
console.log('show all white list');
381355
$.get('whitelists', function (data) {
382356
$("#main-div").html(data);
383357

384358
// edit the special white list
385359
$("[id^=edit-whitelist]").click(function () {
386360
var cur_id = $(this).attr('id').split('-')[2];
387361
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+
});
388387
});
389388

390389

391390
// delete the special white list
392391
$("[id^=del-whitelist]").click(function () {
393392
var cur_id = $(this).attr('id').split('-')[2];
394-
console.log("delete the " + cur_id);
395393
$.post('del_whitelist', {'whitelist_id': cur_id}, function (data) {
396394
showAlert(data.tag, data.msg, "#operate_result");
397395
$("#show_all_whitelists").click();
@@ -410,7 +408,7 @@ $("#add_new_whitelist").click(function () {
410408
$("#add-new-whitelist-button").click(function () {
411409
var project_id = $("#project").val();
412410
var rule_id = $("#rule").val();
413-
var file = $("#file").val();
411+
var path = $("#path").val();
414412
var reason = $("#reason").val();
415413

416414
if (!project_id || project_id == "") {
@@ -423,7 +421,7 @@ $("#add_new_whitelist").click(function () {
423421
return false;
424422
}
425423

426-
if (!file || file == "") {
424+
if (!path || path == "") {
427425
showAlert('danger', 'file cannot be empty.');
428426
return false;
429427
}
@@ -436,7 +434,7 @@ $("#add_new_whitelist").click(function () {
436434
data = {
437435
'project_id': project_id,
438436
'rule_id': rule_id,
439-
'file': file,
437+
'path': path,
440438
'reason': reason
441439
};
442440

app/templates/rulesadmin/add_new_whitelist.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<select id="project" class="form-control">
55
{% for project in data.projects %}
66
<option value="{{ project.id }}">
7-
{{ project.id }}-{{ project.name }}-{{ project.repository }}-{{ project.branch }}
7+
{{ project.id }}-{{ project.name }}-{{ project.repository }}-{{ project.author }}
88
</option>
99
{% endfor %}
1010
</select>
@@ -21,7 +21,7 @@
2121
</div>
2222
<div class="form-group">
2323
<label for="file">File</label>
24-
<input type="text" class="form-control" id="file" placeholder="/path/to/white/file" />
24+
<input type="text" class="form-control" id="path" placeholder="/path/to/white/file" />
2525
</div>
2626
<div class="form-group">
2727
<label for="reason">Reason</label>

app/templates/rulesadmin/edit_project.html

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ <h3>
1313
<label for="name">Project Name</label>
1414
<input type="text" class="form-control" id="name" value="{{ data.project.name }}"/>
1515
</div>
16-
<br>
1716
<div class="form-group">
1817
<label for="repository">Repository</label>
1918
<input type="text" class="form-control" id="repository" value="{{ data.project.repository }}"/>
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

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
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>
19-
<td>{{ whitelist.file }}</td>
19+
<td>{{ whitelist.path }}</td>
2020
<td>{{ whitelist.reason }}</td>
2121
<td>{{ whitelist.updated_at }}</td>
2222
<td>

0 commit comments

Comments
 (0)