2023-05-22 03:22:58 +02:00
|
|
|
// Copyright 2023 wanderer <a_mirre at utb dot cz>
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-05-29 22:42:18 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2023-05-22 03:22:58 +02:00
|
|
|
"net/http"
|
2023-06-02 20:00:14 +02:00
|
|
|
"strings"
|
2023-05-22 03:22:58 +02:00
|
|
|
|
|
|
|
"git.dotya.ml/mirre-mt/pcmt/ent"
|
|
|
|
moduser "git.dotya.ml/mirre-mt/pcmt/modules/user"
|
2023-05-30 23:50:37 +02:00
|
|
|
"github.com/gorilla/sessions"
|
2023-05-22 03:22:58 +02:00
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
)
|
|
|
|
|
2023-05-30 23:50:37 +02:00
|
|
|
func ManageUsers() echo.HandlerFunc {
|
2023-05-22 03:22:58 +02:00
|
|
|
return func(c echo.Context) error {
|
|
|
|
addHeaders(c)
|
|
|
|
|
2023-05-30 23:50:37 +02:00
|
|
|
u, ok := c.Get("sessUsr").(moduser.User)
|
|
|
|
if !ok {
|
2023-05-22 03:22:58 +02:00
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusUnauthorized,
|
|
|
|
http.StatusText(http.StatusUnauthorized),
|
2023-05-30 23:50:37 +02:00
|
|
|
"it appears there is no user",
|
2023-05-22 03:22:58 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-30 23:50:37 +02:00
|
|
|
sess, ok := c.Get("sess").(*sessions.Session)
|
|
|
|
if !ok {
|
2023-05-22 03:22:58 +02:00
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
2023-05-30 23:50:37 +02:00
|
|
|
"missing the session",
|
2023-05-22 03:22:58 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-30 23:50:37 +02:00
|
|
|
ctx, ok := c.Get("sloggerCtx").(context.Context)
|
|
|
|
if !ok {
|
|
|
|
ctx = context.WithValue(context.Background(), moduser.CtxKey{}, slogger)
|
|
|
|
}
|
|
|
|
|
2023-05-22 03:22:58 +02:00
|
|
|
if !u.IsAdmin {
|
|
|
|
c.Logger().Debug("this is a restricted endpoint")
|
|
|
|
|
|
|
|
status := http.StatusUnauthorized
|
|
|
|
msg := http.StatusText(status)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c, status, msg+": You should not be here", "Restricted endpoint",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-22 06:47:33 +02:00
|
|
|
data := make(map[string]any)
|
|
|
|
flash := sess.Values["flash"]
|
|
|
|
path := c.Request().URL.Path
|
|
|
|
|
|
|
|
if path == "/manage/users/new" {
|
2023-08-15 18:33:48 +02:00
|
|
|
p := newPage()
|
|
|
|
|
|
|
|
p.Title = "Manage Users - New User"
|
|
|
|
p.Current = "manage-users-new"
|
|
|
|
p.CSRF = c.Get("csrf").(string)
|
|
|
|
p.User = u
|
2023-05-22 06:47:33 +02:00
|
|
|
|
|
|
|
if flash != nil {
|
|
|
|
data["flash"] = flash.(string)
|
|
|
|
|
|
|
|
delete(sess.Values, "flash")
|
|
|
|
|
|
|
|
_ = sess.Save(c.Request(), c.Response())
|
|
|
|
}
|
|
|
|
|
|
|
|
err := c.Render(
|
|
|
|
http.StatusOK,
|
|
|
|
"manage/user-new.tmpl",
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-22 03:22:58 +02:00
|
|
|
var allUsers []*moduser.User
|
|
|
|
|
2023-05-30 23:50:37 +02:00
|
|
|
if users, err := moduser.ListAll(ctx, dbclient); err == nil && users != nil {
|
2023-05-22 03:22:58 +02:00
|
|
|
for _, u := range users {
|
|
|
|
usr := &moduser.User{
|
|
|
|
Username: u.Username,
|
|
|
|
Email: u.Email,
|
|
|
|
ID: u.ID,
|
|
|
|
IsActive: u.IsActive,
|
|
|
|
IsAdmin: u.IsAdmin,
|
|
|
|
CreatedAt: u.CreatedAt,
|
|
|
|
UpdatedAt: u.UpdatedAt,
|
2023-09-04 11:33:50 +02:00
|
|
|
LastLogin: u.LastLogin,
|
2023-05-22 03:22:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
allUsers = append(allUsers, usr)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.Logger().Error(http.StatusText(http.StatusInternalServerError) + " - " + err.Error())
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
data["allusers"] = allUsers
|
|
|
|
|
2023-08-15 18:33:48 +02:00
|
|
|
p := newPage()
|
|
|
|
|
|
|
|
p.Title = "Manage Users"
|
|
|
|
p.Current = "manage-users"
|
|
|
|
p.CSRF = c.Get("csrf").(string)
|
|
|
|
p.User = u
|
|
|
|
p.Data = data
|
2023-05-22 06:47:33 +02:00
|
|
|
|
|
|
|
if flash != nil {
|
|
|
|
data["flash"] = flash.(string)
|
|
|
|
|
|
|
|
delete(sess.Values, "flash")
|
|
|
|
|
|
|
|
_ = sess.Save(c.Request(), c.Response())
|
|
|
|
}
|
|
|
|
|
2023-05-22 03:22:58 +02:00
|
|
|
err := c.Render(
|
|
|
|
http.StatusOK,
|
|
|
|
"manage/user.tmpl",
|
2023-05-22 06:47:33 +02:00
|
|
|
p,
|
2023-05-22 03:22:58 +02:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2023-05-22 06:47:33 +02:00
|
|
|
|
2023-05-30 23:50:37 +02:00
|
|
|
func CreateUser() echo.HandlerFunc { //nolint:gocognit
|
2023-05-22 06:47:33 +02:00
|
|
|
return func(c echo.Context) error {
|
|
|
|
addHeaders(c)
|
|
|
|
|
2023-05-30 23:50:37 +02:00
|
|
|
u, ok := c.Get("sessUsr").(moduser.User)
|
|
|
|
if !ok {
|
2023-05-22 06:47:33 +02:00
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusUnauthorized,
|
|
|
|
http.StatusText(http.StatusUnauthorized),
|
2023-05-30 23:50:37 +02:00
|
|
|
"username was nil",
|
2023-05-22 06:47:33 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-05-30 23:50:37 +02:00
|
|
|
ctx, ok := c.Get("sloggerCtx").(context.Context)
|
|
|
|
if !ok {
|
|
|
|
ctx = context.WithValue(context.Background(), moduser.CtxKey{}, slogger)
|
2023-05-22 06:47:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !u.IsAdmin {
|
|
|
|
c.Logger().Debug("this is a restricted endpoint")
|
|
|
|
|
|
|
|
status := http.StatusUnauthorized
|
|
|
|
msg := http.StatusText(status)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c, status, msg+": You should not be here", "Restricted endpoint",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-15 18:33:48 +02:00
|
|
|
p := newPage()
|
|
|
|
|
|
|
|
p.Title = "Manage Users"
|
|
|
|
p.Current = "manage-users"
|
|
|
|
|
2023-05-30 23:50:37 +02:00
|
|
|
data := make(map[string]any)
|
2023-05-22 06:47:33 +02:00
|
|
|
uc := new(userCreate)
|
|
|
|
|
|
|
|
if err := c.Bind(uc); err != nil {
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
http.StatusText(http.StatusBadRequest),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if uc.Username == "" || uc.Password == "" || uc.RepeatPassword == "" || uc.Password != uc.RepeatPassword {
|
|
|
|
c.Logger().Error("username or password not set, returning to /manage/users/new")
|
|
|
|
|
|
|
|
msg := "Error: both username and password need to be set"
|
|
|
|
|
|
|
|
if uc.Password != uc.RepeatPassword {
|
|
|
|
msg += "; password needs to be passed the same twice"
|
|
|
|
}
|
|
|
|
|
|
|
|
data["flash"] = msg
|
|
|
|
data["form"] = uc
|
2023-05-30 23:50:37 +02:00
|
|
|
p.Data = data
|
2023-05-22 06:47:33 +02:00
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusBadRequest,
|
|
|
|
"manage/user-new.tmpl",
|
2023-05-30 23:50:37 +02:00
|
|
|
p,
|
2023-05-22 06:47:33 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var msg string
|
|
|
|
|
2023-05-30 23:50:37 +02:00
|
|
|
usr, err := moduser.CreateUser(ctx, dbclient, uc.Email, uc.Username, uc.Password, uc.IsAdmin)
|
2023-05-22 06:47:33 +02:00
|
|
|
if err == nil && usr != nil {
|
2023-08-24 18:43:24 +02:00
|
|
|
msg = fmt.Sprintf("Successfully created user %q!", usr.Username)
|
2023-05-22 06:47:33 +02:00
|
|
|
|
2023-05-30 23:50:37 +02:00
|
|
|
if sess, ok := c.Get("sess").(*sessions.Session); ok {
|
|
|
|
sess.Values["flash"] = msg
|
|
|
|
_ = sess.Save(c.Request(), c.Response())
|
|
|
|
}
|
2023-05-22 06:47:33 +02:00
|
|
|
|
|
|
|
return c.Redirect(http.StatusSeeOther, "/manage/users")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ent.IsNotSingular(err) {
|
|
|
|
c.Logger().Error("user exists")
|
|
|
|
|
|
|
|
msg = "Error: user already exists: " + err.Error()
|
|
|
|
} else {
|
|
|
|
msg = "Error: " + err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
data["flash"] = msg
|
|
|
|
data["form"] = uc
|
2023-05-30 23:50:37 +02:00
|
|
|
p.Data = data
|
2023-05-22 06:47:33 +02:00
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
"manage/user-new.tmpl",
|
2023-05-30 23:50:37 +02:00
|
|
|
p,
|
2023-05-22 06:47:33 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-05-29 22:42:18 +02:00
|
|
|
|
2023-05-30 23:50:37 +02:00
|
|
|
func ViewUser() echo.HandlerFunc {
|
2023-05-29 22:42:18 +02:00
|
|
|
return func(c echo.Context) error {
|
|
|
|
addHeaders(c)
|
|
|
|
|
2023-05-30 23:50:37 +02:00
|
|
|
u, ok := c.Get("sessUsr").(moduser.User)
|
|
|
|
if !ok {
|
2023-05-29 22:42:18 +02:00
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusUnauthorized,
|
|
|
|
http.StatusText(http.StatusUnauthorized),
|
2023-05-30 23:50:37 +02:00
|
|
|
"username was nil",
|
2023-05-29 22:42:18 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !u.IsAdmin {
|
|
|
|
c.Logger().Debug("this is a restricted endpoint")
|
|
|
|
|
|
|
|
status := http.StatusUnauthorized
|
|
|
|
msg := http.StatusText(status)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c, status, msg+": You should not be here", "Restricted endpoint",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-24 18:43:24 +02:00
|
|
|
sess, ok := c.Get("sess").(*sessions.Session)
|
|
|
|
if !ok {
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
"missing the session",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-06-04 11:36:07 +02:00
|
|
|
ctx, ok := c.Get("sloggerCtx").(context.Context)
|
|
|
|
if !ok {
|
|
|
|
ctx = context.WithValue(context.Background(), moduser.CtxKey{}, slogger)
|
|
|
|
}
|
|
|
|
|
2023-08-15 18:33:48 +02:00
|
|
|
p := newPage()
|
|
|
|
|
|
|
|
p.Title = "Manage Users - User Details"
|
|
|
|
p.Current = "manage-users-user-details"
|
|
|
|
p.User = u
|
|
|
|
|
2023-05-29 22:42:18 +02:00
|
|
|
data := make(map[string]any)
|
|
|
|
uid := new(userID)
|
|
|
|
|
|
|
|
err := c.Bind(uid)
|
2023-06-02 20:00:14 +02:00
|
|
|
if err == nil { //nolint:dupl
|
2023-05-30 23:50:37 +02:00
|
|
|
usr, err := getUserByID(ctx, dbclient, uid.ID)
|
2023-05-29 22:42:18 +02:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, moduser.ErrUserNotFound) { //nolint:gocritic
|
|
|
|
c.Logger().Errorf("user not found by ID: '%s'", uid.ID)
|
|
|
|
|
2023-08-24 18:43:24 +02:00
|
|
|
data["flash"] = fmt.Sprintf("No user found with UUID: %q", uid.ID)
|
2023-05-30 21:08:34 +02:00
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
2023-05-29 22:42:18 +02:00
|
|
|
http.StatusNotFound,
|
2023-05-30 21:08:34 +02:00
|
|
|
"manage/user-details.tmpl",
|
|
|
|
p,
|
2023-05-29 22:42:18 +02:00
|
|
|
)
|
|
|
|
} else if errors.Is(err, moduser.ErrFailedToQueryUser) {
|
|
|
|
c.Logger().Errorf("failed to query user by ID: '%s'", uid.ID)
|
|
|
|
|
2023-08-24 18:43:24 +02:00
|
|
|
data["flash"] = fmt.Sprintf("Error: failed to query user by UUID %q", uid.ID)
|
2023-05-30 21:08:34 +02:00
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
2023-05-29 22:42:18 +02:00
|
|
|
http.StatusInternalServerError,
|
2023-05-30 21:08:34 +02:00
|
|
|
"manage/user-details.tmpl",
|
|
|
|
p,
|
2023-05-29 22:42:18 +02:00
|
|
|
)
|
|
|
|
} else if errors.Is(err, moduser.ErrBadUUID) {
|
2023-05-30 21:08:34 +02:00
|
|
|
c.Logger().Errorf("Invalid UUID '%s': %q", uid.ID, err)
|
2023-05-29 22:42:18 +02:00
|
|
|
|
2023-08-24 18:43:24 +02:00
|
|
|
data["flash"] = fmt.Sprintf("Error: invalid UUID %q", uid.ID)
|
2023-05-29 22:42:18 +02:00
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
2023-05-30 21:08:34 +02:00
|
|
|
http.StatusBadRequest,
|
2023-05-29 22:42:18 +02:00
|
|
|
"manage/user-details.tmpl",
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Logger().Errorf("UUID-related issue for UUID '%s': %q", uid.ID, err)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
data["user"] = usr
|
2023-08-24 18:43:24 +02:00
|
|
|
|
|
|
|
flash := sess.Values["flash"]
|
|
|
|
if flash != nil {
|
|
|
|
data["flash"] = flash.(string)
|
|
|
|
|
|
|
|
delete(sess.Values, "flash")
|
|
|
|
|
|
|
|
_ = sess.Save(c.Request(), c.Response())
|
|
|
|
}
|
|
|
|
|
2023-05-29 22:42:18 +02:00
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusOK,
|
|
|
|
"manage/user-details.tmpl",
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
http.StatusText(http.StatusBadRequest),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-06-02 20:00:14 +02:00
|
|
|
|
|
|
|
//nolint:dupl
|
|
|
|
func EditUser() echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
addHeaders(c)
|
|
|
|
|
|
|
|
u, ok := c.Get("sessUsr").(moduser.User)
|
|
|
|
if !ok {
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusUnauthorized,
|
|
|
|
http.StatusText(http.StatusUnauthorized),
|
|
|
|
"username was nil",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !u.IsAdmin {
|
|
|
|
c.Logger().Debug("this is a restricted endpoint")
|
|
|
|
|
|
|
|
status := http.StatusUnauthorized
|
|
|
|
msg := http.StatusText(status)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c, status, msg+": You should not be here", "Restricted endpoint",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, ok := c.Get("sloggerCtx").(context.Context)
|
|
|
|
if !ok {
|
|
|
|
ctx = context.WithValue(context.Background(), moduser.CtxKey{}, slogger)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl := "manage/user-edit.tmpl"
|
|
|
|
data := make(map[string]any)
|
|
|
|
id := strings.TrimPrefix(strings.TrimSuffix(c.Request().URL.Path, "/edit"), "/manage/users/")
|
2023-08-15 18:33:48 +02:00
|
|
|
p := newPage()
|
|
|
|
|
|
|
|
p.Title = "Manage Users - Edit User"
|
|
|
|
p.Current = "manage-users-edit-user"
|
|
|
|
p.User = u
|
2023-06-02 20:00:14 +02:00
|
|
|
|
|
|
|
usr, err := getUserByID(ctx, dbclient, id)
|
|
|
|
if err != nil {
|
|
|
|
//nolint:dupl
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, moduser.ErrUserNotFound):
|
|
|
|
c.Logger().Errorf("user not found by ID: '%s'", id)
|
|
|
|
|
|
|
|
data["flash"] = fmt.Sprintf("No user found with the UUID: %q", id)
|
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusNotFound,
|
|
|
|
tmpl,
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
|
|
|
|
case errors.Is(err, moduser.ErrFailedToQueryUser):
|
|
|
|
c.Logger().Errorf("failed to query user by ID: '%s'", id)
|
|
|
|
|
|
|
|
data["flash"] = fmt.Sprintf("failed to query user by UUID %q", id)
|
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
tmpl,
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
|
|
|
|
case errors.Is(err, moduser.ErrBadUUID):
|
|
|
|
c.Logger().Errorf("Invalid UUID '%s': %q", id, err)
|
|
|
|
|
|
|
|
data["flash"] = fmt.Sprintf("invalid UUID %q", id)
|
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusBadRequest,
|
|
|
|
"manage/user-details.tmpl",
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Logger().Errorf("UUID-related issue for UUID '%s': %q", id, err)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
data["user"] = usr
|
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusOK,
|
|
|
|
tmpl,
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//nolint:dupl
|
|
|
|
func UpdateUser() echo.HandlerFunc { //nolint:gocognit
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
addHeaders(c)
|
|
|
|
|
|
|
|
u, ok := c.Get("sessUsr").(moduser.User)
|
|
|
|
if !ok {
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusUnauthorized,
|
|
|
|
http.StatusText(http.StatusUnauthorized),
|
|
|
|
"username was nil",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !u.IsAdmin {
|
|
|
|
c.Logger().Debug("this is a restricted endpoint")
|
|
|
|
|
|
|
|
status := http.StatusUnauthorized
|
|
|
|
msg := http.StatusText(status)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c, status, msg+": You should not be here", "Restricted endpoint",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, ok := c.Get("sloggerCtx").(context.Context)
|
|
|
|
if !ok {
|
|
|
|
ctx = context.WithValue(context.Background(), moduser.CtxKey{}, slogger)
|
|
|
|
}
|
|
|
|
|
|
|
|
tmpl := "manage/user-edit.tmpl"
|
|
|
|
data := make(map[string]any)
|
|
|
|
id := strings.TrimPrefix(strings.TrimSuffix(c.Request().URL.Path, "/update"), "/manage/users/")
|
2023-08-15 18:33:48 +02:00
|
|
|
p := newPage()
|
|
|
|
|
|
|
|
p.Title = "Manage Users - Edit User"
|
|
|
|
p.Current = "manage-users-edit-user"
|
|
|
|
p.User = u
|
2023-06-02 20:00:14 +02:00
|
|
|
|
|
|
|
usr, err := getUserByID(ctx, dbclient, id)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, moduser.ErrUserNotFound):
|
|
|
|
c.Logger().Errorf("user not found by ID: '%s'", id)
|
|
|
|
|
|
|
|
data["flash"] = fmt.Sprintf("No user found with the UUID: %q", id)
|
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusNotFound,
|
|
|
|
tmpl,
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
|
|
|
|
case errors.Is(err, moduser.ErrFailedToQueryUser):
|
|
|
|
c.Logger().Errorf("failed to query user by ID: '%s'", id)
|
|
|
|
|
|
|
|
data["flash"] = fmt.Sprintf("failed to query user by UUID %q", id)
|
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
tmpl,
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
|
|
|
|
case errors.Is(err, moduser.ErrBadUUID):
|
|
|
|
c.Logger().Errorf("Invalid UUID '%s': %q", id, err)
|
|
|
|
|
|
|
|
data["flash"] = fmt.Sprintf("invalid UUID %q", id)
|
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusBadRequest,
|
|
|
|
"manage/user-details.tmpl",
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Logger().Errorf("UUID-related issue for UUID '%s': %q", id, err)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
uu := new(userCreate)
|
|
|
|
|
|
|
|
if err := c.Bind(uu); err != nil {
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
http.StatusText(http.StatusBadRequest),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-08-24 18:43:24 +02:00
|
|
|
f, err := moduser.UsrFinishedSetup(ctx, dbclient, usr.ID)
|
|
|
|
if err != nil {
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
2023-06-02 20:00:14 +02:00
|
|
|
|
2023-08-24 18:43:24 +02:00
|
|
|
if f {
|
|
|
|
if uu.Username == "" {
|
|
|
|
c.Logger().Error("username not set, returning to /manage/users/edit")
|
|
|
|
|
|
|
|
msg := "Error: the username needs to be set"
|
|
|
|
|
|
|
|
data["flash"] = msg
|
|
|
|
data["form"] = uu
|
|
|
|
p.Data = data
|
2023-06-02 20:00:14 +02:00
|
|
|
|
2023-08-24 18:43:24 +02:00
|
|
|
return c.Render(
|
|
|
|
http.StatusBadRequest,
|
|
|
|
"manage/user-edit.tmpl",
|
|
|
|
p,
|
|
|
|
)
|
2023-06-02 20:00:14 +02:00
|
|
|
}
|
2023-08-24 18:43:24 +02:00
|
|
|
} else {
|
|
|
|
if uu.Username == "" || uu.Password == "" || uu.RepeatPassword == "" || uu.Password != uu.RepeatPassword {
|
|
|
|
c.Logger().Error("username or password not set, returning to /manage/users/edit")
|
2023-06-02 20:00:14 +02:00
|
|
|
|
2023-08-24 18:43:24 +02:00
|
|
|
msg := "Error: both username and password need to be set"
|
2023-06-02 20:00:14 +02:00
|
|
|
|
2023-08-24 18:43:24 +02:00
|
|
|
if uu.Password != uu.RepeatPassword {
|
|
|
|
msg += "; the same password needs to be passed in twice"
|
|
|
|
}
|
|
|
|
|
|
|
|
data["flash"] = msg
|
|
|
|
data["form"] = uu
|
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusBadRequest,
|
|
|
|
"manage/user-edit.tmpl",
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
}
|
2023-06-02 20:00:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if usr.Username != uu.Username {
|
|
|
|
exists, err := moduser.UsernameExists(ctx, dbclient, uu.Username)
|
|
|
|
if err != nil {
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
msg := fmt.Sprintf("Error: username %q is already taken", uu.Username)
|
|
|
|
|
|
|
|
c.Logger().Warn(msg)
|
|
|
|
|
|
|
|
data["flash"] = msg
|
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusBadRequest,
|
|
|
|
"manage/user-edit.tmpl",
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if usr.Email != uu.Email {
|
|
|
|
exists, err := moduser.EmailExists(ctx, dbclient, uu.Email)
|
|
|
|
if err != nil {
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
msg := fmt.Sprintf("Error: email %q is already taken", uu.Email)
|
|
|
|
|
|
|
|
c.Logger().Error(msg)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusBadRequest,
|
|
|
|
msg,
|
|
|
|
msg,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now update
|
2023-08-24 18:43:24 +02:00
|
|
|
err = moduser.UpdateUserByAdmin(
|
|
|
|
ctx, dbclient,
|
|
|
|
usr.ID, uu.Email, uu.Username, uu.Password, uu.IsAdmin, uu.IsActive,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
2023-06-02 20:00:14 +02:00
|
|
|
|
|
|
|
data["user"] = usr
|
|
|
|
p.Data = data
|
|
|
|
|
2023-08-24 18:43:24 +02:00
|
|
|
if sess, ok := c.Get("sess").(*sessions.Session); ok {
|
|
|
|
sess.Values["flash"] = fmt.Sprintf("Successfully updated user %q", uu.Username)
|
|
|
|
_ = sess.Save(c.Request(), c.Response())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/manage/users/%s", usr.ID))
|
2023-06-02 20:00:14 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-07 21:29:30 +02:00
|
|
|
|
|
|
|
// DeleteUserConfirmation displays user deletion confirmation confirmation page.
|
|
|
|
func DeleteUserConfirmation() echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
addHeaders(c)
|
|
|
|
|
|
|
|
u, ok := c.Get("sessUsr").(moduser.User)
|
|
|
|
if !ok {
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusUnauthorized,
|
|
|
|
http.StatusText(http.StatusUnauthorized),
|
|
|
|
"username was nil",
|
|
|
|
)
|
|
|
|
} else if !u.IsAdmin {
|
|
|
|
status := http.StatusUnauthorized
|
|
|
|
msg := http.StatusText(status)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c, status, msg+": You should not be here", "Restricted endpoint",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, ok := c.Get("sloggerCtx").(context.Context)
|
|
|
|
if !ok {
|
|
|
|
ctx = context.WithValue(context.Background(), moduser.CtxKey{}, slogger)
|
|
|
|
}
|
|
|
|
|
|
|
|
csrf := c.Get("csrf").(string)
|
|
|
|
tmpl := "manage/user-delete.tmpl"
|
|
|
|
data := make(map[string]any)
|
|
|
|
id := strings.TrimPrefix(strings.TrimSuffix(c.Request().URL.Path, "/delete"), "/manage/users/")
|
2023-08-15 18:33:48 +02:00
|
|
|
p := newPage()
|
|
|
|
|
|
|
|
p.Title = "Manage Users - Delete User"
|
|
|
|
p.Current = "manage-users-delete-user"
|
|
|
|
p.CSRF = csrf
|
|
|
|
p.User = u
|
2023-08-07 21:29:30 +02:00
|
|
|
|
|
|
|
usr, err := getUserByID(ctx, dbclient, id)
|
|
|
|
if err != nil { // nolint:dupl
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, moduser.ErrUserNotFound):
|
|
|
|
c.Logger().Errorf("user not found by ID: '%s'", id)
|
|
|
|
|
|
|
|
data["flash"] = fmt.Sprintf("No user found with the UUID: %q", id)
|
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusNotFound,
|
|
|
|
tmpl,
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
|
|
|
|
case errors.Is(err, moduser.ErrFailedToQueryUser):
|
|
|
|
c.Logger().Errorf("failed to query user by ID: '%s'", id)
|
|
|
|
|
|
|
|
data["flash"] = fmt.Sprintf("failed to query user by UUID %q", id)
|
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
tmpl,
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
|
|
|
|
case errors.Is(err, moduser.ErrBadUUID):
|
|
|
|
c.Logger().Errorf("Invalid UUID '%s': %q", id, err)
|
|
|
|
|
|
|
|
data["flash"] = fmt.Sprintf("invalid UUID %q", id)
|
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusBadRequest,
|
|
|
|
"manage/user-details.tmpl",
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Logger().Errorf("UUID-related issue for UUID '%s': %q", id, err)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
data["user"] = usr
|
|
|
|
p.Data = data
|
|
|
|
|
|
|
|
return c.Render(
|
|
|
|
http.StatusOK,
|
|
|
|
tmpl,
|
|
|
|
p,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteUser handles user deletion POST requests.
|
|
|
|
func DeleteUser() echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
addHeaders(c)
|
|
|
|
|
|
|
|
u, ok := c.Get("sessUsr").(moduser.User)
|
|
|
|
if !ok {
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusUnauthorized,
|
|
|
|
http.StatusText(http.StatusUnauthorized),
|
|
|
|
"username was nil",
|
|
|
|
)
|
|
|
|
} else if !u.IsAdmin {
|
|
|
|
status := http.StatusUnauthorized
|
|
|
|
msg := http.StatusText(status)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c, status, msg+": You should not be here", "Restricted endpoint",
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, ok := c.Get("sloggerCtx").(context.Context)
|
|
|
|
if !ok {
|
|
|
|
ctx = context.WithValue(context.Background(), moduser.CtxKey{}, slogger)
|
|
|
|
}
|
|
|
|
|
|
|
|
id := strings.TrimPrefix(strings.TrimSuffix(c.Request().URL.Path, "/delete"), "/manage/users/")
|
|
|
|
|
|
|
|
usr, err := getUserByID(ctx, dbclient, id)
|
|
|
|
if err != nil {
|
|
|
|
switch {
|
|
|
|
case errors.Is(err, moduser.ErrUserNotFound):
|
|
|
|
c.Logger().Errorf("user not found by ID: '%s'", id)
|
|
|
|
|
|
|
|
if sess, ok := c.Get("sess").(*sessions.Session); ok {
|
|
|
|
sess.Values["flash"] = fmt.Sprintf("Error: Could not delete user - no user found with UUID %q", id)
|
|
|
|
_ = sess.Save(c.Request(), c.Response())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusSeeOther, "/manage/users")
|
|
|
|
|
|
|
|
case errors.Is(err, moduser.ErrFailedToQueryUser):
|
|
|
|
c.Logger().Errorf("failed to query user by ID: '%s'", id)
|
|
|
|
|
|
|
|
if sess, ok := c.Get("sess").(*sessions.Session); ok {
|
|
|
|
sess.Values["flash"] = fmt.Sprintf("failed to query user by UUID %q", id)
|
|
|
|
_ = sess.Save(c.Request(), c.Response())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusSeeOther, "/manage/users")
|
|
|
|
|
|
|
|
case errors.Is(err, moduser.ErrBadUUID):
|
|
|
|
c.Logger().Errorf("Invalid UUID '%s': %q", id, err)
|
|
|
|
|
|
|
|
if sess, ok := c.Get("sess").(*sessions.Session); ok {
|
|
|
|
sess.Values["flash"] = fmt.Sprintf("invalid UUID %q", id)
|
|
|
|
_ = sess.Save(c.Request(), c.Response())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusSeeOther, c.Request().URL.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Logger().Errorf("Encountered a UUID-related issue for UUID '%s': %q", id, err)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = moduser.DeleteUserByID(ctx, dbclient, usr.ID.String()); err != nil {
|
|
|
|
c.Logger().Errorf("Could not delete user (id: %q), error: %q", id, err)
|
|
|
|
|
|
|
|
return renderErrorPage(
|
|
|
|
c,
|
|
|
|
http.StatusInternalServerError,
|
|
|
|
http.StatusText(http.StatusInternalServerError),
|
|
|
|
err.Error(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if sess, ok := c.Get("sess").(*sessions.Session); ok {
|
|
|
|
sess.Values["flash"] = fmt.Sprintf("Successfully deleted user %q!", usr.Username)
|
|
|
|
_ = sess.Save(c.Request(), c.Response())
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.Redirect(http.StatusSeeOther, "/manage/users")
|
|
|
|
}
|
|
|
|
}
|