@@ -41,6 +41,12 module Redmine | |||
|
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 |
@@ -21,9 +21,6 module Redmine | |||
|
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 |
@@ -38,7 +35,25 module Redmine | |||
|
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 | |
@@ -88,7 +103,7 module Redmine | |||
|
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 |
@@ -121,6 +121,66 class ActivityTest < ActiveSupport::TestCase | |||
|
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={}) |
General Comments 0
You need to be logged in to leave comments.
Login now