mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-09-24 16:05:44 +08:00
Add admin compliance acknowledgement gate
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/ip"
|
||||
"github.com/Wei-Shaw/sub2api/internal/pkg/response"
|
||||
"github.com/Wei-Shaw/sub2api/internal/server/middleware"
|
||||
"github.com/Wei-Shaw/sub2api/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type ComplianceHandler struct {
|
||||
settingService *service.SettingService
|
||||
}
|
||||
|
||||
func NewComplianceHandler(settingService *service.SettingService) *ComplianceHandler {
|
||||
return &ComplianceHandler{settingService: settingService}
|
||||
}
|
||||
|
||||
type AcceptAdminComplianceRequest struct {
|
||||
Phrase string `json:"phrase" binding:"required"`
|
||||
Language string `json:"language"`
|
||||
}
|
||||
|
||||
func (h *ComplianceHandler) GetStatus(c *gin.Context) {
|
||||
subject, ok := middleware.GetAuthSubjectFromContext(c)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "User not authenticated")
|
||||
return
|
||||
}
|
||||
|
||||
status, err := h.settingService.GetAdminComplianceStatus(c.Request.Context(), subject.UserID)
|
||||
if err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, status)
|
||||
}
|
||||
|
||||
func (h *ComplianceHandler) Accept(c *gin.Context) {
|
||||
var req AcceptAdminComplianceRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "Invalid request: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
subject, ok := middleware.GetAuthSubjectFromContext(c)
|
||||
if !ok {
|
||||
response.Unauthorized(c, "User not authenticated")
|
||||
return
|
||||
}
|
||||
|
||||
status, err := h.settingService.AcceptAdminCompliance(c.Request.Context(), service.AdminComplianceAcceptInput{
|
||||
AdminUserID: subject.UserID,
|
||||
Phrase: req.Phrase,
|
||||
Language: req.Language,
|
||||
IPAddress: ip.GetClientIP(c),
|
||||
UserAgent: strings.TrimSpace(c.GetHeader("User-Agent")),
|
||||
})
|
||||
if err != nil {
|
||||
response.ErrorFrom(c, err)
|
||||
return
|
||||
}
|
||||
response.Success(c, status)
|
||||
}
|
||||
@@ -36,6 +36,7 @@ type AdminHandlers struct {
|
||||
ContentModeration *admin.ContentModerationHandler
|
||||
Payment *admin.PaymentHandler
|
||||
Affiliate *admin.AffiliateHandler
|
||||
Compliance *admin.ComplianceHandler
|
||||
}
|
||||
|
||||
// Handlers contains all HTTP handlers
|
||||
|
||||
@@ -277,6 +277,7 @@ func RegisterPageRoutes(v1 *gin.RouterGroup, dataDir string, jwtAuth gin.Handler
|
||||
// Admin-only: list all available pages
|
||||
adminPages := v1.Group("/pages")
|
||||
adminPages.Use(adminAuth)
|
||||
adminPages.Use(middleware2.AdminComplianceGuard(settingService))
|
||||
{
|
||||
adminPages.GET("", h.ListPages)
|
||||
}
|
||||
|
||||
@@ -39,6 +39,7 @@ func ProvideAdminHandlers(
|
||||
contentModerationHandler *admin.ContentModerationHandler,
|
||||
paymentHandler *admin.PaymentHandler,
|
||||
affiliateHandler *admin.AffiliateHandler,
|
||||
complianceHandler *admin.ComplianceHandler,
|
||||
) *AdminHandlers {
|
||||
return &AdminHandlers{
|
||||
Dashboard: dashboardHandler,
|
||||
@@ -71,6 +72,7 @@ func ProvideAdminHandlers(
|
||||
ContentModeration: contentModerationHandler,
|
||||
Payment: paymentHandler,
|
||||
Affiliate: affiliateHandler,
|
||||
Compliance: complianceHandler,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -184,6 +186,7 @@ var ProviderSet = wire.NewSet(
|
||||
admin.NewContentModerationHandler,
|
||||
admin.NewPaymentHandler,
|
||||
admin.NewAffiliateHandler,
|
||||
admin.NewComplianceHandler,
|
||||
|
||||
// AdminHandlers and Handlers constructors
|
||||
ProvideAdminHandlers,
|
||||
|
||||
Reference in New Issue
Block a user