user.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  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. if err := s.deleteUserFeeds(userID); err != nil {
  392. logger.Error(`%v`, err)
  393. return
  394. }
  395. s.db.Exec(`DELETE FROM users WHERE id=$1`, userID)
  396. s.db.Exec(`DELETE FROM integrations WHERE user_id=$1`, userID)
  397. logger.Debug(`[MASS DELETE] User #%d has been deleted (%d GoRoutines)`, userID, runtime.NumGoroutine())
  398. }()
  399. }
  400. func (s *Storage) deleteUserFeeds(userID int64) error {
  401. rows, err := s.db.Query(`SELECT id FROM feeds WHERE user_id=$1`, userID)
  402. if err != nil {
  403. return fmt.Errorf(`store: unable to get user feeds: %v`, err)
  404. }
  405. defer rows.Close()
  406. for rows.Next() {
  407. var feedID int64
  408. rows.Scan(&feedID)
  409. logger.Debug(`[USER DELETION] Deleting feed #%d for user #%d (%d GoRoutines)`, feedID, userID, runtime.NumGoroutine())
  410. if err := s.RemoveFeed(userID, feedID); err != nil {
  411. return err
  412. }
  413. }
  414. return nil
  415. }
  416. // Users returns all users.
  417. func (s *Storage) Users() (model.Users, error) {
  418. query := `
  419. SELECT
  420. id,
  421. username,
  422. is_admin,
  423. theme,
  424. language,
  425. timezone,
  426. entry_direction,
  427. entries_per_page,
  428. keyboard_shortcuts,
  429. show_reading_time,
  430. entry_swipe,
  431. last_login_at,
  432. stylesheet,
  433. google_id,
  434. openid_connect_id,
  435. display_mode
  436. FROM
  437. users
  438. ORDER BY username ASC
  439. `
  440. rows, err := s.db.Query(query)
  441. if err != nil {
  442. return nil, fmt.Errorf(`store: unable to fetch users: %v`, err)
  443. }
  444. defer rows.Close()
  445. var users model.Users
  446. for rows.Next() {
  447. var user model.User
  448. err := rows.Scan(
  449. &user.ID,
  450. &user.Username,
  451. &user.IsAdmin,
  452. &user.Theme,
  453. &user.Language,
  454. &user.Timezone,
  455. &user.EntryDirection,
  456. &user.EntriesPerPage,
  457. &user.KeyboardShortcuts,
  458. &user.ShowReadingTime,
  459. &user.EntrySwipe,
  460. &user.LastLoginAt,
  461. &user.Stylesheet,
  462. &user.GoogleID,
  463. &user.OpenIDConnectID,
  464. &user.DisplayMode,
  465. )
  466. if err != nil {
  467. return nil, fmt.Errorf(`store: unable to fetch users row: %v`, err)
  468. }
  469. users = append(users, &user)
  470. }
  471. return users, nil
  472. }
  473. // CheckPassword validate the hashed password.
  474. func (s *Storage) CheckPassword(username, password string) error {
  475. var hash string
  476. username = strings.ToLower(username)
  477. err := s.db.QueryRow("SELECT password FROM users WHERE username=$1", username).Scan(&hash)
  478. if err == sql.ErrNoRows {
  479. return fmt.Errorf(`store: unable to find this user: %s`, username)
  480. } else if err != nil {
  481. return fmt.Errorf(`store: unable to fetch user: %v`, err)
  482. }
  483. if err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)); err != nil {
  484. return fmt.Errorf(`store: invalid password for "%s" (%v)`, username, err)
  485. }
  486. return nil
  487. }
  488. // HasPassword returns true if the given user has a password defined.
  489. func (s *Storage) HasPassword(userID int64) (bool, error) {
  490. var result bool
  491. query := `SELECT true FROM users WHERE id=$1 AND password <> ''`
  492. err := s.db.QueryRow(query, userID).Scan(&result)
  493. if err == sql.ErrNoRows {
  494. return false, nil
  495. } else if err != nil {
  496. return false, fmt.Errorf(`store: unable to execute query: %v`, err)
  497. }
  498. if result {
  499. return true, nil
  500. }
  501. return false, nil
  502. }
  503. func hashPassword(password string) (string, error) {
  504. bytes, err := bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
  505. return string(bytes), err
  506. }