user.go 17 KB

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