user.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 GetSelf(c *gin.Context) {
  237. id := c.GetInt("id")
  238. user, err := model.GetUserById(id, false)
  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": user,
  250. })
  251. return
  252. }
  253. func UpdateUser(c *gin.Context) {
  254. var updatedUser model.User
  255. err := json.NewDecoder(c.Request.Body).Decode(&updatedUser)
  256. if err != nil || updatedUser.Id == 0 {
  257. c.JSON(http.StatusOK, gin.H{
  258. "success": false,
  259. "message": "无效的参数",
  260. })
  261. return
  262. }
  263. if updatedUser.Password == "" {
  264. updatedUser.Password = "$I_LOVE_U" // make Validator happy :)
  265. }
  266. if err := common.Validate.Struct(&updatedUser); err != nil {
  267. c.JSON(http.StatusOK, gin.H{
  268. "success": false,
  269. "message": "输入不合法 " + err.Error(),
  270. })
  271. return
  272. }
  273. originUser, err := model.GetUserById(updatedUser.Id, false)
  274. if err != nil {
  275. c.JSON(http.StatusOK, gin.H{
  276. "success": false,
  277. "message": err.Error(),
  278. })
  279. return
  280. }
  281. myRole := c.GetInt("role")
  282. if myRole <= originUser.Role {
  283. c.JSON(http.StatusOK, gin.H{
  284. "success": false,
  285. "message": "无权更新同权限等级或更高权限等级的用户信息",
  286. })
  287. return
  288. }
  289. if myRole <= updatedUser.Role {
  290. c.JSON(http.StatusOK, gin.H{
  291. "success": false,
  292. "message": "无权将其他用户权限等级提升到大于等于自己的权限等级",
  293. })
  294. return
  295. }
  296. if updatedUser.Password == "$I_LOVE_U" {
  297. updatedUser.Password = "" // rollback to what it should be
  298. }
  299. updatePassword := updatedUser.Password != ""
  300. if err := updatedUser.Update(updatePassword); err != nil {
  301. c.JSON(http.StatusOK, gin.H{
  302. "success": false,
  303. "message": err.Error(),
  304. })
  305. return
  306. }
  307. c.JSON(http.StatusOK, gin.H{
  308. "success": true,
  309. "message": "",
  310. })
  311. return
  312. }
  313. func UpdateSelf(c *gin.Context) {
  314. var user model.User
  315. err := json.NewDecoder(c.Request.Body).Decode(&user)
  316. if err != nil {
  317. c.JSON(http.StatusOK, gin.H{
  318. "success": false,
  319. "message": "无效的参数",
  320. })
  321. return
  322. }
  323. if user.Password == "" {
  324. user.Password = "$I_LOVE_U" // make Validator happy :)
  325. }
  326. if err := common.Validate.Struct(&user); err != nil {
  327. c.JSON(http.StatusOK, gin.H{
  328. "success": false,
  329. "message": "输入不合法 " + err.Error(),
  330. })
  331. return
  332. }
  333. cleanUser := model.User{
  334. Id: c.GetInt("id"),
  335. Username: user.Username,
  336. Password: user.Password,
  337. DisplayName: user.DisplayName,
  338. }
  339. if user.Password == "$I_LOVE_U" {
  340. user.Password = "" // rollback to what it should be
  341. cleanUser.Password = ""
  342. }
  343. updatePassword := user.Password != ""
  344. if err := cleanUser.Update(updatePassword); err != nil {
  345. c.JSON(http.StatusOK, gin.H{
  346. "success": false,
  347. "message": err.Error(),
  348. })
  349. return
  350. }
  351. c.JSON(http.StatusOK, gin.H{
  352. "success": true,
  353. "message": "",
  354. })
  355. return
  356. }
  357. func DeleteUser(c *gin.Context) {
  358. id, err := strconv.Atoi(c.Param("id"))
  359. if err != nil {
  360. c.JSON(http.StatusOK, gin.H{
  361. "success": false,
  362. "message": err.Error(),
  363. })
  364. return
  365. }
  366. originUser, err := model.GetUserById(id, false)
  367. if err != nil {
  368. c.JSON(http.StatusOK, gin.H{
  369. "success": false,
  370. "message": err.Error(),
  371. })
  372. return
  373. }
  374. myRole := c.GetInt("role")
  375. if myRole <= originUser.Role {
  376. c.JSON(http.StatusOK, gin.H{
  377. "success": false,
  378. "message": "无权删除同权限等级或更高权限等级的用户",
  379. })
  380. return
  381. }
  382. err = model.DeleteUserById(id)
  383. if err != nil {
  384. c.JSON(http.StatusOK, gin.H{
  385. "success": true,
  386. "message": "",
  387. })
  388. return
  389. }
  390. }
  391. func DeleteSelf(c *gin.Context) {
  392. id := c.GetInt("id")
  393. err := model.DeleteUserById(id)
  394. if err != nil {
  395. c.JSON(http.StatusOK, gin.H{
  396. "success": false,
  397. "message": err.Error(),
  398. })
  399. return
  400. }
  401. c.JSON(http.StatusOK, gin.H{
  402. "success": true,
  403. "message": "",
  404. })
  405. return
  406. }
  407. func CreateUser(c *gin.Context) {
  408. var user model.User
  409. err := json.NewDecoder(c.Request.Body).Decode(&user)
  410. if err != nil || user.Username == "" || user.Password == "" {
  411. c.JSON(http.StatusOK, gin.H{
  412. "success": false,
  413. "message": "无效的参数",
  414. })
  415. return
  416. }
  417. if user.DisplayName == "" {
  418. user.DisplayName = user.Username
  419. }
  420. myRole := c.GetInt("role")
  421. if user.Role >= myRole {
  422. c.JSON(http.StatusOK, gin.H{
  423. "success": false,
  424. "message": "无法创建权限大于等于自己的用户",
  425. })
  426. return
  427. }
  428. // Even for admin users, we cannot fully trust them!
  429. cleanUser := model.User{
  430. Username: user.Username,
  431. Password: user.Password,
  432. DisplayName: user.DisplayName,
  433. }
  434. if err := cleanUser.Insert(); err != nil {
  435. c.JSON(http.StatusOK, gin.H{
  436. "success": false,
  437. "message": err.Error(),
  438. })
  439. return
  440. }
  441. c.JSON(http.StatusOK, gin.H{
  442. "success": true,
  443. "message": "",
  444. })
  445. return
  446. }
  447. type ManageRequest struct {
  448. Username string `json:"username"`
  449. Action string `json:"action"`
  450. }
  451. // ManageUser Only admin user can do this
  452. func ManageUser(c *gin.Context) {
  453. var req ManageRequest
  454. err := json.NewDecoder(c.Request.Body).Decode(&req)
  455. if err != nil {
  456. c.JSON(http.StatusOK, gin.H{
  457. "success": false,
  458. "message": "无效的参数",
  459. })
  460. return
  461. }
  462. user := model.User{
  463. Username: req.Username,
  464. }
  465. // Fill attributes
  466. model.DB.Where(&user).First(&user)
  467. if user.Id == 0 {
  468. c.JSON(http.StatusOK, gin.H{
  469. "success": false,
  470. "message": "用户不存在",
  471. })
  472. return
  473. }
  474. myRole := c.GetInt("role")
  475. if myRole <= user.Role && myRole != common.RoleRootUser {
  476. c.JSON(http.StatusOK, gin.H{
  477. "success": false,
  478. "message": "无权更新同权限等级或更高权限等级的用户信息",
  479. })
  480. return
  481. }
  482. switch req.Action {
  483. case "disable":
  484. user.Status = common.UserStatusDisabled
  485. case "enable":
  486. user.Status = common.UserStatusEnabled
  487. case "delete":
  488. if err := user.Delete(); err != nil {
  489. c.JSON(http.StatusOK, gin.H{
  490. "success": false,
  491. "message": err.Error(),
  492. })
  493. return
  494. }
  495. case "promote":
  496. if myRole != common.RoleRootUser {
  497. c.JSON(http.StatusOK, gin.H{
  498. "success": false,
  499. "message": "普通管理员用户无法提升其他用户为管理员",
  500. })
  501. return
  502. }
  503. user.Role = common.RoleAdminUser
  504. case "demote":
  505. user.Role = common.RoleCommonUser
  506. }
  507. if err := user.Update(false); err != nil {
  508. c.JSON(http.StatusOK, gin.H{
  509. "success": false,
  510. "message": err.Error(),
  511. })
  512. return
  513. }
  514. clearUser := model.User{
  515. Role: user.Role,
  516. Status: user.Status,
  517. }
  518. c.JSON(http.StatusOK, gin.H{
  519. "success": true,
  520. "message": "",
  521. "data": clearUser,
  522. })
  523. return
  524. }
  525. func EmailBind(c *gin.Context) {
  526. email := c.Query("email")
  527. code := c.Query("code")
  528. if !common.VerifyCodeWithKey(email, code, common.EmailVerificationPurpose) {
  529. c.JSON(http.StatusOK, gin.H{
  530. "success": false,
  531. "message": "验证码错误或已过期",
  532. })
  533. return
  534. }
  535. id := c.GetInt("id")
  536. user := model.User{
  537. Id: id,
  538. }
  539. err := user.FillUserById()
  540. if err != nil {
  541. c.JSON(http.StatusOK, gin.H{
  542. "success": false,
  543. "message": err.Error(),
  544. })
  545. return
  546. }
  547. user.Email = email
  548. // no need to check if this email already taken, because we have used verification code to check it
  549. err = user.Update(false)
  550. if err != nil {
  551. c.JSON(http.StatusOK, gin.H{
  552. "success": false,
  553. "message": err.Error(),
  554. })
  555. return
  556. }
  557. c.JSON(http.StatusOK, gin.H{
  558. "success": true,
  559. "message": "",
  560. })
  561. return
  562. }