statping/handlers/users.go

112 lines
3.0 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 (
"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)
}
2018-07-08 21:06:10 +00:00
func UsersEditHandler(w http.ResponseWriter, r *http.Request) {
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)
}
func UpdateUserHandler(w http.ResponseWriter, r *http.Request) {
if !IsAuthenticated(r) {
http.Redirect(w, r, "/", http.StatusSeeOther)
return
}
r.ParseForm()
vars := mux.Vars(r)
id, _ := strconv.Atoi(vars["id"])
user, _ := core.SelectUser(int64(id))
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)
}
2018-07-14 02:37:39 +00:00
core.UpdateUser(user)
2018-07-08 21:06:10 +00:00
users, _ := core.SelectAllUsers()
ExecuteResponse(w, r, "users.html", users)
}
2018-06-30 00:57:05 +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")
2018-07-14 02:37:39 +00:00
user := &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"),
2018-06-30 00:57:05 +00:00
}
2018-07-14 02:37:39 +00:00
_, err := core.CreateUser(user)
2018-06-30 00:57:05 +00:00
if err != nil {
utils.Log(2, err)
}
core.OnNewUser(user)
2018-06-30 00:57:05 +00:00
http.Redirect(w, r, "/users", http.StatusSeeOther)
}
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
}
2018-07-14 02:37:39 +00:00
core.DeleteUser(user)
2018-06-30 00:57:05 +00:00
http.Redirect(w, r, "/users", http.StatusSeeOther)
}