user.go 17 KB

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