2023-08-04 13:34:18 +02:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
2025-01-11 18:44:55 +01:00
|
|
|
"net/http"
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
"github.com/go-pkgz/routegroup"
|
2025-02-28 08:29:40 +01:00
|
|
|
|
2023-08-04 13:34:18 +02:00
|
|
|
"github.com/h44z/wg-portal/internal/app"
|
2025-03-09 21:16:42 +01:00
|
|
|
"github.com/h44z/wg-portal/internal/app/api/core/request"
|
|
|
|
"github.com/h44z/wg-portal/internal/app/api/core/respond"
|
2023-08-04 13:34:18 +02:00
|
|
|
"github.com/h44z/wg-portal/internal/app/api/v0/model"
|
|
|
|
"github.com/h44z/wg-portal/internal/domain"
|
|
|
|
)
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
type UserEndpoint struct {
|
2023-08-04 13:34:18 +02:00
|
|
|
app *app.App
|
2025-03-09 21:16:42 +01:00
|
|
|
authenticator Authenticator
|
|
|
|
validator Validator
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
func NewUserEndpoint(app *app.App, authenticator Authenticator, validator Validator) UserEndpoint {
|
|
|
|
return UserEndpoint{
|
|
|
|
app: app,
|
|
|
|
authenticator: authenticator,
|
|
|
|
validator: validator,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e UserEndpoint) GetName() string {
|
2023-08-04 13:34:18 +02:00
|
|
|
return "UserEndpoint"
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
func (e UserEndpoint) RegisterRoutes(g *routegroup.Bundle) {
|
|
|
|
apiGroup := g.Mount("/user")
|
|
|
|
apiGroup.Use(e.authenticator.LoggedIn())
|
|
|
|
|
|
|
|
apiGroup.With(e.authenticator.LoggedIn(ScopeAdmin)).HandleFunc("GET /all", e.handleAllGet())
|
|
|
|
apiGroup.With(e.authenticator.UserIdMatch("id")).HandleFunc("GET /{id}", e.handleSingleGet())
|
|
|
|
apiGroup.With(e.authenticator.UserIdMatch("id")).HandleFunc("PUT /{id}", e.handleUpdatePut())
|
|
|
|
apiGroup.With(e.authenticator.UserIdMatch("id")).HandleFunc("DELETE /{id}", e.handleDelete())
|
|
|
|
apiGroup.With(e.authenticator.LoggedIn(ScopeAdmin)).HandleFunc("POST /new", e.handleCreatePost())
|
|
|
|
apiGroup.With(e.authenticator.UserIdMatch("id")).HandleFunc("GET /{id}/peers", e.handlePeersGet())
|
|
|
|
apiGroup.With(e.authenticator.UserIdMatch("id")).HandleFunc("GET /{id}/stats", e.handleStatsGet())
|
|
|
|
apiGroup.With(e.authenticator.UserIdMatch("id")).HandleFunc("GET /{id}/interfaces", e.handleInterfacesGet())
|
|
|
|
apiGroup.With(e.authenticator.UserIdMatch("id")).HandleFunc("POST /{id}/api/enable", e.handleApiEnablePost())
|
|
|
|
apiGroup.With(e.authenticator.UserIdMatch("id")).HandleFunc("POST /{id}/api/disable", e.handleApiDisablePost())
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
// handleAllGet returns a gorm Handler function.
|
2023-08-04 13:34:18 +02:00
|
|
|
//
|
|
|
|
// @ID users_handleAllGet
|
|
|
|
// @Tags Users
|
|
|
|
// @Summary Get all user records.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} []model.User
|
|
|
|
// @Failure 500 {object} model.Error
|
|
|
|
// @Router /user/all [get]
|
2025-03-09 21:16:42 +01:00
|
|
|
func (e UserEndpoint) handleAllGet() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
users, err := e.app.GetAllUsers(r.Context())
|
2023-08-04 13:34:18 +02:00
|
|
|
if err != nil {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusInternalServerError,
|
2025-01-11 18:44:55 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: err.Error()})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusOK, model.NewUsers(users))
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
// handleSingleGet returns a gorm Handler function.
|
2023-08-04 13:34:18 +02:00
|
|
|
//
|
|
|
|
// @ID users_handleSingleGet
|
|
|
|
// @Tags Users
|
|
|
|
// @Summary Get a single user record.
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "The user identifier"
|
|
|
|
// @Success 200 {object} model.User
|
|
|
|
// @Failure 500 {object} model.Error
|
|
|
|
// @Router /user/{id} [get]
|
2025-03-09 21:16:42 +01:00
|
|
|
func (e UserEndpoint) handleSingleGet() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := Base64UrlDecode(request.Path(r, "id"))
|
2023-08-04 13:34:18 +02:00
|
|
|
if id == "" {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusBadRequest, model.Error{Code: http.StatusBadRequest, Message: "missing user id"})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
user, err := e.app.GetUser(r.Context(), domain.UserIdentifier(id))
|
2023-08-04 13:34:18 +02:00
|
|
|
if err != nil {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusInternalServerError,
|
2025-01-11 18:44:55 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: err.Error()})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusOK, model.NewUser(user, true))
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
// handleUpdatePut returns a gorm Handler function.
|
2023-08-04 13:34:18 +02:00
|
|
|
//
|
|
|
|
// @ID users_handleUpdatePut
|
|
|
|
// @Tags Users
|
|
|
|
// @Summary Update the user record.
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "The user identifier"
|
|
|
|
// @Param request body model.User true "The user data"
|
|
|
|
// @Success 200 {object} model.User
|
|
|
|
// @Failure 400 {object} model.Error
|
|
|
|
// @Failure 500 {object} model.Error
|
|
|
|
// @Router /user/{id} [put]
|
2025-03-09 21:16:42 +01:00
|
|
|
func (e UserEndpoint) handleUpdatePut() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := Base64UrlDecode(request.Path(r, "id"))
|
2023-08-04 13:34:18 +02:00
|
|
|
if id == "" {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusBadRequest, model.Error{Code: http.StatusBadRequest, Message: "missing user id"})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var user model.User
|
2025-03-09 21:16:42 +01:00
|
|
|
if err := request.BodyJson(r, &user); err != nil {
|
|
|
|
respond.JSON(w, http.StatusBadRequest, model.Error{Code: http.StatusBadRequest, Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := e.validator.Struct(user); err != nil {
|
|
|
|
respond.JSON(w, http.StatusBadRequest, model.Error{Code: http.StatusBadRequest, Message: err.Error()})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if id != user.Identifier {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusBadRequest,
|
|
|
|
model.Error{Code: http.StatusBadRequest, Message: "user id mismatch"})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
updateUser, err := e.app.UpdateUser(r.Context(), model.NewDomainUser(&user))
|
2023-08-04 13:34:18 +02:00
|
|
|
if err != nil {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusInternalServerError,
|
2025-01-11 18:44:55 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: err.Error()})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusOK, model.NewUser(updateUser, false))
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
// handleCreatePost returns a gorm Handler function.
|
2023-08-04 13:34:18 +02:00
|
|
|
//
|
|
|
|
// @ID users_handleCreatePost
|
|
|
|
// @Tags Users
|
|
|
|
// @Summary Create the new user record.
|
|
|
|
// @Produce json
|
|
|
|
// @Param request body model.User true "The user data"
|
|
|
|
// @Success 200 {object} model.User
|
|
|
|
// @Failure 400 {object} model.Error
|
|
|
|
// @Failure 500 {object} model.Error
|
|
|
|
// @Router /user/new [post]
|
2025-03-09 21:16:42 +01:00
|
|
|
func (e UserEndpoint) handleCreatePost() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2023-08-04 13:34:18 +02:00
|
|
|
var user model.User
|
2025-03-09 21:16:42 +01:00
|
|
|
if err := request.BodyJson(r, &user); err != nil {
|
|
|
|
respond.JSON(w, http.StatusBadRequest, model.Error{Code: http.StatusBadRequest, Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err := e.validator.Struct(user); err != nil {
|
|
|
|
respond.JSON(w, http.StatusBadRequest, model.Error{Code: http.StatusBadRequest, Message: err.Error()})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
newUser, err := e.app.CreateUser(r.Context(), model.NewDomainUser(&user))
|
2023-08-04 13:34:18 +02:00
|
|
|
if err != nil {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusInternalServerError,
|
2025-01-11 18:44:55 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: err.Error()})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusOK, model.NewUser(newUser, false))
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
// handlePeersGet returns a gorm Handler function.
|
2023-08-04 13:34:18 +02:00
|
|
|
//
|
|
|
|
// @ID users_handlePeersGet
|
|
|
|
// @Tags Users
|
|
|
|
// @Summary Get peers for the given user.
|
2025-01-26 11:35:24 +01:00
|
|
|
// @Param id path string true "The user identifier"
|
2023-08-04 13:34:18 +02:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} []model.Peer
|
|
|
|
// @Failure 400 {object} model.Error
|
|
|
|
// @Failure 500 {object} model.Error
|
|
|
|
// @Router /user/{id}/peers [get]
|
2025-03-09 21:16:42 +01:00
|
|
|
func (e UserEndpoint) handlePeersGet() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userId := Base64UrlDecode(request.Path(r, "id"))
|
2025-01-26 11:35:24 +01:00
|
|
|
if userId == "" {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusBadRequest,
|
2025-01-11 18:44:55 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: "missing id parameter"})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
peers, err := e.app.GetUserPeers(r.Context(), domain.UserIdentifier(userId))
|
2023-08-04 13:34:18 +02:00
|
|
|
if err != nil {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusInternalServerError,
|
2025-01-11 18:44:55 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: err.Error()})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusOK, model.NewPeers(peers))
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
// handleStatsGet returns a gorm Handler function.
|
2023-08-04 13:34:18 +02:00
|
|
|
//
|
|
|
|
// @ID users_handleStatsGet
|
|
|
|
// @Tags Users
|
|
|
|
// @Summary Get peer stats for the given user.
|
2025-01-26 11:35:24 +01:00
|
|
|
// @Param id path string true "The user identifier"
|
2023-08-04 13:34:18 +02:00
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} model.PeerStats
|
|
|
|
// @Failure 400 {object} model.Error
|
|
|
|
// @Failure 500 {object} model.Error
|
|
|
|
// @Router /user/{id}/stats [get]
|
2025-03-09 21:16:42 +01:00
|
|
|
func (e UserEndpoint) handleStatsGet() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userId := Base64UrlDecode(request.Path(r, "id"))
|
2023-08-04 13:34:18 +02:00
|
|
|
if userId == "" {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusBadRequest,
|
2025-01-11 18:44:55 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: "missing id parameter"})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
stats, err := e.app.GetUserPeerStats(r.Context(), domain.UserIdentifier(userId))
|
2023-08-04 13:34:18 +02:00
|
|
|
if err != nil {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusInternalServerError,
|
2025-01-11 18:44:55 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: err.Error()})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusOK, model.NewPeerStats(e.app.Config.Statistics.CollectPeerData, stats))
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
// handleInterfacesGet returns a gorm Handler function.
|
2025-01-26 11:35:24 +01:00
|
|
|
//
|
|
|
|
// @ID users_handleInterfacesGet
|
|
|
|
// @Tags Users
|
|
|
|
// @Summary Get interfaces for the given user. Returns an empty list if self provisioning is disabled.
|
|
|
|
// @Param id path string true "The user identifier"
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} []model.Interface
|
|
|
|
// @Failure 400 {object} model.Error
|
|
|
|
// @Failure 500 {object} model.Error
|
|
|
|
// @Router /user/{id}/interfaces [get]
|
2025-03-09 21:16:42 +01:00
|
|
|
func (e UserEndpoint) handleInterfacesGet() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userId := Base64UrlDecode(request.Path(r, "id"))
|
2025-01-26 11:35:24 +01:00
|
|
|
if userId == "" {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusBadRequest,
|
2025-01-26 11:35:24 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: "missing id parameter"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
peers, err := e.app.GetUserInterfaces(r.Context(), domain.UserIdentifier(userId))
|
2025-01-26 11:35:24 +01:00
|
|
|
if err != nil {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusInternalServerError,
|
2025-01-26 11:35:24 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusOK, model.NewInterfaces(peers, nil))
|
2025-01-26 11:35:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
// handleDelete returns a gorm Handler function.
|
2023-08-04 13:34:18 +02:00
|
|
|
//
|
|
|
|
// @ID users_handleDelete
|
|
|
|
// @Tags Users
|
|
|
|
// @Summary Delete the user record.
|
|
|
|
// @Produce json
|
|
|
|
// @Param id path string true "The user identifier"
|
|
|
|
// @Success 204 "No content if deletion was successful"
|
|
|
|
// @Failure 400 {object} model.Error
|
|
|
|
// @Failure 500 {object} model.Error
|
|
|
|
// @Router /user/{id} [delete]
|
2025-03-09 21:16:42 +01:00
|
|
|
func (e UserEndpoint) handleDelete() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
id := Base64UrlDecode(request.Path(r, "id"))
|
2023-08-04 13:34:18 +02:00
|
|
|
if id == "" {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusBadRequest, model.Error{Code: http.StatusBadRequest, Message: "missing user id"})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
err := e.app.DeleteUser(r.Context(), domain.UserIdentifier(id))
|
2023-08-04 13:34:18 +02:00
|
|
|
if err != nil {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusInternalServerError,
|
2025-01-11 18:44:55 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: err.Error()})
|
2023-08-04 13:34:18 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.Status(w, http.StatusNoContent)
|
2023-08-04 13:34:18 +02:00
|
|
|
}
|
|
|
|
}
|
2025-01-11 18:44:55 +01:00
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
// handleApiEnablePost returns a gorm Handler function.
|
2025-01-11 18:44:55 +01:00
|
|
|
//
|
|
|
|
// @ID users_handleApiEnablePost
|
|
|
|
// @Tags Users
|
|
|
|
// @Summary Enable the REST API for the given user.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} model.User
|
|
|
|
// @Failure 400 {object} model.Error
|
|
|
|
// @Failure 500 {object} model.Error
|
|
|
|
// @Router /user/{id}/api/enable [post]
|
2025-03-09 21:16:42 +01:00
|
|
|
func (e UserEndpoint) handleApiEnablePost() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userId := Base64UrlDecode(request.Path(r, "id"))
|
2025-01-11 18:44:55 +01:00
|
|
|
if userId == "" {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusBadRequest,
|
2025-01-11 18:44:55 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: "missing id parameter"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
user, err := e.app.ActivateApi(r.Context(), domain.UserIdentifier(userId))
|
2025-01-11 18:44:55 +01:00
|
|
|
if err != nil {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusInternalServerError,
|
2025-01-11 18:44:55 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusOK, model.NewUser(user, true))
|
2025-01-11 18:44:55 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
// handleApiDisablePost returns a gorm Handler function.
|
2025-01-11 18:44:55 +01:00
|
|
|
//
|
|
|
|
// @ID users_handleApiDisablePost
|
|
|
|
// @Tags Users
|
|
|
|
// @Summary Disable the REST API for the given user.
|
|
|
|
// @Produce json
|
|
|
|
// @Success 200 {object} model.User
|
|
|
|
// @Failure 400 {object} model.Error
|
|
|
|
// @Failure 500 {object} model.Error
|
|
|
|
// @Router /user/{id}/api/disable [post]
|
2025-03-09 21:16:42 +01:00
|
|
|
func (e UserEndpoint) handleApiDisablePost() http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
userId := Base64UrlDecode(request.Path(r, "id"))
|
2025-01-11 18:44:55 +01:00
|
|
|
if userId == "" {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusBadRequest,
|
2025-01-11 18:44:55 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: "missing id parameter"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
user, err := e.app.DeactivateApi(r.Context(), domain.UserIdentifier(userId))
|
2025-01-11 18:44:55 +01:00
|
|
|
if err != nil {
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusInternalServerError,
|
2025-01-11 18:44:55 +01:00
|
|
|
model.Error{Code: http.StatusInternalServerError, Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2025-03-09 21:16:42 +01:00
|
|
|
respond.JSON(w, http.StatusOK, model.NewUser(user, false))
|
2025-01-11 18:44:55 +01:00
|
|
|
}
|
|
|
|
}
|