user.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "one-api/common"
  7. "one-api/model"
  8. "one-api/setting"
  9. "strconv"
  10. "strings"
  11. "sync"
  12. "github.com/gin-contrib/sessions"
  13. "github.com/gin-gonic/gin"
  14. "one-api/constant"
  15. )
  16. type LoginRequest struct {
  17. Username string `json:"username"`
  18. Password string `json:"password"`
  19. }
  20. func Login(c *gin.Context) {
  21. if !common.PasswordLoginEnabled {
  22. c.JSON(http.StatusOK, gin.H{
  23. "message": "管理员关闭了密码登录",
  24. "success": false,
  25. })
  26. return
  27. }
  28. var loginRequest LoginRequest
  29. err := json.NewDecoder(c.Request.Body).Decode(&loginRequest)
  30. if err != nil {
  31. c.JSON(http.StatusOK, gin.H{
  32. "message": "无效的参数",
  33. "success": false,
  34. })
  35. return
  36. }
  37. username := loginRequest.Username
  38. password := loginRequest.Password
  39. if username == "" || password == "" {
  40. c.JSON(http.StatusOK, gin.H{
  41. "message": "无效的参数",
  42. "success": false,
  43. })
  44. return
  45. }
  46. user := model.User{
  47. Username: username,
  48. Password: password,
  49. }
  50. err = user.ValidateAndFill()
  51. if err != nil {
  52. c.JSON(http.StatusOK, gin.H{
  53. "message": err.Error(),
  54. "success": false,
  55. })
  56. return
  57. }
  58. setupLogin(&user, c)
  59. }
  60. // setup session & cookies and then return user info
  61. func setupLogin(user *model.User, c *gin.Context) {
  62. session := sessions.Default(c)
  63. session.Set("id", user.Id)
  64. session.Set("username", user.Username)
  65. session.Set("role", user.Role)
  66. session.Set("status", user.Status)
  67. session.Set("group", user.Group)
  68. err := session.Save()
  69. if err != nil {
  70. c.JSON(http.StatusOK, gin.H{
  71. "message": "无法保存会话信息,请重试",
  72. "success": false,
  73. })
  74. return
  75. }
  76. cleanUser := model.User{
  77. Id: user.Id,
  78. Username: user.Username,
  79. DisplayName: user.DisplayName,
  80. Role: user.Role,
  81. Status: user.Status,
  82. Group: user.Group,
  83. }
  84. c.JSON(http.StatusOK, gin.H{
  85. "message": "",
  86. "success": true,
  87. "data": cleanUser,
  88. })
  89. }
  90. func Logout(c *gin.Context) {
  91. session := sessions.Default(c)
  92. session.Clear()
  93. err := session.Save()
  94. if err != nil {
  95. c.JSON(http.StatusOK, gin.H{
  96. "message": err.Error(),
  97. "success": false,
  98. })
  99. return
  100. }
  101. c.JSON(http.StatusOK, gin.H{
  102. "message": "",
  103. "success": true,
  104. })
  105. }
  106. func Register(c *gin.Context) {
  107. if !common.RegisterEnabled {
  108. c.JSON(http.StatusOK, gin.H{
  109. "message": "管理员关闭了新用户注册",
  110. "success": false,
  111. })
  112. return
  113. }
  114. if !common.PasswordRegisterEnabled {
  115. c.JSON(http.StatusOK, gin.H{
  116. "message": "管理员关闭了通过密码进行注册,请使用第三方账户验证的形式进行注册",
  117. "success": false,
  118. })
  119. return
  120. }
  121. var user model.User
  122. err := json.NewDecoder(c.Request.Body).Decode(&user)
  123. if err != nil {
  124. c.JSON(http.StatusOK, gin.H{
  125. "success": false,
  126. "message": "无效的参数",
  127. })
  128. return
  129. }
  130. if err := common.Validate.Struct(&user); err != nil {
  131. c.JSON(http.StatusOK, gin.H{
  132. "success": false,
  133. "message": "输入不合法 " + err.Error(),
  134. })
  135. return
  136. }
  137. if common.EmailVerificationEnabled {
  138. if user.Email == "" || user.VerificationCode == "" {
  139. c.JSON(http.StatusOK, gin.H{
  140. "success": false,
  141. "message": "管理员开启了邮箱验证,请输入邮箱地址和验证码",
  142. })
  143. return
  144. }
  145. if !common.VerifyCodeWithKey(user.Email, user.VerificationCode, common.EmailVerificationPurpose) {
  146. c.JSON(http.StatusOK, gin.H{
  147. "success": false,
  148. "message": "验证码错误或已过期",
  149. })
  150. return
  151. }
  152. }
  153. exist, err := model.CheckUserExistOrDeleted(user.Username, user.Email)
  154. if err != nil {
  155. c.JSON(http.StatusOK, gin.H{
  156. "success": false,
  157. "message": "数据库错误,请稍后重试",
  158. })
  159. common.SysError(fmt.Sprintf("CheckUserExistOrDeleted error: %v", err))
  160. return
  161. }
  162. if exist {
  163. c.JSON(http.StatusOK, gin.H{
  164. "success": false,
  165. "message": "用户名已存在,或已注销",
  166. })
  167. return
  168. }
  169. affCode := user.AffCode // this code is the inviter's code, not the user's own code
  170. inviterId, _ := model.GetUserIdByAffCode(affCode)
  171. cleanUser := model.User{
  172. Username: user.Username,
  173. Password: user.Password,
  174. DisplayName: user.Username,
  175. InviterId: inviterId,
  176. }
  177. if common.EmailVerificationEnabled {
  178. cleanUser.Email = user.Email
  179. }
  180. if err := cleanUser.Insert(inviterId); err != nil {
  181. c.JSON(http.StatusOK, gin.H{
  182. "success": false,
  183. "message": err.Error(),
  184. })
  185. return
  186. }
  187. // 获取插入后的用户ID
  188. var insertedUser model.User
  189. if err := model.DB.Where("username = ?", cleanUser.Username).First(&insertedUser).Error; err != nil {
  190. c.JSON(http.StatusOK, gin.H{
  191. "success": false,
  192. "message": "用户注册失败或用户ID获取失败",
  193. })
  194. return
  195. }
  196. // 生成默认令牌
  197. if constant.GenerateDefaultToken {
  198. key, err := common.GenerateKey()
  199. if err != nil {
  200. c.JSON(http.StatusOK, gin.H{
  201. "success": false,
  202. "message": "生成默认令牌失败",
  203. })
  204. common.SysError("failed to generate token key: " + err.Error())
  205. return
  206. }
  207. // 生成默认令牌
  208. token := model.Token{
  209. UserId: insertedUser.Id, // 使用插入后的用户ID
  210. Name: cleanUser.Username + "的初始令牌",
  211. Key: key,
  212. CreatedTime: common.GetTimestamp(),
  213. AccessedTime: common.GetTimestamp(),
  214. ExpiredTime: -1, // 永不过期
  215. RemainQuota: 500000, // 示例额度
  216. UnlimitedQuota: true,
  217. ModelLimitsEnabled: false,
  218. }
  219. if err := token.Insert(); err != nil {
  220. c.JSON(http.StatusOK, gin.H{
  221. "success": false,
  222. "message": "创建默认令牌失败",
  223. })
  224. return
  225. }
  226. }
  227. c.JSON(http.StatusOK, gin.H{
  228. "success": true,
  229. "message": "",
  230. })
  231. return
  232. }
  233. func GetAllUsers(c *gin.Context) {
  234. p, _ := strconv.Atoi(c.Query("p"))
  235. if p < 0 {
  236. p = 0
  237. }
  238. users, err := model.GetAllUsers(p*common.ItemsPerPage, common.ItemsPerPage)
  239. if err != nil {
  240. c.JSON(http.StatusOK, gin.H{
  241. "success": false,
  242. "message": err.Error(),
  243. })
  244. return
  245. }
  246. c.JSON(http.StatusOK, gin.H{
  247. "success": true,
  248. "message": "",
  249. "data": users,
  250. })
  251. return
  252. }
  253. func SearchUsers(c *gin.Context) {
  254. keyword := c.Query("keyword")
  255. group := c.Query("group")
  256. users, err := model.SearchUsers(keyword, group)
  257. if err != nil {
  258. c.JSON(http.StatusOK, gin.H{
  259. "success": false,
  260. "message": err.Error(),
  261. })
  262. return
  263. }
  264. c.JSON(http.StatusOK, gin.H{
  265. "success": true,
  266. "message": "",
  267. "data": users,
  268. })
  269. return
  270. }
  271. func GetUser(c *gin.Context) {
  272. id, err := strconv.Atoi(c.Param("id"))
  273. if err != nil {
  274. c.JSON(http.StatusOK, gin.H{
  275. "success": false,
  276. "message": err.Error(),
  277. })
  278. return
  279. }
  280. user, err := model.GetUserById(id, false)
  281. if err != nil {
  282. c.JSON(http.StatusOK, gin.H{
  283. "success": false,
  284. "message": err.Error(),
  285. })
  286. return
  287. }
  288. myRole := c.GetInt("role")
  289. if myRole <= user.Role && myRole != common.RoleRootUser {
  290. c.JSON(http.StatusOK, gin.H{
  291. "success": false,
  292. "message": "无权获取同级或更高等级用户的信息",
  293. })
  294. return
  295. }
  296. c.JSON(http.StatusOK, gin.H{
  297. "success": true,
  298. "message": "",
  299. "data": user,
  300. })
  301. return
  302. }
  303. func GenerateAccessToken(c *gin.Context) {
  304. id := c.GetInt("id")
  305. user, err := model.GetUserById(id, true)
  306. if err != nil {
  307. c.JSON(http.StatusOK, gin.H{
  308. "success": false,
  309. "message": err.Error(),
  310. })
  311. return
  312. }
  313. // get rand int 28-32
  314. randI := common.GetRandomInt(4)
  315. key, err := common.GenerateRandomKey(29 + randI)
  316. if err != nil {
  317. c.JSON(http.StatusOK, gin.H{
  318. "success": false,
  319. "message": "生成失败",
  320. })
  321. common.SysError("failed to generate key: " + err.Error())
  322. return
  323. }
  324. user.SetAccessToken(key)
  325. if model.DB.Where("access_token = ?", user.AccessToken).First(user).RowsAffected != 0 {
  326. c.JSON(http.StatusOK, gin.H{
  327. "success": false,
  328. "message": "请重试,系统生成的 UUID 竟然重复了!",
  329. })
  330. return
  331. }
  332. if err := user.Update(false); err != nil {
  333. c.JSON(http.StatusOK, gin.H{
  334. "success": false,
  335. "message": err.Error(),
  336. })
  337. return
  338. }
  339. c.JSON(http.StatusOK, gin.H{
  340. "success": true,
  341. "message": "",
  342. "data": user.AccessToken,
  343. })
  344. return
  345. }
  346. type TransferAffQuotaRequest struct {
  347. Quota int `json:"quota" binding:"required"`
  348. }
  349. func TransferAffQuota(c *gin.Context) {
  350. id := c.GetInt("id")
  351. user, err := model.GetUserById(id, true)
  352. if err != nil {
  353. c.JSON(http.StatusOK, gin.H{
  354. "success": false,
  355. "message": err.Error(),
  356. })
  357. return
  358. }
  359. tran := TransferAffQuotaRequest{}
  360. if err := c.ShouldBindJSON(&tran); err != nil {
  361. c.JSON(http.StatusOK, gin.H{
  362. "success": false,
  363. "message": err.Error(),
  364. })
  365. return
  366. }
  367. err = user.TransferAffQuotaToQuota(tran.Quota)
  368. if err != nil {
  369. c.JSON(http.StatusOK, gin.H{
  370. "success": false,
  371. "message": "划转失败 " + err.Error(),
  372. })
  373. return
  374. }
  375. c.JSON(http.StatusOK, gin.H{
  376. "success": true,
  377. "message": "划转成功",
  378. })
  379. }
  380. func GetAffCode(c *gin.Context) {
  381. id := c.GetInt("id")
  382. user, err := model.GetUserById(id, true)
  383. if err != nil {
  384. c.JSON(http.StatusOK, gin.H{
  385. "success": false,
  386. "message": err.Error(),
  387. })
  388. return
  389. }
  390. if user.AffCode == "" {
  391. user.AffCode = common.GetRandomString(4)
  392. if err := user.Update(false); err != nil {
  393. c.JSON(http.StatusOK, gin.H{
  394. "success": false,
  395. "message": err.Error(),
  396. })
  397. return
  398. }
  399. }
  400. c.JSON(http.StatusOK, gin.H{
  401. "success": true,
  402. "message": "",
  403. "data": user.AffCode,
  404. })
  405. return
  406. }
  407. func GetSelf(c *gin.Context) {
  408. id := c.GetInt("id")
  409. user, err := model.GetUserById(id, false)
  410. if err != nil {
  411. c.JSON(http.StatusOK, gin.H{
  412. "success": false,
  413. "message": err.Error(),
  414. })
  415. return
  416. }
  417. c.JSON(http.StatusOK, gin.H{
  418. "success": true,
  419. "message": "",
  420. "data": user,
  421. })
  422. return
  423. }
  424. func GetUserModels(c *gin.Context) {
  425. id, err := strconv.Atoi(c.Param("id"))
  426. if err != nil {
  427. id = c.GetInt("id")
  428. }
  429. user, err := model.GetUserById(id, true)
  430. if err != nil {
  431. c.JSON(http.StatusOK, gin.H{
  432. "success": false,
  433. "message": err.Error(),
  434. })
  435. return
  436. }
  437. groups := setting.GetUserUsableGroups(user.Group)
  438. var models []string
  439. for group := range groups {
  440. models = append(models, model.GetGroupModels(group)...)
  441. }
  442. c.JSON(http.StatusOK, gin.H{
  443. "success": true,
  444. "message": "",
  445. "data": models,
  446. })
  447. return
  448. }
  449. func UpdateUser(c *gin.Context) {
  450. var updatedUser model.User
  451. err := json.NewDecoder(c.Request.Body).Decode(&updatedUser)
  452. if err != nil || updatedUser.Id == 0 {
  453. c.JSON(http.StatusOK, gin.H{
  454. "success": false,
  455. "message": "无效的参数",
  456. })
  457. return
  458. }
  459. if updatedUser.Password == "" {
  460. updatedUser.Password = "$I_LOVE_U" // make Validator happy :)
  461. }
  462. if err := common.Validate.Struct(&updatedUser); err != nil {
  463. c.JSON(http.StatusOK, gin.H{
  464. "success": false,
  465. "message": "输入不合法 " + err.Error(),
  466. })
  467. return
  468. }
  469. originUser, err := model.GetUserById(updatedUser.Id, false)
  470. if err != nil {
  471. c.JSON(http.StatusOK, gin.H{
  472. "success": false,
  473. "message": err.Error(),
  474. })
  475. return
  476. }
  477. myRole := c.GetInt("role")
  478. if myRole <= originUser.Role && myRole != common.RoleRootUser {
  479. c.JSON(http.StatusOK, gin.H{
  480. "success": false,
  481. "message": "无权更新同权限等级或更高权限等级的用户信息",
  482. })
  483. return
  484. }
  485. if myRole <= updatedUser.Role && myRole != common.RoleRootUser {
  486. c.JSON(http.StatusOK, gin.H{
  487. "success": false,
  488. "message": "无权将其他用户权限等级提升到大于等于自己的权限等级",
  489. })
  490. return
  491. }
  492. if updatedUser.Password == "$I_LOVE_U" {
  493. updatedUser.Password = "" // rollback to what it should be
  494. }
  495. updatePassword := updatedUser.Password != ""
  496. if err := updatedUser.Edit(updatePassword); err != nil {
  497. c.JSON(http.StatusOK, gin.H{
  498. "success": false,
  499. "message": err.Error(),
  500. })
  501. return
  502. }
  503. if originUser.Quota != updatedUser.Quota {
  504. model.RecordLog(originUser.Id, model.LogTypeManage, fmt.Sprintf("管理员将用户额度从 %s修改为 %s", common.LogQuota(originUser.Quota), common.LogQuota(updatedUser.Quota)))
  505. }
  506. c.JSON(http.StatusOK, gin.H{
  507. "success": true,
  508. "message": "",
  509. })
  510. return
  511. }
  512. func UpdateSelf(c *gin.Context) {
  513. var user model.User
  514. err := json.NewDecoder(c.Request.Body).Decode(&user)
  515. if err != nil {
  516. c.JSON(http.StatusOK, gin.H{
  517. "success": false,
  518. "message": "无效的参数",
  519. })
  520. return
  521. }
  522. if user.Password == "" {
  523. user.Password = "$I_LOVE_U" // make Validator happy :)
  524. }
  525. if err := common.Validate.Struct(&user); err != nil {
  526. c.JSON(http.StatusOK, gin.H{
  527. "success": false,
  528. "message": "输入不合法 " + err.Error(),
  529. })
  530. return
  531. }
  532. cleanUser := model.User{
  533. Id: c.GetInt("id"),
  534. Username: user.Username,
  535. Password: user.Password,
  536. DisplayName: user.DisplayName,
  537. }
  538. if user.Password == "$I_LOVE_U" {
  539. user.Password = "" // rollback to what it should be
  540. cleanUser.Password = ""
  541. }
  542. updatePassword := user.Password != ""
  543. if err := cleanUser.Update(updatePassword); err != nil {
  544. c.JSON(http.StatusOK, gin.H{
  545. "success": false,
  546. "message": err.Error(),
  547. })
  548. return
  549. }
  550. c.JSON(http.StatusOK, gin.H{
  551. "success": true,
  552. "message": "",
  553. })
  554. return
  555. }
  556. func DeleteUser(c *gin.Context) {
  557. id, err := strconv.Atoi(c.Param("id"))
  558. if err != nil {
  559. c.JSON(http.StatusOK, gin.H{
  560. "success": false,
  561. "message": err.Error(),
  562. })
  563. return
  564. }
  565. originUser, err := model.GetUserById(id, false)
  566. if err != nil {
  567. c.JSON(http.StatusOK, gin.H{
  568. "success": false,
  569. "message": err.Error(),
  570. })
  571. return
  572. }
  573. myRole := c.GetInt("role")
  574. if myRole <= originUser.Role {
  575. c.JSON(http.StatusOK, gin.H{
  576. "success": false,
  577. "message": "无权删除同权限等级或更高权限等级的用户",
  578. })
  579. return
  580. }
  581. err = model.HardDeleteUserById(id)
  582. if err != nil {
  583. c.JSON(http.StatusOK, gin.H{
  584. "success": true,
  585. "message": "",
  586. })
  587. return
  588. }
  589. }
  590. func DeleteSelf(c *gin.Context) {
  591. id := c.GetInt("id")
  592. user, _ := model.GetUserById(id, false)
  593. if user.Role == common.RoleRootUser {
  594. c.JSON(http.StatusOK, gin.H{
  595. "success": false,
  596. "message": "不能删除超级管理员账户",
  597. })
  598. return
  599. }
  600. err := model.DeleteUserById(id)
  601. if err != nil {
  602. c.JSON(http.StatusOK, gin.H{
  603. "success": false,
  604. "message": err.Error(),
  605. })
  606. return
  607. }
  608. c.JSON(http.StatusOK, gin.H{
  609. "success": true,
  610. "message": "",
  611. })
  612. return
  613. }
  614. func CreateUser(c *gin.Context) {
  615. var user model.User
  616. err := json.NewDecoder(c.Request.Body).Decode(&user)
  617. user.Username = strings.TrimSpace(user.Username)
  618. if err != nil || user.Username == "" || user.Password == "" {
  619. c.JSON(http.StatusOK, gin.H{
  620. "success": false,
  621. "message": "无效的参数",
  622. })
  623. return
  624. }
  625. if err := common.Validate.Struct(&user); err != nil {
  626. c.JSON(http.StatusOK, gin.H{
  627. "success": false,
  628. "message": "输入不合法 " + err.Error(),
  629. })
  630. return
  631. }
  632. if user.DisplayName == "" {
  633. user.DisplayName = user.Username
  634. }
  635. myRole := c.GetInt("role")
  636. if user.Role >= myRole {
  637. c.JSON(http.StatusOK, gin.H{
  638. "success": false,
  639. "message": "无法创建权限大于等于自己的用户",
  640. })
  641. return
  642. }
  643. // Even for admin users, we cannot fully trust them!
  644. cleanUser := model.User{
  645. Username: user.Username,
  646. Password: user.Password,
  647. DisplayName: user.DisplayName,
  648. }
  649. if err := cleanUser.Insert(0); err != nil {
  650. c.JSON(http.StatusOK, gin.H{
  651. "success": false,
  652. "message": err.Error(),
  653. })
  654. return
  655. }
  656. c.JSON(http.StatusOK, gin.H{
  657. "success": true,
  658. "message": "",
  659. })
  660. return
  661. }
  662. type ManageRequest struct {
  663. Id int `json:"id"`
  664. Action string `json:"action"`
  665. }
  666. // ManageUser Only admin user can do this
  667. func ManageUser(c *gin.Context) {
  668. var req ManageRequest
  669. err := json.NewDecoder(c.Request.Body).Decode(&req)
  670. if err != nil {
  671. c.JSON(http.StatusOK, gin.H{
  672. "success": false,
  673. "message": "无效的参数",
  674. })
  675. return
  676. }
  677. user := model.User{
  678. Id: req.Id,
  679. }
  680. // Fill attributes
  681. model.DB.Unscoped().Where(&user).First(&user)
  682. if user.Id == 0 {
  683. c.JSON(http.StatusOK, gin.H{
  684. "success": false,
  685. "message": "用户不存在",
  686. })
  687. return
  688. }
  689. myRole := c.GetInt("role")
  690. if myRole <= user.Role && myRole != common.RoleRootUser {
  691. c.JSON(http.StatusOK, gin.H{
  692. "success": false,
  693. "message": "无权更新同权限等级或更高权限等级的用户信息",
  694. })
  695. return
  696. }
  697. switch req.Action {
  698. case "disable":
  699. user.Status = common.UserStatusDisabled
  700. if user.Role == common.RoleRootUser {
  701. c.JSON(http.StatusOK, gin.H{
  702. "success": false,
  703. "message": "无法禁用超级管理员用户",
  704. })
  705. return
  706. }
  707. case "enable":
  708. user.Status = common.UserStatusEnabled
  709. case "delete":
  710. if user.Role == common.RoleRootUser {
  711. c.JSON(http.StatusOK, gin.H{
  712. "success": false,
  713. "message": "无法删除超级管理员用户",
  714. })
  715. return
  716. }
  717. if err := user.Delete(); err != nil {
  718. c.JSON(http.StatusOK, gin.H{
  719. "success": false,
  720. "message": err.Error(),
  721. })
  722. return
  723. }
  724. case "promote":
  725. if myRole != common.RoleRootUser {
  726. c.JSON(http.StatusOK, gin.H{
  727. "success": false,
  728. "message": "普通管理员用户无法提升其他用户为管理员",
  729. })
  730. return
  731. }
  732. if user.Role >= common.RoleAdminUser {
  733. c.JSON(http.StatusOK, gin.H{
  734. "success": false,
  735. "message": "该用户已经是管理员",
  736. })
  737. return
  738. }
  739. user.Role = common.RoleAdminUser
  740. case "demote":
  741. if user.Role == common.RoleRootUser {
  742. c.JSON(http.StatusOK, gin.H{
  743. "success": false,
  744. "message": "无法降级超级管理员用户",
  745. })
  746. return
  747. }
  748. if user.Role == common.RoleCommonUser {
  749. c.JSON(http.StatusOK, gin.H{
  750. "success": false,
  751. "message": "该用户已经是普通用户",
  752. })
  753. return
  754. }
  755. user.Role = common.RoleCommonUser
  756. }
  757. if err := user.Update(false); err != nil {
  758. c.JSON(http.StatusOK, gin.H{
  759. "success": false,
  760. "message": err.Error(),
  761. })
  762. return
  763. }
  764. clearUser := model.User{
  765. Role: user.Role,
  766. Status: user.Status,
  767. }
  768. c.JSON(http.StatusOK, gin.H{
  769. "success": true,
  770. "message": "",
  771. "data": clearUser,
  772. })
  773. return
  774. }
  775. func EmailBind(c *gin.Context) {
  776. email := c.Query("email")
  777. code := c.Query("code")
  778. if !common.VerifyCodeWithKey(email, code, common.EmailVerificationPurpose) {
  779. c.JSON(http.StatusOK, gin.H{
  780. "success": false,
  781. "message": "验证码错误或已过期",
  782. })
  783. return
  784. }
  785. id := c.GetInt("id")
  786. user := model.User{
  787. Id: id,
  788. }
  789. err := user.FillUserById()
  790. if err != nil {
  791. c.JSON(http.StatusOK, gin.H{
  792. "success": false,
  793. "message": err.Error(),
  794. })
  795. return
  796. }
  797. user.Email = email
  798. // no need to check if this email already taken, because we have used verification code to check it
  799. err = user.Update(false)
  800. if err != nil {
  801. c.JSON(http.StatusOK, gin.H{
  802. "success": false,
  803. "message": err.Error(),
  804. })
  805. return
  806. }
  807. if user.Role == common.RoleRootUser {
  808. common.RootUserEmail = email
  809. }
  810. c.JSON(http.StatusOK, gin.H{
  811. "success": true,
  812. "message": "",
  813. })
  814. return
  815. }
  816. type topUpRequest struct {
  817. Key string `json:"key"`
  818. }
  819. var topUpLock = sync.Mutex{}
  820. func TopUp(c *gin.Context) {
  821. topUpLock.Lock()
  822. defer topUpLock.Unlock()
  823. req := topUpRequest{}
  824. err := c.ShouldBindJSON(&req)
  825. if err != nil {
  826. c.JSON(http.StatusOK, gin.H{
  827. "success": false,
  828. "message": err.Error(),
  829. })
  830. return
  831. }
  832. id := c.GetInt("id")
  833. quota, err := model.Redeem(req.Key, id)
  834. if err != nil {
  835. c.JSON(http.StatusOK, gin.H{
  836. "success": false,
  837. "message": err.Error(),
  838. })
  839. return
  840. }
  841. c.JSON(http.StatusOK, gin.H{
  842. "success": true,
  843. "message": "",
  844. "data": quota,
  845. })
  846. return
  847. }