statping/handlers/users.go

118 lines
3.0 KiB
Go
Raw Normal View History

2018-12-04 05:57:11 +00:00
// Statping
2018-08-16 06:22:20 +00:00
// Copyright (C) 2018. Hunter Long and the project contributors
// Written by Hunter Long <info@socialeck.com> and the project contributors
//
2018-12-04 04:17:29 +00:00
// https://github.com/hunterlong/statping
2018-08-16 06:22:20 +00:00
//
// 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 (
2018-11-13 19:28:21 +00:00
"encoding/json"
"errors"
2018-11-17 17:14:14 +00:00
"fmt"
2018-06-30 00:57:05 +00:00
"github.com/gorilla/mux"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/core"
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
2018-06-30 00:57:05 +00:00
"net/http"
"strconv"
)
func usersHandler(w http.ResponseWriter, r *http.Request) {
2018-06-30 00:57:05 +00:00
users, _ := core.SelectAllUsers()
2018-12-06 19:03:55 +00:00
ExecuteResponse(w, r, "users.gohtml", 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
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
user, _ := core.SelectUser(int64(id))
2018-12-06 19:03:55 +00:00
ExecuteResponse(w, r, "user.gohtml", user, nil)
2018-07-08 21:06:10 +00:00
}
2018-11-13 19:28:21 +00:00
func apiUserHandler(w http.ResponseWriter, r *http.Request) {
2018-07-08 21:06:10 +00:00
vars := mux.Vars(r)
user, err := core.SelectUser(utils.ToInt(vars["id"]))
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
}
2018-11-13 19:28:21 +00:00
user.Password = ""
returnJson(user, w, r)
2018-11-13 19:28:21 +00:00
}
2018-07-08 21:06:10 +00:00
2018-11-13 19:28:21 +00:00
func apiUserUpdateHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
user, err := core.SelectUser(utils.ToInt(vars["id"]))
2018-11-13 19:28:21 +00:00
if err != nil {
2018-11-17 17:14:14 +00:00
sendErrorJson(fmt.Errorf("user #%v was not found", vars["id"]), w, r)
2018-11-13 19:28:21 +00:00
return
2018-07-08 21:06:10 +00:00
}
2018-11-13 19:28:21 +00:00
decoder := json.NewDecoder(r.Body)
2018-11-17 17:14:14 +00:00
decoder.Decode(&user)
if user.Password != "" {
user.Password = utils.HashPassword(user.Password)
}
err = user.Update()
if err != nil {
2018-11-17 17:14:14 +00:00
sendErrorJson(fmt.Errorf("issue updating user #%v: %v", user.Id, err), w, r)
2018-11-13 19:28:21 +00:00
return
}
2018-11-17 17:14:14 +00:00
sendJsonAction(user, "update", w, r)
2018-07-08 21:06:10 +00:00
}
2018-11-13 19:28:21 +00:00
func apiUserDeleteHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
users := core.CountUsers()
if users == 1 {
sendErrorJson(errors.New("cannot delete the last user"), w, r)
return
}
user, err := core.SelectUser(utils.ToInt(vars["id"]))
2018-11-13 19:28:21 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
err = user.Delete()
2018-06-30 00:57:05 +00:00
if err != nil {
2018-11-13 19:28:21 +00:00
sendErrorJson(err, w, r)
return
}
2018-11-17 17:14:14 +00:00
sendJsonAction(user, "delete", w, r)
2018-06-30 00:57:05 +00:00
}
2018-11-13 19:28:21 +00:00
func apiAllUsersHandler(w http.ResponseWriter, r *http.Request) {
users, err := core.SelectAllUsers()
if err != nil {
sendErrorJson(err, w, r)
return
}
returnJson(users, w, r)
2018-11-13 19:28:21 +00:00
}
2018-06-30 00:57:05 +00:00
2018-11-13 19:28:21 +00:00
func apiCreateUsersHandler(w http.ResponseWriter, r *http.Request) {
var user *types.User
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&user)
if err != nil {
sendErrorJson(err, w, r)
2018-06-30 00:57:05 +00:00
return
}
2018-11-13 19:28:21 +00:00
newUser := core.ReturnUser(user)
2018-11-17 17:14:14 +00:00
_, err = newUser.Create()
2018-11-13 19:28:21 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
2018-11-17 17:14:14 +00:00
sendJsonAction(newUser, "create", w, r)
2018-06-30 00:57:05 +00:00
}