You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

135 lines
3.4 KiB

  1. # frozen_string_literal: true
  2. class Api::V1::Admin::AccountsController < Api::BaseController
  3. include Authorization
  4. include AccountableConcern
  5. LIMIT = 100
  6. before_action -> { doorkeeper_authorize! :'admin:read', :'admin:read:accounts' }, only: [:index, :show]
  7. before_action -> { doorkeeper_authorize! :'admin:write', :'admin:write:accounts' }, except: [:index, :show]
  8. before_action :require_staff!
  9. before_action :set_accounts, only: :index
  10. before_action :set_account, except: :index
  11. before_action :require_local_account!, only: [:enable, :approve, :reject]
  12. after_action :insert_pagination_headers, only: :index
  13. FILTER_PARAMS = %i(
  14. local
  15. remote
  16. by_domain
  17. active
  18. pending
  19. disabled
  20. silenced
  21. suspended
  22. username
  23. display_name
  24. email
  25. ip
  26. staff
  27. ).freeze
  28. PAGINATION_PARAMS = (%i(limit) + FILTER_PARAMS).freeze
  29. def index
  30. authorize :account, :index?
  31. render json: @accounts, each_serializer: REST::Admin::AccountSerializer
  32. end
  33. def show
  34. authorize @account, :show?
  35. render json: @account, serializer: REST::Admin::AccountSerializer
  36. end
  37. def enable
  38. authorize @account.user, :enable?
  39. @account.user.enable!
  40. log_action :enable, @account.user
  41. render json: @account, serializer: REST::Admin::AccountSerializer
  42. end
  43. def approve
  44. authorize @account.user, :approve?
  45. @account.user.approve!
  46. render json: @account, serializer: REST::Admin::AccountSerializer
  47. end
  48. def reject
  49. authorize @account.user, :reject?
  50. DeleteAccountService.new.call(@account, reserve_email: false, reserve_username: false)
  51. render json: @account, serializer: REST::Admin::AccountSerializer
  52. end
  53. def destroy
  54. authorize @account, :destroy?
  55. Admin::AccountDeletionWorker.perform_async(@account.id)
  56. render json: @account, serializer: REST::Admin::AccountSerializer
  57. end
  58. def unsilence
  59. authorize @account, :unsilence?
  60. @account.unsilence!
  61. log_action :unsilence, @account
  62. render json: @account, serializer: REST::Admin::AccountSerializer
  63. end
  64. def unsuspend
  65. authorize @account, :unsuspend?
  66. @account.unsuspend!
  67. Admin::UnsuspensionWorker.perform_async(@account.id)
  68. log_action :unsuspend, @account
  69. render json: @account, serializer: REST::Admin::AccountSerializer
  70. end
  71. private
  72. def set_accounts
  73. @accounts = filtered_accounts.order(id: :desc).includes(user: [:invite_request, :invite]).to_a_paginated_by_id(limit_param(LIMIT), params_slice(:max_id, :since_id, :min_id))
  74. end
  75. def set_account
  76. @account = Account.find(params[:id])
  77. end
  78. def filtered_accounts
  79. AccountFilter.new(filter_params).results
  80. end
  81. def filter_params
  82. params.permit(*FILTER_PARAMS)
  83. end
  84. def insert_pagination_headers
  85. set_pagination_headers(next_path, prev_path)
  86. end
  87. def next_path
  88. api_v1_admin_accounts_url(pagination_params(max_id: pagination_max_id)) if records_continue?
  89. end
  90. def prev_path
  91. api_v1_admin_accounts_url(pagination_params(min_id: pagination_since_id)) unless @accounts.empty?
  92. end
  93. def pagination_max_id
  94. @accounts.last.id
  95. end
  96. def pagination_since_id
  97. @accounts.first.id
  98. end
  99. def records_continue?
  100. @accounts.size == limit_param(LIMIT)
  101. end
  102. def pagination_params(core_params)
  103. params.slice(*PAGINATION_PARAMS).permit(*PAGINATION_PARAMS).merge(core_params)
  104. end
  105. def require_local_account!
  106. forbidden unless @account.local? && @account.user.present?
  107. end
  108. end