Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

fix bug: no more anonymous user when notifying of group membership removals

Christophe Bliard582c8110910e
diff --git a/app/services/groups/concerns/membership_manipulation.rb b/app/services/groups/concerns/membership_manipulation.rb
index 5450fd6d9c3..7d24d9fe0df 100644
--- a/app/services/groups/concerns/membership_manipulation.rb
+++ b/app/services/groups/concerns/membership_manipulation.rb
@@ -78,7 +78,12 @@ module Groups::Concerns
end
def send_notifications(member_ids, message, send_notifications)
- Notifications::GroupMemberAlteredJob.perform_later(member_ids, message, send_notifications)
+ Notifications::GroupMemberAlteredJob.perform_later(
+ User.current,
+ member_ids,
+ message,
+ send_notifications
+ )
end
end
end
diff --git a/app/workers/notifications/group_member_altered_job.rb b/app/workers/notifications/group_member_altered_job.rb
index 5bbead47c0b..f57903a6713 100644
--- a/app/workers/notifications/group_member_altered_job.rb
+++ b/app/workers/notifications/group_member_altered_job.rb
@@ -31,12 +31,14 @@
class Notifications::GroupMemberAlteredJob < ApplicationJob
queue_with_priority :notification
- def perform(members_ids, message, send_notifications)
- each_member(members_ids) do |member|
- OpenProject::Notifications.send(event_type(member),
- member: member,
- message: message,
- send_notifications: send_notifications)
+ def perform(current_user, members_ids, message, send_notifications)
+ User.execute_as(current_user) do
+ each_member(members_ids) do |member|
+ OpenProject::Notifications.send(event_type(member),
+ member: member,
+ message: message,
+ send_notifications: send_notifications)
+ end
end
end
diff --git a/spec/services/groups/add_users_service_integration_spec.rb b/spec/services/groups/add_users_service_integration_spec.rb
index 8fd816b6401..1afde48a18b 100644
--- a/spec/services/groups/add_users_service_integration_spec.rb
+++ b/spec/services/groups/add_users_service_integration_spec.rb
@@ -76,7 +76,8 @@ describe Groups::AddUsersService, 'integration', type: :model do
expect(Notifications::GroupMemberAlteredJob)
.to have_received(:perform_later)
- .with(a_collection_containing_exactly(*ids),
+ .with(current_user,
+ a_collection_containing_exactly(*ids),
message,
true)
end
diff --git a/spec/services/groups/cleanup_inherited_roles_service_integration_spec.rb b/spec/services/groups/cleanup_inherited_roles_service_integration_spec.rb
index a1a25eeaecc..e7d0cd57140 100644
--- a/spec/services/groups/cleanup_inherited_roles_service_integration_spec.rb
+++ b/spec/services/groups/cleanup_inherited_roles_service_integration_spec.rb
@@ -145,7 +145,8 @@ describe Groups::CleanupInheritedRolesService, 'integration', type: :model do
expect(Notifications::GroupMemberAlteredJob)
.to have_received(:perform_later)
- .with([first_user_member.id],
+ .with(current_user,
+ [first_user_member.id],
message,
true)
end
@@ -187,7 +188,8 @@ describe Groups::CleanupInheritedRolesService, 'integration', type: :model do
expect(Notifications::GroupMemberAlteredJob)
.to have_received(:perform_later)
- .with([first_user_member.id],
+ .with(current_user,
+ [first_user_member.id],
message,
true)
end
diff --git a/spec/services/groups/update_roles_service_integration_spec.rb b/spec/services/groups/update_roles_service_integration_spec.rb
index ec1c5a9c8fa..a6363b7f54d 100644
--- a/spec/services/groups/update_roles_service_integration_spec.rb
+++ b/spec/services/groups/update_roles_service_integration_spec.rb
@@ -82,7 +82,8 @@ describe Groups::UpdateRolesService, 'integration', type: :model do
expect(Notifications::GroupMemberAlteredJob)
.to have_received(:perform_later)
- .with(a_collection_containing_exactly(*Member.where(principal: user).pluck(:id)),
+ .with(current_user,
+ a_collection_containing_exactly(*Member.where(principal: user).pluck(:id)),
message,
true)
end
diff --git a/spec/workers/notifications/group_member_altered_job_spec.rb b/spec/workers/notifications/group_member_altered_job_spec.rb
index e81128a60ac..e869a06ce5a 100644
--- a/spec/workers/notifications/group_member_altered_job_spec.rb
+++ b/spec/workers/notifications/group_member_altered_job_spec.rb
@@ -32,9 +32,10 @@ require 'spec_helper'
describe Notifications::GroupMemberAlteredJob, type: :model do
subject(:service_call) do
- described_class.new.perform(members_ids, message, send_notification)
+ described_class.new.perform(current_user, members_ids, message, send_notification)
end
+ let(:current_user) { build_stubbed(:user) }
let(:time) { Time.now }
let(:member1) do
build_stubbed(:member, updated_at: time, created_at: time)
@@ -72,4 +73,16 @@ describe Notifications::GroupMemberAlteredJob, type: :model do
.to have_received(:send)
.with(OpenProject::Events::MEMBER_UPDATED, member: member2, message: message, send_notifications: send_notification)
end
+
+ it 'propagates the given current user when sending notifications' do
+ notifications_called = false
+ allow(OpenProject::Notifications)
+ .to receive(:send) do |_args|
+ expect(User.current).to be(current_user)
+ notifications_called = true
+ end
+
+ service_call
+ expect(notifications_called).to be(true)
+ end
end