user.go 19 KB

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