Skip to content
This repository has been archived by the owner on Aug 18, 2022. It is now read-only.

Commit

Permalink
Add ability to delete rules.
Browse files Browse the repository at this point in the history
Issue #4
  • Loading branch information
ThomasHabets committed Sep 11, 2016
1 parent 4f592be commit 9b78a49
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 20 deletions.
3 changes: 3 additions & 0 deletions cmd/ui/static/acl.css
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@ table#acl-rules {
table#acl-rules tbody tr.selected {
background-color: #FFFF99;
}
table#acl-commands input {
width: 100%;
}
32 changes: 28 additions & 4 deletions cmd/ui/static/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ $(document).ready(function() {
$("#acl-selection").change(function(e) {
window.location.href = "/acl/" + $(this).val();
});
$("#button-move").click(move);

$("body").keypress(keypressHandler);
$("#new-acl").keydown(function(e) {
Expand All @@ -22,8 +21,25 @@ $(document).ready(function() {
$("#acl-rules input[type=text],#acl-rules select").change(f);
$("#acl-rules input[type=text]").keydown(f);
$("#button-save").click(save);
$("#button-move").click(move);
$("#button-delete").click(delete_button);
});

function delete_button() {
var rules = get_all_checked();
var data = {"rules": rules};
$.post("/rule/delete", data)
.done(function() {
console.log("Delete successful");
for (var i = 0; i < rules.length; i++) {
$("#acl-rules-row-" + rules[i]).remove();
changeSelected(0);
}
}).fail(function (o, text, error) {
console.log("Delete failed");
});
}

function get_ruleid_by_index(n) {
return $("#acl-rules tbody tr:nth-child("+(selected_rule+1)+") input.checked-rules").data("ruleid");
}
Expand All @@ -50,7 +66,7 @@ function ruleTextChanged(me) {
// Remove all checkmarks.
$("#acl-rules input.checked-rules").prop("checked", false);
$("#acl-rules tr").removeClass("selected");
$("#button-move").attr("disabled", "disabled");
updateActionButtons();

// Disable all but active rule for editing.
$("#acl-rules input,#acl-rules select").each(function(index) {
Expand Down Expand Up @@ -95,8 +111,11 @@ function keyCheck() {
var o = $("#acl-rules tbody tr:nth-child("+(selected_rule+1)+") input.checked-rules");
o.prop("checked", !o.prop("checked"));
checkedRulesChanged(o);
updateActionButtons();
}

o = $("#button-move");
function updateActionButtons() {
var o = $(".button-check-action");
if ($(".checked-rules:checked").length > 0) {
o.removeAttr("disabled");
} else {
Expand Down Expand Up @@ -149,12 +168,17 @@ function changeSelected(delta) {
window.scroll(0, window.scrollY+delta);
}

function move() {
function get_all_checked() {
var rules = new Array;
$(".checked-rules:checked").each(function(index) {
rules[index] = $(this).data("ruleid");
});
return rules;
}

function move() {
var data = {};
var rules = get_all_checked();
data["destination"] = $("#acl-move-selection").val();
data["rules"] = rules;
$.post("/acl/move", data)
Expand Down
26 changes: 17 additions & 9 deletions cmd/ui/templates/acl.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@
{{if .Current.ACLID}}
<h2>ACL: {{.Current.Comment}}</h2>

<input type="button" id="button-move" value="move" disabled /> to
<select id="acl-move-selection">
<option value="">[no ACL selected]</option>
{{range .ACLs}}
<option value="{{.ACLID}}">{{.Comment}}</option>
{{end}}
</select>
<table id="acl-commands">
<tbody>
<tr>
<td>
<select id="acl-move-selection">
<option value="">[no ACL selected]</option>
{{range .ACLs}}
<option value="{{.ACLID}}">{{.Comment}}</option>
{{end}}
</select>
</td>
<td><input type="button" class="button-check-action" id="button-move" value="move" disabled /></td>
</tr>
<tr><td></td><td><input type="button" class="button-check-action" id="button-delete" value="delete" disabled /></td></tr>
<tr><td></td><td><input type="button" id="button-save" value="save" disabled /></td></tr>
</tbody>
</table>

<br/>
<input type="button" id="button-save" value="save" disabled />

<table id="acl-rules" class="standard">
<thead>
Expand Down
36 changes: 29 additions & 7 deletions cmd/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,27 @@ func getSources() ([]source, error) {
return sources, nil
}

func ruleDeleteHandler(r *http.Request) (interface{}, error) {
r.ParseForm()
var rules []string
for _, ruleID := range r.Form["rules[]"] {
if !reUUID.MatchString(ruleID) {
return nil, fmt.Errorf("%q is not valid rule ID", ruleID)
}
rules = append(rules, ruleID)
}
log.Printf("Deleting %s", strings.Join(rules, ", "))
return "OK", txWrap(func(tx *sql.Tx) error {
if _, err := tx.Exec(fmt.Sprintf(`DELETE FROM aclrules WHERE rule_id IN ('%s')`, strings.Join(rules, "','"))); err != nil {
return err
}
if _, err := tx.Exec(fmt.Sprintf(`DELETE FROM rules WHERE rule_id IN ('%s')`, strings.Join(rules, "','"))); err != nil {
return err
}
return nil
})
}

func ruleEditHandler(r *http.Request) (interface{}, error) {
ruleID := ruleID(mux.Vars(r)["ruleID"])
r.ParseForm()
Expand Down Expand Up @@ -718,19 +739,20 @@ func main() {
log.Printf("Running...")
r := mux.NewRouter()
r.HandleFunc("/", errWrap(rootHandler)).Methods("GET", "HEAD")
r.HandleFunc("/acl/", errWrap(aclHandler)).Methods("GET", "HEAD")
r.HandleFunc("/acl/{aclID}", errWrap(aclHandler)).Methods("GET", "HEAD")
r.HandleFunc("/rule/{ruleID}", errWrapJSON(ruleEditHandler)).Methods("POST")
r.HandleFunc("/acl/move", errWrapJSON(aclMoveHandler)).Methods("POST")
r.HandleFunc("/acl/new", errWrapJSON(aclNewHandler)).Methods("POST")
r.HandleFunc("/access/", errWrap(accessHandler)).Methods("GET", "HEAD")
r.HandleFunc("/access/{groupID}", errWrap(accessHandler)).Methods("GET", "HEAD")
r.HandleFunc("/access/{groupID}", errWrapJSON(accessUpdateHandler)).Methods("POST")
r.HandleFunc("/members/", errWrap(membersHandler)).Methods("GET", "HEAD")
r.HandleFunc("/members/{groupID}", errWrap(membersHandler)).Methods("GET", "HEAD")
r.HandleFunc("/acl/", errWrap(aclHandler)).Methods("GET", "HEAD")
r.HandleFunc("/acl/move", errWrapJSON(aclMoveHandler)).Methods("POST")
r.HandleFunc("/acl/new", errWrapJSON(aclNewHandler)).Methods("POST")
r.HandleFunc("/acl/{aclID}", errWrap(aclHandler)).Methods("GET", "HEAD")
r.HandleFunc("/ajax/allow", allowHandler).Methods("POST")
r.HandleFunc("/ajax/tail-log", tailLogHandler).Methods("GET")
r.HandleFunc("/ajax/tail-log/stream", tailHandler)
r.HandleFunc("/members/", errWrap(membersHandler)).Methods("GET", "HEAD")
r.HandleFunc("/members/{groupID}", errWrap(membersHandler)).Methods("GET", "HEAD")
r.HandleFunc("/rule/delete", errWrapJSON(ruleDeleteHandler)).Methods("POST")
r.HandleFunc("/rule/{ruleID}", errWrapJSON(ruleEditHandler)).Methods("POST")

fs := http.FileServer(http.Dir(*staticDir))
http.Handle("/static/", http.StripPrefix("/static/", fs))
Expand Down

0 comments on commit 9b78a49

Please sign in to comment.