##// END OF EJS Templates
Fixed that relations to issues that are not visible are displayed in the issue history (#1005)....
Jean-Philippe Lang -
r11784:7509dda1ff58
parent child
Show More
@@ -1,152 +1,155
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Journal < ActiveRecord::Base
18 class Journal < ActiveRecord::Base
19 belongs_to :journalized, :polymorphic => true
19 belongs_to :journalized, :polymorphic => true
20 # added as a quick fix to allow eager loading of the polymorphic association
20 # added as a quick fix to allow eager loading of the polymorphic association
21 # since always associated to an issue, for now
21 # since always associated to an issue, for now
22 belongs_to :issue, :foreign_key => :journalized_id
22 belongs_to :issue, :foreign_key => :journalized_id
23
23
24 belongs_to :user
24 belongs_to :user
25 has_many :details, :class_name => "JournalDetail", :dependent => :delete_all
25 has_many :details, :class_name => "JournalDetail", :dependent => :delete_all
26 attr_accessor :indice
26 attr_accessor :indice
27
27
28 acts_as_event :title => Proc.new {|o| status = ((s = o.new_status) ? " (#{s})" : nil); "#{o.issue.tracker} ##{o.issue.id}#{status}: #{o.issue.subject}" },
28 acts_as_event :title => Proc.new {|o| status = ((s = o.new_status) ? " (#{s})" : nil); "#{o.issue.tracker} ##{o.issue.id}#{status}: #{o.issue.subject}" },
29 :description => :notes,
29 :description => :notes,
30 :author => :user,
30 :author => :user,
31 :group => :issue,
31 :group => :issue,
32 :type => Proc.new {|o| (s = o.new_status) ? (s.is_closed? ? 'issue-closed' : 'issue-edit') : 'issue-note' },
32 :type => Proc.new {|o| (s = o.new_status) ? (s.is_closed? ? 'issue-closed' : 'issue-edit') : 'issue-note' },
33 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.issue.id, :anchor => "change-#{o.id}"}}
33 :url => Proc.new {|o| {:controller => 'issues', :action => 'show', :id => o.issue.id, :anchor => "change-#{o.id}"}}
34
34
35 acts_as_activity_provider :type => 'issues',
35 acts_as_activity_provider :type => 'issues',
36 :author_key => :user_id,
36 :author_key => :user_id,
37 :find_options => {:include => [{:issue => :project}, :details, :user],
37 :find_options => {:include => [{:issue => :project}, :details, :user],
38 :conditions => "#{Journal.table_name}.journalized_type = 'Issue' AND" +
38 :conditions => "#{Journal.table_name}.journalized_type = 'Issue' AND" +
39 " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')"}
39 " (#{JournalDetail.table_name}.prop_key = 'status_id' OR #{Journal.table_name}.notes <> '')"}
40
40
41 before_create :split_private_notes
41 before_create :split_private_notes
42
42
43 scope :visible, lambda {|*args|
43 scope :visible, lambda {|*args|
44 user = args.shift || User.current
44 user = args.shift || User.current
45
45
46 includes(:issue => :project).
46 includes(:issue => :project).
47 where(Issue.visible_condition(user, *args)).
47 where(Issue.visible_condition(user, *args)).
48 where("(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(user, :view_private_notes, *args)}))", false)
48 where("(#{Journal.table_name}.private_notes = ? OR (#{Project.allowed_to_condition(user, :view_private_notes, *args)}))", false)
49 }
49 }
50
50
51 def save(*args)
51 def save(*args)
52 # Do not save an empty journal
52 # Do not save an empty journal
53 (details.empty? && notes.blank?) ? false : super
53 (details.empty? && notes.blank?) ? false : super
54 end
54 end
55
55
56 # Returns journal details that are visible to user
56 def visible_details(user=User.current)
57 def visible_details(user=User.current)
57 details.select do |detail|
58 details.select do |detail|
58 if detail.property == 'cf'
59 if detail.property == 'cf'
59 field_id = detail.prop_key
60 field_id = detail.prop_key
60 field = CustomField.find_by_id(field_id)
61 field = CustomField.find_by_id(field_id)
61 field && field.visible_by?(project, user)
62 field && field.visible_by?(project, user)
63 elsif detail.property == 'relation'
64 Issue.find_by_id(detail.value || detail.old_value).try(:visible?, user)
62 else
65 else
63 true
66 true
64 end
67 end
65 end
68 end
66 end
69 end
67
70
68 # Returns the new status if the journal contains a status change, otherwise nil
71 # Returns the new status if the journal contains a status change, otherwise nil
69 def new_status
72 def new_status
70 c = details.detect {|detail| detail.prop_key == 'status_id'}
73 c = details.detect {|detail| detail.prop_key == 'status_id'}
71 (c && c.value) ? IssueStatus.find_by_id(c.value.to_i) : nil
74 (c && c.value) ? IssueStatus.find_by_id(c.value.to_i) : nil
72 end
75 end
73
76
74 def new_value_for(prop)
77 def new_value_for(prop)
75 c = details.detect {|detail| detail.prop_key == prop}
78 c = details.detect {|detail| detail.prop_key == prop}
76 c ? c.value : nil
79 c ? c.value : nil
77 end
80 end
78
81
79 def editable_by?(usr)
82 def editable_by?(usr)
80 usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project)))
83 usr && usr.logged? && (usr.allowed_to?(:edit_issue_notes, project) || (self.user == usr && usr.allowed_to?(:edit_own_issue_notes, project)))
81 end
84 end
82
85
83 def project
86 def project
84 journalized.respond_to?(:project) ? journalized.project : nil
87 journalized.respond_to?(:project) ? journalized.project : nil
85 end
88 end
86
89
87 def attachments
90 def attachments
88 journalized.respond_to?(:attachments) ? journalized.attachments : nil
91 journalized.respond_to?(:attachments) ? journalized.attachments : nil
89 end
92 end
90
93
91 # Returns a string of css classes
94 # Returns a string of css classes
92 def css_classes
95 def css_classes
93 s = 'journal'
96 s = 'journal'
94 s << ' has-notes' unless notes.blank?
97 s << ' has-notes' unless notes.blank?
95 s << ' has-details' unless details.blank?
98 s << ' has-details' unless details.blank?
96 s << ' private-notes' if private_notes?
99 s << ' private-notes' if private_notes?
97 s
100 s
98 end
101 end
99
102
100 def notify?
103 def notify?
101 @notify != false
104 @notify != false
102 end
105 end
103
106
104 def notify=(arg)
107 def notify=(arg)
105 @notify = arg
108 @notify = arg
106 end
109 end
107
110
108 def notified_users
111 def notified_users
109 notified = journalized.notified_users
112 notified = journalized.notified_users
110 if private_notes?
113 if private_notes?
111 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
114 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
112 end
115 end
113 notified
116 notified
114 end
117 end
115
118
116 def recipients
119 def recipients
117 notified_users.map(&:mail)
120 notified_users.map(&:mail)
118 end
121 end
119
122
120 def notified_watchers
123 def notified_watchers
121 notified = journalized.notified_watchers
124 notified = journalized.notified_watchers
122 if private_notes?
125 if private_notes?
123 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
126 notified = notified.select {|user| user.allowed_to?(:view_private_notes, journalized.project)}
124 end
127 end
125 notified
128 notified
126 end
129 end
127
130
128 def watcher_recipients
131 def watcher_recipients
129 notified_watchers.map(&:mail)
132 notified_watchers.map(&:mail)
130 end
133 end
131
134
132 private
135 private
133
136
134 def split_private_notes
137 def split_private_notes
135 if private_notes?
138 if private_notes?
136 if notes.present?
139 if notes.present?
137 if details.any?
140 if details.any?
138 # Split the journal (notes/changes) so we don't have half-private journals
141 # Split the journal (notes/changes) so we don't have half-private journals
139 journal = Journal.new(:journalized => journalized, :user => user, :notes => nil, :private_notes => false)
142 journal = Journal.new(:journalized => journalized, :user => user, :notes => nil, :private_notes => false)
140 journal.details = details
143 journal.details = details
141 journal.save
144 journal.save
142 self.details = []
145 self.details = []
143 self.created_on = journal.created_on
146 self.created_on = journal.created_on
144 end
147 end
145 else
148 else
146 # Blank notes should not be private
149 # Blank notes should not be private
147 self.private_notes = false
150 self.private_notes = false
148 end
151 end
149 end
152 end
150 true
153 true
151 end
154 end
152 end
155 end
@@ -1,178 +1,197
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 require File.expand_path('../../test_helper', __FILE__)
18 require File.expand_path('../../test_helper', __FILE__)
19
19
20 class JournalTest < ActiveSupport::TestCase
20 class JournalTest < ActiveSupport::TestCase
21 fixtures :projects, :issues, :issue_statuses, :journals, :journal_details,
21 fixtures :projects, :issues, :issue_statuses, :journals, :journal_details,
22 :users, :members, :member_roles, :roles, :enabled_modules,
22 :users, :members, :member_roles, :roles, :enabled_modules,
23 :projects_trackers, :trackers
23 :projects_trackers, :trackers
24
24
25 def setup
25 def setup
26 @journal = Journal.find 1
26 @journal = Journal.find 1
27 end
27 end
28
28
29 def test_journalized_is_an_issue
29 def test_journalized_is_an_issue
30 issue = @journal.issue
30 issue = @journal.issue
31 assert_kind_of Issue, issue
31 assert_kind_of Issue, issue
32 assert_equal 1, issue.id
32 assert_equal 1, issue.id
33 end
33 end
34
34
35 def test_new_status
35 def test_new_status
36 status = @journal.new_status
36 status = @journal.new_status
37 assert_not_nil status
37 assert_not_nil status
38 assert_kind_of IssueStatus, status
38 assert_kind_of IssueStatus, status
39 assert_equal 2, status.id
39 assert_equal 2, status.id
40 end
40 end
41
41
42 def test_create_should_send_email_notification
42 def test_create_should_send_email_notification
43 ActionMailer::Base.deliveries.clear
43 ActionMailer::Base.deliveries.clear
44 issue = Issue.first
44 issue = Issue.first
45 user = User.first
45 user = User.first
46 journal = issue.init_journal(user, issue)
46 journal = issue.init_journal(user, issue)
47
47
48 assert journal.save
48 assert journal.save
49 assert_equal 1, ActionMailer::Base.deliveries.size
49 assert_equal 1, ActionMailer::Base.deliveries.size
50 end
50 end
51
51
52 def test_should_not_save_journal_with_blank_notes_and_no_details
52 def test_should_not_save_journal_with_blank_notes_and_no_details
53 journal = Journal.new(:journalized => Issue.first, :user => User.first)
53 journal = Journal.new(:journalized => Issue.first, :user => User.first)
54
54
55 assert_no_difference 'Journal.count' do
55 assert_no_difference 'Journal.count' do
56 assert_equal false, journal.save
56 assert_equal false, journal.save
57 end
57 end
58 end
58 end
59
59
60 def test_create_should_not_split_non_private_notes
60 def test_create_should_not_split_non_private_notes
61 assert_difference 'Journal.count' do
61 assert_difference 'Journal.count' do
62 assert_no_difference 'JournalDetail.count' do
62 assert_no_difference 'JournalDetail.count' do
63 journal = Journal.generate!(:notes => 'Notes')
63 journal = Journal.generate!(:notes => 'Notes')
64 end
64 end
65 end
65 end
66
66
67 assert_difference 'Journal.count' do
67 assert_difference 'Journal.count' do
68 assert_difference 'JournalDetail.count' do
68 assert_difference 'JournalDetail.count' do
69 journal = Journal.generate!(:notes => 'Notes', :details => [JournalDetail.new])
69 journal = Journal.generate!(:notes => 'Notes', :details => [JournalDetail.new])
70 end
70 end
71 end
71 end
72
72
73 assert_difference 'Journal.count' do
73 assert_difference 'Journal.count' do
74 assert_difference 'JournalDetail.count' do
74 assert_difference 'JournalDetail.count' do
75 journal = Journal.generate!(:notes => '', :details => [JournalDetail.new])
75 journal = Journal.generate!(:notes => '', :details => [JournalDetail.new])
76 end
76 end
77 end
77 end
78 end
78 end
79
79
80 def test_create_should_split_private_notes
80 def test_create_should_split_private_notes
81 assert_difference 'Journal.count' do
81 assert_difference 'Journal.count' do
82 assert_no_difference 'JournalDetail.count' do
82 assert_no_difference 'JournalDetail.count' do
83 journal = Journal.generate!(:notes => 'Notes', :private_notes => true)
83 journal = Journal.generate!(:notes => 'Notes', :private_notes => true)
84 journal.reload
84 journal.reload
85 assert_equal true, journal.private_notes
85 assert_equal true, journal.private_notes
86 assert_equal 'Notes', journal.notes
86 assert_equal 'Notes', journal.notes
87 end
87 end
88 end
88 end
89
89
90 assert_difference 'Journal.count', 2 do
90 assert_difference 'Journal.count', 2 do
91 assert_difference 'JournalDetail.count' do
91 assert_difference 'JournalDetail.count' do
92 journal = Journal.generate!(:notes => 'Notes', :private_notes => true, :details => [JournalDetail.new])
92 journal = Journal.generate!(:notes => 'Notes', :private_notes => true, :details => [JournalDetail.new])
93 journal.reload
93 journal.reload
94 assert_equal true, journal.private_notes
94 assert_equal true, journal.private_notes
95 assert_equal 'Notes', journal.notes
95 assert_equal 'Notes', journal.notes
96 assert_equal 0, journal.details.size
96 assert_equal 0, journal.details.size
97
97
98 journal_with_changes = Journal.order('id DESC').offset(1).first
98 journal_with_changes = Journal.order('id DESC').offset(1).first
99 assert_equal false, journal_with_changes.private_notes
99 assert_equal false, journal_with_changes.private_notes
100 assert_nil journal_with_changes.notes
100 assert_nil journal_with_changes.notes
101 assert_equal 1, journal_with_changes.details.size
101 assert_equal 1, journal_with_changes.details.size
102 assert_equal journal.created_on, journal_with_changes.created_on
102 assert_equal journal.created_on, journal_with_changes.created_on
103 end
103 end
104 end
104 end
105
105
106 assert_difference 'Journal.count' do
106 assert_difference 'Journal.count' do
107 assert_difference 'JournalDetail.count' do
107 assert_difference 'JournalDetail.count' do
108 journal = Journal.generate!(:notes => '', :private_notes => true, :details => [JournalDetail.new])
108 journal = Journal.generate!(:notes => '', :private_notes => true, :details => [JournalDetail.new])
109 journal.reload
109 journal.reload
110 assert_equal false, journal.private_notes
110 assert_equal false, journal.private_notes
111 assert_equal '', journal.notes
111 assert_equal '', journal.notes
112 assert_equal 1, journal.details.size
112 assert_equal 1, journal.details.size
113 end
113 end
114 end
114 end
115 end
115 end
116
116
117 def test_visible_scope_for_anonymous
117 def test_visible_scope_for_anonymous
118 # Anonymous user should see issues of public projects only
118 # Anonymous user should see issues of public projects only
119 journals = Journal.visible(User.anonymous).all
119 journals = Journal.visible(User.anonymous).all
120 assert journals.any?
120 assert journals.any?
121 assert_nil journals.detect {|journal| !journal.issue.project.is_public?}
121 assert_nil journals.detect {|journal| !journal.issue.project.is_public?}
122 # Anonymous user should not see issues without permission
122 # Anonymous user should not see issues without permission
123 Role.anonymous.remove_permission!(:view_issues)
123 Role.anonymous.remove_permission!(:view_issues)
124 journals = Journal.visible(User.anonymous).all
124 journals = Journal.visible(User.anonymous).all
125 assert journals.empty?
125 assert journals.empty?
126 end
126 end
127
127
128 def test_visible_scope_for_user
128 def test_visible_scope_for_user
129 user = User.find(9)
129 user = User.find(9)
130 assert user.projects.empty?
130 assert user.projects.empty?
131 # Non member user should see issues of public projects only
131 # Non member user should see issues of public projects only
132 journals = Journal.visible(user).all
132 journals = Journal.visible(user).all
133 assert journals.any?
133 assert journals.any?
134 assert_nil journals.detect {|journal| !journal.issue.project.is_public?}
134 assert_nil journals.detect {|journal| !journal.issue.project.is_public?}
135 # Non member user should not see issues without permission
135 # Non member user should not see issues without permission
136 Role.non_member.remove_permission!(:view_issues)
136 Role.non_member.remove_permission!(:view_issues)
137 user.reload
137 user.reload
138 journals = Journal.visible(user).all
138 journals = Journal.visible(user).all
139 assert journals.empty?
139 assert journals.empty?
140 # User should see issues of projects for which user has view_issues permissions only
140 # User should see issues of projects for which user has view_issues permissions only
141 Member.create!(:principal => user, :project_id => 1, :role_ids => [1])
141 Member.create!(:principal => user, :project_id => 1, :role_ids => [1])
142 user.reload
142 user.reload
143 journals = Journal.visible(user).all
143 journals = Journal.visible(user).all
144 assert journals.any?
144 assert journals.any?
145 assert_nil journals.detect {|journal| journal.issue.project_id != 1}
145 assert_nil journals.detect {|journal| journal.issue.project_id != 1}
146 end
146 end
147
147
148 def test_visible_scope_for_admin
148 def test_visible_scope_for_admin
149 user = User.find(1)
149 user = User.find(1)
150 user.members.each(&:destroy)
150 user.members.each(&:destroy)
151 assert user.projects.empty?
151 assert user.projects.empty?
152 journals = Journal.visible(user).all
152 journals = Journal.visible(user).all
153 assert journals.any?
153 assert journals.any?
154 # Admin should see issues on private projects that admin does not belong to
154 # Admin should see issues on private projects that admin does not belong to
155 assert journals.detect {|journal| !journal.issue.project.is_public?}
155 assert journals.detect {|journal| !journal.issue.project.is_public?}
156 end
156 end
157
157
158 def test_details_should_normalize_dates
158 def test_details_should_normalize_dates
159 j = JournalDetail.create!(:old_value => Date.parse('2012-11-03'), :value => Date.parse('2013-01-02'))
159 j = JournalDetail.create!(:old_value => Date.parse('2012-11-03'), :value => Date.parse('2013-01-02'))
160 j.reload
160 j.reload
161 assert_equal '2012-11-03', j.old_value
161 assert_equal '2012-11-03', j.old_value
162 assert_equal '2013-01-02', j.value
162 assert_equal '2013-01-02', j.value
163 end
163 end
164
164
165 def test_details_should_normalize_true_values
165 def test_details_should_normalize_true_values
166 j = JournalDetail.create!(:old_value => true, :value => true)
166 j = JournalDetail.create!(:old_value => true, :value => true)
167 j.reload
167 j.reload
168 assert_equal '1', j.old_value
168 assert_equal '1', j.old_value
169 assert_equal '1', j.value
169 assert_equal '1', j.value
170 end
170 end
171
171
172 def test_details_should_normalize_false_values
172 def test_details_should_normalize_false_values
173 j = JournalDetail.create!(:old_value => false, :value => false)
173 j = JournalDetail.create!(:old_value => false, :value => false)
174 j.reload
174 j.reload
175 assert_equal '0', j.old_value
175 assert_equal '0', j.old_value
176 assert_equal '0', j.value
176 assert_equal '0', j.value
177 end
177 end
178
179 def test_visible_details_should_include_relations_to_visible_issues_only
180 issue = Issue.generate!
181 visible_issue = Issue.generate!
182 IssueRelation.create!(:issue_from => issue, :issue_to => visible_issue, :relation_type => 'relates')
183 hidden_issue = Issue.generate!(:is_private => true)
184 IssueRelation.create!(:issue_from => issue, :issue_to => hidden_issue, :relation_type => 'relates')
185 issue.reload
186 assert_equal 1, issue.journals.size
187 journal = issue.journals.first
188 assert_equal 2, journal.details.size
189
190 visible_details = journal.visible_details(User.anonymous)
191 assert_equal 1, visible_details.size
192 assert_equal visible_issue.id.to_s, visible_details.first.value
193
194 visible_details = journal.visible_details(User.find(2))
195 assert_equal 2, visible_details.size
196 end
178 end
197 end
General Comments 0
You need to be logged in to leave comments. Login now