feat(admin): add super admin initialization endpoint
Add POST /api/v1/admin/setup-super-admin endpoint to initialize the first super admin. The endpoint can only be used once - after a super admin exists, subsequent requests are rejected. Requires valid setup_secret configured via APP_SETUP_SECRET environment variable or setup_secret in config file.
This commit is contained in:
38
internal/handler/setup_handler.go
Normal file
38
internal/handler/setup_handler.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"with_you/internal/pkg/response"
|
||||
"with_you/internal/service"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type SetupHandler struct {
|
||||
setupService service.SetupService
|
||||
}
|
||||
|
||||
func NewSetupHandler(setupService service.SetupService) *SetupHandler {
|
||||
return &SetupHandler{
|
||||
setupService: setupService,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *SetupHandler) SetupSuperAdmin(c *gin.Context) {
|
||||
type SetupSuperAdminRequest struct {
|
||||
UserID string `json:"user_id" binding:"required"`
|
||||
SetupSecret string `json:"setup_secret" binding:"required"`
|
||||
}
|
||||
|
||||
var req SetupSuperAdminRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.BadRequest(c, "user_id and setup_secret are required")
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.setupService.SetupSuperAdmin(c.Request.Context(), req.UserID, req.SetupSecret); err != nil {
|
||||
response.HandleError(c, err, "failed to setup super admin")
|
||||
return
|
||||
}
|
||||
|
||||
response.SuccessWithMessage(c, "超级管理员设置成功", nil)
|
||||
}
|
||||
Reference in New Issue
Block a user