##// END OF EJS Templates
Only show issue details in time entry activity events if the issue is visible (#23803)....
Jean-Philippe Lang -
r15902:868f83fe357e
parent child
Show More
@@ -1,162 +1,166
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2016 Jean-Philippe Lang
2 # Copyright (C) 2006-2016 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 TimeEntry < ActiveRecord::Base
18 class TimeEntry < ActiveRecord::Base
19 include Redmine::SafeAttributes
19 include Redmine::SafeAttributes
20 # could have used polymorphic association
20 # could have used polymorphic association
21 # project association here allows easy loading of time entries at project level with one database trip
21 # project association here allows easy loading of time entries at project level with one database trip
22 belongs_to :project
22 belongs_to :project
23 belongs_to :issue
23 belongs_to :issue
24 belongs_to :user
24 belongs_to :user
25 belongs_to :activity, :class_name => 'TimeEntryActivity'
25 belongs_to :activity, :class_name => 'TimeEntryActivity'
26
26
27 attr_protected :user_id, :tyear, :tmonth, :tweek
27 attr_protected :user_id, :tyear, :tmonth, :tweek
28
28
29 acts_as_customizable
29 acts_as_customizable
30 acts_as_event :title => Proc.new {|o| "#{l_hours(o.hours)} (#{(o.issue || o.project).event_title})"},
30 acts_as_event :title => Proc.new { |o|
31 related = o.issue if o.issue && o.issue.visible?
32 related ||= o.project
33 "#{l_hours(o.hours)} (#{related.event_title})"
34 },
31 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
35 :url => Proc.new {|o| {:controller => 'timelog', :action => 'index', :project_id => o.project, :issue_id => o.issue}},
32 :author => :user,
36 :author => :user,
33 :group => :issue,
37 :group => :issue,
34 :description => :comments
38 :description => :comments
35
39
36 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
40 acts_as_activity_provider :timestamp => "#{table_name}.created_on",
37 :author_key => :user_id,
41 :author_key => :user_id,
38 :scope => joins(:project).preload(:project)
42 :scope => joins(:project).preload(:project)
39
43
40 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
44 validates_presence_of :user_id, :activity_id, :project_id, :hours, :spent_on
41 validates_presence_of :issue_id, :if => lambda { Setting.timelog_required_fields.include?('issue_id') }
45 validates_presence_of :issue_id, :if => lambda { Setting.timelog_required_fields.include?('issue_id') }
42 validates_presence_of :comments, :if => lambda { Setting.timelog_required_fields.include?('comments') }
46 validates_presence_of :comments, :if => lambda { Setting.timelog_required_fields.include?('comments') }
43 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
47 validates_numericality_of :hours, :allow_nil => true, :message => :invalid
44 validates_length_of :comments, :maximum => 1024, :allow_nil => true
48 validates_length_of :comments, :maximum => 1024, :allow_nil => true
45 validates :spent_on, :date => true
49 validates :spent_on, :date => true
46 before_validation :set_project_if_nil
50 before_validation :set_project_if_nil
47 validate :validate_time_entry
51 validate :validate_time_entry
48
52
49 scope :visible, lambda {|*args|
53 scope :visible, lambda {|*args|
50 joins(:project).
54 joins(:project).
51 where(TimeEntry.visible_condition(args.shift || User.current, *args))
55 where(TimeEntry.visible_condition(args.shift || User.current, *args))
52 }
56 }
53 scope :on_issue, lambda {|issue|
57 scope :on_issue, lambda {|issue|
54 joins(:issue).
58 joins(:issue).
55 where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}")
59 where("#{Issue.table_name}.root_id = #{issue.root_id} AND #{Issue.table_name}.lft >= #{issue.lft} AND #{Issue.table_name}.rgt <= #{issue.rgt}")
56 }
60 }
57
61
58 safe_attributes 'hours', 'comments', 'project_id', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
62 safe_attributes 'hours', 'comments', 'project_id', 'issue_id', 'activity_id', 'spent_on', 'custom_field_values', 'custom_fields'
59
63
60 # Returns a SQL conditions string used to find all time entries visible by the specified user
64 # Returns a SQL conditions string used to find all time entries visible by the specified user
61 def self.visible_condition(user, options={})
65 def self.visible_condition(user, options={})
62 Project.allowed_to_condition(user, :view_time_entries, options) do |role, user|
66 Project.allowed_to_condition(user, :view_time_entries, options) do |role, user|
63 if role.time_entries_visibility == 'all'
67 if role.time_entries_visibility == 'all'
64 nil
68 nil
65 elsif role.time_entries_visibility == 'own' && user.id && user.logged?
69 elsif role.time_entries_visibility == 'own' && user.id && user.logged?
66 "#{table_name}.user_id = #{user.id}"
70 "#{table_name}.user_id = #{user.id}"
67 else
71 else
68 '1=0'
72 '1=0'
69 end
73 end
70 end
74 end
71 end
75 end
72
76
73 # Returns true if user or current user is allowed to view the time entry
77 # Returns true if user or current user is allowed to view the time entry
74 def visible?(user=nil)
78 def visible?(user=nil)
75 (user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user|
79 (user || User.current).allowed_to?(:view_time_entries, self.project) do |role, user|
76 if role.time_entries_visibility == 'all'
80 if role.time_entries_visibility == 'all'
77 true
81 true
78 elsif role.time_entries_visibility == 'own'
82 elsif role.time_entries_visibility == 'own'
79 self.user == user
83 self.user == user
80 else
84 else
81 false
85 false
82 end
86 end
83 end
87 end
84 end
88 end
85
89
86 def initialize(attributes=nil, *args)
90 def initialize(attributes=nil, *args)
87 super
91 super
88 if new_record? && self.activity.nil?
92 if new_record? && self.activity.nil?
89 if default_activity = TimeEntryActivity.default
93 if default_activity = TimeEntryActivity.default
90 self.activity_id = default_activity.id
94 self.activity_id = default_activity.id
91 end
95 end
92 self.hours = nil if hours == 0
96 self.hours = nil if hours == 0
93 end
97 end
94 end
98 end
95
99
96 def safe_attributes=(attrs, user=User.current)
100 def safe_attributes=(attrs, user=User.current)
97 if attrs
101 if attrs
98 attrs = super(attrs)
102 attrs = super(attrs)
99 if issue_id_changed? && issue
103 if issue_id_changed? && issue
100 if issue.visible?(user) && user.allowed_to?(:log_time, issue.project)
104 if issue.visible?(user) && user.allowed_to?(:log_time, issue.project)
101 if attrs[:project_id].blank? && issue.project_id != project_id
105 if attrs[:project_id].blank? && issue.project_id != project_id
102 self.project_id = issue.project_id
106 self.project_id = issue.project_id
103 end
107 end
104 @invalid_issue_id = nil
108 @invalid_issue_id = nil
105 else
109 else
106 @invalid_issue_id = issue_id
110 @invalid_issue_id = issue_id
107 end
111 end
108 end
112 end
109 end
113 end
110 attrs
114 attrs
111 end
115 end
112
116
113 def set_project_if_nil
117 def set_project_if_nil
114 self.project = issue.project if issue && project.nil?
118 self.project = issue.project if issue && project.nil?
115 end
119 end
116
120
117 def validate_time_entry
121 def validate_time_entry
118 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
122 errors.add :hours, :invalid if hours && (hours < 0 || hours >= 1000)
119 errors.add :project_id, :invalid if project.nil?
123 errors.add :project_id, :invalid if project.nil?
120 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id
124 errors.add :issue_id, :invalid if (issue_id && !issue) || (issue && project!=issue.project) || @invalid_issue_id
121 errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity)
125 errors.add :activity_id, :inclusion if activity_id_changed? && project && !project.activities.include?(activity)
122 end
126 end
123
127
124 def hours=(h)
128 def hours=(h)
125 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
129 write_attribute :hours, (h.is_a?(String) ? (h.to_hours || h) : h)
126 end
130 end
127
131
128 def hours
132 def hours
129 h = read_attribute(:hours)
133 h = read_attribute(:hours)
130 if h.is_a?(Float)
134 if h.is_a?(Float)
131 h.round(2)
135 h.round(2)
132 else
136 else
133 h
137 h
134 end
138 end
135 end
139 end
136
140
137 # tyear, tmonth, tweek assigned where setting spent_on attributes
141 # tyear, tmonth, tweek assigned where setting spent_on attributes
138 # these attributes make time aggregations easier
142 # these attributes make time aggregations easier
139 def spent_on=(date)
143 def spent_on=(date)
140 super
144 super
141 self.tyear = spent_on ? spent_on.year : nil
145 self.tyear = spent_on ? spent_on.year : nil
142 self.tmonth = spent_on ? spent_on.month : nil
146 self.tmonth = spent_on ? spent_on.month : nil
143 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
147 self.tweek = spent_on ? Date.civil(spent_on.year, spent_on.month, spent_on.day).cweek : nil
144 end
148 end
145
149
146 # Returns true if the time entry can be edited by usr, otherwise false
150 # Returns true if the time entry can be edited by usr, otherwise false
147 def editable_by?(usr)
151 def editable_by?(usr)
148 visible?(usr) && (
152 visible?(usr) && (
149 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
153 (usr == user && usr.allowed_to?(:edit_own_time_entries, project)) || usr.allowed_to?(:edit_time_entries, project)
150 )
154 )
151 end
155 end
152
156
153 # Returns the custom_field_values that can be edited by the given user
157 # Returns the custom_field_values that can be edited by the given user
154 def editable_custom_field_values(user=nil)
158 def editable_custom_field_values(user=nil)
155 visible_custom_field_values
159 visible_custom_field_values
156 end
160 end
157
161
158 # Returns the custom fields that can be edited by the given user
162 # Returns the custom fields that can be edited by the given user
159 def editable_custom_fields(user=nil)
163 def editable_custom_fields(user=nil)
160 editable_custom_field_values(user).map(&:custom_field).uniq
164 editable_custom_field_values(user).map(&:custom_field).uniq
161 end
165 end
162 end
166 end
General Comments 0
You need to be logged in to leave comments. Login now