2019-06-19 17:52:34 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Api::V1::Admin::AccountsController < Api::BaseController
|
|
|
|
include Authorization
|
|
|
|
include AccountableConcern
|
|
|
|
|
|
|
|
LIMIT = 100
|
|
|
|
|
2022-01-17 00:41:33 -08:00
|
|
|
before_action -> { authorize_if_got_token! :'admin:read', :'admin:read:accounts' }, only: [:index, :show]
|
|
|
|
before_action -> { authorize_if_got_token! :'admin:write', :'admin:write:accounts' }, except: [:index, :show]
|
2019-06-19 17:52:34 -07:00
|
|
|
before_action :set_accounts, only: :index
|
|
|
|
before_action :set_account, except: :index
|
|
|
|
before_action :require_local_account!, only: [:enable, :approve, :reject]
|
|
|
|
|
2022-07-04 17:41:40 -07:00
|
|
|
after_action :verify_authorized
|
2019-06-19 17:52:34 -07:00
|
|
|
after_action :insert_pagination_headers, only: :index
|
|
|
|
|
|
|
|
FILTER_PARAMS = %i(
|
|
|
|
local
|
|
|
|
remote
|
|
|
|
by_domain
|
|
|
|
active
|
|
|
|
pending
|
|
|
|
disabled
|
2020-11-04 11:45:01 -08:00
|
|
|
sensitized
|
2019-06-19 17:52:34 -07:00
|
|
|
silenced
|
|
|
|
suspended
|
|
|
|
username
|
|
|
|
display_name
|
|
|
|
email
|
|
|
|
ip
|
|
|
|
staff
|
|
|
|
).freeze
|
|
|
|
|
|
|
|
PAGINATION_PARAMS = (%i(limit) + FILTER_PARAMS).freeze
|
|
|
|
|
|
|
|
def index
|
|
|
|
authorize :account, :index?
|
|
|
|
render json: @accounts, each_serializer: REST::Admin::AccountSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
authorize @account, :show?
|
|
|
|
render json: @account, serializer: REST::Admin::AccountSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def enable
|
|
|
|
authorize @account.user, :enable?
|
|
|
|
@account.user.enable!
|
|
|
|
log_action :enable, @account.user
|
|
|
|
render json: @account, serializer: REST::Admin::AccountSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def approve
|
|
|
|
authorize @account.user, :approve?
|
|
|
|
@account.user.approve!
|
2022-12-06 15:25:18 -08:00
|
|
|
log_action :approve, @account.user
|
2019-06-19 17:52:34 -07:00
|
|
|
render json: @account, serializer: REST::Admin::AccountSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def reject
|
|
|
|
authorize @account.user, :reject?
|
2020-09-15 05:37:58 -07:00
|
|
|
DeleteAccountService.new.call(@account, reserve_email: false, reserve_username: false)
|
2022-12-06 15:25:18 -08:00
|
|
|
log_action :reject, @account.user
|
2022-10-29 17:43:57 -07:00
|
|
|
render_empty
|
2020-09-15 05:37:58 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
authorize @account, :destroy?
|
|
|
|
Admin::AccountDeletionWorker.perform_async(@account.id)
|
2022-10-29 17:43:57 -07:00
|
|
|
render_empty
|
2019-06-19 17:52:34 -07:00
|
|
|
end
|
|
|
|
|
2020-11-04 11:45:01 -08:00
|
|
|
def unsensitive
|
|
|
|
authorize @account, :unsensitive?
|
|
|
|
@account.unsensitize!
|
|
|
|
log_action :unsensitive, @account
|
|
|
|
render json: @account, serializer: REST::Admin::AccountSerializer
|
|
|
|
end
|
|
|
|
|
2019-06-19 17:52:34 -07:00
|
|
|
def unsilence
|
|
|
|
authorize @account, :unsilence?
|
|
|
|
@account.unsilence!
|
|
|
|
log_action :unsilence, @account
|
|
|
|
render json: @account, serializer: REST::Admin::AccountSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
def unsuspend
|
|
|
|
authorize @account, :unsuspend?
|
|
|
|
@account.unsuspend!
|
2020-09-15 05:37:58 -07:00
|
|
|
Admin::UnsuspensionWorker.perform_async(@account.id)
|
2019-06-19 17:52:34 -07:00
|
|
|
log_action :unsuspend, @account
|
|
|
|
render json: @account, serializer: REST::Admin::AccountSerializer
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def set_accounts
|
2022-01-16 04:23:50 -08:00
|
|
|
@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))
|
2019-06-19 17:52:34 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def set_account
|
|
|
|
@account = Account.find(params[:id])
|
|
|
|
end
|
|
|
|
|
|
|
|
def filtered_accounts
|
2022-03-28 14:57:38 -07:00
|
|
|
AccountFilter.new(translated_filter_params).results
|
2019-06-19 17:52:34 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def filter_params
|
|
|
|
params.permit(*FILTER_PARAMS)
|
|
|
|
end
|
|
|
|
|
2022-03-28 14:57:38 -07:00
|
|
|
def translated_filter_params
|
|
|
|
translated_params = { origin: 'local', status: 'active' }.merge(filter_params.slice(*AccountFilter::KEYS))
|
|
|
|
|
|
|
|
translated_params[:origin] = 'remote' if params[:remote].present?
|
|
|
|
|
|
|
|
%i(active pending disabled silenced suspended).each do |status|
|
|
|
|
translated_params[:status] = status.to_s if params[status].present?
|
|
|
|
end
|
|
|
|
|
2023-02-18 03:37:47 -08:00
|
|
|
translated_params[:role_ids] = UserRole.that_can(:manage_reports).map(&:id) if params[:staff].present?
|
2022-03-28 14:57:38 -07:00
|
|
|
|
|
|
|
translated_params
|
|
|
|
end
|
|
|
|
|
2019-06-19 17:52:34 -07:00
|
|
|
def next_path
|
|
|
|
api_v1_admin_accounts_url(pagination_params(max_id: pagination_max_id)) if records_continue?
|
|
|
|
end
|
|
|
|
|
|
|
|
def prev_path
|
|
|
|
api_v1_admin_accounts_url(pagination_params(min_id: pagination_since_id)) unless @accounts.empty?
|
|
|
|
end
|
|
|
|
|
2024-03-13 01:51:44 -07:00
|
|
|
def pagination_collection
|
|
|
|
@accounts
|
2019-06-19 17:52:34 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
def records_continue?
|
|
|
|
@accounts.size == limit_param(LIMIT)
|
|
|
|
end
|
|
|
|
|
|
|
|
def pagination_params(core_params)
|
|
|
|
params.slice(*PAGINATION_PARAMS).permit(*PAGINATION_PARAMS).merge(core_params)
|
|
|
|
end
|
|
|
|
|
|
|
|
def require_local_account!
|
|
|
|
forbidden unless @account.local? && @account.user.present?
|
|
|
|
end
|
|
|
|
end
|