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

View file

@ -31,4 +31,32 @@
</tbody>
</table>
<h4>Add user:</h4>
<form class="form-horizontal" method="POST" action="/admin/users/add/">
<div class="control-group">
<label class="control-label" for="username">Username</label>
<div class="controls">
<input type="text" id="username" name="username">
</div>
</div>
<div class="control-group">
<label class="control-label" for="role">Role</label>
<div class="controls">
<input type="text" id="role" name="role" value="moderator">
</div>
</div>
<div class="control-group">
<label class="control-label" for="password">Password</label>
<div class="controls">
<input type="password" id="password" name="password">
</div>
</div>
<div class="form-actions">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
{{template "footer.html"}}