FEATURE: Allow admins to delete reviewables via API (#21174)

This PR adds the ability to destroy reviewables for a passed user via the API. This was not possible before as this action was reserved for reviewables for you created only.

If a user is an admin and calls the `#destroy` action from the API they are able to destroy a reviewable for a passed user. A user can be targeted by passed either their:
- username
- external_id (for SSO) 

to the request.

In the case you attempt to destroy a non-personal reviewable and
- You are not an admin
- You do not access the `#destroy` action via the API

you will raise a `Discourse::InvalidAccess` (403) and will not succeed in destroying the reviewable.
This commit is contained in:
Isaac Janzen
2023-04-20 09:38:41 -05:00
committed by GitHub
parent ba2adc7793
commit dd495a0e19
3 changed files with 84 additions and 4 deletions

View File

@ -155,7 +155,18 @@ class ReviewablesController < ApplicationController
end
def destroy
reviewable = Reviewable.find_by(id: params[:reviewable_id], created_by: current_user)
user =
if is_api?
if @guardian.is_admin?
fetch_user_from_params
else
raise Discourse::InvalidAccess
end
else
current_user
end
reviewable = Reviewable.find_by(id: params[:reviewable_id], created_by: user)
raise Discourse::NotFound.new if reviewable.blank?
reviewable.perform(current_user, :delete)