statping/handlers/users.go

118 lines
3.2 KiB
Go
Raw Normal View History

2018-08-16 06:22:20 +00:00
// Statup
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
// https://github.com/hunterlong/statup
//
// The licenses for most software and other practical works are designed
// to take away your freedom to share and change the works. By contrast,
// the GNU General Public License is intended to guarantee your freedom to
// share and change all versions of a program--to make sure it remains free
// software for all its users.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2018-06-30 00:57:05 +00:00
package handlers
import (
"fmt"
2018-06-30 00:57:05 +00:00
"github.com/gorilla/mux"
"github.com/hunterlong/statup/core"
"github.com/hunterlong/statup/types"
"github.com/hunterlong/statup/utils"
"net/http"
"strconv"
)
func usersHandler(w http.ResponseWriter, r *http.Request) {
2018-07-08 21:06:10 +00:00
if !IsAuthenticated(r) {
2018-06-30 00:57:05 +00:00
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
users, _ := core.SelectAllUsers()
executeResponse(w, r, "users.html", users, nil)
2018-06-30 00:57:05 +00:00
}
func usersEditHandler(w http.ResponseWriter, r *http.Request) {
2018-07-08 21:06:10 +00:00
if !IsAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
user, _ := core.SelectUser(int64(id))
executeResponse(w, r, "user.html", user, nil)
2018-07-08 21:06:10 +00:00
}
func updateUserHandler(w http.ResponseWriter, r *http.Request) {
2018-07-08 21:06:10 +00:00
if !IsAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
r.ParseForm()
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
user, err := core.SelectUser(int64(id))
if err != nil {
utils.Log(3, fmt.Sprintf("user error: %v", err))
w.WriteHeader(http.StatusInternalServerError)
return
}
2018-07-08 21:06:10 +00:00
user.Username = r.PostForm.Get("username")
user.Email = r.PostForm.Get("email")
user.Admin = (r.PostForm.Get("admin") == "on")
password := r.PostForm.Get("password")
if password != "##########" {
user.Password = utils.HashPassword(password)
}
user.Update()
2018-07-08 21:06:10 +00:00
users, _ := core.SelectAllUsers()
executeResponse(w, r, "users.html", users, "/users")
2018-07-08 21:06:10 +00:00
}
func createUserHandler(w http.ResponseWriter, r *http.Request) {
2018-07-08 21:06:10 +00:00
if !IsAuthenticated(r) {
2018-06-30 00:57:05 +00:00
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
r.ParseForm()
username := r.PostForm.Get("username")
password := r.PostForm.Get("password")
email := r.PostForm.Get("email")
2018-07-02 06:21:41 +00:00
admin := r.PostForm.Get("admin")
user := core.ReturnUser(&types.User{
2018-06-30 00:57:05 +00:00
Username: username,
Password: password,
Email: email,
2018-07-02 06:21:41 +00:00
Admin: (admin == "on"),
})
_, err := user.Create()
2018-06-30 00:57:05 +00:00
if err != nil {
utils.Log(3, err)
2018-06-30 00:57:05 +00:00
}
//notifiers.OnNewUser(user)
executeResponse(w, r, "users.html", user, "/users")
2018-06-30 00:57:05 +00:00
}
func usersDeleteHandler(w http.ResponseWriter, r *http.Request) {
2018-07-08 21:06:10 +00:00
if !IsAuthenticated(r) {
2018-06-30 00:57:05 +00:00
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
user, _ := core.SelectUser(int64(id))
users, _ := core.SelectAllUsers()
if len(users) == 1 {
2018-07-28 01:50:13 +00:00
utils.Log(2, "cannot delete the only user in the system")
2018-06-30 00:57:05 +00:00
http.Redirect(w, r, "/users", http.StatusSeeOther)
return
}
user.Delete()
2018-06-30 00:57:05 +00:00
http.Redirect(w, r, "/users", http.StatusSeeOther)
}