Update user.go

This commit is contained in:
analogpvt 2023-05-19 23:10:10 +05:30 committed by GitHub
parent 2ad75c1186
commit 87a54f52c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -18,7 +18,7 @@ type LoginRequest struct {
func Login(c *gin.Context) {
if !common.PasswordLoginEnabled {
c.JSON(http.StatusOK, gin.H{
"message": "管理员关闭了密码登录",
"message": "Admin turned off password login",
"success": false,
})
return
@ -27,7 +27,7 @@ func Login(c *gin.Context) {
err := json.NewDecoder(c.Request.Body).Decode(&loginRequest)
if err != nil {
c.JSON(http.StatusOK, gin.H{
"message": "无效的参数",
"message": "invalid parameter",
"success": false,
})
return
@ -36,7 +36,7 @@ func Login(c *gin.Context) {
password := loginRequest.Password
if username == "" || password == "" {
c.JSON(http.StatusOK, gin.H{
"message": "无效的参数",
"message": "invalid parameter",
"success": false,
})
return
@ -66,7 +66,7 @@ func setupLogin(user *model.User, c *gin.Context) {
err := session.Save()
if err != nil {
c.JSON(http.StatusOK, gin.H{
"message": "无法保存会话信息,请重试",
"message": "Unable to save session information, please try again",
"success": false,
})
return
@ -105,14 +105,14 @@ func Logout(c *gin.Context) {
func Register(c *gin.Context) {
if !common.RegisterEnabled {
c.JSON(http.StatusOK, gin.H{
"message": "管理员关闭了新用户注册",
"message": "Admin turned off new user registration",
"success": false,
})
return
}
if !common.PasswordRegisterEnabled {
c.JSON(http.StatusOK, gin.H{
"message": "管理员关闭了通过密码进行注册,请使用第三方账户验证的形式进行注册",
"message": "The administrator has closed the registration by password, please use the form of third-party account verification to register",
"success": false,
})
return
@ -122,14 +122,14 @@ func Register(c *gin.Context) {
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无效的参数",
"message": "invalid parameter",
})
return
}
if err := common.Validate.Struct(&user); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "输入不合法 " + err.Error(),
"message": "Illegal input " + err.Error(),
})
return
}
@ -137,14 +137,14 @@ func Register(c *gin.Context) {
if user.Email == "" || user.VerificationCode == "" {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "管理员开启了邮箱验证,请输入邮箱地址和验证码",
"message": "The administrator has enabled email verification, please enter the email address and verification code",
})
return
}
if !common.VerifyCodeWithKey(user.Email, user.VerificationCode, common.EmailVerificationPurpose) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "验证码错误或已过期",
"message": "The verification code is wrong or has expired",
})
return
}
@ -231,7 +231,7 @@ func GetUser(c *gin.Context) {
if myRole <= user.Role {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无权获取同级或更高等级用户的信息",
"message": "Do not have the right to obtain the information of users of the same level or higher level",
})
return
}
@ -258,7 +258,7 @@ func GenerateAccessToken(c *gin.Context) {
if model.DB.Where("token = ?", user.AccessToken).First(user).RowsAffected != 0 {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "请重试,系统生成的 UUID 竟然重复了",
"message": "Please try again, the UUID generated by the system is duplicated",
})
return
}
@ -303,7 +303,7 @@ func UpdateUser(c *gin.Context) {
if err != nil || updatedUser.Id == 0 {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无效的参数",
"message": "invalid parameter",
})
return
}
@ -313,7 +313,7 @@ func UpdateUser(c *gin.Context) {
if err := common.Validate.Struct(&updatedUser); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "输入不合法 " + err.Error(),
"message": "Illegal input " + err.Error(),
})
return
}
@ -329,14 +329,14 @@ func UpdateUser(c *gin.Context) {
if myRole <= originUser.Role {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无权更新同权限等级或更高权限等级的用户信息",
"message": "Not authorized to update user information at the same level or higher",
})
return
}
if myRole <= updatedUser.Role {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无权将其他用户权限等级提升到大于等于自己的权限等级",
"message": "Do not have the right to elevate the authority level of other users to be greater than or equal to their own authority level",
})
return
}
@ -364,7 +364,7 @@ func UpdateSelf(c *gin.Context) {
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无效的参数",
"message": "invalid parameter",
})
return
}
@ -374,7 +374,7 @@ func UpdateSelf(c *gin.Context) {
if err := common.Validate.Struct(&user); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "输入不合法 " + err.Error(),
"message": "Illegal input " + err.Error(),
})
return
}
@ -426,7 +426,7 @@ func DeleteUser(c *gin.Context) {
if myRole <= originUser.Role {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无权删除同权限等级或更高权限等级的用户",
"message": "Not authorized to delete users with the same or higher privilege level",
})
return
}
@ -463,14 +463,14 @@ func CreateUser(c *gin.Context) {
if err != nil || user.Username == "" || user.Password == "" {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无效的参数",
"message": "invalid parameter",
})
return
}
if err := common.Validate.Struct(&user); err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "输入不合法 " + err.Error(),
"message": "Illegal input " + err.Error(),
})
return
}
@ -481,7 +481,7 @@ func CreateUser(c *gin.Context) {
if user.Role >= myRole {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无法创建权限大于等于自己的用户",
"message": "Unable to create users with permissions greater than or equal to their own",
})
return
}
@ -519,7 +519,7 @@ func ManageUser(c *gin.Context) {
if err != nil {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无效的参数",
"message": "invalid parameter",
})
return
}
@ -531,7 +531,7 @@ func ManageUser(c *gin.Context) {
if user.Id == 0 {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "用户不存在",
"message": "User does not exist",
})
return
}
@ -539,7 +539,7 @@ func ManageUser(c *gin.Context) {
if myRole <= user.Role && myRole != common.RoleRootUser {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无权更新同权限等级或更高权限等级的用户信息",
"message": "Not authorized to update user information at the same level or higher",
})
return
}
@ -549,7 +549,7 @@ func ManageUser(c *gin.Context) {
if user.Role == common.RoleRootUser {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无法禁用超级管理员用户",
"message": "Unable to disable super admin user",
})
return
}
@ -559,7 +559,7 @@ func ManageUser(c *gin.Context) {
if user.Role == common.RoleRootUser {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无法删除超级管理员用户",
"message": "Unable to delete super admin user",
})
return
}
@ -574,14 +574,14 @@ func ManageUser(c *gin.Context) {
if myRole != common.RoleRootUser {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "普通管理员用户无法提升其他用户为管理员",
"message": "Regular admin users cannot promote other users to be admins",
})
return
}
if user.Role >= common.RoleAdminUser {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "该用户已经是管理员",
"message": "This user is already an administrator",
})
return
}
@ -590,14 +590,14 @@ func ManageUser(c *gin.Context) {
if user.Role == common.RoleRootUser {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "无法降级超级管理员用户",
"message": "Unable to demote super administrator users",
})
return
}
if user.Role == common.RoleCommonUser {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "该用户已经是普通用户",
"message": "This user is already a regular user",
})
return
}
@ -629,7 +629,7 @@ func EmailBind(c *gin.Context) {
if !common.VerifyCodeWithKey(email, code, common.EmailVerificationPurpose) {
c.JSON(http.StatusOK, gin.H{
"success": false,
"message": "验证码错误或已过期",
"message": "Verification code is incorrect or has expired",
})
return
}