Skip to content

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

SECURITY: Staff can modify any user's group notification level

Isaac Janzendb4a4b076b6c
diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb
index eae8bb72968..e5768c7e519 100644
--- a/app/controllers/groups_controller.rb
+++ b/app/controllers/groups_controller.rb
@@ -656,10 +656,10 @@ class GroupsController < ApplicationController
user_id = current_user.id
user_id = params[:user_id] || user_id if guardian.is_staff?
- GroupUser
- .where(group_id: group.id)
- .where(user_id: user_id)
- .update_all(notification_level: notification_level)
+ group_user = GroupUser.find_by(group_id: group.id, user_id: user_id)
+ raise Discourse::InvalidParameters.new(:user_id) if group_user.blank?
+
+ group_user.update!(notification_level: notification_level)
render json: success_json
end
diff --git a/spec/requests/groups_controller_spec.rb b/spec/requests/groups_controller_spec.rb
index 82978184c28..07779583e18 100644
--- a/spec/requests/groups_controller_spec.rb
+++ b/spec/requests/groups_controller_spec.rb
@@ -2915,6 +2915,78 @@ RSpec.describe GroupsController do
end
end
+ describe "#set_notifications" do
+ fab!(:target_user, :user)
+ fab!(:non_member_user, :user)
+ fab!(:group)
+
+ context "when target user is in group" do
+ before do
+ group.add(moderator)
+ group.add(target_user)
+ end
+
+ it "allows a staff member to change notification level for a user who is a member of the group" do
+ sign_in(moderator)
+
+ group_user = GroupUser.find_by(group_id: group.id, user_id: target_user.id)
+
+ post "/groups/#{group.name}/notifications.json",
+ params: {
+ notification_level: NotificationLevels.all[:muted],
+ user_id: target_user.id,
+ }
+
+ expect(response.status).to eq(200)
+ expect(group_user.reload.notification_level).to eq(NotificationLevels.all[:muted])
+ end
+
+ it "does not allow a staff member to change notification level for a user who is not a member of the group" do
+ sign_in(moderator)
+
+ post "/groups/#{group.name}/notifications.json",
+ params: {
+ notification_level: NotificationLevels.all[:muted],
+ user_id: non_member_user.id,
+ }
+
+ expect(response.status).to eq(400)
+ end
+
+ it "does not allow a regular user to change another user's notification level" do
+ sign_in(target_user)
+
+ mod_group_user = GroupUser.find_by(group_id: group.id, user_id: moderator.id)
+ original_level = mod_group_user.notification_level
+
+ post "/groups/#{group.name}/notifications.json",
+ params: {
+ notification_level: NotificationLevels.all[:muted],
+ user_id: moderator.id,
+ }
+
+ expect(response.status).to eq(200)
+ expect(mod_group_user.reload.notification_level).to eq(original_level)
+ end
+ end
+
+ context "when target user is not in group" do
+ before { group.add(moderator) }
+
+ it "does not allow a staff member to change notification level for a user who is not a member of the group" do
+ sign_in(moderator)
+
+ post "/groups/#{group.name}/notifications.json",
+ params: {
+ notification_level: NotificationLevels.all[:muted],
+ user_id: target_user.id,
+ }
+
+ expect(response.status).to eq(400)
+ end
+ end
+ end
+
describe "requires_login for state-changing actions" do
fab!(:group)