user.go 19 KB

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