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

Avoid returning all users for watcher_recipients (#9513)

Oliver Günther4bb8b9c32579
diff --git a/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb
index b4471d531b5..11f8682d640 100644
--- a/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb
+++ b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb
@@ -161,13 +161,24 @@ module Redmine
watcher_user_ids.any? { |uid| uid == user.id })
end
- # Returns an array of watchers
- # This method, for the time being only returns users which have their mail notification setting
- # set to watched: true
+ # Returns a scope of users watching the instance that should be notified via mail upon updates to the instance.
+ # The users need to have the necessary permissions to see the instance as defined by the watchable_permission.
+ # Additionally, the users need to have their mail notification setting set to watched: true or all: true.
def watcher_recipients
+ possible_watcher_users
+ .where(id: watcher_users_with_active_notification)
+ end
+
+ protected
+
+ # Ensure that only watcher users with mail=all notification or mail=watched are returned here.
+ def watcher_users_with_active_notification
mail_settings = NotificationSetting.applicable(project).mail
- possible_watcher_users.where(id: mail_settings.where(all: true).or(mail_settings).where(watched: true).select(:user_id))
+ mail_settings
+ .where(all: true, user_id: watcher_users)
+ .or(mail_settings.where(watched: true, user_id: watcher_users))
+ .select(:user_id)
end
module ClassMethods
diff --git a/spec/factories/user_factory.rb b/spec/factories/user_factory.rb
index 2e55ff7d112..a13f7a024c5 100644
--- a/spec/factories/user_factory.rb
+++ b/spec/factories/user_factory.rb
@@ -61,7 +61,7 @@ FactoryBot.define do
user.notification_settings = [
FactoryBot.create(:mail_notification_setting, user: user, all: true),
FactoryBot.create(:in_app_notification_setting, user: user, all: true),
- FactoryBot.create(:mail_digest_notification_setting, user: user),
+ FactoryBot.create(:mail_digest_notification_setting, user: user)
]
end
diff --git a/spec/factories/watcher_factory.rb b/spec/factories/watcher_factory.rb
index dd8651cb5be..2ba726db219 100644
--- a/spec/factories/watcher_factory.rb
+++ b/spec/factories/watcher_factory.rb
@@ -30,5 +30,9 @@ FactoryBot.define do
factory :watcher do
association :watchable
association :user
+
+ trait :skip_validate do
+ to_create { |instance| instance.save(validate: false) }
+ end
end
end
diff --git a/spec/lib/acts_as_watchable/lib/acts_as_watchable_spec.rb b/spec/lib/acts_as_watchable/lib/acts_as_watchable_spec.rb
new file mode 100644
index 00000000000..3f9a25c5ced
--- /dev/null
+++ b/spec/lib/acts_as_watchable/lib/acts_as_watchable_spec.rb
@@ -0,0 +1,129 @@
+#-- copyright
+# OpenProject is an open source project management software.
+# Copyright (C) 2012-2021 the OpenProject GmbH
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License version 3.
+#
+# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
+# Copyright (C) 2006-2013 Jean-Philippe Lang
+# Copyright (C) 2010-2013 the ChiliProject Team
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# See docs/COPYRIGHT.rdoc for more details.
+#++
+
+require 'spec_helper'
+
+describe 'acts_as_watchable including model (e.g. WikiPage)', type: :model do
+ let(:klass) { WikiPage }
+ let(:project) { wiki.project }
+ let(:wiki) { FactoryBot.create(:wiki) }
+ let(:instance) { FactoryBot.create(:wiki_page, wiki: wiki) }
+
+ describe '#watcher_recipients' do
+ subject(:watcher_recipients) do
+ instance.watcher_recipients
+ end
+
+ let(:watcher_all_notifications) do
+ FactoryBot.create(:watcher,
+ watchable: instance,
+ user: watcher_user_all_notifications)
+ end
+ let(:watcher_user_all_notifications) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: %i(view_wiki_pages))
+ end
+
+ let(:watcher_watched_notifications) do
+ FactoryBot.create(:watcher,
+ watchable: instance,
+ user: watcher_user_watched_notifications)
+ end
+ let(:watcher_user_watched_notifications) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: %i(view_wiki_pages)).tap do |user|
+ user.notification_settings.mail.update_all(involved: false,
+ mentioned: false,
+ watched: true,
+ all: false)
+ end
+ end
+
+ let(:watcher_no_notifications) do
+ FactoryBot.create(:watcher,
+ watchable: instance,
+ user: watcher_user_no_notifications)
+ end
+ let(:watcher_user_no_notifications) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: %i(view_wiki_pages)).tap do |user|
+ user.notification_settings.mail.update_all(involved: false,
+ mentioned: false,
+ watched: false,
+ all: false)
+ end
+ end
+
+ let(:watcher_no_permission) do
+ FactoryBot.create(:watcher,
+ :skip_validate,
+ watchable: instance,
+ user: watcher_user_no_permission)
+ end
+ let(:watcher_user_no_permission) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: %i())
+ end
+
+ let(:watcher_locked) do
+ FactoryBot.create(:watcher,
+ :skip_validate,
+ watchable: instance,
+ user: watcher_user_no_permission)
+ end
+ let(:watcher_user_locked) do
+ FactoryBot.create(:locked_user,
+ member_in_project: project,
+ member_with_permissions: %i(view_wiki_pages))
+ end
+
+ let(:non_watcher_user_all_notifications) do
+ FactoryBot.create(:user,
+ member_in_project: project,
+ member_with_permissions: %i(view_wiki_pages))
+ end
+
+ before do
+ watcher_all_notifications
+ watcher_watched_notifications
+ watcher_no_notifications
+ watcher_no_permission
+
+ non_watcher_user_all_notifications
+ end
+
+ it 'includes users watching the instance and having notification settings and permissions' do
+ expect(watcher_recipients)
+ .to match_array([watcher_user_all_notifications, watcher_user_watched_notifications])
+ end
+ end
+end