user.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 api
  5. import (
  6. "errors"
  7. "github.com/miniflux/miniflux/http/handler"
  8. )
  9. // CreateUser is the API handler to create a new user.
  10. func (c *Controller) CreateUser(ctx *handler.Context, request *handler.Request, response *handler.Response) {
  11. if !ctx.IsAdminUser() {
  12. response.JSON().Forbidden()
  13. return
  14. }
  15. user, err := decodeUserPayload(request.Body())
  16. if err != nil {
  17. response.JSON().BadRequest(err)
  18. return
  19. }
  20. if err := user.ValidateUserCreation(); err != nil {
  21. response.JSON().BadRequest(err)
  22. return
  23. }
  24. if c.store.UserExists(user.Username) {
  25. response.JSON().BadRequest(errors.New("This user already exists"))
  26. return
  27. }
  28. err = c.store.CreateUser(user)
  29. if err != nil {
  30. response.JSON().ServerError(errors.New("Unable to create this user"))
  31. return
  32. }
  33. user.Password = ""
  34. response.JSON().Created(user)
  35. }
  36. // UpdateUser is the API handler to update the given user.
  37. func (c *Controller) UpdateUser(ctx *handler.Context, request *handler.Request, response *handler.Response) {
  38. if !ctx.IsAdminUser() {
  39. response.JSON().Forbidden()
  40. return
  41. }
  42. userID, err := request.IntegerParam("userID")
  43. if err != nil {
  44. response.JSON().BadRequest(err)
  45. return
  46. }
  47. user, err := decodeUserPayload(request.Body())
  48. if err != nil {
  49. response.JSON().BadRequest(err)
  50. return
  51. }
  52. if err := user.ValidateUserModification(); err != nil {
  53. response.JSON().BadRequest(err)
  54. return
  55. }
  56. originalUser, err := c.store.UserByID(userID)
  57. if err != nil {
  58. response.JSON().BadRequest(errors.New("Unable to fetch this user from the database"))
  59. return
  60. }
  61. if originalUser == nil {
  62. response.JSON().NotFound(errors.New("User not found"))
  63. return
  64. }
  65. originalUser.Merge(user)
  66. if err = c.store.UpdateUser(originalUser); err != nil {
  67. response.JSON().ServerError(errors.New("Unable to update this user"))
  68. return
  69. }
  70. response.JSON().Created(originalUser)
  71. }
  72. // Users is the API handler to get the list of users.
  73. func (c *Controller) Users(ctx *handler.Context, request *handler.Request, response *handler.Response) {
  74. if !ctx.IsAdminUser() {
  75. response.JSON().Forbidden()
  76. return
  77. }
  78. users, err := c.store.Users()
  79. if err != nil {
  80. response.JSON().ServerError(errors.New("Unable to fetch the list of users"))
  81. return
  82. }
  83. users.UseTimezone(ctx.UserTimezone())
  84. response.JSON().Standard(users)
  85. }
  86. // UserByID is the API handler to fetch the given user by the ID.
  87. func (c *Controller) UserByID(ctx *handler.Context, request *handler.Request, response *handler.Response) {
  88. if !ctx.IsAdminUser() {
  89. response.JSON().Forbidden()
  90. return
  91. }
  92. userID, err := request.IntegerParam("userID")
  93. if err != nil {
  94. response.JSON().BadRequest(err)
  95. return
  96. }
  97. user, err := c.store.UserByID(userID)
  98. if err != nil {
  99. response.JSON().BadRequest(errors.New("Unable to fetch this user from the database"))
  100. return
  101. }
  102. if user == nil {
  103. response.JSON().NotFound(errors.New("User not found"))
  104. return
  105. }
  106. user.UseTimezone(ctx.UserTimezone())
  107. response.JSON().Standard(user)
  108. }
  109. // UserByUsername is the API handler to fetch the given user by the username.
  110. func (c *Controller) UserByUsername(ctx *handler.Context, request *handler.Request, response *handler.Response) {
  111. if !ctx.IsAdminUser() {
  112. response.JSON().Forbidden()
  113. return
  114. }
  115. username := request.StringParam("username", "")
  116. user, err := c.store.UserByUsername(username)
  117. if err != nil {
  118. response.JSON().BadRequest(errors.New("Unable to fetch this user from the database"))
  119. return
  120. }
  121. if user == nil {
  122. response.JSON().NotFound(errors.New("User not found"))
  123. return
  124. }
  125. response.JSON().Standard(user)
  126. }
  127. // RemoveUser is the API handler to remove an existing user.
  128. func (c *Controller) RemoveUser(ctx *handler.Context, request *handler.Request, response *handler.Response) {
  129. if !ctx.IsAdminUser() {
  130. response.JSON().Forbidden()
  131. return
  132. }
  133. userID, err := request.IntegerParam("userID")
  134. if err != nil {
  135. response.JSON().BadRequest(err)
  136. return
  137. }
  138. user, err := c.store.UserByID(userID)
  139. if err != nil {
  140. response.JSON().ServerError(errors.New("Unable to fetch this user from the database"))
  141. return
  142. }
  143. if user == nil {
  144. response.JSON().NotFound(errors.New("User not found"))
  145. return
  146. }
  147. if err := c.store.RemoveUser(user.ID); err != nil {
  148. response.JSON().BadRequest(errors.New("Unable to remove this user from the database"))
  149. return
  150. }
  151. response.JSON().NoContent()
  152. }