mirror of
https://github.com/getAlby/lndhub.go.git
synced 2025-12-18 13:14:56 +01:00
* Update Makefile * Optionally load test DB from env variable * Add option to soft-delete a user This allows users to be marked as deleted. An additional middleware checks if a user is deleted or deactivated and rejects requests for those as StatusUnauthorized. note: the middelware adds an additional DB query to load the user.
71 lines
2.4 KiB
Go
71 lines
2.4 KiB
Go
package v2controllers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/getAlby/lndhub.go/lib/responses"
|
|
"github.com/getAlby/lndhub.go/lib/service"
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
// UpdateUserController : Update user controller struct
|
|
type UpdateUserController struct {
|
|
svc *service.LndhubService
|
|
}
|
|
|
|
func NewUpdateUserController(svc *service.LndhubService) *UpdateUserController {
|
|
return &UpdateUserController{svc: svc}
|
|
}
|
|
|
|
type UpdateUserResponseBody struct {
|
|
Login string `json:"login"`
|
|
Deactivated bool `json:"deactivated"`
|
|
Deleted bool `json:"deleted"`
|
|
ID int64 `json:"id"`
|
|
}
|
|
type UpdateUserRequestBody struct {
|
|
Login *string `json:"login,omitempty"`
|
|
Password *string `json:"password,omitempty"`
|
|
Deactivated *bool `json:"deactivated,omitempty"`
|
|
Deleted *bool `json:"deleted,omitempty"`
|
|
ID int64 `json:"id" validate:"required"`
|
|
}
|
|
|
|
// UpdateUser godoc
|
|
// @Summary Update an account
|
|
// @Description Update an account with a new a login, password and activation status. Requires Authorization header with admin token.
|
|
// @Accept json
|
|
// @Produce json
|
|
// @Tags Account
|
|
// @Param account body UpdateUserRequestBody false "Update User"
|
|
// @Success 200 {object} UpdateUserResponseBody
|
|
// @Failure 400 {object} responses.ErrorResponse
|
|
// @Failure 500 {object} responses.ErrorResponse
|
|
// @Router /v2/admin/users [put]
|
|
func (controller *UpdateUserController) UpdateUser(c echo.Context) error {
|
|
|
|
var body UpdateUserRequestBody
|
|
|
|
if err := c.Bind(&body); err != nil {
|
|
c.Logger().Errorf("Failed to load update user request body: %v", err)
|
|
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
|
|
}
|
|
if err := c.Validate(&body); err != nil {
|
|
c.Logger().Errorf("Invalid update user request body error: %v", err)
|
|
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
|
|
}
|
|
user, err := controller.svc.UpdateUser(c.Request().Context(), body.ID, body.Login, body.Password, body.Deactivated, body.Deleted)
|
|
if err != nil {
|
|
c.Logger().Errorf("Failed to update user: %v", err)
|
|
return c.JSON(http.StatusBadRequest, responses.BadArgumentsError)
|
|
}
|
|
|
|
var ResponseBody UpdateUserResponseBody
|
|
ResponseBody.Login = user.Login
|
|
ResponseBody.Deactivated = user.Deactivated
|
|
ResponseBody.Deleted = user.Deleted
|
|
ResponseBody.ID = user.ID
|
|
|
|
return c.JSON(http.StatusOK, &ResponseBody)
|
|
}
|