docbrown/app/controllers/concerns/api/users_controller.rb
Fernando Valverde c6c39813a1
New suspend user API endpoint (#18043)
* New suspend user API endpoint

* Restrict admin access only to suspend action

* Add suspended? check on new endpoint spec

* Replace POST with PUT action

* Replace redundant response payload with empty 200 response

* Rely on user_policy instead of before_action method

* Use alias with matching method name instead of unpublish

* Use already existing toggle_suspension_status? method in policy

* Remove manual creation of note

* Update suspend success spec
2022-07-15 09:07:57 -06:00

50 lines
1.4 KiB
Ruby

module Api
module UsersController
extend ActiveSupport::Concern
SHOW_ATTRIBUTES_FOR_SERIALIZATION = %i[
id username name summary twitter_username github_username website_url
location created_at profile_image registered
].freeze
def show
relation = User.joins(:profile).select(SHOW_ATTRIBUTES_FOR_SERIALIZATION)
@user = if params[:id] == "by_username"
relation.find_by!(username: params[:url])
else
relation.find(params[:id])
end
not_found unless @user.registered
end
def me
render :show
end
def suspend
authorize(@user, :toggle_suspension_status?)
target_user = User.find(params[:id])
suspend_params = { note_for_current_role: params[:note], user_status: "Suspended" }
begin
Moderator::ManageActivityAndRoles.handle_user_roles(admin: @user,
user: target_user,
user_params: suspend_params)
render head: :ok
rescue StandardError
render json: {
success: false,
message: @user.errors_as_sentence
}, status: :unprocessable_entity
end
end
def unpublish
authorize(@user, :unpublish_all_articles?)
Moderator::UnpublishAllArticlesWorker.perform_async(params[:id].to_i)
render head: :ok
end
end
end