| 443 | } |
| 444 | |
| 445 | func UpdateUser(c *gin.Context) { |
| 446 | var updatedUser model.User |
| 447 | err := json.NewDecoder(c.Request.Body).Decode(&updatedUser) |
| 448 | if err != nil || updatedUser.Id == 0 { |
| 449 | c.JSON(http.StatusOK, gin.H{ |
| 450 | "success": false, |
| 451 | "message": "无效的参数", |
| 452 | }) |
| 453 | return |
| 454 | } |
| 455 | if updatedUser.Password == "" { |
| 456 | updatedUser.Password = "$I_LOVE_U" // make Validator happy :) |
| 457 | } |
| 458 | if err := common.Validate.Struct(&updatedUser); err != nil { |
| 459 | c.JSON(http.StatusOK, gin.H{ |
| 460 | "success": false, |
| 461 | "message": "输入不合法 " + err.Error(), |
| 462 | }) |
| 463 | return |
| 464 | } |
| 465 | originUser, err := model.GetUserById(updatedUser.Id, false) |
| 466 | if err != nil { |
| 467 | common.ApiError(c, err) |
| 468 | return |
| 469 | } |
| 470 | myRole := c.GetInt("role") |
| 471 | if myRole <= originUser.Role && myRole != common.RoleRootUser { |
| 472 | c.JSON(http.StatusOK, gin.H{ |
| 473 | "success": false, |
| 474 | "message": "无权更新同权限等级或更高权限等级的用户信息", |
| 475 | }) |
| 476 | return |
| 477 | } |
| 478 | if myRole <= updatedUser.Role && myRole != common.RoleRootUser { |
| 479 | c.JSON(http.StatusOK, gin.H{ |
| 480 | "success": false, |
| 481 | "message": "无权将其他用户权限等级提升到大于等于自己的权限等级", |
| 482 | }) |
| 483 | return |
| 484 | } |
| 485 | if updatedUser.Password == "$I_LOVE_U" { |
| 486 | updatedUser.Password = "" // rollback to what it should be |
| 487 | } |
| 488 | updatePassword := updatedUser.Password != "" |
| 489 | if err := updatedUser.Edit(updatePassword); err != nil { |
| 490 | common.ApiError(c, err) |
| 491 | return |
| 492 | } |
| 493 | if originUser.Quota != updatedUser.Quota { |
| 494 | model.RecordLog(originUser.Id, model.LogTypeManage, fmt.Sprintf("管理员将用户额度从 %s修改为 %s", common.LogQuota(originUser.Quota), common.LogQuota(updatedUser.Quota))) |
| 495 | } |
| 496 | c.JSON(http.StatusOK, gin.H{ |
| 497 | "success": true, |
| 498 | "message": "", |
| 499 | }) |
| 500 | return |
| 501 | } |
| 502 | |