36 lines
710 B
Go
36 lines
710 B
Go
|
|
package middleware
|
||
|
|
|
||
|
|
import (
|
||
|
|
"github.com/gin-gonic/gin"
|
||
|
|
|
||
|
|
"carrot_bbs/internal/model"
|
||
|
|
"carrot_bbs/internal/pkg/response"
|
||
|
|
"carrot_bbs/internal/repository"
|
||
|
|
)
|
||
|
|
|
||
|
|
func RequireVerification(userRepo repository.UserRepository) gin.HandlerFunc {
|
||
|
|
return func(c *gin.Context) {
|
||
|
|
userID, exists := c.Get("user_id")
|
||
|
|
if !exists {
|
||
|
|
response.Unauthorized(c, "未授权")
|
||
|
|
c.Abort()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
user, err := userRepo.GetByID(userID.(string))
|
||
|
|
if err != nil {
|
||
|
|
response.HandleError(c, err, "获取用户信息失败")
|
||
|
|
c.Abort()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
if user.VerificationStatus != model.VerificationStatusApproved {
|
||
|
|
response.Forbidden(c, "需要完成身份认证")
|
||
|
|
c.Abort()
|
||
|
|
return
|
||
|
|
}
|
||
|
|
|
||
|
|
c.Next()
|
||
|
|
}
|
||
|
|
}
|