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

Commit

Permalink
Add /access/ to edit acl activation
Browse files Browse the repository at this point in the history
Bug #4
  • Loading branch information
ThomasHabets committed Aug 27, 2016
1 parent a2b2713 commit ed2decc
Show file tree
Hide file tree
Showing 6 changed files with 230 additions and 2 deletions.
33 changes: 33 additions & 0 deletions cmd/ui/static/access.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
var selected_group = 0;

$(document).ready(function() {
$("body").keypress(keypressHandler);
$("#access-group-selection").change(function(e) {
window.location.href = "/access/" + $(this).val();
});
$("#button-update").click(update);
// $("table#acl-rules input.checked-rules").change(function() {checkedRulesChanged($(this))});
//changeSelected(1);
});

function update() {
var active = new Array;
var comments = new Array;
$(".access-acl-checked:checked").each(function(index) {
var aclid = $(this).data("aclid");
active[index] = aclid;
comments[index] = $("#access-comment-" + aclid).val();
});
var data = {};
data["acls"] = active;
data["comments"] = comments;
$.post("/access/" + $("#access-group-selection").val(), data)
.done(function() {
console.log("success");
}).fail(function(o, text, error) {
console.log("Failed!");
});
}

function keypressHandler(event) {
}
2 changes: 1 addition & 1 deletion cmd/ui/static/acl.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ $(document).ready(function() {
newACL($(this).val());
});
$("table#acl-rules input.checked-rules").change(function() {checkedRulesChanged($(this))});
changeSelected(1);
changeSelected(0);
});

function newACL(name) {
Expand Down
3 changes: 3 additions & 0 deletions cmd/ui/static/squidwarden.css
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,6 @@ td.min {
td.max {
width: 100%;
}
.maxwidth {
width: 100%;
}
37 changes: 37 additions & 0 deletions cmd/ui/templates/access.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{{$root := .}}
<script type="text/javascript" src="/static/access.js"></script>
<link rel="stylesheet" type="text/css" href="/static/access.css" media="screen"/>
Go to group:
<select id="access-group-selection">
<option value="">[no group selected]</option>
{{range .Groups}}
<option value="{{.GroupID}}"{{if groupIDEQ $root.Current.GroupID .GroupID}} selected{{end}}>{{.Comment}}</option>
{{end}}
</select>


{{if .Current.GroupID}}
<input type="button" id="button-update" value="Update" />
<table class="standard">
<thead>
<tr>
<th></th>
<th></th>
<th>ID</th>
<th>ACL</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
{{range .ACLs}}
<tr>
<td></td>
<td class="min"><input type="checkbox" class="access-acl-checked" data-aclid="{{.ACL.ACLID}}" {{if .Active}}checked{{end}}/></td>
<td class="min fixed uuid">{{.ACL.ACLID}}</td>
<td class="min" id="access-acl-comment-{{.ACL.ACLID}}">{{.ACL.Comment}}</td>
<td class="max"><input type="text" class="maxwidth" id="access-comment-{{.ACL.ACLID}}" value="{{.Comment}}" /></td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
3 changes: 2 additions & 1 deletion cmd/ui/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
<body>
<div id="nav">
<a href="/">Squidwarden</a>
<a href="/acl/list">ACLs</a>
<a href="/acl/">ACLs</a>
<a href="/access/">Access</a>
<span id="nav-time">{{.Now}}</span>
</div>
<div id="content">{{.Content}}</div>
Expand Down
154 changes: 154 additions & 0 deletions cmd/ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,157 @@ func aclMoveHandler(r *http.Request) (interface{}, error) {
})
}

func accessUpdateHandler(r *http.Request) (interface{}, error) {
groupID := groupID(mux.Vars(r)["groupID"])
r.ParseForm()

var acls []string
for _, aclID := range r.Form["acls[]"] {
if !reUUID.MatchString(aclID) {
return nil, fmt.Errorf("%q is not valid acl ID", aclID)
}
acls = append(acls, aclID)
}

comments := r.Form["comments[]"]
if len(comments) != len(acls) {
return nil, fmt.Errorf("acl list and comment list length unequal. acl=%d comment=%d", len(acls), len(comments))
}

return "OK", txWrap(func(tx *sql.Tx) error {
if _, err := tx.Exec(`DELETE FROM groupaccess WHERE group_id=?`, string(groupID)); err != nil {
return err
}
for n := range acls {
if _, err := tx.Exec(`INSERT INTO groupaccess(group_id, acl_id, comment) VALUES(?,?,?)`, string(groupID), acls[n], comments[n]); err != nil {
return err
}
}
return nil
})
}

type groupID string
type group struct {
GroupID groupID
Comment string
}

func accessHandler(r *http.Request) (template.HTML, error) {
current := groupID(mux.Vars(r)["groupID"])

type maybeACL struct {
Active bool
Comment string
ACL acl
}
data := struct {
Groups []group
Current group
ACLs []maybeACL
}{}
{
rows, err := db.Query(`SELECT group_id, comment FROM groups ORDER BY comment`)
if err != nil {
return "", err
}
defer rows.Close()

for rows.Next() {
var s string
var c sql.NullString
if err := rows.Scan(&s, &c); err != nil {
return "", err
}
e := group{
GroupID: groupID(s),
Comment: c.String,
}
data.Groups = append(data.Groups, e)
if current == e.GroupID {
data.Current = e
}
}
if err := rows.Err(); err != nil {
return "", err
}
}
if len(current) > 0 {
active, err := getGroupACLs(current)
if err != nil {
return "", err
}

acls, err := getACLs()
if err != nil {
return "", err
}
for _, a := range acls {
e := maybeACL{ACL: a}
e.Comment, e.Active = active[a.ACLID]
data.ACLs = append(data.ACLs, e)
}
}

tmpl := template.Must(template.New("access.html").Funcs(template.FuncMap{
"groupIDEQ": func(a, b groupID) bool { return a == b },
}).ParseFiles(path.Join(*templates, "access.html")))
var buf bytes.Buffer
if err := tmpl.Execute(&buf, &data); err != nil {
return "", fmt.Errorf("template execute fail: %v", err)
}
return template.HTML(buf.String()), nil
}

func getGroupACLs(g groupID) (map[aclID]string, error) {
acls := make(map[aclID]string)

rows, err := db.Query(`SELECT acl_id, comment FROM groupaccess WHERE group_id=?`, string(g))
if err != nil {
return nil, err
}
defer rows.Close()

for rows.Next() {
var s string
var c sql.NullString
if err := rows.Scan(&s, &c); err != nil {
return nil, err
}
acls[aclID(s)] = c.String
}
if err := rows.Err(); err != nil {
return nil, err
}
return acls, nil
}

func getACLs() ([]acl, error) {
var acls []acl
rows, err := db.Query(`SELECT acl_id, comment FROM acls ORDER BY comment`)
if err != nil {
return nil, err
}
defer rows.Close()

for rows.Next() {
var s string
var c sql.NullString
if err := rows.Scan(&s, &c); err != nil {
return nil, err
}
e := acl{
ACLID: aclID(s),
Comment: c.String,
}
acls = append(acls, e)
}
if err := rows.Err(); err != nil {
return nil, err
}
return acls, nil
}

func aclHandler(r *http.Request) (template.HTML, error) {
current := aclID(mux.Vars(r)["aclID"])

Expand Down Expand Up @@ -422,6 +573,9 @@ func main() {
r.HandleFunc("/acl/{aclID}", errWrap(aclHandler)).Methods("GET", "HEAD")
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("/ajax/allow", allowHandler).Methods("POST")
r.HandleFunc("/ajax/tail-log", tailLogHandler).Methods("GET")
r.HandleFunc("/ajax/tail-log/stream", tailHandler)
Expand Down

0 comments on commit ed2decc

Please sign in to comment.