statping/handlers/users.go

101 lines
2.6 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"
2020-02-25 13:18:29 +00:00
"github.com/hunterlong/statping/database"
2018-12-04 04:17:29 +00:00
"github.com/hunterlong/statping/types"
"github.com/hunterlong/statping/utils"
2018-06-30 00:57:05 +00:00
"net/http"
)
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)
2020-03-03 06:42:37 +00:00
users := database.AllUsers()
if len(users) == 1 {
2018-11-13 19:28:21 +00:00
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) {
2020-02-25 17:39:38 +00:00
users := core.SelectAllUsers()
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
}
2020-02-25 13:18:29 +00:00
_, err = database.Create(user)
2018-11-13 19:28:21 +00:00
if err != nil {
sendErrorJson(err, w, r)
return
}
2020-02-25 13:18:29 +00:00
sendJsonAction(user, "create", w, r)
2018-06-30 00:57:05 +00:00
}