##// END OF EJS Templates
Show action not allowed for time entries in closed projects (#24297)....
Jean-Philippe Lang -
r15573:e9184adafc2c
parent child
Show More
@@ -1,282 +1,286
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 TimelogController < ApplicationController
18 class TimelogController < ApplicationController
19 menu_item :time_entries
19 menu_item :time_entries
20
20
21 before_action :find_time_entry, :only => [:show, :edit, :update]
21 before_action :find_time_entry, :only => [:show, :edit, :update]
22 before_action :check_editability, :only => [:edit, :update]
22 before_action :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy]
23 before_action :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy]
23 before_action :authorize, :only => [:show, :edit, :update, :bulk_edit, :bulk_update, :destroy]
24 before_action :authorize, :only => [:show, :edit, :update, :bulk_edit, :bulk_update, :destroy]
24
25
25 before_action :find_optional_issue, :only => [:new, :create]
26 before_action :find_optional_issue, :only => [:new, :create]
26 before_action :find_optional_project, :only => [:index, :report]
27 before_action :find_optional_project, :only => [:index, :report]
27 before_action :authorize_global, :only => [:new, :create, :index, :report]
28 before_action :authorize_global, :only => [:new, :create, :index, :report]
28
29
29 accept_rss_auth :index
30 accept_rss_auth :index
30 accept_api_auth :index, :show, :create, :update, :destroy
31 accept_api_auth :index, :show, :create, :update, :destroy
31
32
32 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
33
34
34 helper :sort
35 helper :sort
35 include SortHelper
36 include SortHelper
36 helper :issues
37 helper :issues
37 include TimelogHelper
38 include TimelogHelper
38 helper :custom_fields
39 helper :custom_fields
39 include CustomFieldsHelper
40 include CustomFieldsHelper
40 helper :queries
41 helper :queries
41 include QueriesHelper
42 include QueriesHelper
42
43
43 def index
44 def index
44 retrieve_time_entry_query
45 retrieve_time_entry_query
45 sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria)
46 sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria)
46 sort_update(@query.sortable_columns)
47 sort_update(@query.sortable_columns)
47 scope = time_entry_scope(:order => sort_clause).
48 scope = time_entry_scope(:order => sort_clause).
48 preload(:issue => [:project, :tracker, :status, :assigned_to, :priority]).
49 preload(:issue => [:project, :tracker, :status, :assigned_to, :priority]).
49 preload(:project, :user)
50 preload(:project, :user)
50
51
51 respond_to do |format|
52 respond_to do |format|
52 format.html {
53 format.html {
53 @entry_count = scope.count
54 @entry_count = scope.count
54 @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
55 @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
55 @entries = scope.offset(@entry_pages.offset).limit(@entry_pages.per_page).to_a
56 @entries = scope.offset(@entry_pages.offset).limit(@entry_pages.per_page).to_a
56
57
57 render :layout => !request.xhr?
58 render :layout => !request.xhr?
58 }
59 }
59 format.api {
60 format.api {
60 @entry_count = scope.count
61 @entry_count = scope.count
61 @offset, @limit = api_offset_and_limit
62 @offset, @limit = api_offset_and_limit
62 @entries = scope.offset(@offset).limit(@limit).preload(:custom_values => :custom_field).to_a
63 @entries = scope.offset(@offset).limit(@limit).preload(:custom_values => :custom_field).to_a
63 }
64 }
64 format.atom {
65 format.atom {
65 entries = scope.limit(Setting.feeds_limit.to_i).reorder("#{TimeEntry.table_name}.created_on DESC").to_a
66 entries = scope.limit(Setting.feeds_limit.to_i).reorder("#{TimeEntry.table_name}.created_on DESC").to_a
66 render_feed(entries, :title => l(:label_spent_time))
67 render_feed(entries, :title => l(:label_spent_time))
67 }
68 }
68 format.csv {
69 format.csv {
69 # Export all entries
70 # Export all entries
70 @entries = scope.to_a
71 @entries = scope.to_a
71 send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
72 send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
72 }
73 }
73 end
74 end
74 end
75 end
75
76
76 def report
77 def report
77 retrieve_time_entry_query
78 retrieve_time_entry_query
78 scope = time_entry_scope
79 scope = time_entry_scope
79
80
80 @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope)
81 @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope)
81
82
82 respond_to do |format|
83 respond_to do |format|
83 format.html { render :layout => !request.xhr? }
84 format.html { render :layout => !request.xhr? }
84 format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
85 format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
85 end
86 end
86 end
87 end
87
88
88 def show
89 def show
89 respond_to do |format|
90 respond_to do |format|
90 # TODO: Implement html response
91 # TODO: Implement html response
91 format.html { head 406 }
92 format.html { head 406 }
92 format.api
93 format.api
93 end
94 end
94 end
95 end
95
96
96 def new
97 def new
97 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
98 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
98 @time_entry.safe_attributes = params[:time_entry]
99 @time_entry.safe_attributes = params[:time_entry]
99 end
100 end
100
101
101 def create
102 def create
102 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
103 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
103 @time_entry.safe_attributes = params[:time_entry]
104 @time_entry.safe_attributes = params[:time_entry]
104 if @time_entry.project && !User.current.allowed_to?(:log_time, @time_entry.project)
105 if @time_entry.project && !User.current.allowed_to?(:log_time, @time_entry.project)
105 render_403
106 render_403
106 return
107 return
107 end
108 end
108
109
109 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
110 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
110
111
111 if @time_entry.save
112 if @time_entry.save
112 respond_to do |format|
113 respond_to do |format|
113 format.html {
114 format.html {
114 flash[:notice] = l(:notice_successful_create)
115 flash[:notice] = l(:notice_successful_create)
115 if params[:continue]
116 if params[:continue]
116 options = {
117 options = {
117 :time_entry => {
118 :time_entry => {
118 :project_id => params[:time_entry][:project_id],
119 :project_id => params[:time_entry][:project_id],
119 :issue_id => @time_entry.issue_id,
120 :issue_id => @time_entry.issue_id,
120 :activity_id => @time_entry.activity_id
121 :activity_id => @time_entry.activity_id
121 },
122 },
122 :back_url => params[:back_url]
123 :back_url => params[:back_url]
123 }
124 }
124 if params[:project_id] && @time_entry.project
125 if params[:project_id] && @time_entry.project
125 redirect_to new_project_time_entry_path(@time_entry.project, options)
126 redirect_to new_project_time_entry_path(@time_entry.project, options)
126 elsif params[:issue_id] && @time_entry.issue
127 elsif params[:issue_id] && @time_entry.issue
127 redirect_to new_issue_time_entry_path(@time_entry.issue, options)
128 redirect_to new_issue_time_entry_path(@time_entry.issue, options)
128 else
129 else
129 redirect_to new_time_entry_path(options)
130 redirect_to new_time_entry_path(options)
130 end
131 end
131 else
132 else
132 redirect_back_or_default project_time_entries_path(@time_entry.project)
133 redirect_back_or_default project_time_entries_path(@time_entry.project)
133 end
134 end
134 }
135 }
135 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
136 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
136 end
137 end
137 else
138 else
138 respond_to do |format|
139 respond_to do |format|
139 format.html { render :action => 'new' }
140 format.html { render :action => 'new' }
140 format.api { render_validation_errors(@time_entry) }
141 format.api { render_validation_errors(@time_entry) }
141 end
142 end
142 end
143 end
143 end
144 end
144
145
145 def edit
146 def edit
146 @time_entry.safe_attributes = params[:time_entry]
147 @time_entry.safe_attributes = params[:time_entry]
147 end
148 end
148
149
149 def update
150 def update
150 @time_entry.safe_attributes = params[:time_entry]
151 @time_entry.safe_attributes = params[:time_entry]
151
152
152 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
153 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
153
154
154 if @time_entry.save
155 if @time_entry.save
155 respond_to do |format|
156 respond_to do |format|
156 format.html {
157 format.html {
157 flash[:notice] = l(:notice_successful_update)
158 flash[:notice] = l(:notice_successful_update)
158 redirect_back_or_default project_time_entries_path(@time_entry.project)
159 redirect_back_or_default project_time_entries_path(@time_entry.project)
159 }
160 }
160 format.api { render_api_ok }
161 format.api { render_api_ok }
161 end
162 end
162 else
163 else
163 respond_to do |format|
164 respond_to do |format|
164 format.html { render :action => 'edit' }
165 format.html { render :action => 'edit' }
165 format.api { render_validation_errors(@time_entry) }
166 format.api { render_validation_errors(@time_entry) }
166 end
167 end
167 end
168 end
168 end
169 end
169
170
170 def bulk_edit
171 def bulk_edit
171 @available_activities = @projects.map(&:activities).reduce(:&)
172 @available_activities = @projects.map(&:activities).reduce(:&)
172 @custom_fields = TimeEntry.first.available_custom_fields.select {|field| field.format.bulk_edit_supported}
173 @custom_fields = TimeEntry.first.available_custom_fields.select {|field| field.format.bulk_edit_supported}
173 end
174 end
174
175
175 def bulk_update
176 def bulk_update
176 attributes = parse_params_for_bulk_update(params[:time_entry])
177 attributes = parse_params_for_bulk_update(params[:time_entry])
177
178
178 unsaved_time_entry_ids = []
179 unsaved_time_entry_ids = []
179 @time_entries.each do |time_entry|
180 @time_entries.each do |time_entry|
180 time_entry.reload
181 time_entry.reload
181 time_entry.safe_attributes = attributes
182 time_entry.safe_attributes = attributes
182 call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
183 call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
183 unless time_entry.save
184 unless time_entry.save
184 logger.info "time entry could not be updated: #{time_entry.errors.full_messages}" if logger && logger.info?
185 logger.info "time entry could not be updated: #{time_entry.errors.full_messages}" if logger && logger.info?
185 # Keep unsaved time_entry ids to display them in flash error
186 # Keep unsaved time_entry ids to display them in flash error
186 unsaved_time_entry_ids << time_entry.id
187 unsaved_time_entry_ids << time_entry.id
187 end
188 end
188 end
189 end
189 set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
190 set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
190 redirect_back_or_default project_time_entries_path(@projects.first)
191 redirect_back_or_default project_time_entries_path(@projects.first)
191 end
192 end
192
193
193 def destroy
194 def destroy
194 destroyed = TimeEntry.transaction do
195 destroyed = TimeEntry.transaction do
195 @time_entries.each do |t|
196 @time_entries.each do |t|
196 unless t.destroy && t.destroyed?
197 unless t.destroy && t.destroyed?
197 raise ActiveRecord::Rollback
198 raise ActiveRecord::Rollback
198 end
199 end
199 end
200 end
200 end
201 end
201
202
202 respond_to do |format|
203 respond_to do |format|
203 format.html {
204 format.html {
204 if destroyed
205 if destroyed
205 flash[:notice] = l(:notice_successful_delete)
206 flash[:notice] = l(:notice_successful_delete)
206 else
207 else
207 flash[:error] = l(:notice_unable_delete_time_entry)
208 flash[:error] = l(:notice_unable_delete_time_entry)
208 end
209 end
209 redirect_back_or_default project_time_entries_path(@projects.first)
210 redirect_back_or_default project_time_entries_path(@projects.first)
210 }
211 }
211 format.api {
212 format.api {
212 if destroyed
213 if destroyed
213 render_api_ok
214 render_api_ok
214 else
215 else
215 render_validation_errors(@time_entries)
216 render_validation_errors(@time_entries)
216 end
217 end
217 }
218 }
218 end
219 end
219 end
220 end
220
221
221 private
222 private
222 def find_time_entry
223 def find_time_entry
223 @time_entry = TimeEntry.find(params[:id])
224 @time_entry = TimeEntry.find(params[:id])
225 @project = @time_entry.project
226 rescue ActiveRecord::RecordNotFound
227 render_404
228 end
229
230 def check_editability
224 unless @time_entry.editable_by?(User.current)
231 unless @time_entry.editable_by?(User.current)
225 render_403
232 render_403
226 return false
233 return false
227 end
234 end
228 @project = @time_entry.project
229 rescue ActiveRecord::RecordNotFound
230 render_404
231 end
235 end
232
236
233 def find_time_entries
237 def find_time_entries
234 @time_entries = TimeEntry.where(:id => params[:id] || params[:ids]).
238 @time_entries = TimeEntry.where(:id => params[:id] || params[:ids]).
235 preload(:project => :time_entry_activities).
239 preload(:project => :time_entry_activities).
236 preload(:user).to_a
240 preload(:user).to_a
237
241
238 raise ActiveRecord::RecordNotFound if @time_entries.empty?
242 raise ActiveRecord::RecordNotFound if @time_entries.empty?
239 raise Unauthorized unless @time_entries.all? {|t| t.editable_by?(User.current)}
243 raise Unauthorized unless @time_entries.all? {|t| t.editable_by?(User.current)}
240 @projects = @time_entries.collect(&:project).compact.uniq
244 @projects = @time_entries.collect(&:project).compact.uniq
241 @project = @projects.first if @projects.size == 1
245 @project = @projects.first if @projects.size == 1
242 rescue ActiveRecord::RecordNotFound
246 rescue ActiveRecord::RecordNotFound
243 render_404
247 render_404
244 end
248 end
245
249
246 def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
250 def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
247 if unsaved_time_entry_ids.empty?
251 if unsaved_time_entry_ids.empty?
248 flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
252 flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
249 else
253 else
250 flash[:error] = l(:notice_failed_to_save_time_entries,
254 flash[:error] = l(:notice_failed_to_save_time_entries,
251 :count => unsaved_time_entry_ids.size,
255 :count => unsaved_time_entry_ids.size,
252 :total => time_entries.size,
256 :total => time_entries.size,
253 :ids => '#' + unsaved_time_entry_ids.join(', #'))
257 :ids => '#' + unsaved_time_entry_ids.join(', #'))
254 end
258 end
255 end
259 end
256
260
257 def find_optional_issue
261 def find_optional_issue
258 if params[:issue_id].present?
262 if params[:issue_id].present?
259 @issue = Issue.find(params[:issue_id])
263 @issue = Issue.find(params[:issue_id])
260 @project = @issue.project
264 @project = @issue.project
261 else
265 else
262 find_optional_project
266 find_optional_project
263 end
267 end
264 end
268 end
265
269
266 def find_optional_project
270 def find_optional_project
267 if params[:project_id].present?
271 if params[:project_id].present?
268 @project = Project.find(params[:project_id])
272 @project = Project.find(params[:project_id])
269 end
273 end
270 rescue ActiveRecord::RecordNotFound
274 rescue ActiveRecord::RecordNotFound
271 render_404
275 render_404
272 end
276 end
273
277
274 # Returns the TimeEntry scope for index and report actions
278 # Returns the TimeEntry scope for index and report actions
275 def time_entry_scope(options={})
279 def time_entry_scope(options={})
276 @query.results_scope(options)
280 @query.results_scope(options)
277 end
281 end
278
282
279 def retrieve_time_entry_query
283 def retrieve_time_entry_query
280 retrieve_query(TimeEntryQuery, false)
284 retrieve_query(TimeEntryQuery, false)
281 end
285 end
282 end
286 end
General Comments 0
You need to be logged in to leave comments. Login now