-
Notifications
You must be signed in to change notification settings - Fork 0
/
groupAdd.go
68 lines (60 loc) · 1.76 KB
/
groupAdd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package httpserver
import (
"fmt"
"html/template"
"net/http"
"strings"
"github.com/ansel1/merry"
"github.com/gorilla/csrf"
"github.com/joshsziegler/zauth/pkg/user"
)
type formNewGroup struct {
Name string
Description string
}
type newGroupPageData struct {
User *user.User
ErrorMessage string
Form formNewGroup
CSRFField template.HTML
}
func newFormNewGroup(r *http.Request) formNewGroup {
f := formNewGroup{}
f.Name = strings.Trim(r.FormValue("Name"), " ")
f.Description = strings.Trim(r.FormValue("Description"), " ")
return f
}
// NewGroupGet is a sub-handler that shows the Group creation page.
func NewGroupGet(c *Context, w http.ResponseWriter, r *http.Request) error {
// Check permissions
if !c.User.IsAdmin() {
return ErrPermissionDenied.Here()
}
// Handle the request
data := newGroupPageData{User: c.User, CSRFField: csrf.TemplateField(r)}
Render(w, "group_new.html", data)
return nil
}
// NewGroupPost is a sub-handler that processes the Group creation form.
func NewGroupPost(c *Context, w http.ResponseWriter, r *http.Request) error {
// Check permissions
if !c.User.IsAdmin() {
return ErrPermissionDenied.Here()
}
// Handle the request
data := newGroupPageData{User: c.User, CSRFField: csrf.TemplateField(r)}
form := newFormNewGroup(r)
err := user.AddGroup(c.Tx, form.Name, form.Description)
if err != nil {
data.Form = form // Show current form values along with error
//data.ErrorMessage = merry.UserMessage(err)
data.ErrorMessage = merry.Details(err)
Render(w, "group_new.html", data)
return nil
}
// New group created successfully, redirect them to the list page?
msg := fmt.Sprintf("Group %s successfully created.", form.Name)
c.AddNormalFlash(msg)
http.Redirect(w, r, "/groups", 302)
return nil
}