Project

General

Profile

Patch #37065 » 0001-Respect-group-memberships-when-checking-if-an-object.patch

Holger Just, 2022-06-16 17:44

View differences:

lib/plugins/acts_as_watchable/lib/acts_as_watchable.rb
15 15
            has_many :watchers, :as => :watchable, :dependent => :delete_all
16 16
            has_many :watcher_users, :through => :watchers, :source => :user, :validate => false
17 17

  
18
            scope :watched_by, lambda { |user_id|
18
            scope :watched_by, lambda { |principal|
19
              user_ids = Array(principal.id)
20
              user_ids |= principal.group_ids if principal.is_a?(User)
21
              user_ids.compact!
22

  
19 23
              joins(:watchers).
20
              where("#{Watcher.table_name}.user_id = ?", user_id)
24
              where("#{Watcher.table_name}.user_id IN (?)", user_ids)
21 25
            }
22 26
          end
23 27
          send :include, Redmine::Acts::Watchable::InstanceMethods
......
66 70
          super user_ids
67 71
        end
68 72

  
69
        # Returns true if object is watched by +user+
70
        def watched_by?(user)
71
          !!(user && self.watcher_user_ids.detect {|uid| uid == user.id })
73
        # Returns true if object is watched by +principal+, that is
74
        # either by a given group,
75
        # or by a given user or any of their groups
76
        def watched_by?(principal)
77
          return false unless principal
78

  
79
          user_ids = Array(principal.id)
80
          user_ids |= principal.group_ids if principal.is_a?(User)
81
          user_ids.compact!
82

  
83
          (self.watcher_user_ids & user_ids).any?
72 84
        end
73 85

  
74 86
        def notified_watchers
test/unit/watcher_test.rb
20 20
require File.expand_path('../../test_helper', __FILE__)
21 21

  
22 22
class WatcherTest < ActiveSupport::TestCase
23
  fixtures :projects, :users, :email_addresses, :members, :member_roles, :roles, :enabled_modules,
23
  fixtures :projects, :groups_users, :users, :email_addresses, :members, :member_roles, :roles, :enabled_modules,
24 24
           :issues, :issue_statuses, :enumerations, :trackers, :projects_trackers,
25 25
           :boards, :messages,
26 26
           :wikis, :wiki_pages,
......
60 60
    assert Issue.watched_by(@user).include?(@issue)
61 61
  end
62 62

  
63
  def test_watched_by_group
64
    group = Group.find(10)
65
    user = User.find(8)
66
    assert @issue.add_watcher(group)
67
    @issue.reload
68

  
69
    assert @issue.watched_by?(group)
70
    assert Issue.watched_by(group).include?(@issue)
71

  
72
    assert @issue.watched_by?(user)
73
    assert Issue.watched_by(user).include?(@issue)
74
  end
75

  
63 76
  def test_watcher_users
64 77
    watcher_users = Issue.find(2).watcher_users
65 78
    assert_kind_of Array, watcher_users.collect{|w| w}
(3-3/3)