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.

143 lines
3.6 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. sensitized
  21. silenced
  22. suspended
  23. username
  24. display_name
  25. email
  26. ip
  27. staff
  28. ).freeze
  29. PAGINATION_PARAMS = (%i(limit) + FILTER_PARAMS).freeze
  30. def index
  31. authorize :account, :index?
  32. render json: @accounts, each_serializer: REST::Admin::AccountSerializer
  33. end
  34. def show
  35. authorize @account, :show?
  36. render json: @account, serializer: REST::Admin::AccountSerializer
  37. end
  38. def enable
  39. authorize @account.user, :enable?
  40. @account.user.enable!
  41. log_action :enable, @account.user
  42. render json: @account, serializer: REST::Admin::AccountSerializer
  43. end
  44. def approve
  45. authorize @account.user, :approve?
  46. @account.user.approve!
  47. render json: @account, serializer: REST::Admin::AccountSerializer
  48. end
  49. def reject
  50. authorize @account.user, :reject?
  51. DeleteAccountService.new.call(@account, reserve_email: false, reserve_username: false)
  52. render json: @account, serializer: REST::Admin::AccountSerializer
  53. end
  54. def destroy
  55. authorize @account, :destroy?
  56. Admin::AccountDeletionWorker.perform_async(@account.id)
  57. render json: @account, serializer: REST::Admin::AccountSerializer
  58. end
  59. def unsensitive
  60. authorize @account, :unsensitive?
  61. @account.unsensitize!
  62. log_action :unsensitive, @account
  63. render json: @account, serializer: REST::Admin::AccountSerializer
  64. end
  65. def unsilence
  66. authorize @account, :unsilence?
  67. @account.unsilence!
  68. log_action :unsilence, @account
  69. render json: @account, serializer: REST::Admin::AccountSerializer
  70. end
  71. def unsuspend
  72. authorize @account, :unsuspend?
  73. @account.unsuspend!
  74. Admin::UnsuspensionWorker.perform_async(@account.id)
  75. log_action :unsuspend, @account
  76. render json: @account, serializer: REST::Admin::AccountSerializer
  77. end
  78. private
  79. def set_accounts
  80. @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))
  81. end
  82. def set_account
  83. @account = Account.find(params[:id])
  84. end
  85. def filtered_accounts
  86. AccountFilter.new(filter_params).results
  87. end
  88. def filter_params
  89. params.permit(*FILTER_PARAMS)
  90. end
  91. def insert_pagination_headers
  92. set_pagination_headers(next_path, prev_path)
  93. end
  94. def next_path
  95. api_v1_admin_accounts_url(pagination_params(max_id: pagination_max_id)) if records_continue?
  96. end
  97. def prev_path
  98. api_v1_admin_accounts_url(pagination_params(min_id: pagination_since_id)) unless @accounts.empty?
  99. end
  100. def pagination_max_id
  101. @accounts.last.id
  102. end
  103. def pagination_since_id
  104. @accounts.first.id
  105. end
  106. def records_continue?
  107. @accounts.size == limit_param(LIMIT)
  108. end
  109. def pagination_params(core_params)
  110. params.slice(*PAGINATION_PARAMS).permit(*PAGINATION_PARAMS).merge(core_params)
  111. end
  112. def require_local_account!
  113. forbidden unless @account.local? && @account.user.present?
  114. end
  115. end