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

Commit

Permalink
Merge pull request #4 from LiGhT1EsS/master
Browse files Browse the repository at this point in the history
improves whitelist
  • Loading branch information
FeeiCN committed Jun 3, 2016
2 parents 6074edb + cac1d75 commit 26a4b1a
Show file tree
Hide file tree
Showing 7 changed files with 250 additions and 67 deletions.
78 changes: 58 additions & 20 deletions app/controller/RulesAdmin.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,37 +282,28 @@ def edit_project(project_id):
# get data from request
project_id = request.form.get('project_id')
name = request.form.get('name')
repo_type = request.form.get('repo_type')
repository = request.form.get('repository')
branch = request.form.get('branch')
username = request.form.get('username')
password = request.form.get('password')
author = request.form.get('author')
remark = request.form.get('remark')

# check data
if not project_id or project_id == "":
return jsonify(tag='danger', msg='wrong project id.')
if not name or name == "":
return jsonify(tag='danger', msg='name cannot be empty')
if not repo_type or repo_type == "":
return jsonify(tag='danger', msg='repo type cannot be empty')
if not repository or repository == "":
return jsonify(tag='danger', msg='repository can not be empty')
if not branch or branch == "":
return jsonify(tag='danger', msg="branch can not be empty")

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

# update project data
project.name = name
project.repo_type = 1 if repo_type == 'git' else 2
project.author = author
project.remark = remark
project.repository = repository
project.branch = branch
project.username = username if username and username != "" else None
project.password = password if password and password != "" else None
project.updated_at = current_time
try:
db.session.add(project)
Expand Down Expand Up @@ -343,22 +334,22 @@ def add_whitelist():
if request.method == 'POST':
project_id = request.form.get('project_id')
rule_id = request.form.get('rule_id')
file = request.form.get('file')
path = request.form.get('path')
reason = request.form.get('reason')

if not project_id or project_id == "":
return jsonify(tag='danger', msg='project id error.')
if not rule_id or rule_id == "":
return jsonify(tag='danger', msg='rule id error.')
if not file or file == "":
if not path or path == "":
return jsonify(tag='danger', msg='file error.')
if not reason or reason == "":
return jsonify(tag='danger', msg='reason error.')

current_time = time.strftime('%Y-%m-%d %X', time.localtime())
if file[0] != '/':
file = '/' + file
whitelist = CobraWhiteList(project_id, rule_id, file, reason, 1, current_time, current_time)
if path[0] != '/':
path = '/' + path
whitelist = CobraWhiteList(project_id, rule_id, path, reason, 1, current_time, current_time)
try:
db.session.add(whitelist)
db.session.commit()
Expand Down Expand Up @@ -393,5 +384,52 @@ def del_whitelist():

# edit the special white list
@web.route(ADMIN_URL + '/edit_whitelist/<int:whitelist_id>', methods=['GET', 'POST'])
def edit_whitelist():
pass
def edit_whitelist(whitelist_id):
if request.method == 'POST':
whitelist_id = request.form.get('whitelist_id')
project_id = request.form.get('project')
rule_id = request.form.get('rule')
path = request.form.get('path')
reason = request.form.get('reason')
status = request.form.get('status')

if not whitelist_id or whitelist_id == "":
return jsonify(tag='danger', msg='wrong whitelist')
if not project_id or project_id == "":
return jsonify(tag='danger', msg='project can not be empty')
if not rule_id or rule_id == "":
return jsonify(tag='danger', msg='rule can not be empty')
if not path or path == "":
return jsonify(tag='danger', msg='path can not be empty')
if not reason or reason == "":
return jsonify(tag='danger', msg='reason can not be empty')
if not status or status == "":
return jsonify(tag='danger', msg='status can not be empty')

whitelist = CobraWhiteList.query.filter_by(id=whitelist_id).first()
if not whitelist:
return jsonify(tag='danger', msg='wrong whitelist')

whitelist.project_id = project_id
whitelist.rule_id = rule_id
whitelist.path = path
whitelist.reason = reason
whitelist.status = status

try:
db.session.add(whitelist)
db.session.commit()
return jsonify(tag='success', msg='update success.')
except:
return jsonify(tag='danger', msg='unknown error.')
else:
rules = CobraRules.query.all()
projects = CobraProjects.query.all()
whitelist = CobraWhiteList.query.filter_by(id=whitelist_id).first()
data = {
'rules': rules,
'projects': projects,
'whitelist': whitelist,
}

return render_template('rulesadmin/edit_whitelist.html', data=data)
82 changes: 40 additions & 42 deletions app/templates/asset/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,63 +289,38 @@ $("#show_all_projects").click(function () {

$("#edit-project-button").click(function () {
var name = $("#name").val();
var repo_type = $("input[name=repo_type]:checked").val();
var repository = $("#repository").val();
var branch = $("#branch").val();
var username = $("#username").val();
var password = $("#password").val();
var author = $("#author").val();
var remark = $("#remark").val();

if (!name || name == "") {
var tres = '<div class="alert alert-danger 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>name cannot be empty!</strong></div>';
$("#edit-project-result").html(tres).fadeIn(1000);
showAlert('danger', 'name can not be empty!', 'edit-project-result');
return false;
}

if (!repo_type || repo_type == "") {
var tres = '<div class="alert alert-danger 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>repo type error.</strong></div>';
$("#edit-project-result").html(tres).fadeIn(1000);
if (!repository || repository == "") {
showAlert('danger', 'repository can not be empty!', '#edit-project-result');
return false;
}

if (!repository || repository == "") {
var tres = '<div class="alert alert-danger 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>repository cannot be empty!</strong></div>';
$("#edit-project-result").html(tres).fadeIn(1000);
if (!remark || remark == "") {
showAlert('danger', 'remark can not be empty!', '#edit-project-result');
return false;
}

if (!branch || branch == "") {
var tres = '<div class="alert alert-danger 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>branch cannot be empty!</strong></div>';
$("#edit-project-result").html(tres).fadeIn(1000);
if (!author || author == "") {
showAlert('danger', 'author cannot be empty!', '#edit-project-result');
return false;
}

data = {
'project_id': cur_project_id,
'name': name,
'repo_type': repo_type,
'repository' : repository,
'branch' : branch,
'username': username,
'password': password
'author': author,
'remark': remark
};
$.post('edit_project/'+cur_project_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-project-result").html(tres).fadeIn(1000);
showAlert(res.tag, res.msg, '#edit-project-result');
});
});
});
Expand Down Expand Up @@ -377,21 +352,44 @@ $("#show_all_projects").click(function () {

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

// edit the special white list
$("[id^=edit-whitelist]").click(function () {
var cur_id = $(this).attr('id').split('-')[2];
console.log("edit the " + cur_id);

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

$("#edit-whitelist-button").click(function () {
var project = $("#project").val();
var rule = $("#rule").val();
var path = $("#path").val();
var reason = $("#reason").val();
var status = $("#status:checked").val();

data = {
'whitelist_id': cur_id,
'project': project,
'rule': rule,
'path': path,
'reason': reason,
'status': status
};

$.post("edit_whitelist/"+cur_id, data, function (result) {
showAlert(result.tag, result.msg, '#edit-whitelist-result');
});
});
});
});


// delete the special white list
$("[id^=del-whitelist]").click(function () {
var cur_id = $(this).attr('id').split('-')[2];
console.log("delete the " + cur_id);
$.post('del_whitelist', {'whitelist_id': cur_id}, function (data) {
showAlert(data.tag, data.msg, "#operate_result");
$("#show_all_whitelists").click();
Expand All @@ -410,7 +408,7 @@ $("#add_new_whitelist").click(function () {
$("#add-new-whitelist-button").click(function () {
var project_id = $("#project").val();
var rule_id = $("#rule").val();
var file = $("#file").val();
var path = $("#path").val();
var reason = $("#reason").val();

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

if (!file || file == "") {
if (!path || path == "") {
showAlert('danger', 'file cannot be empty.');
return false;
}
Expand All @@ -436,7 +434,7 @@ $("#add_new_whitelist").click(function () {
data = {
'project_id': project_id,
'rule_id': rule_id,
'file': file,
'path': path,
'reason': reason
};

Expand Down
4 changes: 2 additions & 2 deletions app/templates/rulesadmin/add_new_whitelist.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<select id="project" class="form-control">
{% for project in data.projects %}
<option value="{{ project.id }}">
{{ project.id }}-{{ project.name }}-{{ project.repository }}-{{ project.branch }}
{{ project.id }}-{{ project.name }}-{{ project.repository }}-{{ project.author }}
</option>
{% endfor %}
</select>
Expand All @@ -21,7 +21,7 @@
</div>
<div class="form-group">
<label for="file">File</label>
<input type="text" class="form-control" id="file" placeholder="/path/to/white/file" />
<input type="text" class="form-control" id="path" placeholder="/path/to/white/file" />
</div>
<div class="form-group">
<label for="reason">Reason</label>
Expand Down
1 change: 0 additions & 1 deletion app/templates/rulesadmin/edit_project.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ <h3>
<label for="name">Project Name</label>
<input type="text" class="form-control" id="name" value="{{ data.project.name }}"/>
</div>
<br>
<div class="form-group">
<label for="repository">Repository</label>
<input type="text" class="form-control" id="repository" value="{{ data.project.repository }}"/>
Expand Down
53 changes: 53 additions & 0 deletions app/templates/rulesadmin/edit_whitelist.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<div class="row clearfix">
<div class="col-md-12 column">
<div class="page-header">
<h3>
Edit White List
<small></small>
</h3>
</div>
</div>
</div>
<form role="form">
<div class="form-group">
<label for="project">Project</label>
<select id="project" class="form-control">
{% for project in data.projects %}
<option value="{{ project.id }}" {% if data.whitelist.project_id == project.id %}selected{% endif %}>
{{ project.id }}-{{ project.name }}-{{ project.repository }}-{{ project.author }}
</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="rule">Rule</label>
<select id="rule" class="form-control">
{% for rule in data.rules %}
<option value="{{ rule.id }}" {% if data.whitelist.rule_id == rule.id %}selected{% endif %}>
{{ rule.id }}-{{ rule.description }}
</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="path">Path</label>
<input type="text" class="form-control" id="path" value="{{ data.whitelist.path }}"/>
</div>
<div class="form-group">
<label for="reason">Reason</label>
<textarea class="form-control" id="reason">{{ data.whitelist.reason }}</textarea>
</div>
<div class="form-group">
<label for="status">Status</label>&nbsp;&nbsp;&nbsp;&nbsp;
<label class="radio-inline">
<input type="radio" name="status" id="status" value="1"
{% if data.whitelist.status == 1 %}checked{% endif %}> On
</label>
<label class="radio-inline">
<input type="radio" name="status" id="status" value="2"
{% if data.whitelist.status == 2 %}checked{% endif %}> Off
</label>
</div>
<div id="edit-whitelist-result" hidden></div>
<button type="button" class="btn btn-success" id="edit-whitelist-button">Save</button>
</form>
4 changes: 2 additions & 2 deletions app/templates/rulesadmin/whitelists.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
</thead>
<tbody id="main-table">
{% for whitelist in data.whitelists %}
<tr>
<tr {% if whitelist.status == 1 %}class="success"{% else %}class="danger"{% endif %}>
<td>{{ whitelist.id }}</td>
<td>{{ whitelist.project_id }}</td>
<td>{{ whitelist.rule_id }}</td>
<td>{{ whitelist.file }}</td>
<td>{{ whitelist.path }}</td>
<td>{{ whitelist.reason }}</td>
<td>{{ whitelist.updated_at }}</td>
<td>
Expand Down
Loading

0 comments on commit 26a4b1a

Please sign in to comment.