user.go 14 KB

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