diff options
author | Marius Balteanu <marius.balteanu@zitec.com> | 2022-06-20 05:53:43 +0000 |
---|---|---|
committer | Marius Balteanu <marius.balteanu@zitec.com> | 2022-06-20 05:53:43 +0000 |
commit | 6ffbd3c29e33b0396b825df3251f286af5c16644 (patch) | |
tree | ba31a75e5a1182c9e2307cb6892bf1c123bcf8d0 /lib | |
parent | 1b300f138f4626b3cebd8ff1ff1d2af47ad856cc (diff) | |
download | redmine-6ffbd3c29e33b0396b825df3251f286af5c16644.tar.gz redmine-6ffbd3c29e33b0396b825df3251f286af5c16644.zip |
Respect group memberships when checking if an object is watched (#37065).
Patch Holger Just.
git-svn-id: https://svn.redmine.org/redmine/trunk@21661 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'lib')
-rw-r--r-- | lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb | 22 |
1 files changed, 17 insertions, 5 deletions
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 bb71a2884..907535ec7 100644 --- a/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb +++ b/lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb @@ -15,9 +15,13 @@ module Redmine has_many :watchers, :as => :watchable, :dependent => :delete_all has_many :watcher_users, :through => :watchers, :source => :user, :validate => false - scope :watched_by, lambda { |user_id| + scope :watched_by, lambda { |principal| + user_ids = Array(principal.id) + user_ids |= principal.group_ids if principal.is_a?(User) + user_ids.compact! + joins(:watchers). - where("#{Watcher.table_name}.user_id = ?", user_id) + where("#{Watcher.table_name}.user_id IN (?)", user_ids) } end send :include, Redmine::Acts::Watchable::InstanceMethods @@ -66,9 +70,17 @@ module Redmine super user_ids end - # Returns true if object is watched by +user+ - def watched_by?(user) - !!(user && self.watcher_user_ids.detect {|uid| uid == user.id }) + # Returns true if object is watched by +principal+, that is + # either by a given group, + # or by a given user or any of their groups + def watched_by?(principal) + return false unless principal + + user_ids = Array(principal.id) + user_ids |= principal.group_ids if principal.is_a?(User) + user_ids.compact! + + (self.watcher_user_ids & user_ids).any? end def notified_watchers |