##// END OF EJS Templates
Fixed that Redmine::Activity::Fetcher should consider activity provider permission option if given (#18832)....
Jean-Philippe Lang -
r13513:886b9c14d0cb
parent child
Show More
@@ -1,46 +1,52
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 module Activity
20 20
21 21 mattr_accessor :available_event_types, :default_event_types, :providers
22 22
23 23 @@available_event_types = []
24 24 @@default_event_types = []
25 25 @@providers = Hash.new {|h,k| h[k]=[] }
26 26
27 27 class << self
28 28 def map(&block)
29 29 yield self
30 30 end
31 31
32 32 # Registers an activity provider
33 33 def register(event_type, options={})
34 34 options.assert_valid_keys(:class_name, :default)
35 35
36 36 event_type = event_type.to_s
37 37 providers = options[:class_name] || event_type.classify
38 38 providers = ([] << providers) unless providers.is_a?(Array)
39 39
40 40 @@available_event_types << event_type unless @@available_event_types.include?(event_type)
41 41 @@default_event_types << event_type unless options[:default] == false
42 42 @@providers[event_type] += providers
43 43 end
44
45 def delete(event_type)
46 @@available_event_types.delete event_type
47 @@default_event_types.delete event_type
48 @@providers.delete(event_type)
49 end
44 50 end
45 51 end
46 52 end
@@ -1,95 +1,110
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 module Redmine
19 19 module Activity
20 20 # Class used to retrieve activity events
21 21 class Fetcher
22 22 attr_reader :user, :project, :scope
23 23
24 # Needs to be unloaded in development mode
25 @@constantized_providers = Hash.new {|h,k| h[k] = Redmine::Activity.providers[k].collect {|t| t.constantize } }
26
27 24 def initialize(user, options={})
28 25 options.assert_valid_keys(:project, :with_subprojects, :author)
29 26 @user = user
30 27 @project = options[:project]
31 28 @options = options
32 29
33 30 @scope = event_types
34 31 end
35 32
36 33 # Returns an array of available event types
37 34 def event_types
38 35 return @event_types unless @event_types.nil?
39 36
40 37 @event_types = Redmine::Activity.available_event_types
41 @event_types = @event_types.select {|o| @project.self_and_descendants.detect {|p| @user.allowed_to?("view_#{o}".to_sym, p)}} if @project
38 if @project
39 projects = @project.self_and_descendants
40 @event_types = @event_types.select do |event_type|
41 keep = false
42 constantized_providers(event_type).each do |provider|
43 options = provider.activity_provider_options[event_type]
44 permission = options[:permission]
45 unless options.key?(:permission)
46 permission ||= "view_#{event_type}".to_sym
47 end
48 if permission
49 keep |= projects.any? {|p| @user.allowed_to?(permission, p)}
50 else
51 keep = true
52 end
53 end
54 keep
55 end
56 end
42 57 @event_types
43 58 end
44 59
45 60 # Yields to filter the activity scope
46 61 def scope_select(&block)
47 62 @scope = @scope.select {|t| yield t }
48 63 end
49 64
50 65 # Sets the scope
51 66 # Argument can be :all, :default or an array of event types
52 67 def scope=(s)
53 68 case s
54 69 when :all
55 70 @scope = event_types
56 71 when :default
57 72 default_scope!
58 73 else
59 74 @scope = s & event_types
60 75 end
61 76 end
62 77
63 78 # Resets the scope to the default scope
64 79 def default_scope!
65 80 @scope = Redmine::Activity.default_event_types
66 81 end
67 82
68 83 # Returns an array of events for the given date range
69 84 # sorted in reverse chronological order
70 85 def events(from = nil, to = nil, options={})
71 86 e = []
72 87 @options[:limit] = options[:limit]
73 88
74 89 @scope.each do |event_type|
75 90 constantized_providers(event_type).each do |provider|
76 91 e += provider.find_events(event_type, @user, from, to, @options)
77 92 end
78 93 end
79 94
80 95 e.sort! {|a,b| b.event_datetime <=> a.event_datetime}
81 96
82 97 if options[:limit]
83 98 e = e.slice(0, options[:limit])
84 99 end
85 100 e
86 101 end
87 102
88 103 private
89 104
90 105 def constantized_providers(event_type)
91 @@constantized_providers[event_type]
106 Redmine::Activity.providers[event_type].map(&:constantize)
92 107 end
93 108 end
94 109 end
95 110 end
@@ -1,129 +1,189
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class ActivityTest < ActiveSupport::TestCase
21 21 fixtures :projects, :versions, :attachments, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
22 22 :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages, :time_entries,
23 23 :wikis, :wiki_pages, :wiki_contents, :wiki_content_versions
24 24
25 25 def setup
26 26 @project = Project.find(1)
27 27 end
28 28
29 29 def test_activity_without_subprojects
30 30 events = find_events(User.anonymous, :project => @project)
31 31 assert_not_nil events
32 32
33 33 assert events.include?(Issue.find(1))
34 34 assert !events.include?(Issue.find(4))
35 35 # subproject issue
36 36 assert !events.include?(Issue.find(5))
37 37 end
38 38
39 39 def test_activity_with_subprojects
40 40 events = find_events(User.anonymous, :project => @project, :with_subprojects => 1)
41 41 assert_not_nil events
42 42
43 43 assert events.include?(Issue.find(1))
44 44 # subproject issue
45 45 assert events.include?(Issue.find(5))
46 46 end
47 47
48 48 def test_global_activity_anonymous
49 49 events = find_events(User.anonymous)
50 50 assert_not_nil events
51 51
52 52 assert events.include?(Issue.find(1))
53 53 assert events.include?(Message.find(5))
54 54 # Issue of a private project
55 55 assert !events.include?(Issue.find(4))
56 56 # Private issue and comment
57 57 assert !events.include?(Issue.find(14))
58 58 assert !events.include?(Journal.find(5))
59 59 end
60 60
61 61 def test_global_activity_logged_user
62 62 events = find_events(User.find(2)) # manager
63 63 assert_not_nil events
64 64
65 65 assert events.include?(Issue.find(1))
66 66 # Issue of a private project the user belongs to
67 67 assert events.include?(Issue.find(4))
68 68 end
69 69
70 70 def test_user_activity
71 71 user = User.find(2)
72 72 events = Redmine::Activity::Fetcher.new(User.anonymous, :author => user).events(nil, nil, :limit => 10)
73 73
74 74 assert(events.size > 0)
75 75 assert(events.size <= 10)
76 76 assert_nil(events.detect {|e| e.event_author != user})
77 77 end
78 78
79 79 def test_files_activity
80 80 f = Redmine::Activity::Fetcher.new(User.anonymous, :project => Project.find(1))
81 81 f.scope = ['files']
82 82 events = f.events
83 83
84 84 assert_kind_of Array, events
85 85 assert events.include?(Attachment.find_by_container_type_and_container_id('Project', 1))
86 86 assert events.include?(Attachment.find_by_container_type_and_container_id('Version', 1))
87 87 assert_equal [Attachment], events.collect(&:class).uniq
88 88 assert_equal %w(Project Version), events.collect(&:container_type).uniq.sort
89 89 end
90 90
91 91 def test_event_group_for_issue
92 92 issue = Issue.find(1)
93 93 assert_equal issue, issue.event_group
94 94 end
95 95
96 96 def test_event_group_for_journal
97 97 issue = Issue.find(1)
98 98 journal = issue.journals.first
99 99 assert_equal issue, journal.event_group
100 100 end
101 101
102 102 def test_event_group_for_issue_time_entry
103 103 time = TimeEntry.where(:issue_id => 1).first
104 104 assert_equal time.issue, time.event_group
105 105 end
106 106
107 107 def test_event_group_for_project_time_entry
108 108 time = TimeEntry.where(:issue_id => nil).first
109 109 assert_equal time, time.event_group
110 110 end
111 111
112 112 def test_event_group_for_message
113 113 message = Message.find(1)
114 114 reply = message.children.first
115 115 assert_equal message, message.event_group
116 116 assert_equal message, reply.event_group
117 117 end
118 118
119 119 def test_event_group_for_wiki_content_version
120 120 content = WikiContent::Version.find(1)
121 121 assert_equal content.page, content.event_group
122 122 end
123 123
124 class TestActivityProviderWithPermission
125 def self.activity_provider_options
126 {'test' => {:permission => :custom_permission}}
127 end
128 end
129
130 class TestActivityProviderWithNilPermission
131 def self.activity_provider_options
132 {'test' => {:permission => nil}}
133 end
134 end
135
136 class TestActivityProviderWithoutPermission
137 def self.activity_provider_options
138 {'test' => {}}
139 end
140 end
141
142 class MockUser
143 def initialize(*permissions)
144 @permissions = permissions
145 end
146
147 def allowed_to?(permission, *args)
148 @permissions.include?(permission)
149 end
150 end
151
152 def test_event_types_should_consider_activity_provider_permission
153 Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithPermission'
154 user = MockUser.new(:custom_permission)
155 f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1))
156 assert_include 'test', f.event_types
157 ensure
158 Redmine::Activity.delete 'test'
159 end
160
161 def test_event_types_should_include_activity_provider_with_nil_permission
162 Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithNilPermission'
163 user = MockUser.new()
164 f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1))
165 assert_include 'test', f.event_types
166 ensure
167 Redmine::Activity.delete 'test'
168 end
169
170 def test_event_types_should_use_default_permission_for_activity_provider_without_permission
171 Redmine::Activity.register 'test', :class_name => 'ActivityTest::TestActivityProviderWithoutPermission'
172
173 user = MockUser.new()
174 f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1))
175 assert_not_include 'test', f.event_types
176
177 user = MockUser.new(:view_test)
178 f = Redmine::Activity::Fetcher.new(user, :project => Project.find(1))
179 assert_include 'test', f.event_types
180 ensure
181 Redmine::Activity.delete 'test'
182 end
183
124 184 private
125 185
126 186 def find_events(user, options={})
127 187 Redmine::Activity::Fetcher.new(user, options).events(Date.today - 30, Date.today + 1)
128 188 end
129 189 end
General Comments 0
You need to be logged in to leave comments. Login now