user.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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 user.DisplayName == "" {
  450. user.DisplayName = user.Username
  451. }
  452. myRole := c.GetInt("role")
  453. if user.Role >= myRole {
  454. c.JSON(http.StatusOK, gin.H{
  455. "success": false,
  456. "message": "无法创建权限大于等于自己的用户",
  457. })
  458. return
  459. }
  460. // Even for admin users, we cannot fully trust them!
  461. cleanUser := model.User{
  462. Username: user.Username,
  463. Password: user.Password,
  464. DisplayName: user.DisplayName,
  465. }
  466. if err := cleanUser.Insert(); err != nil {
  467. c.JSON(http.StatusOK, gin.H{
  468. "success": false,
  469. "message": err.Error(),
  470. })
  471. return
  472. }
  473. c.JSON(http.StatusOK, gin.H{
  474. "success": true,
  475. "message": "",
  476. })
  477. return
  478. }
  479. type ManageRequest struct {
  480. Username string `json:"username"`
  481. Action string `json:"action"`
  482. }
  483. // ManageUser Only admin user can do this
  484. func ManageUser(c *gin.Context) {
  485. var req ManageRequest
  486. err := json.NewDecoder(c.Request.Body).Decode(&req)
  487. if err != nil {
  488. c.JSON(http.StatusOK, gin.H{
  489. "success": false,
  490. "message": "无效的参数",
  491. })
  492. return
  493. }
  494. user := model.User{
  495. Username: req.Username,
  496. }
  497. // Fill attributes
  498. model.DB.Where(&user).First(&user)
  499. if user.Id == 0 {
  500. c.JSON(http.StatusOK, gin.H{
  501. "success": false,
  502. "message": "用户不存在",
  503. })
  504. return
  505. }
  506. myRole := c.GetInt("role")
  507. if myRole <= user.Role && myRole != common.RoleRootUser {
  508. c.JSON(http.StatusOK, gin.H{
  509. "success": false,
  510. "message": "无权更新同权限等级或更高权限等级的用户信息",
  511. })
  512. return
  513. }
  514. switch req.Action {
  515. case "disable":
  516. user.Status = common.UserStatusDisabled
  517. if user.Role == common.RoleRootUser {
  518. c.JSON(http.StatusOK, gin.H{
  519. "success": false,
  520. "message": "无法禁用超级管理员用户",
  521. })
  522. return
  523. }
  524. case "enable":
  525. user.Status = common.UserStatusEnabled
  526. case "delete":
  527. if user.Role == common.RoleRootUser {
  528. c.JSON(http.StatusOK, gin.H{
  529. "success": false,
  530. "message": "无法删除超级管理员用户",
  531. })
  532. return
  533. }
  534. if err := user.Delete(); err != nil {
  535. c.JSON(http.StatusOK, gin.H{
  536. "success": false,
  537. "message": err.Error(),
  538. })
  539. return
  540. }
  541. case "promote":
  542. if myRole != common.RoleRootUser {
  543. c.JSON(http.StatusOK, gin.H{
  544. "success": false,
  545. "message": "普通管理员用户无法提升其他用户为管理员",
  546. })
  547. return
  548. }
  549. if user.Role >= common.RoleAdminUser {
  550. c.JSON(http.StatusOK, gin.H{
  551. "success": false,
  552. "message": "该用户已经是管理员",
  553. })
  554. return
  555. }
  556. user.Role = common.RoleAdminUser
  557. case "demote":
  558. if user.Role == common.RoleRootUser {
  559. c.JSON(http.StatusOK, gin.H{
  560. "success": false,
  561. "message": "无法降级超级管理员用户",
  562. })
  563. return
  564. }
  565. if user.Role == common.RoleCommonUser {
  566. c.JSON(http.StatusOK, gin.H{
  567. "success": false,
  568. "message": "该用户已经是普通用户",
  569. })
  570. return
  571. }
  572. user.Role = common.RoleCommonUser
  573. }
  574. if err := user.Update(false); err != nil {
  575. c.JSON(http.StatusOK, gin.H{
  576. "success": false,
  577. "message": err.Error(),
  578. })
  579. return
  580. }
  581. clearUser := model.User{
  582. Role: user.Role,
  583. Status: user.Status,
  584. }
  585. c.JSON(http.StatusOK, gin.H{
  586. "success": true,
  587. "message": "",
  588. "data": clearUser,
  589. })
  590. return
  591. }
  592. func EmailBind(c *gin.Context) {
  593. email := c.Query("email")
  594. code := c.Query("code")
  595. if !common.VerifyCodeWithKey(email, code, common.EmailVerificationPurpose) {
  596. c.JSON(http.StatusOK, gin.H{
  597. "success": false,
  598. "message": "验证码错误或已过期",
  599. })
  600. return
  601. }
  602. id := c.GetInt("id")
  603. user := model.User{
  604. Id: id,
  605. }
  606. err := user.FillUserById()
  607. if err != nil {
  608. c.JSON(http.StatusOK, gin.H{
  609. "success": false,
  610. "message": err.Error(),
  611. })
  612. return
  613. }
  614. user.Email = email
  615. // no need to check if this email already taken, because we have used verification code to check it
  616. err = user.Update(false)
  617. if err != nil {
  618. c.JSON(http.StatusOK, gin.H{
  619. "success": false,
  620. "message": err.Error(),
  621. })
  622. return
  623. }
  624. c.JSON(http.StatusOK, gin.H{
  625. "success": true,
  626. "message": "",
  627. })
  628. return
  629. }
  630. type topUpRequest struct {
  631. Key string `json:"key"`
  632. }
  633. func TopUp(c *gin.Context) {
  634. req := topUpRequest{}
  635. err := c.ShouldBindJSON(&req)
  636. if err != nil {
  637. c.JSON(http.StatusOK, gin.H{
  638. "success": false,
  639. "message": err.Error(),
  640. })
  641. return
  642. }
  643. id := c.GetInt("id")
  644. quota, err := model.Redeem(req.Key, id)
  645. if err != nil {
  646. c.JSON(http.StatusOK, gin.H{
  647. "success": false,
  648. "message": err.Error(),
  649. })
  650. return
  651. }
  652. c.JSON(http.StatusOK, gin.H{
  653. "success": true,
  654. "message": "",
  655. "data": quota,
  656. })
  657. return
  658. }