user_remove.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2018 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 ui
  5. import (
  6. "net/http"
  7. "github.com/miniflux/miniflux/http/context"
  8. "github.com/miniflux/miniflux/http/request"
  9. "github.com/miniflux/miniflux/http/response"
  10. "github.com/miniflux/miniflux/http/response/html"
  11. "github.com/miniflux/miniflux/http/route"
  12. )
  13. // RemoveUser deletes a user from the database.
  14. func (c *Controller) RemoveUser(w http.ResponseWriter, r *http.Request) {
  15. ctx := context.New(r)
  16. user, err := c.store.UserByID(ctx.UserID())
  17. if err != nil {
  18. html.ServerError(w, err)
  19. return
  20. }
  21. if !user.IsAdmin {
  22. html.Forbidden(w)
  23. return
  24. }
  25. userID, err := request.IntParam(r, "userID")
  26. if err != nil {
  27. html.BadRequest(w, err)
  28. return
  29. }
  30. selectedUser, err := c.store.UserByID(userID)
  31. if err != nil {
  32. html.ServerError(w, err)
  33. return
  34. }
  35. if selectedUser == nil {
  36. html.NotFound(w)
  37. return
  38. }
  39. if err := c.store.RemoveUser(selectedUser.ID); err != nil {
  40. html.ServerError(w, err)
  41. return
  42. }
  43. response.Redirect(w, r, route.Path(c.router, "users"))
  44. }