user.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. package controller
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/gin-contrib/sessions"
  6. "github.com/gin-gonic/gin"
  7. "net/http"
  8. "one-api/common"
  9. "one-api/model"
  10. "strconv"
  11. )
  12. type LoginRequest struct {
  13. Username string `json:"username"`
  14. Password string `json:"password"`
  15. }
  16. func Login(c *gin.Context) {
  17. if !common.PasswordLoginEnabled {
  18. c.JSON(http.StatusOK, gin.H{
  19. "message": "管理员关闭了密码登录",
  20. "success": false,
  21. })
  22. return
  23. }
  24. var loginRequest LoginRequest
  25. err := json.NewDecoder(c.Request.Body).Decode(&loginRequest)
  26. if err != nil {
  27. c.JSON(http.StatusOK, gin.H{
  28. "message": "无效的参数",
  29. "success": false,
  30. })
  31. return
  32. }
  33. username := loginRequest.Username
  34. password := loginRequest.Password
  35. if username == "" || password == "" {
  36. c.JSON(http.StatusOK, gin.H{
  37. "message": "无效的参数",
  38. "success": false,
  39. })
  40. return
  41. }
  42. user := model.User{
  43. Username: username,
  44. Password: password,
  45. }
  46. err = user.ValidateAndFill()
  47. if err != nil {
  48. c.JSON(http.StatusOK, gin.H{
  49. "message": err.Error(),
  50. "success": false,
  51. })
  52. return
  53. }
  54. setupLogin(&user, c)
  55. }
  56. // setup session & cookies and then return user info
  57. func setupLogin(user *model.User, c *gin.Context) {
  58. session := sessions.Default(c)
  59. session.Set("id", user.Id)
  60. session.Set("username", user.Username)
  61. session.Set("role", user.Role)
  62. session.Set("status", user.Status)
  63. err := session.Save()
  64. if err != nil {
  65. c.JSON(http.StatusOK, gin.H{
  66. "message": "无法保存会话信息,请重试",
  67. "success": false,
  68. })
  69. return
  70. }
  71. cleanUser := model.User{
  72. Id: user.Id,
  73. Username: user.Username,
  74. DisplayName: user.DisplayName,
  75. Role: user.Role,
  76. Status: user.Status,
  77. }
  78. c.JSON(http.StatusOK, gin.H{
  79. "message": "",
  80. "success": true,
  81. "data": cleanUser,
  82. })
  83. }
  84. func Logout(c *gin.Context) {
  85. session := sessions.Default(c)
  86. session.Clear()
  87. err := session.Save()
  88. if err != nil {
  89. c.JSON(http.StatusOK, gin.H{
  90. "message": err.Error(),
  91. "success": false,
  92. })
  93. return
  94. }
  95. c.JSON(http.StatusOK, gin.H{
  96. "message": "",
  97. "success": true,
  98. })
  99. }
  100. func Register(c *gin.Context) {
  101. if !common.RegisterEnabled {
  102. c.JSON(http.StatusOK, gin.H{
  103. "message": "管理员关闭了新用户注册",
  104. "success": false,
  105. })
  106. return
  107. }
  108. if !common.PasswordRegisterEnabled {
  109. c.JSON(http.StatusOK, gin.H{
  110. "message": "管理员关闭了通过密码进行注册,请使用第三方账户验证的形式进行注册",
  111. "success": false,
  112. })
  113. return
  114. }
  115. var user model.User
  116. err := json.NewDecoder(c.Request.Body).Decode(&user)
  117. if err != nil {
  118. c.JSON(http.StatusOK, gin.H{
  119. "success": false,
  120. "message": "无效的参数",
  121. })
  122. return
  123. }
  124. if err := common.Validate.Struct(&user); err != nil {
  125. c.JSON(http.StatusOK, gin.H{
  126. "success": false,
  127. "message": "输入不合法 " + err.Error(),
  128. })
  129. return
  130. }
  131. if common.EmailVerificationEnabled {
  132. if user.Email == "" || user.VerificationCode == "" {
  133. c.JSON(http.StatusOK, gin.H{
  134. "success": false,
  135. "message": "管理员开启了邮箱验证,请输入邮箱地址和验证码",
  136. })
  137. return
  138. }
  139. if !common.VerifyCodeWithKey(user.Email, user.VerificationCode, common.EmailVerificationPurpose) {
  140. c.JSON(http.StatusOK, gin.H{
  141. "success": false,
  142. "message": "验证码错误或已过期",
  143. })
  144. return
  145. }
  146. }
  147. cleanUser := model.User{
  148. Username: user.Username,
  149. Password: user.Password,
  150. DisplayName: user.Username,
  151. }
  152. if common.EmailVerificationEnabled {
  153. cleanUser.Email = user.Email
  154. }
  155. if err := cleanUser.Insert(); err != nil {
  156. c.JSON(http.StatusOK, gin.H{
  157. "success": false,
  158. "message": err.Error(),
  159. })
  160. return
  161. }
  162. c.JSON(http.StatusOK, gin.H{
  163. "success": true,
  164. "message": "",
  165. })
  166. return
  167. }
  168. func GetAllUsers(c *gin.Context) {
  169. p, _ := strconv.Atoi(c.Query("p"))
  170. if p < 0 {
  171. p = 0
  172. }
  173. users, err := model.GetAllUsers(p*common.ItemsPerPage, common.ItemsPerPage)
  174. if err != nil {
  175. c.JSON(http.StatusOK, gin.H{
  176. "success": false,
  177. "message": err.Error(),
  178. })
  179. return
  180. }
  181. c.JSON(http.StatusOK, gin.H{
  182. "success": true,
  183. "message": "",
  184. "data": users,
  185. })
  186. return
  187. }
  188. func SearchUsers(c *gin.Context) {
  189. keyword := c.Query("keyword")
  190. users, err := model.SearchUsers(keyword)
  191. if err != nil {
  192. c.JSON(http.StatusOK, gin.H{
  193. "success": false,
  194. "message": err.Error(),
  195. })
  196. return
  197. }
  198. c.JSON(http.StatusOK, gin.H{
  199. "success": true,
  200. "message": "",
  201. "data": users,
  202. })
  203. return
  204. }
  205. func GetUser(c *gin.Context) {
  206. id, err := strconv.Atoi(c.Param("id"))
  207. if err != nil {
  208. c.JSON(http.StatusOK, gin.H{
  209. "success": false,
  210. "message": err.Error(),
  211. })
  212. return
  213. }
  214. user, err := model.GetUserById(id, false)
  215. if err != nil {
  216. c.JSON(http.StatusOK, gin.H{
  217. "success": false,
  218. "message": err.Error(),
  219. })
  220. return
  221. }
  222. myRole := c.GetInt("role")
  223. if myRole <= user.Role && myRole != common.RoleRootUser {
  224. c.JSON(http.StatusOK, gin.H{
  225. "success": false,
  226. "message": "无权获取同级或更高等级用户的信息",
  227. })
  228. return
  229. }
  230. c.JSON(http.StatusOK, gin.H{
  231. "success": true,
  232. "message": "",
  233. "data": user,
  234. })
  235. return
  236. }
  237. func GenerateAccessToken(c *gin.Context) {
  238. id := c.GetInt("id")
  239. user, err := model.GetUserById(id, true)
  240. if err != nil {
  241. c.JSON(http.StatusOK, gin.H{
  242. "success": false,
  243. "message": err.Error(),
  244. })
  245. return
  246. }
  247. user.AccessToken = common.GetUUID()
  248. if model.DB.Where("token = ?", user.AccessToken).First(user).RowsAffected != 0 {
  249. c.JSON(http.StatusOK, gin.H{
  250. "success": false,
  251. "message": "请重试,系统生成的 UUID 竟然重复了!",
  252. })
  253. return
  254. }
  255. if err := user.Update(false); 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": user.AccessToken,
  266. })
  267. return
  268. }
  269. func GetSelf(c *gin.Context) {
  270. id := c.GetInt("id")
  271. user, err := model.GetUserById(id, false)
  272. if err != nil {
  273. c.JSON(http.StatusOK, gin.H{
  274. "success": false,
  275. "message": err.Error(),
  276. })
  277. return
  278. }
  279. c.JSON(http.StatusOK, gin.H{
  280. "success": true,
  281. "message": "",
  282. "data": user,
  283. })
  284. return
  285. }
  286. func UpdateUser(c *gin.Context) {
  287. var updatedUser model.User
  288. err := json.NewDecoder(c.Request.Body).Decode(&updatedUser)
  289. if err != nil || updatedUser.Id == 0 {
  290. c.JSON(http.StatusOK, gin.H{
  291. "success": false,
  292. "message": "无效的参数",
  293. })
  294. return
  295. }
  296. if updatedUser.Password == "" {
  297. updatedUser.Password = "$I_LOVE_U" // make Validator happy :)
  298. }
  299. if err := common.Validate.Struct(&updatedUser); err != nil {
  300. c.JSON(http.StatusOK, gin.H{
  301. "success": false,
  302. "message": "输入不合法 " + err.Error(),
  303. })
  304. return
  305. }
  306. originUser, err := model.GetUserById(updatedUser.Id, false)
  307. if err != nil {
  308. c.JSON(http.StatusOK, gin.H{
  309. "success": false,
  310. "message": err.Error(),
  311. })
  312. return
  313. }
  314. myRole := c.GetInt("role")
  315. if myRole <= originUser.Role && myRole != common.RoleRootUser {
  316. c.JSON(http.StatusOK, gin.H{
  317. "success": false,
  318. "message": "无权更新同权限等级或更高权限等级的用户信息",
  319. })
  320. return
  321. }
  322. if myRole <= updatedUser.Role && myRole != common.RoleRootUser {
  323. c.JSON(http.StatusOK, gin.H{
  324. "success": false,
  325. "message": "无权将其他用户权限等级提升到大于等于自己的权限等级",
  326. })
  327. return
  328. }
  329. if updatedUser.Password == "$I_LOVE_U" {
  330. updatedUser.Password = "" // rollback to what it should be
  331. }
  332. updatePassword := updatedUser.Password != ""
  333. if err := updatedUser.Update(updatePassword); err != nil {
  334. c.JSON(http.StatusOK, gin.H{
  335. "success": false,
  336. "message": err.Error(),
  337. })
  338. return
  339. }
  340. if originUser.Quota != updatedUser.Quota {
  341. model.RecordLog(originUser.Id, model.LogTypeManage, fmt.Sprintf("管理员将用户额度从 %d 点修改为 %d 点", originUser.Quota, updatedUser.Quota))
  342. }
  343. c.JSON(http.StatusOK, gin.H{
  344. "success": true,
  345. "message": "",
  346. })
  347. return
  348. }
  349. func UpdateSelf(c *gin.Context) {
  350. var user model.User
  351. err := json.NewDecoder(c.Request.Body).Decode(&user)
  352. if err != nil {
  353. c.JSON(http.StatusOK, gin.H{
  354. "success": false,
  355. "message": "无效的参数",
  356. })
  357. return
  358. }
  359. if user.Password == "" {
  360. user.Password = "$I_LOVE_U" // make Validator happy :)
  361. }
  362. if err := common.Validate.Struct(&user); err != nil {
  363. c.JSON(http.StatusOK, gin.H{
  364. "success": false,
  365. "message": "输入不合法 " + err.Error(),
  366. })
  367. return
  368. }
  369. cleanUser := model.User{
  370. Id: c.GetInt("id"),
  371. Username: user.Username,
  372. Password: user.Password,
  373. DisplayName: user.DisplayName,
  374. }
  375. if user.Password == "$I_LOVE_U" {
  376. user.Password = "" // rollback to what it should be
  377. cleanUser.Password = ""
  378. }
  379. updatePassword := user.Password != ""
  380. if err := cleanUser.Update(updatePassword); err != nil {
  381. c.JSON(http.StatusOK, gin.H{
  382. "success": false,
  383. "message": err.Error(),
  384. })
  385. return
  386. }
  387. c.JSON(http.StatusOK, gin.H{
  388. "success": true,
  389. "message": "",
  390. })
  391. return
  392. }
  393. func DeleteUser(c *gin.Context) {
  394. id, err := strconv.Atoi(c.Param("id"))
  395. if err != nil {
  396. c.JSON(http.StatusOK, gin.H{
  397. "success": false,
  398. "message": err.Error(),
  399. })
  400. return
  401. }
  402. originUser, err := model.GetUserById(id, false)
  403. if err != nil {
  404. c.JSON(http.StatusOK, gin.H{
  405. "success": false,
  406. "message": err.Error(),
  407. })
  408. return
  409. }
  410. myRole := c.GetInt("role")
  411. if myRole <= originUser.Role {
  412. c.JSON(http.StatusOK, gin.H{
  413. "success": false,
  414. "message": "无权删除同权限等级或更高权限等级的用户",
  415. })
  416. return
  417. }
  418. err = model.DeleteUserById(id)
  419. if err != nil {
  420. c.JSON(http.StatusOK, gin.H{
  421. "success": true,
  422. "message": "",
  423. })
  424. return
  425. }
  426. }
  427. func DeleteSelf(c *gin.Context) {
  428. id := c.GetInt("id")
  429. err := model.DeleteUserById(id)
  430. if err != nil {
  431. c.JSON(http.StatusOK, gin.H{
  432. "success": false,
  433. "message": err.Error(),
  434. })
  435. return
  436. }
  437. c.JSON(http.StatusOK, gin.H{
  438. "success": true,
  439. "message": "",
  440. })
  441. return
  442. }
  443. func CreateUser(c *gin.Context) {
  444. var user model.User
  445. err := json.NewDecoder(c.Request.Body).Decode(&user)
  446. if err != nil || user.Username == "" || user.Password == "" {
  447. c.JSON(http.StatusOK, gin.H{
  448. "success": false,
  449. "message": "无效的参数",
  450. })
  451. return
  452. }
  453. if err := common.Validate.Struct(&user); err != nil {
  454. c.JSON(http.StatusOK, gin.H{
  455. "success": false,
  456. "message": "输入不合法 " + err.Error(),
  457. })
  458. return
  459. }
  460. if user.DisplayName == "" {
  461. user.DisplayName = user.Username
  462. }
  463. myRole := c.GetInt("role")
  464. if user.Role >= myRole {
  465. c.JSON(http.StatusOK, gin.H{
  466. "success": false,
  467. "message": "无法创建权限大于等于自己的用户",
  468. })
  469. return
  470. }
  471. // Even for admin users, we cannot fully trust them!
  472. cleanUser := model.User{
  473. Username: user.Username,
  474. Password: user.Password,
  475. DisplayName: user.DisplayName,
  476. }
  477. if err := cleanUser.Insert(); err != nil {
  478. c.JSON(http.StatusOK, gin.H{
  479. "success": false,
  480. "message": err.Error(),
  481. })
  482. return
  483. }
  484. c.JSON(http.StatusOK, gin.H{
  485. "success": true,
  486. "message": "",
  487. })
  488. return
  489. }
  490. type ManageRequest struct {
  491. Username string `json:"username"`
  492. Action string `json:"action"`
  493. }
  494. // ManageUser Only admin user can do this
  495. func ManageUser(c *gin.Context) {
  496. var req ManageRequest
  497. err := json.NewDecoder(c.Request.Body).Decode(&req)
  498. if err != nil {
  499. c.JSON(http.StatusOK, gin.H{
  500. "success": false,
  501. "message": "无效的参数",
  502. })
  503. return
  504. }
  505. user := model.User{
  506. Username: req.Username,
  507. }
  508. // Fill attributes
  509. model.DB.Where(&user).First(&user)
  510. if user.Id == 0 {
  511. c.JSON(http.StatusOK, gin.H{
  512. "success": false,
  513. "message": "用户不存在",
  514. })
  515. return
  516. }
  517. myRole := c.GetInt("role")
  518. if myRole <= user.Role && myRole != common.RoleRootUser {
  519. c.JSON(http.StatusOK, gin.H{
  520. "success": false,
  521. "message": "无权更新同权限等级或更高权限等级的用户信息",
  522. })
  523. return
  524. }
  525. switch req.Action {
  526. case "disable":
  527. user.Status = common.UserStatusDisabled
  528. if user.Role == common.RoleRootUser {
  529. c.JSON(http.StatusOK, gin.H{
  530. "success": false,
  531. "message": "无法禁用超级管理员用户",
  532. })
  533. return
  534. }
  535. case "enable":
  536. user.Status = common.UserStatusEnabled
  537. case "delete":
  538. if user.Role == common.RoleRootUser {
  539. c.JSON(http.StatusOK, gin.H{
  540. "success": false,
  541. "message": "无法删除超级管理员用户",
  542. })
  543. return
  544. }
  545. if err := user.Delete(); err != nil {
  546. c.JSON(http.StatusOK, gin.H{
  547. "success": false,
  548. "message": err.Error(),
  549. })
  550. return
  551. }
  552. case "promote":
  553. if myRole != common.RoleRootUser {
  554. c.JSON(http.StatusOK, gin.H{
  555. "success": false,
  556. "message": "普通管理员用户无法提升其他用户为管理员",
  557. })
  558. return
  559. }
  560. if user.Role >= common.RoleAdminUser {
  561. c.JSON(http.StatusOK, gin.H{
  562. "success": false,
  563. "message": "该用户已经是管理员",
  564. })
  565. return
  566. }
  567. user.Role = common.RoleAdminUser
  568. case "demote":
  569. if user.Role == common.RoleRootUser {
  570. c.JSON(http.StatusOK, gin.H{
  571. "success": false,
  572. "message": "无法降级超级管理员用户",
  573. })
  574. return
  575. }
  576. if user.Role == common.RoleCommonUser {
  577. c.JSON(http.StatusOK, gin.H{
  578. "success": false,
  579. "message": "该用户已经是普通用户",
  580. })
  581. return
  582. }
  583. user.Role = common.RoleCommonUser
  584. }
  585. if err := user.Update(false); err != nil {
  586. c.JSON(http.StatusOK, gin.H{
  587. "success": false,
  588. "message": err.Error(),
  589. })
  590. return
  591. }
  592. clearUser := model.User{
  593. Role: user.Role,
  594. Status: user.Status,
  595. }
  596. c.JSON(http.StatusOK, gin.H{
  597. "success": true,
  598. "message": "",
  599. "data": clearUser,
  600. })
  601. return
  602. }
  603. func EmailBind(c *gin.Context) {
  604. email := c.Query("email")
  605. code := c.Query("code")
  606. if !common.VerifyCodeWithKey(email, code, common.EmailVerificationPurpose) {
  607. c.JSON(http.StatusOK, gin.H{
  608. "success": false,
  609. "message": "验证码错误或已过期",
  610. })
  611. return
  612. }
  613. id := c.GetInt("id")
  614. user := model.User{
  615. Id: id,
  616. }
  617. err := user.FillUserById()
  618. if err != nil {
  619. c.JSON(http.StatusOK, gin.H{
  620. "success": false,
  621. "message": err.Error(),
  622. })
  623. return
  624. }
  625. user.Email = email
  626. // no need to check if this email already taken, because we have used verification code to check it
  627. err = user.Update(false)
  628. if err != nil {
  629. c.JSON(http.StatusOK, gin.H{
  630. "success": false,
  631. "message": err.Error(),
  632. })
  633. return
  634. }
  635. if user.Role == common.RoleRootUser {
  636. common.RootUserEmail = email
  637. }
  638. c.JSON(http.StatusOK, gin.H{
  639. "success": true,
  640. "message": "",
  641. })
  642. return
  643. }
  644. type topUpRequest struct {
  645. Key string `json:"key"`
  646. }
  647. func TopUp(c *gin.Context) {
  648. req := topUpRequest{}
  649. err := c.ShouldBindJSON(&req)
  650. if err != nil {
  651. c.JSON(http.StatusOK, gin.H{
  652. "success": false,
  653. "message": err.Error(),
  654. })
  655. return
  656. }
  657. id := c.GetInt("id")
  658. quota, err := model.Redeem(req.Key, id)
  659. if err != nil {
  660. c.JSON(http.StatusOK, gin.H{
  661. "success": false,
  662. "message": err.Error(),
  663. })
  664. return
  665. }
  666. c.JSON(http.StatusOK, gin.H{
  667. "success": true,
  668. "message": "",
  669. "data": quota,
  670. })
  671. return
  672. }