(c *gin.Context)
| 51 | } |
| 52 | |
| 53 | func PostSetup(c *gin.Context) { |
| 54 | // Check if setup is already completed |
| 55 | if constant.Setup { |
| 56 | c.JSON(400, gin.H{ |
| 57 | "success": false, |
| 58 | "message": "系统已经初始化完成", |
| 59 | }) |
| 60 | return |
| 61 | } |
| 62 | |
| 63 | // Check if root user already exists |
| 64 | rootExists := model.RootUserExists() |
| 65 | |
| 66 | var req SetupRequest |
| 67 | err := c.ShouldBindJSON(&req) |
| 68 | if err != nil { |
| 69 | c.JSON(400, gin.H{ |
| 70 | "success": false, |
| 71 | "message": "请求参数有误", |
| 72 | }) |
| 73 | return |
| 74 | } |
| 75 | |
| 76 | // If root doesn't exist, validate and create admin account |
| 77 | if !rootExists { |
| 78 | // Validate username length: max 12 characters to align with model.User validation |
| 79 | if len(req.Username) > 12 { |
| 80 | c.JSON(400, gin.H{ |
| 81 | "success": false, |
| 82 | "message": "用户名长度不能超过12个字符", |
| 83 | }) |
| 84 | return |
| 85 | } |
| 86 | // Validate password |
| 87 | if req.Password != req.ConfirmPassword { |
| 88 | c.JSON(400, gin.H{ |
| 89 | "success": false, |
| 90 | "message": "两次输入的密码不一致", |
| 91 | }) |
| 92 | return |
| 93 | } |
| 94 | |
| 95 | if len(req.Password) < 8 { |
| 96 | c.JSON(400, gin.H{ |
| 97 | "success": false, |
| 98 | "message": "密码长度至少为8个字符", |
| 99 | }) |
| 100 | return |
| 101 | } |
| 102 | |
| 103 | // Create root user |
| 104 | hashedPassword, err := common.Password2Hash(req.Password) |
| 105 | if err != nil { |
| 106 | c.JSON(500, gin.H{ |
| 107 | "success": false, |
| 108 | "message": "系统错误: " + err.Error(), |
| 109 | }) |
| 110 | return |
nothing calls this directly
no test coverage detected