Skip to content

A platform for community discussion. Free, open, simple.

FEATURE: Add granular API scope for changing post ownership (#36835)

Régis Hanolcd6c6f8abc79
diff --git a/app/models/api_key_scope.rb b/app/models/api_key_scope.rb
index 52e98a0924f..97af146ab83 100644
--- a/app/models/api_key_scope.rb
+++ b/app/models/api_key_scope.rb
@@ -55,6 +55,10 @@ class ApiKeyScope < ActiveRecord::Base
actions: %w[topics#status],
params: %i[topic_id category_id status enabled],
},
+ change_owner: {
+ actions: %w[topics#change_post_owners],
+ params: %i[topic_id],
+ },
},
posts: {
edit: {
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index ba3a504c723..8268c0acde6 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -6550,6 +6550,7 @@ en:
recover: Recover a topic.
read_lists: Read topic lists like top, new, latest, etc. RSS is also supported.
status: "Update a topic's status. Status: closed, archive, visible, pinned. Enabled: true, false. Specify a category_id here and in the request payload to only allow status changes on topics in that category."
+ change_owner: Change the ownership of posts in a topic.
posts:
edit: Edit any post or a specific one.
delete: Delete a post.
diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb
index 3b840f580cd..0e1a8736a16 100644
--- a/spec/requests/topics_controller_spec.rb
+++ b/spec/requests/topics_controller_spec.rb
@@ -1092,6 +1092,43 @@ RSpec.describe TopicsController do
expect(p1.like_count).to eq(0)
end
end
+
+ context "with API key" do
+ let(:api_key) { Fabricate(:api_key, user: admin, created_by: admin) }
+
+ it "allows changing ownership with change_owner scope" do
+ ApiKeyScope.create!(resource: "topics", action: "change_owner", api_key_id: api_key.id)
+
+ post "/t/#{topic.id}/change-owner.json",
+ params: {
+ username: user_a.username_lower,
+ post_ids: [p1.id],
+ },
+ headers: {
+ "HTTP_API_KEY" => api_key.key,
+ "HTTP_API_USERNAME" => api_key.user.username,
+ }
+
+ expect(response.status).to eq(200)
+ expect(p1.reload.user).to eq(user_a)
+ end
+
+ it "denies access without change_owner scope" do
+ ApiKeyScope.create!(resource: "topics", action: "read", api_key_id: api_key.id)
+
+ post "/t/#{topic.id}/change-owner.json",
+ params: {
+ username: user_a.username_lower,
+ post_ids: [p1.id],
+ },
+ headers: {
+ "HTTP_API_KEY" => api_key.key,
+ "HTTP_API_USERNAME" => api_key.user.username,
+ }
+
+ expect(response.status).to eq(403)
+ end
+ end
end
end