user.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. // Copyright 2017 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package storage // import "miniflux.app/storage"
  5. import (
  6. "database/sql"
  7. "fmt"
  8. "runtime"
  9. "strings"
  10. "miniflux.app/logger"
  11. "miniflux.app/model"
  12. "github.com/lib/pq"
  13. "golang.org/x/crypto/bcrypt"
  14. )
  15. // CountUsers returns the total number of users.
  16. func (s *Storage) CountUsers() int {
  17. var result int
  18. err := s.db.QueryRow(`SELECT count(*) FROM users`).Scan(&result)
  19. if err != nil {
  20. return 0
  21. }
  22. return result
  23. }
  24. // SetLastLogin updates the last login date of a user.
  25. func (s *Storage) SetLastLogin(userID int64) error {
  26. query := `UPDATE users SET last_login_at=now() WHERE id=$1`
  27. _, err := s.db.Exec(query, userID)
  28. if err != nil {
  29. return fmt.Errorf(`store: unable to update last login date: %v`, err)
  30. }
  31. return nil
  32. }
  33. // UserExists checks if a user exists by using the given username.
  34. func (s *Storage) UserExists(username string) bool {
  35. var result bool
  36. s.db.QueryRow(`SELECT true FROM users WHERE username=LOWER($1)`, username).Scan(&result)
  37. return result
  38. }
  39. // AnotherUserExists checks if another user exists with the given username.
  40. func (s *Storage) AnotherUserExists(userID int64, username string) bool {
  41. var result bool
  42. s.db.QueryRow(`SELECT true FROM users WHERE id != $1 AND username=LOWER($2)`, userID, username).Scan(&result)
  43. return result
  44. }
  45. // CreateUser creates a new user.
  46. func (s *Storage) CreateUser(userCreationRequest *model.UserCreationRequest) (*model.User, error) {
  47. var hashedPassword string
  48. if userCreationRequest.Password != "" {
  49. var err error
  50. hashedPassword, err = hashPassword(userCreationRequest.Password)
  51. if err != nil {
  52. return nil, err
  53. }
  54. }
  55. query := `
  56. INSERT INTO users
  57. (username, password, is_admin, google_id, openid_connect_id)
  58. VALUES
  59. (LOWER($1), $2, $3, $4, $5)
  60. RETURNING
  61. id,
  62. username,
  63. is_admin,
  64. language,
  65. theme,
  66. timezone,
  67. entry_direction,
  68. entries_per_page,
  69. keyboard_shortcuts,
  70. show_reading_time,
  71. entry_swipe,
  72. stylesheet,
  73. google_id,
  74. openid_connect_id,
  75. display_mode
  76. `
  77. tx, err := s.db.Begin()
  78. if err != nil {
  79. return nil, fmt.Errorf(`store: unable to start transaction: %v`, err)
  80. }
  81. var user model.User
  82. err = tx.QueryRow(
  83. query,
  84. userCreationRequest.Username,
  85. hashedPassword,
  86. userCreationRequest.IsAdmin,
  87. userCreationRequest.GoogleID,
  88. userCreationRequest.OpenIDConnectID,
  89. ).Scan(
  90. &user.ID,
  91. &user.Username,
  92. &user.IsAdmin,
  93. &user.Language,
  94. &user.Theme,
  95. &user.Timezone,
  96. &user.EntryDirection,
  97. &user.EntriesPerPage,
  98. &user.KeyboardShortcuts,
  99. &user.ShowReadingTime,
  100. &user.EntrySwipe,
  101. &user.Stylesheet,
  102. &user.GoogleID,
  103. &user.OpenIDConnectID,
  104. &user.DisplayMode,
  105. )
  106. if err != nil {
  107. tx.Rollback()
  108. return nil, fmt.Errorf(`store: unable to create user: %v`, err)
  109. }
  110. _, err = tx.Exec(`INSERT INTO categories (user_id, title) VALUES ($1, $2)`, user.ID, "All")
  111. if err != nil {
  112. tx.Rollback()
  113. return nil, fmt.Errorf(`store: unable to create user default category: %v`, err)
  114. }
  115. _, err = tx.Exec(`INSERT INTO integrations (user_id) VALUES ($1)`, user.ID)
  116. if err != nil {
  117. tx.Rollback()
  118. return nil, fmt.Errorf(`store: unable to create integration row: %v`, err)
  119. }
  120. if err := tx.Commit(); err != nil {
  121. return nil, fmt.Errorf(`store: unable to commit transaction: %v`, err)
  122. }
  123. return &user, nil
  124. }
  125. // UpdateUser updates a user.
  126. func (s *Storage) UpdateUser(user *model.User) error {
  127. if user.Password != "" {
  128. hashedPassword, err := hashPassword(user.Password)
  129. if err != nil {
  130. return err
  131. }
  132. query := `
  133. UPDATE users SET
  134. username=LOWER($1),
  135. password=$2,
  136. is_admin=$3,
  137. theme=$4,
  138. language=$5,
  139. timezone=$6,
  140. entry_direction=$7,
  141. entries_per_page=$8,
  142. keyboard_shortcuts=$9,
  143. show_reading_time=$10,
  144. entry_swipe=$11,
  145. stylesheet=$12,
  146. google_id=$13,
  147. openid_connect_id=$14,
  148. display_mode=$15
  149. WHERE
  150. id=$16
  151. `
  152. _, err = s.db.Exec(
  153. query,
  154. user.Username,
  155. hashedPassword,
  156. user.IsAdmin,
  157. user.Theme,
  158. user.Language,
  159. user.Timezone,
  160. user.EntryDirection,
  161. user.EntriesPerPage,
  162. user.KeyboardShortcuts,
  163. user.ShowReadingTime,
  164. user.EntrySwipe,
  165. user.Stylesheet,
  166. user.GoogleID,
  167. user.OpenIDConnectID,
  168. user.DisplayMode,
  169. user.ID,
  170. )
  171. if err != nil {
  172. return fmt.Errorf(`store: unable to update user: %v`, err)
  173. }
  174. } else {
  175. query := `
  176. UPDATE users SET
  177. username=LOWER($1),
  178. is_admin=$2,
  179. theme=$3,
  180. language=$4,
  181. timezone=$5,
  182. entry_direction=$6,
  183. entries_per_page=$7,
  184. keyboard_shortcuts=$8,
  185. show_reading_time=$9,
  186. entry_swipe=$10,
  187. stylesheet=$11,
  188. google_id=$12,
  189. openid_connect_id=$13,
  190. display_mode=$14
  191. WHERE
  192. id=$15
  193. `
  194. _, err := s.db.Exec(
  195. query,
  196. user.Username,
  197. user.IsAdmin,
  198. user.Theme,
  199. user.Language,
  200. user.Timezone,
  201. user.EntryDirection,
  202. user.EntriesPerPage,
  203. user.KeyboardShortcuts,
  204. user.ShowReadingTime,
  205. user.EntrySwipe,
  206. user.Stylesheet,
  207. user.GoogleID,
  208. user.OpenIDConnectID,
  209. user.DisplayMode,
  210. user.ID,
  211. )
  212. if err != nil {
  213. return fmt.Errorf(`store: unable to update user: %v`, err)
  214. }
  215. }
  216. return nil
  217. }
  218. // UserLanguage returns the language of the given user.
  219. func (s *Storage) UserLanguage(userID int64) (language string) {
  220. err := s.db.QueryRow(`SELECT language FROM users WHERE id = $1`, userID).Scan(&language)
  221. if err != nil {
  222. return "en_US"
  223. }
  224. return language
  225. }
  226. // UserByID finds a user by the ID.
  227. func (s *Storage) UserByID(userID int64) (*model.User, error) {
  228. query := `
  229. SELECT
  230. id,
  231. username,
  232. is_admin,
  233. theme,
  234. language,
  235. timezone,
  236. entry_direction,
  237. entries_per_page,
  238. keyboard_shortcuts,
  239. show_reading_time,
  240. entry_swipe,
  241. last_login_at,
  242. stylesheet,
  243. google_id,
  244. openid_connect_id,
  245. display_mode
  246. FROM
  247. users
  248. WHERE
  249. id = $1
  250. `
  251. return s.fetchUser(query, userID)
  252. }
  253. // UserByUsername finds a user by the username.
  254. func (s *Storage) UserByUsername(username string) (*model.User, error) {
  255. query := `
  256. SELECT
  257. id,
  258. username,
  259. is_admin,
  260. theme,
  261. language,
  262. timezone,
  263. entry_direction,
  264. entries_per_page,
  265. keyboard_shortcuts,
  266. show_reading_time,
  267. entry_swipe,
  268. last_login_at,
  269. stylesheet,
  270. google_id,
  271. openid_connect_id,
  272. display_mode
  273. FROM
  274. users
  275. WHERE
  276. username=LOWER($1)
  277. `
  278. return s.fetchUser(query, username)
  279. }
  280. // UserByField finds a user by a field value.
  281. func (s *Storage) UserByField(field, value string) (*model.User, error) {
  282. query := `
  283. SELECT
  284. id,
  285. username,
  286. is_admin,
  287. theme,
  288. language,
  289. timezone,
  290. entry_direction,
  291. entries_per_page,
  292. keyboard_shortcuts,
  293. show_reading_time,
  294. entry_swipe,
  295. last_login_at,
  296. stylesheet,
  297. google_id,
  298. openid_connect_id,
  299. display_mode
  300. FROM
  301. users
  302. WHERE
  303. %s=$1
  304. `
  305. return s.fetchUser(fmt.Sprintf(query, pq.QuoteIdentifier(field)), value)
  306. }
  307. // AnotherUserWithFieldExists returns true if a user has the value set for the given field.
  308. func (s *Storage) AnotherUserWithFieldExists(userID int64, field, value string) bool {
  309. var result bool
  310. s.db.QueryRow(fmt.Sprintf(`SELECT true FROM users WHERE id <> $1 AND %s=$2`, pq.QuoteIdentifier(field)), userID, value).Scan(&result)
  311. return result
  312. }
  313. // UserByAPIKey returns a User from an API Key.
  314. func (s *Storage) UserByAPIKey(token string) (*model.User, error) {
  315. query := `
  316. SELECT
  317. u.id,
  318. u.username,
  319. u.is_admin,
  320. u.theme,
  321. u.language,
  322. u.timezone,
  323. u.entry_direction,
  324. u.entries_per_page,
  325. u.keyboard_shortcuts,
  326. u.show_reading_time,
  327. u.entry_swipe,
  328. u.last_login_at,
  329. u.stylesheet,
  330. u.google_id,
  331. u.openid_connect_id,
  332. u.display_mode
  333. FROM
  334. users u
  335. LEFT JOIN
  336. api_keys ON api_keys.user_id=u.id
  337. WHERE
  338. api_keys.token = $1
  339. `
  340. return s.fetchUser(query, token)
  341. }
  342. func (s *Storage) fetchUser(query string, args ...interface{}) (*model.User, error) {
  343. var user model.User
  344. err := s.db.QueryRow(query, args...).Scan(
  345. &user.ID,
  346. &user.Username,
  347. &user.IsAdmin,
  348. &user.Theme,
  349. &user.Language,
  350. &user.Timezone,
  351. &user.EntryDirection,
  352. &user.EntriesPerPage,
  353. &user.KeyboardShortcuts,
  354. &user.ShowReadingTime,
  355. &user.EntrySwipe,
  356. &user.LastLoginAt,
  357. &user.Stylesheet,
  358. &user.GoogleID,
  359. &user.OpenIDConnectID,
  360. &user.DisplayMode,
  361. )
  362. if err == sql.ErrNoRows {
  363. return nil, nil
  364. } else if err != nil {
  365. return nil, fmt.Errorf(`store: unable to fetch user: %v`, err)
  366. }
  367. return &user, nil
  368. }
  369. // RemoveUser deletes a user.
  370. func (s *Storage) RemoveUser(userID int64) error {
  371. tx, err := s.db.Begin()
  372. if err != nil {
  373. return fmt.Errorf(`store: unable to start transaction: %v`, err)
  374. }
  375. if _, err := tx.Exec(`DELETE FROM users WHERE id=$1`, userID); err != nil {
  376. tx.Rollback()
  377. return fmt.Errorf(`store: unable to remove user #%d: %v`, userID, err)
  378. }
  379. if _, err := tx.Exec(`DELETE FROM integrations WHERE user_id=$1`, userID); err != nil {
  380. tx.Rollback()
  381. return fmt.Errorf(`store: unable to remove integration settings for user #%d: %v`, userID, err)
  382. }
  383. if err := tx.Commit(); err != nil {
  384. return fmt.Errorf(`store: unable to commit transaction: %v`, err)
  385. }
  386. return nil
  387. }
  388. // RemoveUserAsync deletes user data without locking the database.
  389. func (s *Storage) RemoveUserAsync(userID int64) {
  390. go func() {
  391. deleteUserFeeds(s.db, userID)
  392. s.db.Exec(`DELETE FROM users WHERE id=$1`, userID)
  393. s.db.Exec(`DELETE FROM integrations WHERE user_id=$1`, userID)
  394. logger.Debug(`[MASS DELETE] User #%d has been deleted (%d GoRoutines)`, userID, runtime.NumGoroutine())
  395. }()
  396. }
  397. // Users returns all users.
  398. func (s *Storage) Users() (model.Users, error) {
  399. query := `
  400. SELECT
  401. id,
  402. username,
  403. is_admin,
  404. theme,
  405. language,
  406. timezone,
  407. entry_direction,
  408. entries_per_page,
  409. keyboard_shortcuts,
  410. show_reading_time,
  411. entry_swipe,
  412. last_login_at,
  413. stylesheet,
  414. google_id,
  415. openid_connect_id,
  416. display_mode
  417. FROM
  418. users
  419. ORDER BY username ASC
  420. `
  421. rows, err := s.db.Query(query)
  422. if err != nil {
  423. return nil, fmt.Errorf(`store: unable to fetch users: %v`, err)
  424. }
  425. defer rows.Close()
  426. var users model.Users
  427. for rows.Next() {
  428. var user model.User
  429. err := rows.Scan(
  430. &user.ID,
  431. &user.Username,
  432. &user.IsAdmin,
  433. &user.Theme,
  434. &user.Language,
  435. &user.Timezone,
  436. &user.EntryDirection,
  437. &user.EntriesPerPage,
  438. &user.KeyboardShortcuts,
  439. &user.ShowReadingTime,
  440. &user.EntrySwipe,
  441. &user.LastLoginAt,
  442. &user.Stylesheet,
  443. &user.GoogleID,
  444. &user.OpenIDConnectID,
  445. &user.DisplayMode,
  446. )
  447. if err != nil {
  448. return nil, fmt.Errorf(`store: unable to fetch users row: %v`, err)
  449. }
  450. users = append(users, &user)
  451. }
  452. return users, nil
  453. }
  454. // CheckPassword validate the hashed password.
  455. func (s *Storage) CheckPassword(username, password string) error {
  456. var hash string
  457. username = strings.ToLower(username)
  458. err := s.db.QueryRow("SELECT password FROM users WHERE username=$1", username).Scan(&hash)
  459. if err == sql.ErrNoRows {
  460. return fmt.Errorf(`store: unable to find this user: %s`, username)
  461. } else if err != nil {
  462. return fmt.Errorf(`store: unable to fetch user: %v`, err)
  463. }
  464. if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)); err != nil {
  465. return fmt.Errorf(`store: invalid password for "%s" (%v)`, username, err)
  466. }
  467. return nil
  468. }
  469. // HasPassword returns true if the given user has a password defined.
  470. func (s *Storage) HasPassword(userID int64) (bool, error) {
  471. var result bool
  472. query := `SELECT true FROM users WHERE id=$1 AND password <> ''`
  473. err := s.db.QueryRow(query, userID).Scan(&result)
  474. if err == sql.ErrNoRows {
  475. return false, nil
  476. } else if err != nil {
  477. return false, fmt.Errorf(`store: unable to execute query: %v`, err)
  478. }
  479. if result {
  480. return true, nil
  481. }
  482. return false, nil
  483. }
  484. func hashPassword(password string) (string, error) {
  485. bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  486. return string(bytes), err
  487. }
  488. func deleteUserFeeds(db *sql.DB, userID int64) {
  489. query := `SELECT id FROM feeds WHERE user_id=$1`
  490. rows, err := db.Query(query, userID)
  491. if err != nil {
  492. logger.Error(`store: unable to get user feeds: %v`, err)
  493. return
  494. }
  495. defer rows.Close()
  496. var feedIDs []int64
  497. for rows.Next() {
  498. var feedID int64
  499. rows.Scan(&feedID)
  500. feedIDs = append(feedIDs, feedID)
  501. }
  502. worker := func(jobs <-chan int64, results chan<- bool) {
  503. for feedID := range jobs {
  504. logger.Debug(`[MASS DELETE] Deleting feed #%d for user #%d (%d GoRoutines)`, feedID, userID, runtime.NumGoroutine())
  505. deleteUserEntries(db, userID, feedID)
  506. db.Exec(`DELETE FROM feeds WHERE id=$1`, feedID)
  507. results <- true
  508. }
  509. }
  510. numJobs := len(feedIDs)
  511. jobs := make(chan int64, numJobs)
  512. results := make(chan bool, numJobs)
  513. for w := 0; w < 2; w++ {
  514. go worker(jobs, results)
  515. }
  516. for j := 0; j < numJobs; j++ {
  517. jobs <- feedIDs[j]
  518. }
  519. close(jobs)
  520. for a := 1; a <= numJobs; a++ {
  521. <-results
  522. }
  523. }
  524. func deleteUserEntries(db *sql.DB, userID int64, feedID int64) {
  525. rows, err := db.Query(`SELECT id FROM entries WHERE user_id=$1 AND feed_id=$2`, userID, feedID)
  526. if err != nil {
  527. logger.Error(`store: unable to get user feed entries: %v`, err)
  528. return
  529. }
  530. defer rows.Close()
  531. for rows.Next() {
  532. var entryID int64
  533. rows.Scan(&entryID)
  534. logger.Debug(`[MASS DELETE] Deleting entry #%d for user #%d (%d GoRoutines)`, entryID, userID, runtime.NumGoroutine())
  535. db.Exec(`DELETE FROM enclosures WHERE entry_id=$1 AND user_id=$2`, entryID, userID)
  536. db.Exec(`DELETE FROM entries WHERE id=$1 AND user_id=$2`, entryID, userID)
  537. }
  538. }