accounts_controller.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 -> { authorize_if_got_token! :'admin:read', :'admin:read:accounts' }, only: [:index, :show]
  7. before_action -> { authorize_if_got_token! :'admin:write', :'admin:write:accounts' }, except: [:index, :show]
  8. before_action :set_accounts, only: :index
  9. before_action :set_account, except: :index
  10. before_action :require_local_account!, only: [:enable, :approve, :reject]
  11. after_action :verify_authorized
  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_empty
  53. end
  54. def destroy
  55. authorize @account, :destroy?
  56. Admin::AccountDeletionWorker.perform_async(@account.id)
  57. render_empty
  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, :ips]).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(translated_filter_params).results
  87. end
  88. def filter_params
  89. params.permit(*FILTER_PARAMS)
  90. end
  91. def translated_filter_params
  92. translated_params = { origin: 'local', status: 'active' }.merge(filter_params.slice(*AccountFilter::KEYS))
  93. translated_params[:origin] = 'remote' if params[:remote].present?
  94. %i(active pending disabled silenced suspended).each do |status|
  95. translated_params[:status] = status.to_s if params[status].present?
  96. end
  97. if params[:staff].present?
  98. translated_params[:role_ids] = UserRole.that_can(:manage_reports).map(&:id)
  99. end
  100. translated_params
  101. end
  102. def insert_pagination_headers
  103. set_pagination_headers(next_path, prev_path)
  104. end
  105. def next_path
  106. api_v1_admin_accounts_url(pagination_params(max_id: pagination_max_id)) if records_continue?
  107. end
  108. def prev_path
  109. api_v1_admin_accounts_url(pagination_params(min_id: pagination_since_id)) unless @accounts.empty?
  110. end
  111. def pagination_max_id
  112. @accounts.last.id
  113. end
  114. def pagination_since_id
  115. @accounts.first.id
  116. end
  117. def records_continue?
  118. @accounts.size == limit_param(LIMIT)
  119. end
  120. def pagination_params(core_params)
  121. params.slice(*PAGINATION_PARAMS).permit(*PAGINATION_PARAMS).merge(core_params)
  122. end
  123. def require_local_account!
  124. forbidden unless @account.local? && @account.user.present?
  125. end
  126. end