2023-09-15 13:06:31 +01:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"github.com/1f349/tulip/database"
|
2024-03-11 12:39:52 +00:00
|
|
|
"github.com/1f349/tulip/database/types"
|
2024-05-13 20:06:17 +01:00
|
|
|
"github.com/1f349/tulip/logger"
|
2023-09-15 13:06:31 +01:00
|
|
|
"github.com/1f349/tulip/pages"
|
2023-09-24 18:24:16 +01:00
|
|
|
"github.com/emersion/go-message/mail"
|
2023-09-15 13:06:31 +01:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/julienschmidt/httprouter"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strconv"
|
2023-10-10 18:06:43 +01:00
|
|
|
"strings"
|
2023-09-24 18:24:16 +01:00
|
|
|
"time"
|
2023-09-15 13:06:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func (h *HttpServer) ManageUsersGet(rw http.ResponseWriter, req *http.Request, _ httprouter.Params, auth UserAuth) {
|
|
|
|
q := req.URL.Query()
|
2024-07-25 22:16:19 +01:00
|
|
|
offset, _ := strconv.Atoi(q.Get("offset"))
|
2023-09-15 13:06:31 +01:00
|
|
|
|
2024-03-11 12:39:52 +00:00
|
|
|
var role types.UserRole
|
2024-03-12 21:04:25 +00:00
|
|
|
var userList []database.GetUserListRow
|
2024-03-11 12:39:52 +00:00
|
|
|
if h.DbTx(rw, func(tx *database.Queries) (err error) {
|
2024-05-31 14:57:54 +01:00
|
|
|
role, err = tx.GetUserRole(req.Context(), auth.Subject)
|
2023-09-15 13:06:31 +01:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2024-03-12 21:04:25 +00:00
|
|
|
userList, err = tx.GetUserList(req.Context(), int64(offset))
|
2023-09-15 13:06:31 +01:00
|
|
|
return
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
2024-03-12 21:04:25 +00:00
|
|
|
|
2024-03-11 12:39:52 +00:00
|
|
|
if role != types.RoleAdmin {
|
2023-09-15 13:06:31 +01:00
|
|
|
http.Error(rw, "403 Forbidden", http.StatusForbidden)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m := map[string]any{
|
2023-10-10 18:06:43 +01:00
|
|
|
"ServiceName": h.conf.ServiceName,
|
2023-09-15 13:06:31 +01:00
|
|
|
"Users": userList,
|
|
|
|
"Offset": offset,
|
|
|
|
"EmailShow": req.URL.Query().Has("show-email"),
|
2024-05-31 14:57:54 +01:00
|
|
|
"CurrentAdmin": auth.Subject,
|
2023-10-10 18:06:43 +01:00
|
|
|
"Namespace": h.conf.Namespace,
|
2023-09-15 13:06:31 +01:00
|
|
|
}
|
|
|
|
if q.Has("edit") {
|
|
|
|
for _, i := range userList {
|
2024-03-12 21:04:25 +00:00
|
|
|
if i.Subject == q.Get("edit") {
|
2024-06-02 12:23:22 +01:00
|
|
|
m["EditUser"] = i
|
|
|
|
rw.Header().Set("Content-Type", "text/html")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
pages.RenderPageTemplate(rw, "manage-users-edit", m)
|
|
|
|
return
|
2023-09-15 13:06:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
http.Error(rw, "400 Bad Request: Invalid user to edit", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.Header().Set("Content-Type", "text/html")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
pages.RenderPageTemplate(rw, "manage-users", m)
|
|
|
|
}
|
|
|
|
|
2024-06-02 12:23:22 +01:00
|
|
|
func (h *HttpServer) ManageUsersCreateGet(rw http.ResponseWriter, req *http.Request, _ httprouter.Params, auth UserAuth) {
|
|
|
|
var roles types.UserRole
|
|
|
|
if h.DbTx(rw, func(tx *database.Queries) (err error) {
|
|
|
|
roles, err = tx.GetUserRole(req.Context(), auth.Subject)
|
|
|
|
return
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
m := map[string]any{
|
|
|
|
"ServiceName": h.conf.ServiceName,
|
|
|
|
"IsAdmin": roles == types.RoleAdmin,
|
|
|
|
"Namespace": h.conf.Namespace,
|
|
|
|
}
|
|
|
|
|
|
|
|
rw.Header().Set("Content-Type", "text/html")
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
pages.RenderPageTemplate(rw, "manage-users-create", m)
|
|
|
|
}
|
|
|
|
|
2023-09-15 13:06:31 +01:00
|
|
|
func (h *HttpServer) ManageUsersPost(rw http.ResponseWriter, req *http.Request, _ httprouter.Params, auth UserAuth) {
|
|
|
|
err := req.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "400 Bad Request: Failed to parse form", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-11 12:39:52 +00:00
|
|
|
var role types.UserRole
|
|
|
|
if h.DbTx(rw, func(tx *database.Queries) (err error) {
|
2024-05-31 14:57:54 +01:00
|
|
|
role, err = tx.GetUserRole(req.Context(), auth.Subject)
|
2023-09-15 13:06:31 +01:00
|
|
|
return
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
2024-03-11 12:39:52 +00:00
|
|
|
if role != types.RoleAdmin {
|
2023-10-15 13:49:58 +01:00
|
|
|
http.Error(rw, "400 Bad Request: Only admin users can manage users", http.StatusBadRequest)
|
2023-09-15 13:06:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
offset := req.Form.Get("offset")
|
|
|
|
action := req.Form.Get("action")
|
|
|
|
name := req.Form.Get("name")
|
|
|
|
username := req.Form.Get("username")
|
|
|
|
email := req.Form.Get("email")
|
|
|
|
newRole, err := parseRoleValue(req.Form.Get("role"))
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "400 Bad Request: Invalid role", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
active := req.Form.Has("active")
|
|
|
|
|
|
|
|
switch action {
|
|
|
|
case "create":
|
2023-09-24 18:24:16 +01:00
|
|
|
// parse email for headers
|
|
|
|
address, err := mail.ParseAddress(email)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(rw, "500 Internal Server Error: Failed to parse user email address", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-10-10 18:06:43 +01:00
|
|
|
n := strings.IndexByte(address.Address, '@')
|
|
|
|
// This case should never happen and fail the above address parsing
|
|
|
|
if n == -1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
addrDomain := address.Address[n+1:]
|
|
|
|
|
2024-03-12 21:04:25 +00:00
|
|
|
var userSub string
|
2024-03-11 12:39:52 +00:00
|
|
|
if h.DbTx(rw, func(tx *database.Queries) (err error) {
|
2024-03-12 21:04:25 +00:00
|
|
|
userSub, err = tx.AddUser(req.Context(), database.AddUserParams{
|
|
|
|
Name: name,
|
|
|
|
Username: username,
|
|
|
|
Password: "",
|
|
|
|
Email: email,
|
|
|
|
EmailVerified: addrDomain == h.conf.Namespace,
|
|
|
|
Role: newRole,
|
|
|
|
UpdatedAt: time.Now(),
|
|
|
|
Active: active,
|
|
|
|
})
|
2023-10-10 18:06:43 +01:00
|
|
|
return err
|
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
2023-09-24 18:24:16 +01:00
|
|
|
|
2024-02-09 15:24:40 +00:00
|
|
|
u, u2 := uuid.NewString(), uuid.NewString()
|
2024-03-12 21:04:25 +00:00
|
|
|
h.mailLinkCache.Set(mailLinkKey{mailLinkResetPassword, u}, userSub, time.Now().Add(10*time.Minute))
|
|
|
|
h.mailLinkCache.Set(mailLinkKey{mailLinkDelete, u2}, userSub, time.Now().Add(10*time.Minute))
|
2023-09-24 18:24:16 +01:00
|
|
|
|
2023-10-10 18:06:43 +01:00
|
|
|
err = h.conf.Mail.SendEmailTemplate("mail-register-admin", "Register", name, address, map[string]any{
|
2024-02-09 15:24:40 +00:00
|
|
|
"RegisterUrl": h.conf.BaseUrl + "/mail/password/" + u,
|
2023-09-24 18:24:16 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
2024-05-13 20:06:17 +01:00
|
|
|
logger.Logger.Warn("Login: Failed to send register email:", "err", err)
|
2023-09-24 18:24:16 +01:00
|
|
|
http.Error(rw, "500 Internal Server Error: Failed to send register email", http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-09-15 13:06:31 +01:00
|
|
|
case "edit":
|
2024-03-11 12:39:52 +00:00
|
|
|
if h.DbTx(rw, func(tx *database.Queries) error {
|
2024-02-09 15:24:40 +00:00
|
|
|
sub := req.Form.Get("subject")
|
2024-03-12 21:04:25 +00:00
|
|
|
return tx.UpdateUserRole(req.Context(), database.UpdateUserRoleParams{
|
|
|
|
Active: active,
|
|
|
|
Role: newRole,
|
|
|
|
Subject: sub,
|
|
|
|
})
|
2023-09-15 13:06:31 +01:00
|
|
|
}) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
http.Error(rw, "400 Bad Request: Invalid action", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
redirectUrl := url.URL{Path: "/manage/users", RawQuery: url.Values{"offset": []string{offset}}.Encode()}
|
|
|
|
http.Redirect(rw, req, redirectUrl.String(), http.StatusFound)
|
|
|
|
}
|
|
|
|
|
2024-03-11 12:39:52 +00:00
|
|
|
func parseRoleValue(role string) (types.UserRole, error) {
|
2023-09-15 13:06:31 +01:00
|
|
|
switch role {
|
|
|
|
case "member":
|
2024-03-11 12:39:52 +00:00
|
|
|
return types.RoleMember, nil
|
2023-09-15 13:06:31 +01:00
|
|
|
case "admin":
|
2024-03-11 12:39:52 +00:00
|
|
|
return types.RoleAdmin, nil
|
2023-09-15 13:06:31 +01:00
|
|
|
}
|
|
|
|
return 0, errors.New("invalid role value")
|
|
|
|
}
|