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

Commit

Permalink
vuls类型操作完成,可以增加,修改,删除vuls.
Browse files Browse the repository at this point in the history
  • Loading branch information
lightless233 committed May 27, 2016
1 parent 95d13a6 commit 163e23c
Show file tree
Hide file tree
Showing 5 changed files with 206 additions and 12 deletions.
64 changes: 62 additions & 2 deletions app/controller/RulesAdmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,15 @@ def rules():


# add new rules button
@web.route(ADMIN_URL + '/add_new_rule', methods=['GET'])
@web.route(ADMIN_URL + '/add_new_rule', methods=['GET', 'POST'])
def add_new_rule():
return render_template('rulesadmin/add_new_rule.html')

if request.method == 'POST':
return '123'
else:
vul_type = CobraVuls.query.all()
print vul_type
return render_template('rulesadmin/add_new_rule.html')


# add new vuls button
Expand All @@ -64,9 +70,63 @@ def add_new_vul():
return render_template('rulesadmin/add_new_vul.html')


# show all vuls click
@web.route(ADMIN_URL + '/vuls', methods=['GET'])
def vuls():
all_vuls = CobraVuls.query.all()
data = {
'vuls': all_vuls
}
return render_template('rulesadmin/vuls.html', data=data)


# del special vul
@web.route(ADMIN_URL + '/del_vul', methods=['POST'])
def del_vul():
vul_id = request.form['vul_id']
if vul_id:
v = CobraVuls.query.filter_by(id=vul_id).first()
try:
db.session.delete(v)
db.session.commit()
return jsonify(tag='success', msg='delete success.')
except:
return jsonify(tag='danger', msg='delete failed. Try again later?')
else:
return jsonify(tag='danger', msg='wrong id')


# edit special vul
@web.route(ADMIN_URL + '/edit_vul/<int:vul_id>', methods=['GET', 'POST'])
def edit_vul(vul_id):
if request.method == 'POST':
name = request.form['name']
description = request.form['description']
if not name or name == "":
return jsonify(tag='danger', msg='name can not be empty')
if not description or description == "":
return jsonify(tag='danger', msg='description can not be empty')
v = CobraVuls.query.filter_by(id=vul_id).first()
v.name = name
v.description = description
try:
db.session.add(v)
db.session.commit()
return jsonify(tag='success', msg='save success.')
except:
return jsonify(tag='danger', msg='save failed. Try again later?')
else:
v = CobraVuls.query.filter_by(id=vul_id).first()
return render_template('rulesadmin/edit_vul.html', data={
'name': v.name,
'description': v.description,
})


# api: get all rules count
@web.route(ADMIN_URL + '/all_rules_count', methods=['GET'])
def all_rules_count():
rules_count = CobraRules.query.count()
return str(rules_count)


21 changes: 21 additions & 0 deletions app/templates/rulesadmin/edit_vul.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h3>
Edit vul type <small></small>
</h3>
</div>
</div>
</div>
<form role="form">
<div class="form-group">
<label for="name">Vul Name</label>
<input type="text" class="form-control" id="name" value="{{ data.name }}"/>
</div>
<div class="form-group">
<label for="description">Vul Description</label>
<input type="text" class="form-control" id="description" value="{{ data.description }}"/>
</div>
<div id="edit-vul-result" hidden></div>
<button type="button" class="btn btn-success" id="edit-vul-button">Save</button>
</form>
103 changes: 95 additions & 8 deletions app/templates/rulesadmin/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,24 @@ <h1>
</div>
<div class="row clearfix">
<div class="col-md-2 column">
<button type="button" class="btn btn-primary btn-block btn-default" id="show_all_rules">Show All Rules</button>
<br />
<button type="button" class="btn btn-default btn-block btn-primary" id="add_new_rules">Add New Rules</button>
<br />
<button type="button" class="btn btn-default btn-block btn-primary" id="add_new_vuls">Add New Vuls</button>
<div class="panel panel-primary">
<div class="panel-heading">Rules Manage</div>
<div class="panel-body">
<button type="button" class="btn btn-primary btn-block" id="show_all_rules">Show All Rules</button>
<button type="button" class="btn btn-primary btn-block" id="add_new_rules">Add New Rules</button>
</div>
</div>
<div class="panel panel-primary">
<div class="panel-heading">Vuls Manage</div>
<div class="panel-body">
<button type="button" class="btn btn-primary btn-block" id="show_all_vuls">Show All Vuls</button>
<button type="button" class="btn btn-primary btn-block" id="add_new_vuls">Add New Vuls</button>
</div>
</div>
</div>
<div class="col-md-10 column" id="main-div">
<h2>Welcome to cobra rules management.</h2>
<div hidden class="col-md-10 column" id="vul_operate_result"></div>
<div class="col-md-10 column" id="main-div" hidden>
<h3>Welcome to cobra rules management.</h3>
<h3>Click left button to select function.</h3>
</div>
</div>
Expand All @@ -32,20 +42,97 @@ <h3>Click left button to select function.</h3>
{% block scripts %}
{{ super() }}
<script>
var ADMIN_URL = 'admin';

$("#main-div").fadeIn(1000);

// show all rules
$("#show_all_rules").click(function () {
$.get('rules', function (data) {
$("#main-div").html(data);
});
});

// add new rules
$("#add_new_rules").click(function () {
$.get('add_new_rule', function (data) {
$("#main-div").html(data);
});
});

// show all vuls
$("#show_all_vuls").click(function () {
$.get('vuls', function (data) {
$("#main-div").html(data);
// delete the special vul
$("[id^=del-vul]").click(function () {
var current_id = $(this).attr('id');
var vul_id = current_id.split('-')[2];

$.post('del_vul', {'vul_id':vul_id}, function (result) {
var tt = '<div class="alert alert-' + result.tag +' alert-dismissible" role="alert">';
tt += '<button type="button" class="close" data-dismiss="alert" aria-label="Close">';
tt += '<span aria-hidden="true">&times;</span></button>';
tt += '<strong>' + result.msg + '</strong></div>';
$("#vul_operate_result").html(tt).fadeIn(1000);
$("#show_all_vuls").click();
});
});
// edit the special vul
$("[id^=edit-vul]").click(function () {
console.log('edit click');
console.log($(this).attr('id'));
var current_id = $(this).attr('id');
var vul_id = current_id.split('-')[2];
console.log(vul_id);

$.get('edit_vul/'+vul_id, function (data) {
$("#main-div").html(data);

$("#edit-vul-button").click(function () {
var name = $("#name").val();
var description = $("#description").val();
if (!name || !description || name == "" || description == "") {
var result = '<div class="alert alert-danger alert-dismissible" role="alert">';
result += '<button type="button" class="close" data-dismiss="alert" aria-label="Close">';
result += '<span aria-hidden="true">&times;</span></button>';
result += '<strong>name or description can not be empty!</strong></div>';
$("#edit-vul-result").html(result).fadeIn(1000);
}
data = {
'vul_id': vul_id,
'name': name,
'description': description
};
$.post('edit_vul/' + vul_id, data, function (res) {
var tres = '<div class="alert alert-' + res.tag + ' alert-dismissible" role="alert">';
tres += '<button type="button" class="close" data-dismiss="alert" aria-label="Close">';
tres += '<span aria-hidden="true">&times;</span></button>';
tres += '<strong>' + res.msg + '</strong></div>';
$("#edit-vul-result").html(tres).fadeIn(1000);
});

});

});

});


});
});

// Add new vuls.
$("#add_new_vuls").click(function () {
$.get('add_new_vul', function (data) {
$("#main-div").html(data);

$("#name").focus(function () {
$("#add-new-vul-result").fadeOut(1000);
});
$("#description").focus(function () {
$("#add-new-vul-result").fadeOut(1000);
});

$("#add-new-vul-button").click(function () {
var name = $("#name").val();
var description = $("#description").val();
Expand Down
4 changes: 2 additions & 2 deletions app/templates/rulesadmin/rules.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<tr>
<th>ID</th>
<th>language</th>
<th>vul type</th>
<th>regex</th>
<th>description</th>
<th>last update time</th>
<th>Operation</th>
</tr>
Expand All @@ -14,8 +14,8 @@
<tr>
<td>{{ rule.id }}</td>
<td>{{ rule.language }}</td>
<td>{{ rule.vul_id }}</td>
<td>{{ rule.regex }}</td>
<td>{{ rule.description }}</td>
<td>{{ rule.updated_at }}</td>
<td>Edit | Delete | View </td>
</tr>
Expand Down
26 changes: 26 additions & 0 deletions app/templates/rulesadmin/vuls.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@

<table class="table table-bordered table-hover">
<thead>
<tr>
<th>ID</th>
<th>name</th>
<th>description</th>
<th>last update time</th>
<th>Operation</th>
</tr>
</thead>
<tbody id="main-table">
{% for vul in data.vuls %}
<tr>
<td>{{ vul.id }}</td>
<td>{{ vul.name }}</td>
<td>{{ vul.description }}</td>
<td>{{ vul.updated_at }}</td>
<td>
<span class="glyphicon glyphicon-pencil" aria-hidden="true" id="edit-vul-{{ vul.id }}"></span>&nbsp;&nbsp;
<span class="glyphicon glyphicon-remove" aria-hidden="true" id="del-vul-{{ vul.id }}"></span>
</td>
</tr>
{% endfor %}
</tbody>
</table>

0 comments on commit 163e23c

Please sign in to comment.