From 240a8a0430e38190eb2fa508a0189da59482d60c Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Sun, 8 Apr 2018 11:11:27 +0000 Subject: [PATCH] Admin is now able to add users --- lib/trantor.go | 1 + lib/user.go | 24 ++++++++++++++++++++++++ templates/user_admin.html | 28 ++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+) diff --git a/lib/trantor.go b/lib/trantor.go index 0c5d5c0..6d4ab2f 100644 --- a/lib/trantor.go +++ b/lib/trantor.go @@ -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") diff --git a/lib/user.go b/lib/user.go index f86e5d0..2f8fde2 100644 --- a/lib/user.go +++ b/lib/user.go @@ -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 diff --git a/templates/user_admin.html b/templates/user_admin.html index b92fd8b..61c37e7 100644 --- a/templates/user_admin.html +++ b/templates/user_admin.html @@ -31,4 +31,32 @@ +

Add user:

+
+
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+ +
+
+ +
+ +
+
+ {{template "footer.html"}}