From c15c7545897cd4bad537fcba7657c1953c442b2a Mon Sep 17 00:00:00 2001 From: Marius Balteanu Date: Mon, 8 Jul 2024 21:33:50 +0000 Subject: [PATCH] Introduces @valid_watcher?@ check on watchables (@40946). MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Patch by Jens Krämer (@jkraemer). git-svn-id: https://svn.redmine.org/redmine/trunk@22916 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/watchers_controller.rb | 11 ++++++----- .../acts_as_watchable/lib/acts_as_watchable.rb | 8 ++++++++ test/functional/watchers_controller_test.rb | 6 +++--- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index a446d6410..34f1ffebe 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -53,7 +53,9 @@ class WatchersController < ApplicationController users = Principal.assignable_watchers.where(:id => user_ids).to_a users.each do |user| @watchables.each do |watchable| - Watcher.create(:watchable => watchable, :user => user) + if watchable.valid_watcher?(user) + Watcher.create(:watchable => watchable, :user => user) + end end end respond_to do |format| @@ -161,10 +163,9 @@ class WatchersController < ApplicationController if @watchables && @watchables.size == 1 watchable_object = @watchables.first users -= watchable_object.visible_watcher_users - - if watchable_object.respond_to?(:visible?) - users.reject! {|user| user.is_a?(User) && !watchable_object.visible?(user)} - end + end + @watchables&.each do |watchable| + users.reject!{|user| !watchable.valid_watcher?(user)} end users end 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 56612d692..c8ba2d199 100644 --- a/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb +++ b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb @@ -70,6 +70,14 @@ module Redmine end end + # true if user can be added as a watcher + def valid_watcher?(user) + return true unless respond_to?(:visible?) + return true unless user.is_a?(User) + + visible?(user) + end + # Adds user as a watcher def add_watcher(user) if persisted? diff --git a/test/functional/watchers_controller_test.rb b/test/functional/watchers_controller_test.rb index ed3c25770..6a333a7f9 100644 --- a/test/functional/watchers_controller_test.rb +++ b/test/functional/watchers_controller_test.rb @@ -477,11 +477,11 @@ class WatchersControllerTest < Redmine::ControllerTest assert_response :success - # All users from two projects eCookbook (7) and Private child of eCookbook (9) - assert_select 'input', :count => 5 + # All users from two projects eCookbook (7) and Private child of eCookbook + # (9) who can see both issues + assert_select 'input', :count => 4 assert_select 'input[name=?][value="1"]', 'watcher[user_ids][]' assert_select 'input[name=?][value="2"]', 'watcher[user_ids][]' - assert_select 'input[name=?][value="3"]', 'watcher[user_ids][]' assert_select 'input[name=?][value="8"]', 'watcher[user_ids][]' assert_select 'input[name=?][value="10"]', 'watcher[user_ids][]' end