First merge from develop to main (WIP) #7
@@ -3,4 +3,10 @@ package exception
|
|||||||
const (
|
const (
|
||||||
CommonErrorInvalidInput = "00001"
|
CommonErrorInvalidInput = "00001"
|
||||||
CommonErrorUnauthorized = "00002"
|
CommonErrorUnauthorized = "00002"
|
||||||
|
CommonErrorInternal = "00003"
|
||||||
|
CommonErrorPermissionDenied = "00004"
|
||||||
|
CommonErrorUuidParseFailed = "00005"
|
||||||
|
CommonErrorDatabase = "00006"
|
||||||
|
CommonErrorMissingUserId = "00007"
|
||||||
|
CommonErrorUserNotFound = "00008"
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,4 +2,59 @@ package exception
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
ApiVersionNotFound = "00001"
|
ApiVersionNotFound = "00001"
|
||||||
|
|
||||||
|
// Auth Service
|
||||||
|
// Endpoint: AuthRedirectEndpoint (01)
|
||||||
|
AuthRedirectTokenInvalid = "00001"
|
||||||
|
AuthRedirectClientNotFound = "00002"
|
||||||
|
AuthRedirectUriMismatch = "00003"
|
||||||
|
AuthRedirectInvalidUri = "00004"
|
||||||
|
|
||||||
|
// Endpoint: AuthMagicEndpoint (02)
|
||||||
|
AuthMagicTurnstileFailed = "00001"
|
||||||
|
AuthMagicCodeGenFailed = "00002"
|
||||||
|
AuthMagicInvalidExternalUrl = "00003"
|
||||||
|
AuthMagicInvalidEmailConfig = "00004"
|
||||||
|
|
||||||
|
// Endpoint: AuthTokenEndpoint (03)
|
||||||
|
AuthTokenInvalidToken = "00001"
|
||||||
|
AuthTokenGenFailed = "00002"
|
||||||
|
|
||||||
|
// Endpoint: AuthRefreshEndpoint (04)
|
||||||
|
AuthRefreshInvalidToken = "00001"
|
||||||
|
AuthRefreshRenewFailed = "00002"
|
||||||
|
|
||||||
|
// Endpoint: AuthExchangeEndpoint (05)
|
||||||
|
AuthExchangeGetUserIdFailed = "00001"
|
||||||
|
AuthExchangeCodeGenFailed = "00002"
|
||||||
|
AuthExchangeInvalidRedirectUri = "00003"
|
||||||
|
|
||||||
|
// Event Service
|
||||||
|
// Endpoint: EventInfoEndpoint (01)
|
||||||
|
EventInfoNotFound = "00001"
|
||||||
|
|
||||||
|
// Endpoint: EventCheckinEndpoint (02)
|
||||||
|
EventCheckinGenCodeFailed = "00001"
|
||||||
|
|
||||||
|
// Endpoint: EventCheckinQueryEndpoint (03)
|
||||||
|
EventCheckinQueryRecordNotFound = "00001"
|
||||||
|
|
||||||
|
// Endpoint: EventCheckinSubmitEndpoint (04)
|
||||||
|
// (None)
|
||||||
|
|
||||||
|
// User Service
|
||||||
|
// Endpoint: UserInfoEndpoint (01)
|
||||||
|
// (None)
|
||||||
|
|
||||||
|
// Endpoint: UserUpdateEndpoint (02)
|
||||||
|
// (None)
|
||||||
|
|
||||||
|
// Endpoint: UserListEndpoint (03)
|
||||||
|
UserListMeilisearchFailed = "00001"
|
||||||
|
|
||||||
|
// Endpoint: UserFullEndpoint (04)
|
||||||
|
// (None)
|
||||||
|
|
||||||
|
// Endpoint: UserCreateEndpoint (05)
|
||||||
|
// (None)
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package middleware
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"nixcn-cms/data"
|
"nixcn-cms/data"
|
||||||
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/utils"
|
"nixcn-cms/utils"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -15,19 +16,40 @@ func Permission(requiredLevel uint) gin.HandlerFunc {
|
|||||||
if !ok {
|
if !ok {
|
||||||
userIdOrig, ok := c.Get("user_id")
|
userIdOrig, ok := c.Get("user_id")
|
||||||
if !ok || userIdOrig.(string) == "" {
|
if !ok || userIdOrig.(string) == "" {
|
||||||
utils.HttpAbort(c, 401, "", "missing user id")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusUser).
|
||||||
|
SetService(exception.MiddlewarePermissionService).
|
||||||
|
SetEndpoint(exception.MiddlewareEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorMissingUserId).
|
||||||
|
Build()
|
||||||
|
utils.HttpAbort(c, 401, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userId, err := uuid.Parse(userIdOrig.(string))
|
userId, err := uuid.Parse(userIdOrig.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpAbort(c, 500, "", "error parsing user id")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.MiddlewarePermissionService).
|
||||||
|
SetEndpoint(exception.MiddlewareEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpAbort(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userData, err := new(data.User).GetByUserId(userId)
|
userData, err := new(data.User).GetByUserId(userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpAbort(c, 404, "", "user not found")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusUser).
|
||||||
|
SetService(exception.MiddlewarePermissionService).
|
||||||
|
SetEndpoint(exception.MiddlewareEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUserNotFound).
|
||||||
|
Build()
|
||||||
|
utils.HttpAbort(c, 404, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -38,7 +60,14 @@ func Permission(requiredLevel uint) gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if permissionLevel < requiredLevel {
|
if permissionLevel < requiredLevel {
|
||||||
utils.HttpAbort(c, 403, "", "permission denied")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusUser).
|
||||||
|
SetService(exception.MiddlewarePermissionService).
|
||||||
|
SetEndpoint(exception.MiddlewareEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorPermissionDenied).
|
||||||
|
Build()
|
||||||
|
utils.HttpAbort(c, 403, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/url"
|
"net/url"
|
||||||
"nixcn-cms/data"
|
"nixcn-cms/data"
|
||||||
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/pkgs/authcode"
|
"nixcn-cms/pkgs/authcode"
|
||||||
"nixcn-cms/utils"
|
"nixcn-cms/utils"
|
||||||
|
|
||||||
@@ -23,38 +24,80 @@ func Exchange(c *gin.Context) {
|
|||||||
err := c.ShouldBindJSON(&exchangeReq)
|
err := c.ShouldBindJSON(&exchangeReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
utils.HttpResponse(c, 400, "", "invalid request")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthExchangeEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userIdOrig, ok := c.Get("user_id")
|
userIdOrig, ok := c.Get("user_id")
|
||||||
if !ok {
|
if !ok {
|
||||||
utils.HttpResponse(c, 401, "", "unauthorized")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthExchangeEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUnauthorized).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 401, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userId, err := uuid.Parse(userIdOrig.(string))
|
userId, err := uuid.Parse(userIdOrig.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "failed to parse uuid")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthExchangeEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userData := new(data.User)
|
userData := new(data.User)
|
||||||
user, err := userData.GetByUserId(userId)
|
user, err := userData.GetByUserId(userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "failed to get user id")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthExchangeEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthExchangeGetUserIdFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
code, err := authcode.NewAuthCode(exchangeReq.ClientId, user.Email)
|
code, err := authcode.NewAuthCode(exchangeReq.ClientId, user.Email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "code gen failed")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthExchangeEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthExchangeCodeGenFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
url, err := url.Parse(exchangeReq.RedirectUri)
|
url, err := url.Parse(exchangeReq.RedirectUri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 400, "", "invalid redirect uri")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthExchangeEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthExchangeInvalidRedirectUri).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
query := url.Query()
|
query := url.Query()
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package auth
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/pkgs/authcode"
|
"nixcn-cms/pkgs/authcode"
|
||||||
"nixcn-cms/pkgs/email"
|
"nixcn-cms/pkgs/email"
|
||||||
"nixcn-cms/pkgs/turnstile"
|
"nixcn-cms/pkgs/turnstile"
|
||||||
@@ -23,27 +24,55 @@ func Magic(c *gin.Context) {
|
|||||||
// Parse request
|
// Parse request
|
||||||
var req MagicRequest
|
var req MagicRequest
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
utils.HttpResponse(c, 400, "", "invalid request")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthMagicEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cloudflare turnstile
|
// Cloudflare turnstile
|
||||||
ok, err := turnstile.VerifyTurnstile(req.TurnstileToken, c.ClientIP())
|
ok, err := turnstile.VerifyTurnstile(req.TurnstileToken, c.ClientIP())
|
||||||
if err != nil || !ok {
|
if err != nil || !ok {
|
||||||
utils.HttpResponse(c, 403, "", "turnstile failed")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthMagicEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthMagicTurnstileFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 403, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
code, err := authcode.NewAuthCode(req.ClientId, req.Email)
|
code, err := authcode.NewAuthCode(req.ClientId, req.Email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "code gen failed")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthMagicEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthMagicCodeGenFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
externalUrl := viper.GetString("server.external_url")
|
externalUrl := viper.GetString("server.external_url")
|
||||||
url, err := url.Parse(externalUrl)
|
url, err := url.Parse(externalUrl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "invalid external url")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthMagicEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthMagicInvalidExternalUrl).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,7 +95,14 @@ func Magic(c *gin.Context) {
|
|||||||
// Send email using resend
|
// Send email using resend
|
||||||
emailClient, err := new(email.Client).NewSMTPClient()
|
emailClient, err := new(email.Client).NewSMTPClient()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "invalid email config")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthMagicEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthMagicInvalidEmailConfig).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
emailClient.Send(
|
emailClient.Send(
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package auth
|
|||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"nixcn-cms/data"
|
"nixcn-cms/data"
|
||||||
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/pkgs/authcode"
|
"nixcn-cms/pkgs/authcode"
|
||||||
"nixcn-cms/utils"
|
"nixcn-cms/utils"
|
||||||
|
|
||||||
@@ -14,19 +15,40 @@ import (
|
|||||||
func Redirect(c *gin.Context) {
|
func Redirect(c *gin.Context) {
|
||||||
clientId := c.Query("client_id")
|
clientId := c.Query("client_id")
|
||||||
if clientId == "" {
|
if clientId == "" {
|
||||||
utils.HttpResponse(c, 400, "", "invalid request")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthRedirectEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
redirectUri := c.Query("redirect_uri")
|
redirectUri := c.Query("redirect_uri")
|
||||||
if redirectUri == "" {
|
if redirectUri == "" {
|
||||||
utils.HttpResponse(c, 400, "", "invalid request")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthRedirectEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
state := c.Query("state")
|
state := c.Query("state")
|
||||||
if state == "" {
|
if state == "" {
|
||||||
utils.HttpResponse(c, 400, "", "invalid request")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthRedirectEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +57,14 @@ func Redirect(c *gin.Context) {
|
|||||||
// Verify email token
|
// Verify email token
|
||||||
authCode, ok := authcode.VerifyAuthCode(code)
|
authCode, ok := authcode.VerifyAuthCode(code)
|
||||||
if !ok {
|
if !ok {
|
||||||
utils.HttpResponse(c, 403, "", "invalid or expired token")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthRedirectEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthRedirectTokenInvalid).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 403, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,11 +81,25 @@ func Redirect(c *gin.Context) {
|
|||||||
user.Username = user.UserId.String()
|
user.Username = user.UserId.String()
|
||||||
user.PermissionLevel = 10
|
user.PermissionLevel = 10
|
||||||
if err := user.Create(); err != nil {
|
if err := user.Create(); err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "internal server error")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthRedirectEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInternal).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
utils.HttpResponse(c, 500, "", "internal server error")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthRedirectEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInternal).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -64,25 +107,53 @@ func Redirect(c *gin.Context) {
|
|||||||
clientData := new(data.Client)
|
clientData := new(data.Client)
|
||||||
client, err := clientData.GetClientByClientId(clientId)
|
client, err := clientData.GetClientByClientId(clientId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 400, "", "client not found")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthRedirectEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthRedirectClientNotFound).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
err = client.ValidateRedirectURI(redirectUri)
|
err = client.ValidateRedirectURI(redirectUri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 400, "", "redirect uri not match")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthRedirectEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthRedirectUriMismatch).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
newCode, err := authcode.NewAuthCode(clientId, authCode.Email)
|
newCode, err := authcode.NewAuthCode(clientId, authCode.Email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "internal server error")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthRedirectEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInternal).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
url, err := url.Parse(redirectUri)
|
url, err := url.Parse(redirectUri)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 400, "", "invalid redirect uri")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthRedirectEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthRedirectInvalidUri).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
query := url.Query()
|
query := url.Query()
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
package auth
|
package auth
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/pkgs/authtoken"
|
"nixcn-cms/pkgs/authtoken"
|
||||||
"nixcn-cms/utils"
|
"nixcn-cms/utils"
|
||||||
|
|
||||||
@@ -14,7 +15,14 @@ func Refresh(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := c.ShouldBindJSON(&req); err != nil {
|
if err := c.ShouldBindJSON(&req); err != nil {
|
||||||
utils.HttpResponse(c, 400, "", "invalid request")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthRefreshEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -24,13 +32,27 @@ func Refresh(c *gin.Context) {
|
|||||||
|
|
||||||
accessToken, err := JwtTool.RefreshAccessToken(req.RefreshToken)
|
accessToken, err := JwtTool.RefreshAccessToken(req.RefreshToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 401, "", "invalid refresh token")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthRefreshEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthRefreshInvalidToken).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 401, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshToken, err := JwtTool.RenewRefreshToken(req.RefreshToken)
|
refreshToken, err := JwtTool.RenewRefreshToken(req.RefreshToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "error renew refresh token")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthRefreshEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthRefreshRenewFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package auth
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"nixcn-cms/data"
|
"nixcn-cms/data"
|
||||||
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/pkgs/authcode"
|
"nixcn-cms/pkgs/authcode"
|
||||||
"nixcn-cms/pkgs/authtoken"
|
"nixcn-cms/pkgs/authtoken"
|
||||||
"nixcn-cms/utils"
|
"nixcn-cms/utils"
|
||||||
@@ -19,20 +20,41 @@ func Token(c *gin.Context) {
|
|||||||
|
|
||||||
err := c.ShouldBindJSON(&req)
|
err := c.ShouldBindJSON(&req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 400, "", "invalid request")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthTokenEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
authCode, ok := authcode.VerifyAuthCode(req.Code)
|
authCode, ok := authcode.VerifyAuthCode(req.Code)
|
||||||
if !ok {
|
if !ok {
|
||||||
utils.HttpResponse(c, 403, "", "invalid or expired token")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthTokenEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthTokenInvalidToken).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 403, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userData := new(data.User)
|
userData := new(data.User)
|
||||||
user, err := userData.GetByEmail(authCode.Email)
|
user, err := userData.GetByEmail(authCode.Email)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "internal server error")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthTokenEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInternal).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,7 +64,14 @@ func Token(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
accessToken, refreshToken, err := JwtTool.IssueTokens(authCode.ClientId, user.UserId)
|
accessToken, refreshToken, err := JwtTool.IssueTokens(authCode.ClientId, user.UserId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "error generating tokens")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.AuthService).
|
||||||
|
SetEndpoint(exception.AuthTokenEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.AuthTokenGenFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package event
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"nixcn-cms/data"
|
"nixcn-cms/data"
|
||||||
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/utils"
|
"nixcn-cms/utils"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -13,31 +14,66 @@ func Checkin(c *gin.Context) {
|
|||||||
data := new(data.Attendance)
|
data := new(data.Attendance)
|
||||||
userIdOrig, ok := c.Get("user_id")
|
userIdOrig, ok := c.Get("user_id")
|
||||||
if !ok {
|
if !ok {
|
||||||
utils.HttpResponse(c, 403, "", "userid error")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusUser).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventCheckinEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorMissingUserId).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 403, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userId, err := uuid.Parse(userIdOrig.(string))
|
userId, err := uuid.Parse(userIdOrig.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "failed to parse uuid")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventCheckinEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get event id from query
|
// Get event id from query
|
||||||
eventIdOrig, ok := c.GetQuery("event_id")
|
eventIdOrig, ok := c.GetQuery("event_id")
|
||||||
if !ok {
|
if !ok {
|
||||||
utils.HttpResponse(c, 400, "", "undefinded event id")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventCheckinEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse event id to uuid
|
// Parse event id to uuid
|
||||||
eventId, err := uuid.Parse(eventIdOrig)
|
eventId, err := uuid.Parse(eventIdOrig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "error parsing string to uuid")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventCheckinEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
data.UserId = userId
|
data.UserId = userId
|
||||||
code, err := data.GenCheckinCode(eventId)
|
code, err := data.GenCheckinCode(eventId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "error generating code")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventCheckinEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.EventCheckinGenCodeFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -56,7 +92,14 @@ func CheckinSubmit(c *gin.Context) {
|
|||||||
attendanceData := new(data.Attendance)
|
attendanceData := new(data.Attendance)
|
||||||
err := attendanceData.VerifyCheckinCode(req.ChekinCode)
|
err := attendanceData.VerifyCheckinCode(req.ChekinCode)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 400, "", "error verify checkin code")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventCheckinSubmitEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,23 +109,51 @@ func CheckinSubmit(c *gin.Context) {
|
|||||||
func CheckinQuery(c *gin.Context) {
|
func CheckinQuery(c *gin.Context) {
|
||||||
userIdOrig, ok := c.Get("user_id")
|
userIdOrig, ok := c.Get("user_id")
|
||||||
if !ok {
|
if !ok {
|
||||||
utils.HttpResponse(c, 400, "", "userid error")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusUser).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorMissingUserId).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userId, err := uuid.Parse(userIdOrig.(string))
|
userId, err := uuid.Parse(userIdOrig.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "failed to parse uuid")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
eventIdOrig, ok := c.GetQuery("event_id")
|
eventIdOrig, ok := c.GetQuery("event_id")
|
||||||
if !ok {
|
if !ok {
|
||||||
utils.HttpResponse(c, 400, "", "could not found event_id")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
eventId, err := uuid.Parse(eventIdOrig)
|
eventId, err := uuid.Parse(eventIdOrig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 400, "", "event_id is not valid")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -90,10 +161,24 @@ func CheckinQuery(c *gin.Context) {
|
|||||||
attendance, err := attendanceData.GetAttendance(userId, eventId)
|
attendance, err := attendanceData.GetAttendance(userId, eventId)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "database error")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorDatabase).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
} else if attendance == nil {
|
} else if attendance == nil {
|
||||||
utils.HttpResponse(c, 404, "", "event checkin record not found")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventCheckinQueryEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.EventCheckinQueryRecordNotFound).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 404, errorCode)
|
||||||
return
|
return
|
||||||
} else if attendance.CheckinAt.IsZero() {
|
} else if attendance.CheckinAt.IsZero() {
|
||||||
utils.HttpResponse(c, 200, "", "success", gin.H{"checkin_at": nil})
|
utils.HttpResponse(c, 200, "", "success", gin.H{"checkin_at": nil})
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package event
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"nixcn-cms/data"
|
"nixcn-cms/data"
|
||||||
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/utils"
|
"nixcn-cms/utils"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -13,20 +14,41 @@ func Info(c *gin.Context) {
|
|||||||
eventData := new(data.Event)
|
eventData := new(data.Event)
|
||||||
eventIdOrig, ok := c.GetQuery("event_id")
|
eventIdOrig, ok := c.GetQuery("event_id")
|
||||||
if !ok {
|
if !ok {
|
||||||
utils.HttpResponse(c, 400, "", "undefinded event id")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventInfoEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse event id
|
// Parse event id
|
||||||
eventId, err := uuid.Parse(eventIdOrig)
|
eventId, err := uuid.Parse(eventIdOrig)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "error parsing string to uuid")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventInfoEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
event, err := eventData.GetEventById(eventId)
|
event, err := eventData.GetEventById(eventId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 404, "", "event id not found")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.EventService).
|
||||||
|
SetEndpoint(exception.EventInfoEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.EventInfoNotFound).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 404, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package user
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"nixcn-cms/data"
|
"nixcn-cms/data"
|
||||||
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/utils"
|
"nixcn-cms/utils"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -11,24 +12,52 @@ import (
|
|||||||
func Full(c *gin.Context) {
|
func Full(c *gin.Context) {
|
||||||
userIdOrig, ok := c.Get("user_id")
|
userIdOrig, ok := c.Get("user_id")
|
||||||
if !ok {
|
if !ok {
|
||||||
utils.HttpResponse(c, 403, "", "userid error")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusUser).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserFullEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorMissingUserId).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 403, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userId, err := uuid.Parse(userIdOrig.(string))
|
userId, err := uuid.Parse(userIdOrig.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "failed to parse uuid")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserFullEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
userData, err := new(data.User).GetByUserId(userId)
|
userData, err := new(data.User).GetByUserId(userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 404, "", "user not found")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusUser).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserFullEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUserNotFound).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 404, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
users, err := userData.GetFullTable()
|
users, err := userData.GetFullTable()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "database error")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserFullEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorDatabase).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package user
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"nixcn-cms/data"
|
"nixcn-cms/data"
|
||||||
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/utils"
|
"nixcn-cms/utils"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@@ -12,19 +13,40 @@ func Info(c *gin.Context) {
|
|||||||
userData := new(data.User)
|
userData := new(data.User)
|
||||||
userIdOrig, ok := c.Get("user_id")
|
userIdOrig, ok := c.Get("user_id")
|
||||||
if !ok {
|
if !ok {
|
||||||
utils.HttpResponse(c, 403, "", "userid error")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusUser).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserInfoEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorMissingUserId).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 403, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userId, err := uuid.Parse(userIdOrig.(string))
|
userId, err := uuid.Parse(userIdOrig.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "failed to parse uuid")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserInfoEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get user from database
|
// Get user from database
|
||||||
user, err := userData.GetByUserId(userId)
|
user, err := userData.GetByUserId(userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 404, "", "user not found")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusUser).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserInfoEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUserNotFound).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 404, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package user
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"nixcn-cms/data"
|
"nixcn-cms/data"
|
||||||
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/utils"
|
"nixcn-cms/utils"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
@@ -16,26 +17,54 @@ func List(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
offset, ok := c.GetQuery("offset")
|
offset, ok := c.GetQuery("offset")
|
||||||
if !ok {
|
if !ok {
|
||||||
utils.HttpResponse(c, 400, "", "offset not found")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserListEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse string to int64
|
// Parse string to int64
|
||||||
limitNum, err := strconv.ParseInt(limit, 10, 64)
|
limitNum, err := strconv.ParseInt(limit, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 400, "", "parse string to int error")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserListEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
offsetNum, err := strconv.ParseInt(offset, 10, 64)
|
offsetNum, err := strconv.ParseInt(offset, 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 400, "", "parse string to int error")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserListEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get user list from search engine
|
// Get user list from search engine
|
||||||
list, err := new(data.User).FastListUsers(limitNum, offsetNum)
|
list, err := new(data.User).FastListUsers(limitNum, offsetNum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "failed list users from meilisearch")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserListEndpoint).
|
||||||
|
SetType(exception.ErrorTypeSpecific).
|
||||||
|
SetOriginal(exception.UserListMeilisearchFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
userListResp := struct {
|
userListResp := struct {
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package user
|
|||||||
import (
|
import (
|
||||||
"net/url"
|
"net/url"
|
||||||
"nixcn-cms/data"
|
"nixcn-cms/data"
|
||||||
|
"nixcn-cms/exception"
|
||||||
"nixcn-cms/internal/cryptography"
|
"nixcn-cms/internal/cryptography"
|
||||||
"nixcn-cms/utils"
|
"nixcn-cms/utils"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
@@ -15,26 +16,54 @@ func Update(c *gin.Context) {
|
|||||||
// New user model
|
// New user model
|
||||||
userIdOrig, ok := c.Get("user_id")
|
userIdOrig, ok := c.Get("user_id")
|
||||||
if !ok {
|
if !ok {
|
||||||
utils.HttpResponse(c, 403, "", "userid error")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusUser).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserUpdateEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorMissingUserId).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 403, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userId, err := uuid.Parse(userIdOrig.(string))
|
userId, err := uuid.Parse(userIdOrig.(string))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "failed to parse uuid")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusServer).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserUpdateEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUuidParseFailed).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var ReqInfo data.User
|
var ReqInfo data.User
|
||||||
err = c.ShouldBindJSON(&ReqInfo)
|
err = c.ShouldBindJSON(&ReqInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 400, "", "invilad request")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserUpdateEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get user info
|
// Get user info
|
||||||
userData, err := new(data.User).GetByUserId(userId)
|
userData, err := new(data.User).GetByUserId(userId)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 500, "", "failed to find user")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusUser).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserUpdateEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorUserNotFound).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 500, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,7 +78,14 @@ func Update(c *gin.Context) {
|
|||||||
|
|
||||||
if ReqInfo.Username != "" {
|
if ReqInfo.Username != "" {
|
||||||
if len(ReqInfo.Username) < 5 || len(ReqInfo.Username) >= 255 {
|
if len(ReqInfo.Username) < 5 || len(ReqInfo.Username) >= 255 {
|
||||||
utils.HttpResponse(c, 400, "", "invalid request")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserUpdateEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userData.Username = ReqInfo.Username
|
userData.Username = ReqInfo.Username
|
||||||
@@ -57,7 +93,14 @@ func Update(c *gin.Context) {
|
|||||||
|
|
||||||
if ReqInfo.Nickname != "" {
|
if ReqInfo.Nickname != "" {
|
||||||
if utf8.RuneCountInString(ReqInfo.Nickname) > 24 {
|
if utf8.RuneCountInString(ReqInfo.Nickname) > 24 {
|
||||||
utils.HttpResponse(c, 400, "", "invalid request")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserUpdateEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userData.Nickname = ReqInfo.Nickname
|
userData.Nickname = ReqInfo.Nickname
|
||||||
@@ -65,7 +108,14 @@ func Update(c *gin.Context) {
|
|||||||
|
|
||||||
if ReqInfo.Subtitle != "" {
|
if ReqInfo.Subtitle != "" {
|
||||||
if utf8.RuneCountInString(ReqInfo.Subtitle) > 32 {
|
if utf8.RuneCountInString(ReqInfo.Subtitle) > 32 {
|
||||||
utils.HttpResponse(c, 400, "", "invalid request")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserUpdateEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userData.Subtitle = ReqInfo.Subtitle
|
userData.Subtitle = ReqInfo.Subtitle
|
||||||
@@ -74,7 +124,14 @@ func Update(c *gin.Context) {
|
|||||||
if ReqInfo.Avatar != "" {
|
if ReqInfo.Avatar != "" {
|
||||||
_, err := url.ParseRequestURI(ReqInfo.Avatar)
|
_, err := url.ParseRequestURI(ReqInfo.Avatar)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.HttpResponse(c, 400, "", "invalid request")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserUpdateEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userData.Avatar = ReqInfo.Avatar
|
userData.Avatar = ReqInfo.Avatar
|
||||||
@@ -82,7 +139,14 @@ func Update(c *gin.Context) {
|
|||||||
|
|
||||||
if ReqInfo.Bio != "" {
|
if ReqInfo.Bio != "" {
|
||||||
if !cryptography.IsBase64Std(ReqInfo.Bio) {
|
if !cryptography.IsBase64Std(ReqInfo.Bio) {
|
||||||
utils.HttpResponse(c, 400, "", "invalid request")
|
errorCode := new(exception.Builder).
|
||||||
|
SetStatus(exception.ErrorStatusClient).
|
||||||
|
SetService(exception.UserService).
|
||||||
|
SetEndpoint(exception.UserUpdateEndpoint).
|
||||||
|
SetType(exception.ErrorTypeCommon).
|
||||||
|
SetOriginal(exception.CommonErrorInvalidInput).
|
||||||
|
Build()
|
||||||
|
utils.HttpResponse(c, 400, errorCode)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
userData.Bio = ReqInfo.Bio
|
userData.Bio = ReqInfo.Bio
|
||||||
|
|||||||
Reference in New Issue
Block a user