2022-08-30 02:30:36 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
2023-02-15 17:40:35 +00:00
|
|
|
"fmt"
|
2022-08-30 02:30:36 +00:00
|
|
|
"net/http"
|
|
|
|
|
2022-08-30 18:05:11 +00:00
|
|
|
"github.com/google/uuid"
|
2022-10-30 04:05:38 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/services"
|
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
2022-10-30 02:15:35 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/sys/validate"
|
2023-04-09 18:39:43 +00:00
|
|
|
"github.com/hay-kot/httpkit/errchain"
|
|
|
|
"github.com/hay-kot/httpkit/server"
|
2022-09-03 18:38:35 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-08-30 02:30:36 +00:00
|
|
|
)
|
|
|
|
|
2023-03-07 06:18:58 +00:00
|
|
|
// HandleUserRegistration godoc
|
|
|
|
//
|
|
|
|
// @Summary Register New User
|
|
|
|
// @Tags User
|
|
|
|
// @Produce json
|
|
|
|
// @Param payload body services.UserRegistration true "User Data"
|
|
|
|
// @Success 204
|
|
|
|
// @Router /v1/users/register [Post]
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleUserRegistration() errchain.HandlerFunc {
|
2022-10-30 02:15:35 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-09-27 23:52:13 +00:00
|
|
|
regData := services.UserRegistration{}
|
2022-08-30 18:05:11 +00:00
|
|
|
|
|
|
|
if err := server.Decode(r, ®Data); err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to decode user registration data")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-08-30 18:05:11 +00:00
|
|
|
}
|
|
|
|
|
2022-10-14 22:02:16 +00:00
|
|
|
if !ctrl.allowRegistration && regData.GroupToken == "" {
|
2023-02-15 17:40:35 +00:00
|
|
|
return validate.NewRequestError(fmt.Errorf("user registration disabled"), http.StatusForbidden)
|
2022-10-14 22:02:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-31 00:40:39 +00:00
|
|
|
_, err := ctrl.svc.User.RegisterUser(r.Context(), regData)
|
2022-08-30 18:05:11 +00:00
|
|
|
if err != nil {
|
2022-09-27 23:52:13 +00:00
|
|
|
log.Err(err).Msg("failed to register user")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-08-30 18:05:11 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusNoContent, nil)
|
2022-08-30 18:05:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-30 02:30:36 +00:00
|
|
|
// HandleUserSelf godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Get User Self
|
|
|
|
// @Tags User
|
|
|
|
// @Produce json
|
2023-03-21 04:32:10 +00:00
|
|
|
// @Success 200 {object} Wrapped{item=repo.UserOut}
|
2023-03-07 06:18:58 +00:00
|
|
|
// @Router /v1/users/self [GET]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleUserSelf() errchain.HandlerFunc {
|
2022-10-30 02:15:35 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-08-30 02:30:36 +00:00
|
|
|
token := services.UseTokenCtx(r.Context())
|
|
|
|
usr, err := ctrl.svc.User.GetSelf(r.Context(), token)
|
2022-08-30 18:05:11 +00:00
|
|
|
if usr.ID == uuid.Nil || err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to get user")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusOK, Wrap(usr))
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 02:42:03 +00:00
|
|
|
// HandleUserSelfUpdate godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Update Account
|
|
|
|
// @Tags User
|
|
|
|
// @Produce json
|
|
|
|
// @Param payload body repo.UserUpdate true "User Data"
|
2023-03-21 04:32:10 +00:00
|
|
|
// @Success 200 {object} Wrapped{item=repo.UserUpdate}
|
2023-03-07 06:18:58 +00:00
|
|
|
// @Router /v1/users/self [PUT]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleUserSelfUpdate() errchain.HandlerFunc {
|
2022-10-30 02:15:35 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-09-27 23:52:13 +00:00
|
|
|
updateData := repo.UserUpdate{}
|
2022-08-30 02:30:36 +00:00
|
|
|
if err := server.Decode(r, &updateData); err != nil {
|
2022-09-03 18:38:35 +00:00
|
|
|
log.Err(err).Msg("failed to decode user update data")
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusBadRequest)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
actor := services.UseUserCtx(r.Context())
|
|
|
|
newData, err := ctrl.svc.User.UpdateSelf(r.Context(), actor.ID, updateData)
|
|
|
|
if err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusOK, Wrap(newData))
|
2022-08-30 02:30:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-04 02:42:03 +00:00
|
|
|
// HandleUserSelfDelete godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Delete Account
|
|
|
|
// @Tags User
|
|
|
|
// @Produce json
|
|
|
|
// @Success 204
|
|
|
|
// @Router /v1/users/self [DELETE]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleUserSelfDelete() errchain.HandlerFunc {
|
2022-10-30 02:15:35 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-10-13 17:37:29 +00:00
|
|
|
if ctrl.isDemo {
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(nil, http.StatusForbidden)
|
2022-10-13 17:37:29 +00:00
|
|
|
}
|
|
|
|
|
2022-09-04 02:42:03 +00:00
|
|
|
actor := services.UseUserCtx(r.Context())
|
|
|
|
if err := ctrl.svc.User.DeleteSelf(r.Context(), actor.ID); err != nil {
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-09-04 02:42:03 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusNoContent, nil)
|
2022-09-04 02:42:03 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-09 17:23:21 +00:00
|
|
|
|
|
|
|
type (
|
|
|
|
ChangePassword struct {
|
|
|
|
Current string `json:"current,omitempty"`
|
|
|
|
New string `json:"new,omitempty"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// HandleUserSelfChangePassword godoc
|
2023-03-07 06:18:58 +00:00
|
|
|
//
|
|
|
|
// @Summary Change Password
|
|
|
|
// @Tags User
|
|
|
|
// @Success 204
|
|
|
|
// @Param payload body ChangePassword true "Password Payload"
|
|
|
|
// @Router /v1/users/change-password [PUT]
|
|
|
|
// @Security Bearer
|
2023-03-21 04:32:10 +00:00
|
|
|
func (ctrl *V1Controller) HandleUserSelfChangePassword() errchain.HandlerFunc {
|
2022-10-30 02:15:35 +00:00
|
|
|
return func(w http.ResponseWriter, r *http.Request) error {
|
2022-10-12 20:53:22 +00:00
|
|
|
if ctrl.isDemo {
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(nil, http.StatusForbidden)
|
2022-10-12 20:53:22 +00:00
|
|
|
}
|
|
|
|
|
2022-10-09 17:23:21 +00:00
|
|
|
var cp ChangePassword
|
|
|
|
err := server.Decode(r, &cp)
|
|
|
|
if err != nil {
|
|
|
|
log.Err(err).Msg("user failed to change password")
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := services.NewContext(r.Context())
|
|
|
|
|
|
|
|
ok := ctrl.svc.User.ChangePassword(ctx, cp.Current, cp.New)
|
|
|
|
if !ok {
|
2022-10-30 02:15:35 +00:00
|
|
|
return validate.NewRequestError(err, http.StatusInternalServerError)
|
2022-10-09 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 04:32:10 +00:00
|
|
|
return server.JSON(w, http.StatusNoContent, nil)
|
2022-10-09 17:23:21 +00:00
|
|
|
}
|
|
|
|
}
|