user.go 19 KB

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