2023-05-28 13:39:20 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/api/apierr"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/api/ginresp"
|
2024-07-16 17:19:55 +02:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/logic"
|
2023-05-28 13:39:20 +02:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/models"
|
|
|
|
"database/sql"
|
2023-07-30 15:58:37 +02:00
|
|
|
"errors"
|
2023-06-18 01:36:34 +02:00
|
|
|
"fmt"
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-07-15 17:26:55 +02:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/ginext"
|
2023-05-28 13:39:20 +02:00
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateUser swaggerdoc
|
|
|
|
//
|
|
|
|
// @Summary Create a new user
|
|
|
|
// @ID api-user-create
|
|
|
|
// @Tags API-v2
|
|
|
|
//
|
|
|
|
// @Param post_body body handler.CreateUser.body false " "
|
|
|
|
//
|
2024-09-15 21:07:46 +02:00
|
|
|
// @Success 200 {object} models.UserWithClientsAndKeys
|
2023-05-28 13:39:20 +02:00
|
|
|
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
|
|
|
|
// @Failure 500 {object} ginresp.apiError "internal server error"
|
|
|
|
//
|
|
|
|
// @Router /api/v2/users [POST]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h APIHandler) CreateUser(pctx ginext.PreContext) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
type body struct {
|
2024-06-01 13:45:28 +02:00
|
|
|
FCMToken string `json:"fcm_token"`
|
|
|
|
ProToken *string `json:"pro_token"`
|
|
|
|
Username *string `json:"username"`
|
|
|
|
AgentModel string `json:"agent_model"`
|
|
|
|
AgentVersion string `json:"agent_version"`
|
|
|
|
ClientName *string `json:"client_name"`
|
|
|
|
ClientType models.ClientType `json:"client_type"`
|
|
|
|
NoClient bool `json:"no_client"`
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var b body
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.Body(&b).Start()
|
2023-05-28 13:39:20 +02:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-09-16 15:17:20 +02:00
|
|
|
return h.app.DoRequest(ctx, g, models.TLockReadWrite, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
var clientType models.ClientType
|
|
|
|
if !b.NoClient {
|
|
|
|
if b.FCMToken == "" {
|
|
|
|
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing FCMToken", nil)
|
|
|
|
}
|
|
|
|
if b.AgentVersion == "" {
|
|
|
|
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing AgentVersion", nil)
|
|
|
|
}
|
|
|
|
if b.ClientType == "" {
|
|
|
|
return ginresp.APIError(g, 400, apierr.INVALID_CLIENTTYPE, "Missing ClientType", nil)
|
|
|
|
}
|
|
|
|
if !b.ClientType.Valid() {
|
|
|
|
return ginresp.APIError(g, 400, apierr.BINDFAIL_BODY_PARAM, "Invalid ClientType", nil)
|
|
|
|
}
|
|
|
|
clientType = b.ClientType
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if b.ProToken != nil {
|
|
|
|
ptok, err := h.app.VerifyProToken(ctx, *b.ProToken)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
|
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if !ptok {
|
|
|
|
return ginresp.APIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
|
|
|
|
}
|
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
readKey := h.app.GenerateRandomAuthKey()
|
|
|
|
sendKey := h.app.GenerateRandomAuthKey()
|
|
|
|
adminKey := h.app.GenerateRandomAuthKey()
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
err := h.database.ClearFCMTokens(ctx, b.FCMToken)
|
2023-05-28 13:39:20 +02:00
|
|
|
if err != nil {
|
2024-07-16 17:19:55 +02:00
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if b.ProToken != nil {
|
|
|
|
err := h.database.ClearProTokens(ctx, *b.ProToken)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing pro tokens", err)
|
|
|
|
}
|
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
username := b.Username
|
|
|
|
if username != nil {
|
|
|
|
username = langext.Ptr(h.app.NormalizeUsername(*username))
|
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
userobj, err := h.database.CreateUser(ctx, b.ProToken, username)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create user in db", err)
|
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
_, err = h.database.CreateKeyToken(ctx, "AdminKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermAdmin}, adminKey)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create admin-key in db", err)
|
|
|
|
}
|
2023-06-18 01:36:34 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
_, err = h.database.CreateKeyToken(ctx, "SendKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermChannelSend}, sendKey)
|
2023-05-28 13:39:20 +02:00
|
|
|
if err != nil {
|
2024-07-16 17:19:55 +02:00
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create send-key in db", err)
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
_, err = h.database.CreateKeyToken(ctx, "ReadKey (default)", userobj.UserID, true, make([]models.ChannelID, 0), models.TokenPermissionList{models.PermUserRead, models.PermChannelRead}, readKey)
|
2023-05-28 13:39:20 +02:00
|
|
|
if err != nil {
|
2024-07-16 17:19:55 +02:00
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create read-key in db", err)
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
log.Info().Msg(fmt.Sprintf("Sucessfully created new user %s (client: %v)", userobj.UserID, b.NoClient))
|
|
|
|
|
|
|
|
if b.NoClient {
|
2024-09-15 21:07:46 +02:00
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, userobj.PreMarshal().WithClients(make([]models.Client, 0), adminKey, sendKey, readKey)))
|
2024-07-16 17:19:55 +02:00
|
|
|
} else {
|
|
|
|
err := h.database.DeleteClientsByFCM(ctx, b.FCMToken)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to delete existing clients in db", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := h.database.CreateClient(ctx, userobj.UserID, clientType, b.FCMToken, b.AgentModel, b.AgentVersion, b.ClientName)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to create client in db", err)
|
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-09-15 21:07:46 +02:00
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, userobj.PreMarshal().WithClients([]models.Client{client}, adminKey, sendKey, readKey)))
|
2024-07-16 17:19:55 +02:00
|
|
|
}
|
|
|
|
})
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetUser swaggerdoc
|
|
|
|
//
|
|
|
|
// @Summary Get a user
|
|
|
|
// @ID api-user-get
|
|
|
|
// @Tags API-v2
|
|
|
|
//
|
2023-05-28 17:04:44 +02:00
|
|
|
// @Param uid path string true "UserID"
|
2023-05-28 13:39:20 +02:00
|
|
|
//
|
2024-09-15 21:07:46 +02:00
|
|
|
// @Success 200 {object} models.User
|
2023-05-28 13:39:20 +02:00
|
|
|
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
|
|
|
|
// @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions"
|
|
|
|
// @Failure 404 {object} ginresp.apiError "user not found"
|
|
|
|
// @Failure 500 {object} ginresp.apiError "internal server error"
|
|
|
|
//
|
|
|
|
// @Router /api/v2/users/{uid} [GET]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h APIHandler) GetUser(pctx ginext.PreContext) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
type uri struct {
|
|
|
|
UserID models.UserID `uri:"uid" binding:"entityid"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var u uri
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.URI(&u).Start()
|
2023-05-28 13:39:20 +02:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-09-16 15:17:20 +02:00
|
|
|
return h.app.DoRequest(ctx, g, models.TLockRead, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if permResp := ctx.CheckPermissionUserRead(u.UserID); permResp != nil {
|
|
|
|
return *permResp
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := h.database.GetUser(ctx, u.UserID)
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return ginresp.APIError(g, 404, apierr.USER_NOT_FOUND, "User not found", err)
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query user", err)
|
|
|
|
}
|
|
|
|
|
2024-09-15 21:07:46 +02:00
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, user.PreMarshal()))
|
2024-07-16 17:19:55 +02:00
|
|
|
|
|
|
|
})
|
2023-05-28 13:39:20 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateUser swaggerdoc
|
|
|
|
//
|
|
|
|
// @Summary (Partially) update a user
|
|
|
|
// @Description The body-values are optional, only send the ones you want to update
|
|
|
|
// @ID api-user-update
|
|
|
|
// @Tags API-v2
|
|
|
|
//
|
2023-06-10 00:15:42 +02:00
|
|
|
// @Param uid path string true "UserID"
|
2023-05-28 13:39:20 +02:00
|
|
|
//
|
|
|
|
// @Param username body string false "Change the username (send an empty string to clear it)"
|
|
|
|
// @Param pro_token body string false "Send a verification of premium purchase"
|
|
|
|
//
|
2024-09-15 21:07:46 +02:00
|
|
|
// @Success 200 {object} models.User
|
2023-05-28 13:39:20 +02:00
|
|
|
// @Failure 400 {object} ginresp.apiError "supplied values/parameters cannot be parsed / are invalid"
|
|
|
|
// @Failure 401 {object} ginresp.apiError "user is not authorized / has missing permissions"
|
|
|
|
// @Failure 404 {object} ginresp.apiError "user not found"
|
|
|
|
// @Failure 500 {object} ginresp.apiError "internal server error"
|
|
|
|
//
|
|
|
|
// @Router /api/v2/users/{uid} [PATCH]
|
2024-07-15 17:26:55 +02:00
|
|
|
func (h APIHandler) UpdateUser(pctx ginext.PreContext) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
type uri struct {
|
|
|
|
UserID models.UserID `uri:"uid" binding:"entityid"`
|
|
|
|
}
|
|
|
|
type body struct {
|
|
|
|
Username *string `json:"username"`
|
|
|
|
ProToken *string `json:"pro_token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
var u uri
|
|
|
|
var b body
|
2024-07-16 17:19:55 +02:00
|
|
|
ctx, g, errResp := pctx.URI(&u).Body(&b).Start()
|
2023-05-28 13:39:20 +02:00
|
|
|
if errResp != nil {
|
|
|
|
return *errResp
|
|
|
|
}
|
|
|
|
defer ctx.Cancel()
|
|
|
|
|
2024-09-16 15:17:20 +02:00
|
|
|
return h.app.DoRequest(ctx, g, models.TLockReadWrite, func(ctx *logic.AppContext, finishSuccess func(r ginext.HTTPResponse) ginext.HTTPResponse) ginext.HTTPResponse {
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if permResp := ctx.CheckPermissionUserAdmin(u.UserID); permResp != nil {
|
|
|
|
return *permResp
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if b.Username != nil {
|
|
|
|
username := langext.Ptr(h.app.NormalizeUsername(*b.Username))
|
|
|
|
if *username == "" {
|
|
|
|
username = nil
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
err := h.database.UpdateUserUsername(ctx, u.UserID, username)
|
2023-05-28 13:39:20 +02:00
|
|
|
if err != nil {
|
2024-07-16 17:19:55 +02:00
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
2024-07-16 17:19:55 +02:00
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
if b.ProToken != nil {
|
|
|
|
if *b.ProToken == "" {
|
|
|
|
err := h.database.UpdateUserProToken(ctx, u.UserID, nil)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ptok, err := h.app.VerifyProToken(ctx, *b.ProToken)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.FAILED_VERIFY_PRO_TOKEN, "Failed to query purchase status", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ptok {
|
|
|
|
return ginresp.APIError(g, 400, apierr.INVALID_PRO_TOKEN, "Purchase token could not be verified", nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.database.ClearProTokens(ctx, *b.ProToken)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to clear existing fcm tokens", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = h.database.UpdateUserProToken(ctx, u.UserID, b.ProToken)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to update user", err)
|
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-16 17:19:55 +02:00
|
|
|
user, err := h.database.GetUser(ctx, u.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return ginresp.APIError(g, 500, apierr.DATABASE_ERROR, "Failed to query (updated) user", err)
|
|
|
|
}
|
2023-05-28 13:39:20 +02:00
|
|
|
|
2024-09-15 21:07:46 +02:00
|
|
|
return finishSuccess(ginext.JSON(http.StatusOK, user.PreMarshal()))
|
2024-07-16 17:19:55 +02:00
|
|
|
})
|
2023-05-28 13:39:20 +02:00
|
|
|
}
|