user.go 18 KB

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