##// END OF EJS Templates
Adds named scopes for projects index....
Jean-Philippe Lang -
r7962:ff0f141126e4
parent child
Show More
@@ -1,84 +1,82
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 AdminController < ApplicationController
19 19 layout 'admin'
20 20 before_filter :require_admin
21 21 helper :sort
22 22 include SortHelper
23 23
24 24 def index
25 25 @no_configuration_data = Redmine::DefaultData::Loader::no_data?
26 26 end
27 27
28 28 def projects
29 @status = params[:status] ? params[:status].to_i : 1
30 c = ARCondition.new(@status == 0 ? "status <> 0" : ["status = ?", @status])
31 unless params[:name].blank?
32 name = "%#{params[:name].strip.downcase}%"
33 c << ["LOWER(identifier) LIKE ? OR LOWER(name) LIKE ?", name, name]
34 end
35 @projects = Project.find :all, :order => 'lft',
36 :conditions => c.conditions
29 @status = params[:status] || 1
30
31 scope = Project.status(@status)
32 scope = scope.like(params[:name]) if params[:name].present?
33
34 @projects = scope.all(:order => 'lft')
37 35
38 36 render :action => "projects", :layout => false if request.xhr?
39 37 end
40 38
41 39 def plugins
42 40 @plugins = Redmine::Plugin.all
43 41 end
44 42
45 43 # Loads the default configuration
46 44 # (roles, trackers, statuses, workflow, enumerations)
47 45 def default_configuration
48 46 if request.post?
49 47 begin
50 48 Redmine::DefaultData::Loader::load(params[:lang])
51 49 flash[:notice] = l(:notice_default_data_loaded)
52 50 rescue Exception => e
53 51 flash[:error] = l(:error_can_t_load_default_data, e.message)
54 52 end
55 53 end
56 54 redirect_to :action => 'index'
57 55 end
58 56
59 57 def test_email
60 58 raise_delivery_errors = ActionMailer::Base.raise_delivery_errors
61 59 # Force ActionMailer to raise delivery errors so we can catch it
62 60 ActionMailer::Base.raise_delivery_errors = true
63 61 begin
64 62 @test = Mailer.deliver_test(User.current)
65 63 flash[:notice] = l(:notice_email_sent, User.current.mail)
66 64 rescue Exception => e
67 65 flash[:error] = l(:notice_email_error, e.message)
68 66 end
69 67 ActionMailer::Base.raise_delivery_errors = raise_delivery_errors
70 68 redirect_to :controller => 'settings', :action => 'edit', :tab => 'notifications'
71 69 end
72 70
73 71 def info
74 72 @db_adapter_name = ActiveRecord::Base.connection.adapter_name
75 73 @checklist = [
76 74 [:text_default_administrator_account_changed,
77 75 User.find(:first,
78 76 :conditions => ["login=? and hashed_password=?", 'admin', User.hash_password('admin')]).nil?],
79 77 [:text_file_repository_writable, File.writable?(Attachment.storage_path)],
80 78 [:text_plugin_assets_writable, File.writable?(Engines.public_directory)],
81 79 [:text_rmagick_available, Object.const_defined?(:Magick)]
82 80 ]
83 81 end
84 82 end
@@ -1,23 +1,23
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 module AdminHelper
19 19 def project_status_options_for_select(selected)
20 20 options_for_select([[l(:label_all), ''],
21 [l(:status_active), 1]], selected)
21 [l(:status_active), '1']], selected.to_s)
22 22 end
23 23 end
@@ -1,879 +1,888
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 Project < ActiveRecord::Base
19 19 include Redmine::SafeAttributes
20 20
21 21 # Project statuses
22 22 STATUS_ACTIVE = 1
23 23 STATUS_ARCHIVED = 9
24 24
25 25 # Maximum length for project identifiers
26 26 IDENTIFIER_MAX_LENGTH = 100
27 27
28 28 # Specific overidden Activities
29 29 has_many :time_entry_activities
30 30 has_many :members, :include => [:user, :roles], :conditions => "#{User.table_name}.type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}"
31 31 has_many :memberships, :class_name => 'Member'
32 32 has_many :member_principals, :class_name => 'Member',
33 33 :include => :principal,
34 34 :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{User::STATUS_ACTIVE})"
35 35 has_many :users, :through => :members
36 36 has_many :principals, :through => :member_principals, :source => :principal
37 37
38 38 has_many :enabled_modules, :dependent => :delete_all
39 39 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
40 40 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
41 41 has_many :issue_changes, :through => :issues, :source => :journals
42 42 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
43 43 has_many :time_entries, :dependent => :delete_all
44 44 has_many :queries, :dependent => :delete_all
45 45 has_many :documents, :dependent => :destroy
46 46 has_many :news, :dependent => :destroy, :include => :author
47 47 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
48 48 has_many :boards, :dependent => :destroy, :order => "position ASC"
49 49 has_one :repository, :dependent => :destroy
50 50 has_many :changesets, :through => :repository
51 51 has_one :wiki, :dependent => :destroy
52 52 # Custom field for the project issues
53 53 has_and_belongs_to_many :issue_custom_fields,
54 54 :class_name => 'IssueCustomField',
55 55 :order => "#{CustomField.table_name}.position",
56 56 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
57 57 :association_foreign_key => 'custom_field_id'
58 58
59 59 acts_as_nested_set :order => 'name', :dependent => :destroy
60 60 acts_as_attachable :view_permission => :view_files,
61 61 :delete_permission => :manage_files
62 62
63 63 acts_as_customizable
64 64 acts_as_searchable :columns => ['name', 'identifier', 'description'], :project_key => 'id', :permission => nil
65 65 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
66 66 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o}},
67 67 :author => nil
68 68
69 69 attr_protected :status
70 70
71 71 validates_presence_of :name, :identifier
72 72 validates_uniqueness_of :identifier
73 73 validates_associated :repository, :wiki
74 74 validates_length_of :name, :maximum => 255
75 75 validates_length_of :homepage, :maximum => 255
76 76 validates_length_of :identifier, :in => 1..IDENTIFIER_MAX_LENGTH
77 77 # donwcase letters, digits, dashes but not digits only
78 78 validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? }
79 79 # reserved words
80 80 validates_exclusion_of :identifier, :in => %w( new )
81 81
82 82 before_destroy :delete_all_members
83 83
84 84 named_scope :has_module, lambda { |mod| { :conditions => ["#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s] } }
85 85 named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
86 named_scope :status, lambda {|arg| arg.blank? ? {} : {:conditions => {:status => arg.to_i}} }
86 87 named_scope :all_public, { :conditions => { :is_public => true } }
87 88 named_scope :visible, lambda {|*args| {:conditions => Project.visible_condition(args.shift || User.current, *args) }}
89 named_scope :like, lambda {|arg|
90 if arg.blank?
91 {}
92 else
93 pattern = "%#{arg.to_s.strip.downcase}%"
94 {:conditions => ["LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p", {:p => pattern}]}
95 end
96 }
88 97
89 98 def initialize(attributes = nil)
90 99 super
91 100
92 101 initialized = (attributes || {}).stringify_keys
93 102 if !initialized.key?('identifier') && Setting.sequential_project_identifiers?
94 103 self.identifier = Project.next_identifier
95 104 end
96 105 if !initialized.key?('is_public')
97 106 self.is_public = Setting.default_projects_public?
98 107 end
99 108 if !initialized.key?('enabled_module_names')
100 109 self.enabled_module_names = Setting.default_projects_modules
101 110 end
102 111 if !initialized.key?('trackers') && !initialized.key?('tracker_ids')
103 112 self.trackers = Tracker.all
104 113 end
105 114 end
106 115
107 116 def identifier=(identifier)
108 117 super unless identifier_frozen?
109 118 end
110 119
111 120 def identifier_frozen?
112 121 errors[:identifier].nil? && !(new_record? || identifier.blank?)
113 122 end
114 123
115 124 # returns latest created projects
116 125 # non public projects will be returned only if user is a member of those
117 126 def self.latest(user=nil, count=5)
118 127 visible(user).find(:all, :limit => count, :order => "created_on DESC")
119 128 end
120 129
121 130 # Returns true if the project is visible to +user+ or to the current user.
122 131 def visible?(user=User.current)
123 132 user.allowed_to?(:view_project, self)
124 133 end
125 134
126 135 # Returns a SQL conditions string used to find all projects visible by the specified user.
127 136 #
128 137 # Examples:
129 138 # Project.visible_condition(admin) => "projects.status = 1"
130 139 # Project.visible_condition(normal_user) => "((projects.status = 1) AND (projects.is_public = 1 OR projects.id IN (1,3,4)))"
131 140 # Project.visible_condition(anonymous) => "((projects.status = 1) AND (projects.is_public = 1))"
132 141 def self.visible_condition(user, options={})
133 142 allowed_to_condition(user, :view_project, options)
134 143 end
135 144
136 145 # Returns a SQL conditions string used to find all projects for which +user+ has the given +permission+
137 146 #
138 147 # Valid options:
139 148 # * :project => limit the condition to project
140 149 # * :with_subprojects => limit the condition to project and its subprojects
141 150 # * :member => limit the condition to the user projects
142 151 def self.allowed_to_condition(user, permission, options={})
143 152 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
144 153 if perm = Redmine::AccessControl.permission(permission)
145 154 unless perm.project_module.nil?
146 155 # If the permission belongs to a project module, make sure the module is enabled
147 156 base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')"
148 157 end
149 158 end
150 159 if options[:project]
151 160 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
152 161 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
153 162 base_statement = "(#{project_statement}) AND (#{base_statement})"
154 163 end
155 164
156 165 if user.admin?
157 166 base_statement
158 167 else
159 168 statement_by_role = {}
160 169 unless options[:member]
161 170 role = user.logged? ? Role.non_member : Role.anonymous
162 171 if role.allowed_to?(permission)
163 172 statement_by_role[role] = "#{Project.table_name}.is_public = #{connection.quoted_true}"
164 173 end
165 174 end
166 175 if user.logged?
167 176 user.projects_by_role.each do |role, projects|
168 177 if role.allowed_to?(permission)
169 178 statement_by_role[role] = "#{Project.table_name}.id IN (#{projects.collect(&:id).join(',')})"
170 179 end
171 180 end
172 181 end
173 182 if statement_by_role.empty?
174 183 "1=0"
175 184 else
176 185 if block_given?
177 186 statement_by_role.each do |role, statement|
178 187 if s = yield(role, user)
179 188 statement_by_role[role] = "(#{statement} AND (#{s}))"
180 189 end
181 190 end
182 191 end
183 192 "((#{base_statement}) AND (#{statement_by_role.values.join(' OR ')}))"
184 193 end
185 194 end
186 195 end
187 196
188 197 # Returns the Systemwide and project specific activities
189 198 def activities(include_inactive=false)
190 199 if include_inactive
191 200 return all_activities
192 201 else
193 202 return active_activities
194 203 end
195 204 end
196 205
197 206 # Will create a new Project specific Activity or update an existing one
198 207 #
199 208 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
200 209 # does not successfully save.
201 210 def update_or_create_time_entry_activity(id, activity_hash)
202 211 if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id')
203 212 self.create_time_entry_activity_if_needed(activity_hash)
204 213 else
205 214 activity = project.time_entry_activities.find_by_id(id.to_i)
206 215 activity.update_attributes(activity_hash) if activity
207 216 end
208 217 end
209 218
210 219 # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity
211 220 #
212 221 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
213 222 # does not successfully save.
214 223 def create_time_entry_activity_if_needed(activity)
215 224 if activity['parent_id']
216 225
217 226 parent_activity = TimeEntryActivity.find(activity['parent_id'])
218 227 activity['name'] = parent_activity.name
219 228 activity['position'] = parent_activity.position
220 229
221 230 if Enumeration.overridding_change?(activity, parent_activity)
222 231 project_activity = self.time_entry_activities.create(activity)
223 232
224 233 if project_activity.new_record?
225 234 raise ActiveRecord::Rollback, "Overridding TimeEntryActivity was not successfully saved"
226 235 else
227 236 self.time_entries.update_all("activity_id = #{project_activity.id}", ["activity_id = ?", parent_activity.id])
228 237 end
229 238 end
230 239 end
231 240 end
232 241
233 242 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
234 243 #
235 244 # Examples:
236 245 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
237 246 # project.project_condition(false) => "projects.id = 1"
238 247 def project_condition(with_subprojects)
239 248 cond = "#{Project.table_name}.id = #{id}"
240 249 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
241 250 cond
242 251 end
243 252
244 253 def self.find(*args)
245 254 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
246 255 project = find_by_identifier(*args)
247 256 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
248 257 project
249 258 else
250 259 super
251 260 end
252 261 end
253 262
254 263 def to_param
255 264 # id is used for projects with a numeric identifier (compatibility)
256 265 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
257 266 end
258 267
259 268 def active?
260 269 self.status == STATUS_ACTIVE
261 270 end
262 271
263 272 def archived?
264 273 self.status == STATUS_ARCHIVED
265 274 end
266 275
267 276 # Archives the project and its descendants
268 277 def archive
269 278 # Check that there is no issue of a non descendant project that is assigned
270 279 # to one of the project or descendant versions
271 280 v_ids = self_and_descendants.collect {|p| p.version_ids}.flatten
272 281 if v_ids.any? && Issue.find(:first, :include => :project,
273 282 :conditions => ["(#{Project.table_name}.lft < ? OR #{Project.table_name}.rgt > ?)" +
274 283 " AND #{Issue.table_name}.fixed_version_id IN (?)", lft, rgt, v_ids])
275 284 return false
276 285 end
277 286 Project.transaction do
278 287 archive!
279 288 end
280 289 true
281 290 end
282 291
283 292 # Unarchives the project
284 293 # All its ancestors must be active
285 294 def unarchive
286 295 return false if ancestors.detect {|a| !a.active?}
287 296 update_attribute :status, STATUS_ACTIVE
288 297 end
289 298
290 299 # Returns an array of projects the project can be moved to
291 300 # by the current user
292 301 def allowed_parents
293 302 return @allowed_parents if @allowed_parents
294 303 @allowed_parents = Project.find(:all, :conditions => Project.allowed_to_condition(User.current, :add_subprojects))
295 304 @allowed_parents = @allowed_parents - self_and_descendants
296 305 if User.current.allowed_to?(:add_project, nil, :global => true) || (!new_record? && parent.nil?)
297 306 @allowed_parents << nil
298 307 end
299 308 unless parent.nil? || @allowed_parents.empty? || @allowed_parents.include?(parent)
300 309 @allowed_parents << parent
301 310 end
302 311 @allowed_parents
303 312 end
304 313
305 314 # Sets the parent of the project with authorization check
306 315 def set_allowed_parent!(p)
307 316 unless p.nil? || p.is_a?(Project)
308 317 if p.to_s.blank?
309 318 p = nil
310 319 else
311 320 p = Project.find_by_id(p)
312 321 return false unless p
313 322 end
314 323 end
315 324 if p.nil?
316 325 if !new_record? && allowed_parents.empty?
317 326 return false
318 327 end
319 328 elsif !allowed_parents.include?(p)
320 329 return false
321 330 end
322 331 set_parent!(p)
323 332 end
324 333
325 334 # Sets the parent of the project
326 335 # Argument can be either a Project, a String, a Fixnum or nil
327 336 def set_parent!(p)
328 337 unless p.nil? || p.is_a?(Project)
329 338 if p.to_s.blank?
330 339 p = nil
331 340 else
332 341 p = Project.find_by_id(p)
333 342 return false unless p
334 343 end
335 344 end
336 345 if p == parent && !p.nil?
337 346 # Nothing to do
338 347 true
339 348 elsif p.nil? || (p.active? && move_possible?(p))
340 349 # Insert the project so that target's children or root projects stay alphabetically sorted
341 350 sibs = (p.nil? ? self.class.roots : p.children)
342 351 to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
343 352 if to_be_inserted_before
344 353 move_to_left_of(to_be_inserted_before)
345 354 elsif p.nil?
346 355 if sibs.empty?
347 356 # move_to_root adds the project in first (ie. left) position
348 357 move_to_root
349 358 else
350 359 move_to_right_of(sibs.last) unless self == sibs.last
351 360 end
352 361 else
353 362 # move_to_child_of adds the project in last (ie.right) position
354 363 move_to_child_of(p)
355 364 end
356 365 Issue.update_versions_from_hierarchy_change(self)
357 366 true
358 367 else
359 368 # Can not move to the given target
360 369 false
361 370 end
362 371 end
363 372
364 373 # Returns an array of the trackers used by the project and its active sub projects
365 374 def rolled_up_trackers
366 375 @rolled_up_trackers ||=
367 376 Tracker.find(:all, :joins => :projects,
368 377 :select => "DISTINCT #{Tracker.table_name}.*",
369 378 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt],
370 379 :order => "#{Tracker.table_name}.position")
371 380 end
372 381
373 382 # Closes open and locked project versions that are completed
374 383 def close_completed_versions
375 384 Version.transaction do
376 385 versions.find(:all, :conditions => {:status => %w(open locked)}).each do |version|
377 386 if version.completed?
378 387 version.update_attribute(:status, 'closed')
379 388 end
380 389 end
381 390 end
382 391 end
383 392
384 393 # Returns a scope of the Versions on subprojects
385 394 def rolled_up_versions
386 395 @rolled_up_versions ||=
387 396 Version.scoped(:include => :project,
388 397 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt])
389 398 end
390 399
391 400 # Returns a scope of the Versions used by the project
392 401 def shared_versions
393 402 @shared_versions ||= begin
394 403 r = root? ? self : root
395 404 Version.scoped(:include => :project,
396 405 :conditions => "#{Project.table_name}.id = #{id}" +
397 406 " OR (#{Project.table_name}.status = #{Project::STATUS_ACTIVE} AND (" +
398 407 " #{Version.table_name}.sharing = 'system'" +
399 408 " OR (#{Project.table_name}.lft >= #{r.lft} AND #{Project.table_name}.rgt <= #{r.rgt} AND #{Version.table_name}.sharing = 'tree')" +
400 409 " OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" +
401 410 " OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" +
402 411 "))")
403 412 end
404 413 end
405 414
406 415 # Returns a hash of project users grouped by role
407 416 def users_by_role
408 417 members.find(:all, :include => [:user, :roles]).inject({}) do |h, m|
409 418 m.roles.each do |r|
410 419 h[r] ||= []
411 420 h[r] << m.user
412 421 end
413 422 h
414 423 end
415 424 end
416 425
417 426 # Deletes all project's members
418 427 def delete_all_members
419 428 me, mr = Member.table_name, MemberRole.table_name
420 429 connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})")
421 430 Member.delete_all(['project_id = ?', id])
422 431 end
423 432
424 433 # Users/groups issues can be assigned to
425 434 def assignable_users
426 435 assignable = Setting.issue_group_assignment? ? member_principals : members
427 436 assignable.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.principal}.sort
428 437 end
429 438
430 439 # Returns the mail adresses of users that should be always notified on project events
431 440 def recipients
432 441 notified_users.collect {|user| user.mail}
433 442 end
434 443
435 444 # Returns the users that should be notified on project events
436 445 def notified_users
437 446 # TODO: User part should be extracted to User#notify_about?
438 447 members.select {|m| m.mail_notification? || m.user.mail_notification == 'all'}.collect {|m| m.user}
439 448 end
440 449
441 450 # Returns an array of all custom fields enabled for project issues
442 451 # (explictly associated custom fields and custom fields enabled for all projects)
443 452 def all_issue_custom_fields
444 453 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
445 454 end
446 455
447 456 # Returns an array of all custom fields enabled for project time entries
448 457 # (explictly associated custom fields and custom fields enabled for all projects)
449 458 def all_time_entry_custom_fields
450 459 @all_time_entry_custom_fields ||= (TimeEntryCustomField.for_all + time_entry_custom_fields).uniq.sort
451 460 end
452 461
453 462 def project
454 463 self
455 464 end
456 465
457 466 def <=>(project)
458 467 name.downcase <=> project.name.downcase
459 468 end
460 469
461 470 def to_s
462 471 name
463 472 end
464 473
465 474 # Returns a short description of the projects (first lines)
466 475 def short_description(length = 255)
467 476 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
468 477 end
469 478
470 479 def css_classes
471 480 s = 'project'
472 481 s << ' root' if root?
473 482 s << ' child' if child?
474 483 s << (leaf? ? ' leaf' : ' parent')
475 484 s
476 485 end
477 486
478 487 # The earliest start date of a project, based on it's issues and versions
479 488 def start_date
480 489 [
481 490 issues.minimum('start_date'),
482 491 shared_versions.collect(&:effective_date),
483 492 shared_versions.collect(&:start_date)
484 493 ].flatten.compact.min
485 494 end
486 495
487 496 # The latest due date of an issue or version
488 497 def due_date
489 498 [
490 499 issues.maximum('due_date'),
491 500 shared_versions.collect(&:effective_date),
492 501 shared_versions.collect {|v| v.fixed_issues.maximum('due_date')}
493 502 ].flatten.compact.max
494 503 end
495 504
496 505 def overdue?
497 506 active? && !due_date.nil? && (due_date < Date.today)
498 507 end
499 508
500 509 # Returns the percent completed for this project, based on the
501 510 # progress on it's versions.
502 511 def completed_percent(options={:include_subprojects => false})
503 512 if options.delete(:include_subprojects)
504 513 total = self_and_descendants.collect(&:completed_percent).sum
505 514
506 515 total / self_and_descendants.count
507 516 else
508 517 if versions.count > 0
509 518 total = versions.collect(&:completed_pourcent).sum
510 519
511 520 total / versions.count
512 521 else
513 522 100
514 523 end
515 524 end
516 525 end
517 526
518 527 # Return true if this project is allowed to do the specified action.
519 528 # action can be:
520 529 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
521 530 # * a permission Symbol (eg. :edit_project)
522 531 def allows_to?(action)
523 532 if action.is_a? Hash
524 533 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
525 534 else
526 535 allowed_permissions.include? action
527 536 end
528 537 end
529 538
530 539 def module_enabled?(module_name)
531 540 module_name = module_name.to_s
532 541 enabled_modules.detect {|m| m.name == module_name}
533 542 end
534 543
535 544 def enabled_module_names=(module_names)
536 545 if module_names && module_names.is_a?(Array)
537 546 module_names = module_names.collect(&:to_s).reject(&:blank?)
538 547 self.enabled_modules = module_names.collect {|name| enabled_modules.detect {|mod| mod.name == name} || EnabledModule.new(:name => name)}
539 548 else
540 549 enabled_modules.clear
541 550 end
542 551 end
543 552
544 553 # Returns an array of the enabled modules names
545 554 def enabled_module_names
546 555 enabled_modules.collect(&:name)
547 556 end
548 557
549 558 # Enable a specific module
550 559 #
551 560 # Examples:
552 561 # project.enable_module!(:issue_tracking)
553 562 # project.enable_module!("issue_tracking")
554 563 def enable_module!(name)
555 564 enabled_modules << EnabledModule.new(:name => name.to_s) unless module_enabled?(name)
556 565 end
557 566
558 567 # Disable a module if it exists
559 568 #
560 569 # Examples:
561 570 # project.disable_module!(:issue_tracking)
562 571 # project.disable_module!("issue_tracking")
563 572 # project.disable_module!(project.enabled_modules.first)
564 573 def disable_module!(target)
565 574 target = enabled_modules.detect{|mod| target.to_s == mod.name} unless enabled_modules.include?(target)
566 575 target.destroy unless target.blank?
567 576 end
568 577
569 578 safe_attributes 'name',
570 579 'description',
571 580 'homepage',
572 581 'is_public',
573 582 'identifier',
574 583 'custom_field_values',
575 584 'custom_fields',
576 585 'tracker_ids',
577 586 'issue_custom_field_ids'
578 587
579 588 safe_attributes 'enabled_module_names',
580 589 :if => lambda {|project, user| project.new_record? || user.allowed_to?(:select_project_modules, project) }
581 590
582 591 # Returns an array of projects that are in this project's hierarchy
583 592 #
584 593 # Example: parents, children, siblings
585 594 def hierarchy
586 595 parents = project.self_and_ancestors || []
587 596 descendants = project.descendants || []
588 597 project_hierarchy = parents | descendants # Set union
589 598 end
590 599
591 600 # Returns an auto-generated project identifier based on the last identifier used
592 601 def self.next_identifier
593 602 p = Project.find(:first, :order => 'created_on DESC')
594 603 p.nil? ? nil : p.identifier.to_s.succ
595 604 end
596 605
597 606 # Copies and saves the Project instance based on the +project+.
598 607 # Duplicates the source project's:
599 608 # * Wiki
600 609 # * Versions
601 610 # * Categories
602 611 # * Issues
603 612 # * Members
604 613 # * Queries
605 614 #
606 615 # Accepts an +options+ argument to specify what to copy
607 616 #
608 617 # Examples:
609 618 # project.copy(1) # => copies everything
610 619 # project.copy(1, :only => 'members') # => copies members only
611 620 # project.copy(1, :only => ['members', 'versions']) # => copies members and versions
612 621 def copy(project, options={})
613 622 project = project.is_a?(Project) ? project : Project.find(project)
614 623
615 624 to_be_copied = %w(wiki versions issue_categories issues members queries boards)
616 625 to_be_copied = to_be_copied & options[:only].to_a unless options[:only].nil?
617 626
618 627 Project.transaction do
619 628 if save
620 629 reload
621 630 to_be_copied.each do |name|
622 631 send "copy_#{name}", project
623 632 end
624 633 Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
625 634 save
626 635 end
627 636 end
628 637 end
629 638
630 639
631 640 # Copies +project+ and returns the new instance. This will not save
632 641 # the copy
633 642 def self.copy_from(project)
634 643 begin
635 644 project = project.is_a?(Project) ? project : Project.find(project)
636 645 if project
637 646 # clear unique attributes
638 647 attributes = project.attributes.dup.except('id', 'name', 'identifier', 'status', 'parent_id', 'lft', 'rgt')
639 648 copy = Project.new(attributes)
640 649 copy.enabled_modules = project.enabled_modules
641 650 copy.trackers = project.trackers
642 651 copy.custom_values = project.custom_values.collect {|v| v.clone}
643 652 copy.issue_custom_fields = project.issue_custom_fields
644 653 return copy
645 654 else
646 655 return nil
647 656 end
648 657 rescue ActiveRecord::RecordNotFound
649 658 return nil
650 659 end
651 660 end
652 661
653 662 # Yields the given block for each project with its level in the tree
654 663 def self.project_tree(projects, &block)
655 664 ancestors = []
656 665 projects.sort_by(&:lft).each do |project|
657 666 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
658 667 ancestors.pop
659 668 end
660 669 yield project, ancestors.size
661 670 ancestors << project
662 671 end
663 672 end
664 673
665 674 private
666 675
667 676 # Copies wiki from +project+
668 677 def copy_wiki(project)
669 678 # Check that the source project has a wiki first
670 679 unless project.wiki.nil?
671 680 self.wiki ||= Wiki.new
672 681 wiki.attributes = project.wiki.attributes.dup.except("id", "project_id")
673 682 wiki_pages_map = {}
674 683 project.wiki.pages.each do |page|
675 684 # Skip pages without content
676 685 next if page.content.nil?
677 686 new_wiki_content = WikiContent.new(page.content.attributes.dup.except("id", "page_id", "updated_on"))
678 687 new_wiki_page = WikiPage.new(page.attributes.dup.except("id", "wiki_id", "created_on", "parent_id"))
679 688 new_wiki_page.content = new_wiki_content
680 689 wiki.pages << new_wiki_page
681 690 wiki_pages_map[page.id] = new_wiki_page
682 691 end
683 692 wiki.save
684 693 # Reproduce page hierarchy
685 694 project.wiki.pages.each do |page|
686 695 if page.parent_id && wiki_pages_map[page.id]
687 696 wiki_pages_map[page.id].parent = wiki_pages_map[page.parent_id]
688 697 wiki_pages_map[page.id].save
689 698 end
690 699 end
691 700 end
692 701 end
693 702
694 703 # Copies versions from +project+
695 704 def copy_versions(project)
696 705 project.versions.each do |version|
697 706 new_version = Version.new
698 707 new_version.attributes = version.attributes.dup.except("id", "project_id", "created_on", "updated_on")
699 708 self.versions << new_version
700 709 end
701 710 end
702 711
703 712 # Copies issue categories from +project+
704 713 def copy_issue_categories(project)
705 714 project.issue_categories.each do |issue_category|
706 715 new_issue_category = IssueCategory.new
707 716 new_issue_category.attributes = issue_category.attributes.dup.except("id", "project_id")
708 717 self.issue_categories << new_issue_category
709 718 end
710 719 end
711 720
712 721 # Copies issues from +project+
713 722 # Note: issues assigned to a closed version won't be copied due to validation rules
714 723 def copy_issues(project)
715 724 # Stores the source issue id as a key and the copied issues as the
716 725 # value. Used to map the two togeather for issue relations.
717 726 issues_map = {}
718 727
719 728 # Get issues sorted by root_id, lft so that parent issues
720 729 # get copied before their children
721 730 project.issues.find(:all, :order => 'root_id, lft').each do |issue|
722 731 new_issue = Issue.new
723 732 new_issue.copy_from(issue)
724 733 new_issue.project = self
725 734 # Reassign fixed_versions by name, since names are unique per
726 735 # project and the versions for self are not yet saved
727 736 if issue.fixed_version
728 737 new_issue.fixed_version = self.versions.select {|v| v.name == issue.fixed_version.name}.first
729 738 end
730 739 # Reassign the category by name, since names are unique per
731 740 # project and the categories for self are not yet saved
732 741 if issue.category
733 742 new_issue.category = self.issue_categories.select {|c| c.name == issue.category.name}.first
734 743 end
735 744 # Parent issue
736 745 if issue.parent_id
737 746 if copied_parent = issues_map[issue.parent_id]
738 747 new_issue.parent_issue_id = copied_parent.id
739 748 end
740 749 end
741 750
742 751 self.issues << new_issue
743 752 if new_issue.new_record?
744 753 logger.info "Project#copy_issues: issue ##{issue.id} could not be copied: #{new_issue.errors.full_messages}" if logger && logger.info
745 754 else
746 755 issues_map[issue.id] = new_issue unless new_issue.new_record?
747 756 end
748 757 end
749 758
750 759 # Relations after in case issues related each other
751 760 project.issues.each do |issue|
752 761 new_issue = issues_map[issue.id]
753 762 unless new_issue
754 763 # Issue was not copied
755 764 next
756 765 end
757 766
758 767 # Relations
759 768 issue.relations_from.each do |source_relation|
760 769 new_issue_relation = IssueRelation.new
761 770 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
762 771 new_issue_relation.issue_to = issues_map[source_relation.issue_to_id]
763 772 if new_issue_relation.issue_to.nil? && Setting.cross_project_issue_relations?
764 773 new_issue_relation.issue_to = source_relation.issue_to
765 774 end
766 775 new_issue.relations_from << new_issue_relation
767 776 end
768 777
769 778 issue.relations_to.each do |source_relation|
770 779 new_issue_relation = IssueRelation.new
771 780 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
772 781 new_issue_relation.issue_from = issues_map[source_relation.issue_from_id]
773 782 if new_issue_relation.issue_from.nil? && Setting.cross_project_issue_relations?
774 783 new_issue_relation.issue_from = source_relation.issue_from
775 784 end
776 785 new_issue.relations_to << new_issue_relation
777 786 end
778 787 end
779 788 end
780 789
781 790 # Copies members from +project+
782 791 def copy_members(project)
783 792 # Copy users first, then groups to handle members with inherited and given roles
784 793 members_to_copy = []
785 794 members_to_copy += project.memberships.select {|m| m.principal.is_a?(User)}
786 795 members_to_copy += project.memberships.select {|m| !m.principal.is_a?(User)}
787 796
788 797 members_to_copy.each do |member|
789 798 new_member = Member.new
790 799 new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on")
791 800 # only copy non inherited roles
792 801 # inherited roles will be added when copying the group membership
793 802 role_ids = member.member_roles.reject(&:inherited?).collect(&:role_id)
794 803 next if role_ids.empty?
795 804 new_member.role_ids = role_ids
796 805 new_member.project = self
797 806 self.members << new_member
798 807 end
799 808 end
800 809
801 810 # Copies queries from +project+
802 811 def copy_queries(project)
803 812 project.queries.each do |query|
804 813 new_query = Query.new
805 814 new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria")
806 815 new_query.sort_criteria = query.sort_criteria if query.sort_criteria
807 816 new_query.project = self
808 817 new_query.user_id = query.user_id
809 818 self.queries << new_query
810 819 end
811 820 end
812 821
813 822 # Copies boards from +project+
814 823 def copy_boards(project)
815 824 project.boards.each do |board|
816 825 new_board = Board.new
817 826 new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id")
818 827 new_board.project = self
819 828 self.boards << new_board
820 829 end
821 830 end
822 831
823 832 def allowed_permissions
824 833 @allowed_permissions ||= begin
825 834 module_names = enabled_modules.all(:select => :name).collect {|m| m.name}
826 835 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
827 836 end
828 837 end
829 838
830 839 def allowed_actions
831 840 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
832 841 end
833 842
834 843 # Returns all the active Systemwide and project specific activities
835 844 def active_activities
836 845 overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
837 846
838 847 if overridden_activity_ids.empty?
839 848 return TimeEntryActivity.shared.active
840 849 else
841 850 return system_activities_and_project_overrides
842 851 end
843 852 end
844 853
845 854 # Returns all the Systemwide and project specific activities
846 855 # (inactive and active)
847 856 def all_activities
848 857 overridden_activity_ids = self.time_entry_activities.collect(&:parent_id)
849 858
850 859 if overridden_activity_ids.empty?
851 860 return TimeEntryActivity.shared
852 861 else
853 862 return system_activities_and_project_overrides(true)
854 863 end
855 864 end
856 865
857 866 # Returns the systemwide active activities merged with the project specific overrides
858 867 def system_activities_and_project_overrides(include_inactive=false)
859 868 if include_inactive
860 869 return TimeEntryActivity.shared.
861 870 find(:all,
862 871 :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) +
863 872 self.time_entry_activities
864 873 else
865 874 return TimeEntryActivity.shared.active.
866 875 find(:all,
867 876 :conditions => ["id NOT IN (?)", self.time_entry_activities.collect(&:parent_id)]) +
868 877 self.time_entry_activities.active
869 878 end
870 879 end
871 880
872 881 # Archives subprojects recursively
873 882 def archive!
874 883 children.each do |subproject|
875 884 subproject.send :archive!
876 885 end
877 886 update_attribute :status, STATUS_ARCHIVED
878 887 end
879 888 end
@@ -1,148 +1,157
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2011 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 require File.expand_path('../../test_helper', __FILE__)
19 19 require 'admin_controller'
20 20
21 21 # Re-raise errors caught by the controller.
22 22 class AdminController; def rescue_action(e) raise e end; end
23 23
24 24 class AdminControllerTest < ActionController::TestCase
25 25 fixtures :projects, :users, :roles
26 26
27 27 def setup
28 28 @controller = AdminController.new
29 29 @request = ActionController::TestRequest.new
30 30 @response = ActionController::TestResponse.new
31 31 User.current = nil
32 32 @request.session[:user_id] = 1 # admin
33 33 end
34 34
35 35 def test_index
36 36 get :index
37 37 assert_no_tag :tag => 'div',
38 38 :attributes => { :class => /nodata/ }
39 39 end
40 40
41 41 def test_index_with_no_configuration_data
42 42 delete_configuration_data
43 43 get :index
44 44 assert_tag :tag => 'div',
45 45 :attributes => { :class => /nodata/ }
46 46 end
47 47
48 48 def test_projects
49 49 get :projects
50 50 assert_response :success
51 51 assert_template 'projects'
52 52 assert_not_nil assigns(:projects)
53 53 # active projects only
54 54 assert_nil assigns(:projects).detect {|u| !u.active?}
55 55 end
56 56
57 def test_projects_with_status_filter
58 get :projects, :status => 1
59 assert_response :success
60 assert_template 'projects'
61 assert_not_nil assigns(:projects)
62 # active projects only
63 assert_nil assigns(:projects).detect {|u| !u.active?}
64 end
65
57 66 def test_projects_with_name_filter
58 67 get :projects, :name => 'store', :status => ''
59 68 assert_response :success
60 69 assert_template 'projects'
61 70 projects = assigns(:projects)
62 71 assert_not_nil projects
63 72 assert_equal 1, projects.size
64 73 assert_equal 'OnlineStore', projects.first.name
65 74 end
66 75
67 76 def test_load_default_configuration_data
68 77 delete_configuration_data
69 78 post :default_configuration, :lang => 'fr'
70 79 assert_response :redirect
71 80 assert_nil flash[:error]
72 81 assert IssueStatus.find_by_name('Nouveau')
73 82 end
74 83
75 84 def test_test_email
76 85 get :test_email
77 86 assert_redirected_to '/settings/edit?tab=notifications'
78 87 mail = ActionMailer::Base.deliveries.last
79 88 assert_kind_of TMail::Mail, mail
80 89 user = User.find(1)
81 90 assert_equal [user.mail], mail.bcc
82 91 end
83 92
84 93 def test_test_email_failure_should_display_the_error
85 94 Mailer.stubs(:deliver_test).raises(Exception, 'Some error message')
86 95 get :test_email
87 96 assert_redirected_to '/settings/edit?tab=notifications'
88 97 assert_match /Some error message/, flash[:error]
89 98 end
90 99
91 100 def test_no_plugins
92 101 Redmine::Plugin.clear
93 102
94 103 get :plugins
95 104 assert_response :success
96 105 assert_template 'plugins'
97 106 end
98 107
99 108 def test_plugins
100 109 # Register a few plugins
101 110 Redmine::Plugin.register :foo do
102 111 name 'Foo plugin'
103 112 author 'John Smith'
104 113 description 'This is a test plugin'
105 114 version '0.0.1'
106 115 settings :default => {'sample_setting' => 'value', 'foo'=>'bar'}, :partial => 'foo/settings'
107 116 end
108 117 Redmine::Plugin.register :bar do
109 118 end
110 119
111 120 get :plugins
112 121 assert_response :success
113 122 assert_template 'plugins'
114 123
115 124 assert_tag :td, :child => { :tag => 'span', :content => 'Foo plugin' }
116 125 assert_tag :td, :child => { :tag => 'span', :content => 'Bar' }
117 126 end
118 127
119 128 def test_info
120 129 get :info
121 130 assert_response :success
122 131 assert_template 'info'
123 132 end
124 133
125 134 def test_admin_menu_plugin_extension
126 135 Redmine::MenuManager.map :admin_menu do |menu|
127 136 menu.push :test_admin_menu_plugin_extension, '/foo/bar', :caption => 'Test'
128 137 end
129 138
130 139 get :index
131 140 assert_response :success
132 141 assert_tag :a, :attributes => { :href => '/foo/bar' },
133 142 :content => 'Test'
134 143
135 144 Redmine::MenuManager.map :admin_menu do |menu|
136 145 menu.delete :test_admin_menu_plugin_extension
137 146 end
138 147 end
139 148
140 149 private
141 150
142 151 def delete_configuration_data
143 152 Role.delete_all('builtin = 0')
144 153 Tracker.delete_all
145 154 IssueStatus.delete_all
146 155 Enumeration.delete_all
147 156 end
148 157 end
General Comments 0
You need to be logged in to leave comments. Login now