user.go 19 KB

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