Admin is now able to add users

This commit is contained in:
Las Zenow 2018-04-08 11:11:27 +00:00
parent 6f906ccae4
commit 240a8a0430
3 changed files with 53 additions and 0 deletions

View file

@ -198,6 +198,7 @@ func InitRouter(db database.DB, sg *StatsGatherer, assetsPath string) http.Handl
r.HandleFunc("/delete/{ids:(?:"+idPattern+"/)+}", sg.Gather(deleteHandler))
r.HandleFunc("/admin/users/", sg.Gather(userAdminHandler)).Methods("GET")
r.HandleFunc("/admin/users/", sg.Gather(userAdminPostHandler)).Methods("POST")
r.HandleFunc("/admin/users/add/", sg.Gather(addUserHandler)).Methods("POST")
r.HandleFunc("/news/", sg.Gather(newsHandler))
r.HandleFunc("/news/edit", sg.Gather(editNewsHandler)).Methods("GET")

View file

@ -145,6 +145,30 @@ func userAdminPostHandler(h handler) {
userAdminHandler(h)
}
func addUserHandler(h handler) {
if !h.sess.IsAdmin() {
notFound(h)
return
}
username := h.r.FormValue("username")
password := h.r.FormValue("password")
role := h.r.FormValue("role")
err := h.db.AddUser(username, password)
if err != nil {
h.sess.Notify("An error ocurred!", err.Error(), "error")
} else {
err := h.db.SetRole(username, role)
if err != nil {
h.sess.Notify("An error ocurred!", err.Error(), "error")
} else {
h.sess.Notify("User created!", "", "success")
}
}
userAdminHandler(h)
}
type userAdminData struct {
S Status
Users []database.User