##// END OF EJS Templates
Accept key authentication to ProjectsController#index (for feeds). #5317...
Eric Davis -
r3663:921b425b8e5e
parent child
Show More
@@ -1,389 +1,389
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2009 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 class ProjectsController < ApplicationController
19 19 menu_item :overview
20 20 menu_item :activity, :only => :activity
21 21 menu_item :roadmap, :only => :roadmap
22 22 menu_item :files, :only => [:list_files, :add_file]
23 23 menu_item :settings, :only => :settings
24 24
25 25 before_filter :find_project, :except => [ :index, :list, :add, :copy, :activity ]
26 26 before_filter :find_optional_project, :only => :activity
27 27 before_filter :authorize, :except => [ :index, :list, :add, :copy, :archive, :unarchive, :destroy, :activity ]
28 28 before_filter :authorize_global, :only => :add
29 29 before_filter :require_admin, :only => [ :copy, :archive, :unarchive, :destroy ]
30 accept_key_auth :activity
30 accept_key_auth :activity, :index
31 31
32 32 after_filter :only => [:add, :edit, :archive, :unarchive, :destroy] do |controller|
33 33 if controller.request.post?
34 34 controller.send :expire_action, :controller => 'welcome', :action => 'robots.txt'
35 35 end
36 36 end
37 37
38 38 helper :sort
39 39 include SortHelper
40 40 helper :custom_fields
41 41 include CustomFieldsHelper
42 42 helper :issues
43 43 helper :queries
44 44 include QueriesHelper
45 45 helper :repositories
46 46 include RepositoriesHelper
47 47 include ProjectsHelper
48 48
49 49 # Lists visible projects
50 50 def index
51 51 respond_to do |format|
52 52 format.html {
53 53 @projects = Project.visible.find(:all, :order => 'lft')
54 54 }
55 55 format.xml {
56 56 @projects = Project.visible.find(:all, :order => 'lft')
57 57 }
58 58 format.atom {
59 59 projects = Project.visible.find(:all, :order => 'created_on DESC',
60 60 :limit => Setting.feeds_limit.to_i)
61 61 render_feed(projects, :title => "#{Setting.app_title}: #{l(:label_project_latest)}")
62 62 }
63 63 end
64 64 end
65 65
66 66 # Add a new project
67 67 def add
68 68 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
69 69 @trackers = Tracker.all
70 70 @project = Project.new(params[:project])
71 71 if request.get?
72 72 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
73 73 @project.trackers = Tracker.all
74 74 @project.is_public = Setting.default_projects_public?
75 75 @project.enabled_module_names = Setting.default_projects_modules
76 76 else
77 77 @project.enabled_module_names = params[:enabled_modules]
78 78 if validate_parent_id && @project.save
79 79 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
80 80 # Add current user as a project member if he is not admin
81 81 unless User.current.admin?
82 82 r = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
83 83 m = Member.new(:user => User.current, :roles => [r])
84 84 @project.members << m
85 85 end
86 86 respond_to do |format|
87 87 format.html {
88 88 flash[:notice] = l(:notice_successful_create)
89 89 redirect_to :controller => 'projects', :action => 'settings', :id => @project
90 90 }
91 91 format.xml { head :created, :location => url_for(:controller => 'projects', :action => 'show', :id => @project.id) }
92 92 end
93 93 else
94 94 respond_to do |format|
95 95 format.html
96 96 format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
97 97 end
98 98 end
99 99 end
100 100 end
101 101
102 102 def copy
103 103 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
104 104 @trackers = Tracker.all
105 105 @root_projects = Project.find(:all,
106 106 :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}",
107 107 :order => 'name')
108 108 @source_project = Project.find(params[:id])
109 109 if request.get?
110 110 @project = Project.copy_from(@source_project)
111 111 if @project
112 112 @project.identifier = Project.next_identifier if Setting.sequential_project_identifiers?
113 113 else
114 114 redirect_to :controller => 'admin', :action => 'projects'
115 115 end
116 116 else
117 117 Mailer.with_deliveries(params[:notifications] == '1') do
118 118 @project = Project.new(params[:project])
119 119 @project.enabled_module_names = params[:enabled_modules]
120 120 if validate_parent_id && @project.copy(@source_project, :only => params[:only])
121 121 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
122 122 flash[:notice] = l(:notice_successful_create)
123 123 redirect_to :controller => 'admin', :action => 'projects'
124 124 elsif !@project.new_record?
125 125 # Project was created
126 126 # But some objects were not copied due to validation failures
127 127 # (eg. issues from disabled trackers)
128 128 # TODO: inform about that
129 129 redirect_to :controller => 'admin', :action => 'projects'
130 130 end
131 131 end
132 132 end
133 133 rescue ActiveRecord::RecordNotFound
134 134 redirect_to :controller => 'admin', :action => 'projects'
135 135 end
136 136
137 137 # Show @project
138 138 def show
139 139 if params[:jump]
140 140 # try to redirect to the requested menu item
141 141 redirect_to_project_menu_item(@project, params[:jump]) && return
142 142 end
143 143
144 144 @users_by_role = @project.users_by_role
145 145 @subprojects = @project.children.visible
146 146 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC")
147 147 @trackers = @project.rolled_up_trackers
148 148
149 149 cond = @project.project_condition(Setting.display_subprojects_issues?)
150 150
151 151 @open_issues_by_tracker = Issue.visible.count(:group => :tracker,
152 152 :include => [:project, :status, :tracker],
153 153 :conditions => ["(#{cond}) AND #{IssueStatus.table_name}.is_closed=?", false])
154 154 @total_issues_by_tracker = Issue.visible.count(:group => :tracker,
155 155 :include => [:project, :status, :tracker],
156 156 :conditions => cond)
157 157
158 158 TimeEntry.visible_by(User.current) do
159 159 @total_hours = TimeEntry.sum(:hours,
160 160 :include => :project,
161 161 :conditions => cond).to_f
162 162 end
163 163 @key = User.current.rss_key
164 164
165 165 respond_to do |format|
166 166 format.html
167 167 format.xml
168 168 end
169 169 end
170 170
171 171 def settings
172 172 @issue_custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position")
173 173 @issue_category ||= IssueCategory.new
174 174 @member ||= @project.members.new
175 175 @trackers = Tracker.all
176 176 @repository ||= @project.repository
177 177 @wiki ||= @project.wiki
178 178 end
179 179
180 180 # Edit @project
181 181 def edit
182 182 if request.get?
183 183 else
184 184 @project.attributes = params[:project]
185 185 if validate_parent_id && @project.save
186 186 @project.set_allowed_parent!(params[:project]['parent_id']) if params[:project].has_key?('parent_id')
187 187 respond_to do |format|
188 188 format.html {
189 189 flash[:notice] = l(:notice_successful_update)
190 190 redirect_to :action => 'settings', :id => @project
191 191 }
192 192 format.xml { head :ok }
193 193 end
194 194 else
195 195 respond_to do |format|
196 196 format.html {
197 197 settings
198 198 render :action => 'settings'
199 199 }
200 200 format.xml { render :xml => @project.errors, :status => :unprocessable_entity }
201 201 end
202 202 end
203 203 end
204 204 end
205 205
206 206 def modules
207 207 @project.enabled_module_names = params[:enabled_modules]
208 208 flash[:notice] = l(:notice_successful_update)
209 209 redirect_to :action => 'settings', :id => @project, :tab => 'modules'
210 210 end
211 211
212 212 def archive
213 213 if request.post?
214 214 unless @project.archive
215 215 flash[:error] = l(:error_can_not_archive_project)
216 216 end
217 217 end
218 218 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
219 219 end
220 220
221 221 def unarchive
222 222 @project.unarchive if request.post? && !@project.active?
223 223 redirect_to(url_for(:controller => 'admin', :action => 'projects', :status => params[:status]))
224 224 end
225 225
226 226 # Delete @project
227 227 def destroy
228 228 @project_to_destroy = @project
229 229 if request.get?
230 230 # display confirmation view
231 231 else
232 232 if params[:format] == 'xml' || params[:confirm]
233 233 @project_to_destroy.destroy
234 234 respond_to do |format|
235 235 format.html { redirect_to :controller => 'admin', :action => 'projects' }
236 236 format.xml { head :ok }
237 237 end
238 238 end
239 239 end
240 240 # hide project in layout
241 241 @project = nil
242 242 end
243 243
244 244 def add_file
245 245 if request.post?
246 246 container = (params[:version_id].blank? ? @project : @project.versions.find_by_id(params[:version_id]))
247 247 attachments = Attachment.attach_files(container, params[:attachments])
248 248 render_attachment_warning_if_needed(container)
249 249
250 250 if !attachments.empty? && Setting.notified_events.include?('file_added')
251 251 Mailer.deliver_attachments_added(attachments[:files])
252 252 end
253 253 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
254 254 return
255 255 end
256 256 @versions = @project.versions.sort
257 257 end
258 258
259 259 def save_activities
260 260 if request.post? && params[:enumerations]
261 261 Project.transaction do
262 262 params[:enumerations].each do |id, activity|
263 263 @project.update_or_create_time_entry_activity(id, activity)
264 264 end
265 265 end
266 266 flash[:notice] = l(:notice_successful_update)
267 267 end
268 268
269 269 redirect_to :controller => 'projects', :action => 'settings', :tab => 'activities', :id => @project
270 270 end
271 271
272 272 def reset_activities
273 273 @project.time_entry_activities.each do |time_entry_activity|
274 274 time_entry_activity.destroy(time_entry_activity.parent)
275 275 end
276 276 flash[:notice] = l(:notice_successful_update)
277 277 redirect_to :controller => 'projects', :action => 'settings', :tab => 'activities', :id => @project
278 278 end
279 279
280 280 def list_files
281 281 sort_init 'filename', 'asc'
282 282 sort_update 'filename' => "#{Attachment.table_name}.filename",
283 283 'created_on' => "#{Attachment.table_name}.created_on",
284 284 'size' => "#{Attachment.table_name}.filesize",
285 285 'downloads' => "#{Attachment.table_name}.downloads"
286 286
287 287 @containers = [ Project.find(@project.id, :include => :attachments, :order => sort_clause)]
288 288 @containers += @project.versions.find(:all, :include => :attachments, :order => sort_clause).sort.reverse
289 289 render :layout => !request.xhr?
290 290 end
291 291
292 292 def roadmap
293 293 @trackers = @project.trackers.find(:all, :order => 'position')
294 294 retrieve_selected_tracker_ids(@trackers, @trackers.select {|t| t.is_in_roadmap?})
295 295 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
296 296 project_ids = @with_subprojects ? @project.self_and_descendants.collect(&:id) : [@project.id]
297 297
298 298 @versions = @project.shared_versions || []
299 299 @versions += @project.rolled_up_versions.visible if @with_subprojects
300 300 @versions = @versions.uniq.sort
301 301 @versions.reject! {|version| version.closed? || version.completed? } unless params[:completed]
302 302
303 303 @issues_by_version = {}
304 304 unless @selected_tracker_ids.empty?
305 305 @versions.each do |version|
306 306 issues = version.fixed_issues.visible.find(:all,
307 307 :include => [:project, :status, :tracker, :priority],
308 308 :conditions => {:tracker_id => @selected_tracker_ids, :project_id => project_ids},
309 309 :order => "#{Project.table_name}.lft, #{Tracker.table_name}.position, #{Issue.table_name}.id")
310 310 @issues_by_version[version] = issues
311 311 end
312 312 end
313 313 @versions.reject! {|version| !project_ids.include?(version.project_id) && @issues_by_version[version].blank?}
314 314 end
315 315
316 316 def activity
317 317 @days = Setting.activity_days_default.to_i
318 318
319 319 if params[:from]
320 320 begin; @date_to = params[:from].to_date + 1; rescue; end
321 321 end
322 322
323 323 @date_to ||= Date.today + 1
324 324 @date_from = @date_to - @days
325 325 @with_subprojects = params[:with_subprojects].nil? ? Setting.display_subprojects_issues? : (params[:with_subprojects] == '1')
326 326 @author = (params[:user_id].blank? ? nil : User.active.find(params[:user_id]))
327 327
328 328 @activity = Redmine::Activity::Fetcher.new(User.current, :project => @project,
329 329 :with_subprojects => @with_subprojects,
330 330 :author => @author)
331 331 @activity.scope_select {|t| !params["show_#{t}"].nil?}
332 332 @activity.scope = (@author.nil? ? :default : :all) if @activity.scope.empty?
333 333
334 334 events = @activity.events(@date_from, @date_to)
335 335
336 336 if events.empty? || stale?(:etag => [events.first, User.current])
337 337 respond_to do |format|
338 338 format.html {
339 339 @events_by_day = events.group_by(&:event_date)
340 340 render :layout => false if request.xhr?
341 341 }
342 342 format.atom {
343 343 title = l(:label_activity)
344 344 if @author
345 345 title = @author.name
346 346 elsif @activity.scope.size == 1
347 347 title = l("label_#{@activity.scope.first.singularize}_plural")
348 348 end
349 349 render_feed(events, :title => "#{@project || Setting.app_title}: #{title}")
350 350 }
351 351 end
352 352 end
353 353
354 354 rescue ActiveRecord::RecordNotFound
355 355 render_404
356 356 end
357 357
358 358 private
359 359 def find_optional_project
360 360 return true unless params[:id]
361 361 @project = Project.find(params[:id])
362 362 authorize
363 363 rescue ActiveRecord::RecordNotFound
364 364 render_404
365 365 end
366 366
367 367 def retrieve_selected_tracker_ids(selectable_trackers, default_trackers=nil)
368 368 if ids = params[:tracker_ids]
369 369 @selected_tracker_ids = (ids.is_a? Array) ? ids.collect { |id| id.to_i.to_s } : ids.split('/').collect { |id| id.to_i.to_s }
370 370 else
371 371 @selected_tracker_ids = (default_trackers || selectable_trackers).collect {|t| t.id.to_s }
372 372 end
373 373 end
374 374
375 375 # Validates parent_id param according to user's permissions
376 376 # TODO: move it to Project model in a validation that depends on User.current
377 377 def validate_parent_id
378 378 return true if User.current.admin?
379 379 parent_id = params[:project] && params[:project][:parent_id]
380 380 if parent_id || @project.new_record?
381 381 parent = parent_id.blank? ? nil : Project.find_by_id(parent_id.to_i)
382 382 unless @project.allowed_parents.include?(parent)
383 383 @project.errors.add :parent_id, :invalid
384 384 return false
385 385 end
386 386 end
387 387 true
388 388 end
389 389 end
General Comments 0
You need to be logged in to leave comments. Login now