##// END OF EJS Templates
Use association instead of a manual JOIN (#21583)....
Jean-Philippe Lang -
r14680:3a52b08405ad
parent child
Show More
@@ -1,1047 +1,1047
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2015 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 include Redmine::NestedSet::ProjectNestedSet
21 21
22 22 # Project statuses
23 23 STATUS_ACTIVE = 1
24 24 STATUS_CLOSED = 5
25 25 STATUS_ARCHIVED = 9
26 26
27 27 # Maximum length for project identifiers
28 28 IDENTIFIER_MAX_LENGTH = 100
29 29
30 30 # Specific overridden Activities
31 31 has_many :time_entry_activities
32 32 has_many :memberships, :class_name => 'Member', :inverse_of => :project
33 33 # Memberships of active users only
34 34 has_many :members,
35 35 lambda { joins(:principal).where(:users => {:type => 'User', :status => Principal::STATUS_ACTIVE}) }
36 36 has_many :enabled_modules, :dependent => :delete_all
37 37 has_and_belongs_to_many :trackers, lambda {order(:position)}
38 38 has_many :issues, :dependent => :destroy
39 39 has_many :issue_changes, :through => :issues, :source => :journals
40 40 has_many :versions, lambda {order("#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC")}, :dependent => :destroy
41 41 belongs_to :default_version, :class_name => 'Version'
42 42 has_many :time_entries, :dependent => :destroy
43 43 has_many :queries, :class_name => 'IssueQuery', :dependent => :delete_all
44 44 has_many :documents, :dependent => :destroy
45 45 has_many :news, lambda {includes(:author)}, :dependent => :destroy
46 46 has_many :issue_categories, lambda {order("#{IssueCategory.table_name}.name")}, :dependent => :delete_all
47 47 has_many :boards, lambda {order("position ASC")}, :dependent => :destroy
48 48 has_one :repository, lambda {where(["is_default = ?", true])}
49 49 has_many :repositories, :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 lambda {order("#{CustomField.table_name}.position")},
55 55 :class_name => 'IssueCustomField',
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_attachable :view_permission => :view_files,
60 60 :edit_permission => :manage_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 => "#{Project.table_name}.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, :if => Proc.new {|p| p.identifier_changed?}
73 73 validates_length_of :name, :maximum => 255
74 74 validates_length_of :homepage, :maximum => 255
75 75 validates_length_of :identifier, :in => 1..IDENTIFIER_MAX_LENGTH
76 76 # downcase letters, digits, dashes but not digits only
77 77 validates_format_of :identifier, :with => /\A(?!\d+$)[a-z0-9\-_]*\z/, :if => Proc.new { |p| p.identifier_changed? }
78 78 # reserved words
79 79 validates_exclusion_of :identifier, :in => %w( new )
80 80 validate :validate_parent
81 81
82 82 after_save :update_inherited_members, :if => Proc.new {|project| project.inherit_members_changed?}
83 83 after_save :remove_inherited_member_roles, :add_inherited_member_roles, :if => Proc.new {|project| project.parent_id_changed?}
84 84 after_update :update_versions_from_hierarchy_change, :if => Proc.new {|project| project.parent_id_changed?}
85 85 before_destroy :delete_all_members
86 86
87 87 scope :has_module, lambda {|mod|
88 88 where("#{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name=?)", mod.to_s)
89 89 }
90 90 scope :active, lambda { where(:status => STATUS_ACTIVE) }
91 91 scope :status, lambda {|arg| where(arg.blank? ? nil : {:status => arg.to_i}) }
92 92 scope :all_public, lambda { where(:is_public => true) }
93 93 scope :visible, lambda {|*args| where(Project.visible_condition(args.shift || User.current, *args)) }
94 94 scope :allowed_to, lambda {|*args|
95 95 user = User.current
96 96 permission = nil
97 97 if args.first.is_a?(Symbol)
98 98 permission = args.shift
99 99 else
100 100 user = args.shift
101 101 permission = args.shift
102 102 end
103 103 where(Project.allowed_to_condition(user, permission, *args))
104 104 }
105 105 scope :like, lambda {|arg|
106 106 if arg.blank?
107 107 where(nil)
108 108 else
109 109 pattern = "%#{arg.to_s.strip.downcase}%"
110 110 where("LOWER(identifier) LIKE :p OR LOWER(name) LIKE :p", :p => pattern)
111 111 end
112 112 }
113 113 scope :sorted, lambda {order(:lft)}
114 114 scope :having_trackers, lambda {
115 115 where("#{Project.table_name}.id IN (SELECT DISTINCT project_id FROM #{table_name_prefix}projects_trackers#{table_name_suffix})")
116 116 }
117 117
118 118 def initialize(attributes=nil, *args)
119 119 super
120 120
121 121 initialized = (attributes || {}).stringify_keys
122 122 if !initialized.key?('identifier') && Setting.sequential_project_identifiers?
123 123 self.identifier = Project.next_identifier
124 124 end
125 125 if !initialized.key?('is_public')
126 126 self.is_public = Setting.default_projects_public?
127 127 end
128 128 if !initialized.key?('enabled_module_names')
129 129 self.enabled_module_names = Setting.default_projects_modules
130 130 end
131 131 if !initialized.key?('trackers') && !initialized.key?('tracker_ids')
132 132 default = Setting.default_projects_tracker_ids
133 133 if default.is_a?(Array)
134 134 self.trackers = Tracker.where(:id => default.map(&:to_i)).sorted.to_a
135 135 else
136 136 self.trackers = Tracker.sorted.to_a
137 137 end
138 138 end
139 139 end
140 140
141 141 def identifier=(identifier)
142 142 super unless identifier_frozen?
143 143 end
144 144
145 145 def identifier_frozen?
146 146 errors[:identifier].blank? && !(new_record? || identifier.blank?)
147 147 end
148 148
149 149 # returns latest created projects
150 150 # non public projects will be returned only if user is a member of those
151 151 def self.latest(user=nil, count=5)
152 152 visible(user).limit(count).
153 153 order(:created_on => :desc).
154 154 where("#{table_name}.created_on >= ?", 30.days.ago).
155 155 to_a
156 156 end
157 157
158 158 # Returns true if the project is visible to +user+ or to the current user.
159 159 def visible?(user=User.current)
160 160 user.allowed_to?(:view_project, self)
161 161 end
162 162
163 163 # Returns a SQL conditions string used to find all projects visible by the specified user.
164 164 #
165 165 # Examples:
166 166 # Project.visible_condition(admin) => "projects.status = 1"
167 167 # Project.visible_condition(normal_user) => "((projects.status = 1) AND (projects.is_public = 1 OR projects.id IN (1,3,4)))"
168 168 # Project.visible_condition(anonymous) => "((projects.status = 1) AND (projects.is_public = 1))"
169 169 def self.visible_condition(user, options={})
170 170 allowed_to_condition(user, :view_project, options)
171 171 end
172 172
173 173 # Returns a SQL conditions string used to find all projects for which +user+ has the given +permission+
174 174 #
175 175 # Valid options:
176 176 # * :project => limit the condition to project
177 177 # * :with_subprojects => limit the condition to project and its subprojects
178 178 # * :member => limit the condition to the user projects
179 179 def self.allowed_to_condition(user, permission, options={})
180 180 perm = Redmine::AccessControl.permission(permission)
181 181 base_statement = (perm && perm.read? ? "#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED}" : "#{Project.table_name}.status = #{Project::STATUS_ACTIVE}")
182 182 if perm && perm.project_module
183 183 # If the permission belongs to a project module, make sure the module is enabled
184 184 base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')"
185 185 end
186 186 if project = options[:project]
187 187 project_statement = project.project_condition(options[:with_subprojects])
188 188 base_statement = "(#{project_statement}) AND (#{base_statement})"
189 189 end
190 190
191 191 if user.admin?
192 192 base_statement
193 193 else
194 194 statement_by_role = {}
195 195 unless options[:member]
196 196 role = user.builtin_role
197 197 if role.allowed_to?(permission)
198 198 s = "#{Project.table_name}.is_public = #{connection.quoted_true}"
199 199 if user.id
200 200 s = "(#{s} AND #{Project.table_name}.id NOT IN (SELECT project_id FROM #{Member.table_name} WHERE user_id = #{user.id}))"
201 201 end
202 202 statement_by_role[role] = s
203 203 end
204 204 end
205 205 user.projects_by_role.each do |role, projects|
206 206 if role.allowed_to?(permission) && projects.any?
207 207 statement_by_role[role] = "#{Project.table_name}.id IN (#{projects.collect(&:id).join(',')})"
208 208 end
209 209 end
210 210 if statement_by_role.empty?
211 211 "1=0"
212 212 else
213 213 if block_given?
214 214 statement_by_role.each do |role, statement|
215 215 if s = yield(role, user)
216 216 statement_by_role[role] = "(#{statement} AND (#{s}))"
217 217 end
218 218 end
219 219 end
220 220 "((#{base_statement}) AND (#{statement_by_role.values.join(' OR ')}))"
221 221 end
222 222 end
223 223 end
224 224
225 225 def override_roles(role)
226 226 @override_members ||= memberships.
227 227 joins(:principal).
228 228 where(:users => {:type => ['GroupAnonymous', 'GroupNonMember']}).to_a
229 229
230 230 group_class = role.anonymous? ? GroupAnonymous : GroupNonMember
231 231 member = @override_members.detect {|m| m.principal.is_a? group_class}
232 232 member ? member.roles.to_a : [role]
233 233 end
234 234
235 235 def principals
236 236 @principals ||= Principal.active.joins(:members).where("#{Member.table_name}.project_id = ?", id).uniq
237 237 end
238 238
239 239 def users
240 240 @users ||= User.active.joins(:members).where("#{Member.table_name}.project_id = ?", id).uniq
241 241 end
242 242
243 243 # Returns the Systemwide and project specific activities
244 244 def activities(include_inactive=false)
245 245 t = TimeEntryActivity.table_name
246 246 scope = TimeEntryActivity.where("#{t}.project_id IS NULL OR #{t}.project_id = ?", id)
247 247
248 248 overridden_activity_ids = self.time_entry_activities.pluck(:parent_id).compact
249 249 if overridden_activity_ids.any?
250 250 scope = scope.where("#{t}.id NOT IN (?)", overridden_activity_ids)
251 251 end
252 252 unless include_inactive
253 253 scope = scope.active
254 254 end
255 255 scope
256 256 end
257 257
258 258 # Will create a new Project specific Activity or update an existing one
259 259 #
260 260 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
261 261 # does not successfully save.
262 262 def update_or_create_time_entry_activity(id, activity_hash)
263 263 if activity_hash.respond_to?(:has_key?) && activity_hash.has_key?('parent_id')
264 264 self.create_time_entry_activity_if_needed(activity_hash)
265 265 else
266 266 activity = project.time_entry_activities.find_by_id(id.to_i)
267 267 activity.update_attributes(activity_hash) if activity
268 268 end
269 269 end
270 270
271 271 # Create a new TimeEntryActivity if it overrides a system TimeEntryActivity
272 272 #
273 273 # This will raise a ActiveRecord::Rollback if the TimeEntryActivity
274 274 # does not successfully save.
275 275 def create_time_entry_activity_if_needed(activity)
276 276 if activity['parent_id']
277 277 parent_activity = TimeEntryActivity.find(activity['parent_id'])
278 278 activity['name'] = parent_activity.name
279 279 activity['position'] = parent_activity.position
280 280 if Enumeration.overriding_change?(activity, parent_activity)
281 281 project_activity = self.time_entry_activities.create(activity)
282 282 if project_activity.new_record?
283 283 raise ActiveRecord::Rollback, "Overriding TimeEntryActivity was not successfully saved"
284 284 else
285 285 self.time_entries.
286 286 where(:activity_id => parent_activity.id).
287 287 update_all(:activity_id => project_activity.id)
288 288 end
289 289 end
290 290 end
291 291 end
292 292
293 293 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
294 294 #
295 295 # Examples:
296 296 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
297 297 # project.project_condition(false) => "projects.id = 1"
298 298 def project_condition(with_subprojects)
299 299 cond = "#{Project.table_name}.id = #{id}"
300 300 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
301 301 cond
302 302 end
303 303
304 304 def self.find(*args)
305 305 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
306 306 project = find_by_identifier(*args)
307 307 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
308 308 project
309 309 else
310 310 super
311 311 end
312 312 end
313 313
314 314 def self.find_by_param(*args)
315 315 self.find(*args)
316 316 end
317 317
318 318 alias :base_reload :reload
319 319 def reload(*args)
320 320 @principals = nil
321 321 @users = nil
322 322 @shared_versions = nil
323 323 @rolled_up_versions = nil
324 324 @rolled_up_trackers = nil
325 325 @all_issue_custom_fields = nil
326 326 @all_time_entry_custom_fields = nil
327 327 @to_param = nil
328 328 @allowed_parents = nil
329 329 @allowed_permissions = nil
330 330 @actions_allowed = nil
331 331 @start_date = nil
332 332 @due_date = nil
333 333 @override_members = nil
334 334 @assignable_users = nil
335 335 base_reload(*args)
336 336 end
337 337
338 338 def to_param
339 339 if new_record?
340 340 nil
341 341 else
342 342 # id is used for projects with a numeric identifier (compatibility)
343 343 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id.to_s : identifier)
344 344 end
345 345 end
346 346
347 347 def active?
348 348 self.status == STATUS_ACTIVE
349 349 end
350 350
351 351 def archived?
352 352 self.status == STATUS_ARCHIVED
353 353 end
354 354
355 355 # Archives the project and its descendants
356 356 def archive
357 357 # Check that there is no issue of a non descendant project that is assigned
358 358 # to one of the project or descendant versions
359 359 version_ids = self_and_descendants.joins(:versions).pluck("#{Version.table_name}.id")
360 360
361 361 if version_ids.any? &&
362 362 Issue.
363 363 includes(:project).
364 364 where("#{Project.table_name}.lft < ? OR #{Project.table_name}.rgt > ?", lft, rgt).
365 365 where(:fixed_version_id => version_ids).
366 366 exists?
367 367 return false
368 368 end
369 369 Project.transaction do
370 370 archive!
371 371 end
372 372 true
373 373 end
374 374
375 375 # Unarchives the project
376 376 # All its ancestors must be active
377 377 def unarchive
378 378 return false if ancestors.detect {|a| !a.active?}
379 379 update_attribute :status, STATUS_ACTIVE
380 380 end
381 381
382 382 def close
383 383 self_and_descendants.status(STATUS_ACTIVE).update_all :status => STATUS_CLOSED
384 384 end
385 385
386 386 def reopen
387 387 self_and_descendants.status(STATUS_CLOSED).update_all :status => STATUS_ACTIVE
388 388 end
389 389
390 390 # Returns an array of projects the project can be moved to
391 391 # by the current user
392 392 def allowed_parents(user=User.current)
393 393 return @allowed_parents if @allowed_parents
394 394 @allowed_parents = Project.allowed_to(user, :add_subprojects).to_a
395 395 @allowed_parents = @allowed_parents - self_and_descendants
396 396 if user.allowed_to?(:add_project, nil, :global => true) || (!new_record? && parent.nil?)
397 397 @allowed_parents << nil
398 398 end
399 399 unless parent.nil? || @allowed_parents.empty? || @allowed_parents.include?(parent)
400 400 @allowed_parents << parent
401 401 end
402 402 @allowed_parents
403 403 end
404 404
405 405 # Sets the parent of the project with authorization check
406 406 def set_allowed_parent!(p)
407 407 ActiveSupport::Deprecation.warn "Project#set_allowed_parent! is deprecated and will be removed in Redmine 4, use #safe_attributes= instead."
408 408 p = p.id if p.is_a?(Project)
409 409 send :safe_attributes, {:project_id => p}
410 410 save
411 411 end
412 412
413 413 # Sets the parent of the project and saves the project
414 414 # Argument can be either a Project, a String, a Fixnum or nil
415 415 def set_parent!(p)
416 416 if p.is_a?(Project)
417 417 self.parent = p
418 418 else
419 419 self.parent_id = p
420 420 end
421 421 save
422 422 end
423 423
424 424 # Returns an array of the trackers used by the project and its active sub projects
425 425 def rolled_up_trackers
426 426 @rolled_up_trackers ||=
427 427 Tracker.
428 joins(:projects).
429 joins("JOIN #{EnabledModule.table_name} ON #{EnabledModule.table_name}.project_id = #{Project.table_name}.id AND #{EnabledModule.table_name}.name = 'issue_tracking'").
428 joins(projects: :enabled_modules).
430 429 where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> ?", lft, rgt, STATUS_ARCHIVED).
430 where("#{EnabledModule.table_name}.name = ?", 'issue_tracking').
431 431 uniq.
432 432 sorted.
433 433 to_a
434 434 end
435 435
436 436 # Closes open and locked project versions that are completed
437 437 def close_completed_versions
438 438 Version.transaction do
439 439 versions.where(:status => %w(open locked)).each do |version|
440 440 if version.completed?
441 441 version.update_attribute(:status, 'closed')
442 442 end
443 443 end
444 444 end
445 445 end
446 446
447 447 # Returns a scope of the Versions on subprojects
448 448 def rolled_up_versions
449 449 @rolled_up_versions ||=
450 450 Version.
451 451 joins(:project).
452 452 where("#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status <> ?", lft, rgt, STATUS_ARCHIVED)
453 453 end
454 454
455 455 # Returns a scope of the Versions used by the project
456 456 def shared_versions
457 457 if new_record?
458 458 Version.
459 459 joins(:project).
460 460 preload(:project).
461 461 where("#{Project.table_name}.status <> ? AND #{Version.table_name}.sharing = 'system'", STATUS_ARCHIVED)
462 462 else
463 463 @shared_versions ||= begin
464 464 r = root? ? self : root
465 465 Version.
466 466 joins(:project).
467 467 preload(:project).
468 468 where("#{Project.table_name}.id = #{id}" +
469 469 " OR (#{Project.table_name}.status <> #{Project::STATUS_ARCHIVED} AND (" +
470 470 " #{Version.table_name}.sharing = 'system'" +
471 471 " OR (#{Project.table_name}.lft >= #{r.lft} AND #{Project.table_name}.rgt <= #{r.rgt} AND #{Version.table_name}.sharing = 'tree')" +
472 472 " OR (#{Project.table_name}.lft < #{lft} AND #{Project.table_name}.rgt > #{rgt} AND #{Version.table_name}.sharing IN ('hierarchy', 'descendants'))" +
473 473 " OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt} AND #{Version.table_name}.sharing = 'hierarchy')" +
474 474 "))")
475 475 end
476 476 end
477 477 end
478 478
479 479 # Returns a hash of project users grouped by role
480 480 def users_by_role
481 481 members.includes(:user, :roles).inject({}) do |h, m|
482 482 m.roles.each do |r|
483 483 h[r] ||= []
484 484 h[r] << m.user
485 485 end
486 486 h
487 487 end
488 488 end
489 489
490 490 # Adds user as a project member with the default role
491 491 # Used for when a non-admin user creates a project
492 492 def add_default_member(user)
493 493 role = Role.givable.find_by_id(Setting.new_project_user_role_id.to_i) || Role.givable.first
494 494 member = Member.new(:project => self, :principal => user, :roles => [role])
495 495 self.members << member
496 496 member
497 497 end
498 498
499 499 # Deletes all project's members
500 500 def delete_all_members
501 501 me, mr = Member.table_name, MemberRole.table_name
502 502 self.class.connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})")
503 503 Member.delete_all(['project_id = ?', id])
504 504 end
505 505
506 506 # Return a Principal scope of users/groups issues can be assigned to
507 507 def assignable_users
508 508 types = ['User']
509 509 types << 'Group' if Setting.issue_group_assignment?
510 510
511 511 @assignable_users ||= Principal.
512 512 active.
513 513 joins(:members => :roles).
514 514 where(:type => types, :members => {:project_id => id}, :roles => {:assignable => true}).
515 515 uniq.
516 516 sorted
517 517 end
518 518
519 519 # Returns the mail addresses of users that should be always notified on project events
520 520 def recipients
521 521 notified_users.collect {|user| user.mail}
522 522 end
523 523
524 524 # Returns the users that should be notified on project events
525 525 def notified_users
526 526 # TODO: User part should be extracted to User#notify_about?
527 527 members.select {|m| m.principal.present? && (m.mail_notification? || m.principal.mail_notification == 'all')}.collect {|m| m.principal}
528 528 end
529 529
530 530 # Returns a scope of all custom fields enabled for project issues
531 531 # (explicitly associated custom fields and custom fields enabled for all projects)
532 532 def all_issue_custom_fields
533 533 if new_record?
534 534 @all_issue_custom_fields ||= IssueCustomField.
535 535 sorted.
536 536 where("is_for_all = ? OR id IN (?)", true, issue_custom_field_ids)
537 537 else
538 538 @all_issue_custom_fields ||= IssueCustomField.
539 539 sorted.
540 540 where("is_for_all = ? OR id IN (SELECT DISTINCT cfp.custom_field_id" +
541 541 " FROM #{table_name_prefix}custom_fields_projects#{table_name_suffix} cfp" +
542 542 " WHERE cfp.project_id = ?)", true, id)
543 543 end
544 544 end
545 545
546 546 def project
547 547 self
548 548 end
549 549
550 550 def <=>(project)
551 551 name.casecmp(project.name)
552 552 end
553 553
554 554 def to_s
555 555 name
556 556 end
557 557
558 558 # Returns a short description of the projects (first lines)
559 559 def short_description(length = 255)
560 560 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
561 561 end
562 562
563 563 def css_classes
564 564 s = 'project'
565 565 s << ' root' if root?
566 566 s << ' child' if child?
567 567 s << (leaf? ? ' leaf' : ' parent')
568 568 unless active?
569 569 if archived?
570 570 s << ' archived'
571 571 else
572 572 s << ' closed'
573 573 end
574 574 end
575 575 s
576 576 end
577 577
578 578 # The earliest start date of a project, based on it's issues and versions
579 579 def start_date
580 580 @start_date ||= [
581 581 issues.minimum('start_date'),
582 582 shared_versions.minimum('effective_date'),
583 583 Issue.fixed_version(shared_versions).minimum('start_date')
584 584 ].compact.min
585 585 end
586 586
587 587 # The latest due date of an issue or version
588 588 def due_date
589 589 @due_date ||= [
590 590 issues.maximum('due_date'),
591 591 shared_versions.maximum('effective_date'),
592 592 Issue.fixed_version(shared_versions).maximum('due_date')
593 593 ].compact.max
594 594 end
595 595
596 596 def overdue?
597 597 active? && !due_date.nil? && (due_date < Date.today)
598 598 end
599 599
600 600 # Returns the percent completed for this project, based on the
601 601 # progress on it's versions.
602 602 def completed_percent(options={:include_subprojects => false})
603 603 if options.delete(:include_subprojects)
604 604 total = self_and_descendants.collect(&:completed_percent).sum
605 605
606 606 total / self_and_descendants.count
607 607 else
608 608 if versions.count > 0
609 609 total = versions.collect(&:completed_percent).sum
610 610
611 611 total / versions.count
612 612 else
613 613 100
614 614 end
615 615 end
616 616 end
617 617
618 618 # Return true if this project allows to do the specified action.
619 619 # action can be:
620 620 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
621 621 # * a permission Symbol (eg. :edit_project)
622 622 def allows_to?(action)
623 623 if archived?
624 624 # No action allowed on archived projects
625 625 return false
626 626 end
627 627 unless active? || Redmine::AccessControl.read_action?(action)
628 628 # No write action allowed on closed projects
629 629 return false
630 630 end
631 631 # No action allowed on disabled modules
632 632 if action.is_a? Hash
633 633 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
634 634 else
635 635 allowed_permissions.include? action
636 636 end
637 637 end
638 638
639 639 # Return the enabled module with the given name
640 640 # or nil if the module is not enabled for the project
641 641 def enabled_module(name)
642 642 name = name.to_s
643 643 enabled_modules.detect {|m| m.name == name}
644 644 end
645 645
646 646 # Return true if the module with the given name is enabled
647 647 def module_enabled?(name)
648 648 enabled_module(name).present?
649 649 end
650 650
651 651 def enabled_module_names=(module_names)
652 652 if module_names && module_names.is_a?(Array)
653 653 module_names = module_names.collect(&:to_s).reject(&:blank?)
654 654 self.enabled_modules = module_names.collect {|name| enabled_modules.detect {|mod| mod.name == name} || EnabledModule.new(:name => name)}
655 655 else
656 656 enabled_modules.clear
657 657 end
658 658 end
659 659
660 660 # Returns an array of the enabled modules names
661 661 def enabled_module_names
662 662 enabled_modules.collect(&:name)
663 663 end
664 664
665 665 # Enable a specific module
666 666 #
667 667 # Examples:
668 668 # project.enable_module!(:issue_tracking)
669 669 # project.enable_module!("issue_tracking")
670 670 def enable_module!(name)
671 671 enabled_modules << EnabledModule.new(:name => name.to_s) unless module_enabled?(name)
672 672 end
673 673
674 674 # Disable a module if it exists
675 675 #
676 676 # Examples:
677 677 # project.disable_module!(:issue_tracking)
678 678 # project.disable_module!("issue_tracking")
679 679 # project.disable_module!(project.enabled_modules.first)
680 680 def disable_module!(target)
681 681 target = enabled_modules.detect{|mod| target.to_s == mod.name} unless enabled_modules.include?(target)
682 682 target.destroy unless target.blank?
683 683 end
684 684
685 685 safe_attributes 'name',
686 686 'description',
687 687 'homepage',
688 688 'is_public',
689 689 'identifier',
690 690 'custom_field_values',
691 691 'custom_fields',
692 692 'tracker_ids',
693 693 'issue_custom_field_ids',
694 694 'parent_id',
695 695 'default_version_id'
696 696
697 697 safe_attributes 'enabled_module_names',
698 698 :if => lambda {|project, user| project.new_record? || user.allowed_to?(:select_project_modules, project) }
699 699
700 700 safe_attributes 'inherit_members',
701 701 :if => lambda {|project, user| project.parent.nil? || project.parent.visible?(user)}
702 702
703 703 def safe_attributes=(attrs, user=User.current)
704 704 return unless attrs.is_a?(Hash)
705 705 attrs = attrs.deep_dup
706 706
707 707 @unallowed_parent_id = nil
708 708 if new_record? || attrs.key?('parent_id')
709 709 parent_id_param = attrs['parent_id'].to_s
710 710 if new_record? || parent_id_param != parent_id.to_s
711 711 p = parent_id_param.present? ? Project.find_by_id(parent_id_param) : nil
712 712 unless allowed_parents(user).include?(p)
713 713 attrs.delete('parent_id')
714 714 @unallowed_parent_id = true
715 715 end
716 716 end
717 717 end
718 718
719 719 super(attrs, user)
720 720 end
721 721
722 722 # Returns an auto-generated project identifier based on the last identifier used
723 723 def self.next_identifier
724 724 p = Project.order('id DESC').first
725 725 p.nil? ? nil : p.identifier.to_s.succ
726 726 end
727 727
728 728 # Copies and saves the Project instance based on the +project+.
729 729 # Duplicates the source project's:
730 730 # * Wiki
731 731 # * Versions
732 732 # * Categories
733 733 # * Issues
734 734 # * Members
735 735 # * Queries
736 736 #
737 737 # Accepts an +options+ argument to specify what to copy
738 738 #
739 739 # Examples:
740 740 # project.copy(1) # => copies everything
741 741 # project.copy(1, :only => 'members') # => copies members only
742 742 # project.copy(1, :only => ['members', 'versions']) # => copies members and versions
743 743 def copy(project, options={})
744 744 project = project.is_a?(Project) ? project : Project.find(project)
745 745
746 746 to_be_copied = %w(wiki versions issue_categories issues members queries boards)
747 747 to_be_copied = to_be_copied & Array.wrap(options[:only]) unless options[:only].nil?
748 748
749 749 Project.transaction do
750 750 if save
751 751 reload
752 752 to_be_copied.each do |name|
753 753 send "copy_#{name}", project
754 754 end
755 755 Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
756 756 save
757 757 else
758 758 false
759 759 end
760 760 end
761 761 end
762 762
763 763 def member_principals
764 764 ActiveSupport::Deprecation.warn "Project#member_principals is deprecated and will be removed in Redmine 4.0. Use #memberships.active instead."
765 765 memberships.active
766 766 end
767 767
768 768 # Returns a new unsaved Project instance with attributes copied from +project+
769 769 def self.copy_from(project)
770 770 project = project.is_a?(Project) ? project : Project.find(project)
771 771 # clear unique attributes
772 772 attributes = project.attributes.dup.except('id', 'name', 'identifier', 'status', 'parent_id', 'lft', 'rgt')
773 773 copy = Project.new(attributes)
774 774 copy.enabled_module_names = project.enabled_module_names
775 775 copy.trackers = project.trackers
776 776 copy.custom_values = project.custom_values.collect {|v| v.clone}
777 777 copy.issue_custom_fields = project.issue_custom_fields
778 778 copy
779 779 end
780 780
781 781 # Yields the given block for each project with its level in the tree
782 782 def self.project_tree(projects, &block)
783 783 ancestors = []
784 784 projects.sort_by(&:lft).each do |project|
785 785 while (ancestors.any? && !project.is_descendant_of?(ancestors.last))
786 786 ancestors.pop
787 787 end
788 788 yield project, ancestors.size
789 789 ancestors << project
790 790 end
791 791 end
792 792
793 793 private
794 794
795 795 def update_inherited_members
796 796 if parent
797 797 if inherit_members? && !inherit_members_was
798 798 remove_inherited_member_roles
799 799 add_inherited_member_roles
800 800 elsif !inherit_members? && inherit_members_was
801 801 remove_inherited_member_roles
802 802 end
803 803 end
804 804 end
805 805
806 806 def remove_inherited_member_roles
807 807 member_roles = memberships.map(&:member_roles).flatten
808 808 member_role_ids = member_roles.map(&:id)
809 809 member_roles.each do |member_role|
810 810 if member_role.inherited_from && !member_role_ids.include?(member_role.inherited_from)
811 811 member_role.destroy
812 812 end
813 813 end
814 814 end
815 815
816 816 def add_inherited_member_roles
817 817 if inherit_members? && parent
818 818 parent.memberships.each do |parent_member|
819 819 member = Member.find_or_new(self.id, parent_member.user_id)
820 820 parent_member.member_roles.each do |parent_member_role|
821 821 member.member_roles << MemberRole.new(:role => parent_member_role.role, :inherited_from => parent_member_role.id)
822 822 end
823 823 member.save!
824 824 end
825 825 memberships.reset
826 826 end
827 827 end
828 828
829 829 def update_versions_from_hierarchy_change
830 830 Issue.update_versions_from_hierarchy_change(self)
831 831 end
832 832
833 833 def validate_parent
834 834 if @unallowed_parent_id
835 835 errors.add(:parent_id, :invalid)
836 836 elsif parent_id_changed?
837 837 unless parent.nil? || (parent.active? && move_possible?(parent))
838 838 errors.add(:parent_id, :invalid)
839 839 end
840 840 end
841 841 end
842 842
843 843 # Copies wiki from +project+
844 844 def copy_wiki(project)
845 845 # Check that the source project has a wiki first
846 846 unless project.wiki.nil?
847 847 wiki = self.wiki || Wiki.new
848 848 wiki.attributes = project.wiki.attributes.dup.except("id", "project_id")
849 849 wiki_pages_map = {}
850 850 project.wiki.pages.each do |page|
851 851 # Skip pages without content
852 852 next if page.content.nil?
853 853 new_wiki_content = WikiContent.new(page.content.attributes.dup.except("id", "page_id", "updated_on"))
854 854 new_wiki_page = WikiPage.new(page.attributes.dup.except("id", "wiki_id", "created_on", "parent_id"))
855 855 new_wiki_page.content = new_wiki_content
856 856 wiki.pages << new_wiki_page
857 857 wiki_pages_map[page.id] = new_wiki_page
858 858 end
859 859
860 860 self.wiki = wiki
861 861 wiki.save
862 862 # Reproduce page hierarchy
863 863 project.wiki.pages.each do |page|
864 864 if page.parent_id && wiki_pages_map[page.id]
865 865 wiki_pages_map[page.id].parent = wiki_pages_map[page.parent_id]
866 866 wiki_pages_map[page.id].save
867 867 end
868 868 end
869 869 end
870 870 end
871 871
872 872 # Copies versions from +project+
873 873 def copy_versions(project)
874 874 project.versions.each do |version|
875 875 new_version = Version.new
876 876 new_version.attributes = version.attributes.dup.except("id", "project_id", "created_on", "updated_on")
877 877 self.versions << new_version
878 878 end
879 879 end
880 880
881 881 # Copies issue categories from +project+
882 882 def copy_issue_categories(project)
883 883 project.issue_categories.each do |issue_category|
884 884 new_issue_category = IssueCategory.new
885 885 new_issue_category.attributes = issue_category.attributes.dup.except("id", "project_id")
886 886 self.issue_categories << new_issue_category
887 887 end
888 888 end
889 889
890 890 # Copies issues from +project+
891 891 def copy_issues(project)
892 892 # Stores the source issue id as a key and the copied issues as the
893 893 # value. Used to map the two together for issue relations.
894 894 issues_map = {}
895 895
896 896 # Store status and reopen locked/closed versions
897 897 version_statuses = versions.reject(&:open?).map {|version| [version, version.status]}
898 898 version_statuses.each do |version, status|
899 899 version.update_attribute :status, 'open'
900 900 end
901 901
902 902 # Get issues sorted by root_id, lft so that parent issues
903 903 # get copied before their children
904 904 project.issues.reorder('root_id, lft').each do |issue|
905 905 new_issue = Issue.new
906 906 new_issue.copy_from(issue, :subtasks => false, :link => false)
907 907 new_issue.project = self
908 908 # Changing project resets the custom field values
909 909 # TODO: handle this in Issue#project=
910 910 new_issue.custom_field_values = issue.custom_field_values.inject({}) {|h,v| h[v.custom_field_id] = v.value; h}
911 911 # Reassign fixed_versions by name, since names are unique per project
912 912 if issue.fixed_version && issue.fixed_version.project == project
913 913 new_issue.fixed_version = self.versions.detect {|v| v.name == issue.fixed_version.name}
914 914 end
915 915 # Reassign version custom field values
916 916 new_issue.custom_field_values.each do |custom_value|
917 917 if custom_value.custom_field.field_format == 'version' && custom_value.value.present?
918 918 versions = Version.where(:id => custom_value.value).to_a
919 919 new_value = versions.map do |version|
920 920 if version.project == project
921 921 self.versions.detect {|v| v.name == version.name}.try(:id)
922 922 else
923 923 version.id
924 924 end
925 925 end
926 926 new_value.compact!
927 927 new_value = new_value.first unless custom_value.custom_field.multiple?
928 928 custom_value.value = new_value
929 929 end
930 930 end
931 931 # Reassign the category by name, since names are unique per project
932 932 if issue.category
933 933 new_issue.category = self.issue_categories.detect {|c| c.name == issue.category.name}
934 934 end
935 935 # Parent issue
936 936 if issue.parent_id
937 937 if copied_parent = issues_map[issue.parent_id]
938 938 new_issue.parent_issue_id = copied_parent.id
939 939 end
940 940 end
941 941
942 942 self.issues << new_issue
943 943 if new_issue.new_record?
944 944 logger.info "Project#copy_issues: issue ##{issue.id} could not be copied: #{new_issue.errors.full_messages}" if logger && logger.info?
945 945 else
946 946 issues_map[issue.id] = new_issue unless new_issue.new_record?
947 947 end
948 948 end
949 949
950 950 # Restore locked/closed version statuses
951 951 version_statuses.each do |version, status|
952 952 version.update_attribute :status, status
953 953 end
954 954
955 955 # Relations after in case issues related each other
956 956 project.issues.each do |issue|
957 957 new_issue = issues_map[issue.id]
958 958 unless new_issue
959 959 # Issue was not copied
960 960 next
961 961 end
962 962
963 963 # Relations
964 964 issue.relations_from.each do |source_relation|
965 965 new_issue_relation = IssueRelation.new
966 966 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
967 967 new_issue_relation.issue_to = issues_map[source_relation.issue_to_id]
968 968 if new_issue_relation.issue_to.nil? && Setting.cross_project_issue_relations?
969 969 new_issue_relation.issue_to = source_relation.issue_to
970 970 end
971 971 new_issue.relations_from << new_issue_relation
972 972 end
973 973
974 974 issue.relations_to.each do |source_relation|
975 975 new_issue_relation = IssueRelation.new
976 976 new_issue_relation.attributes = source_relation.attributes.dup.except("id", "issue_from_id", "issue_to_id")
977 977 new_issue_relation.issue_from = issues_map[source_relation.issue_from_id]
978 978 if new_issue_relation.issue_from.nil? && Setting.cross_project_issue_relations?
979 979 new_issue_relation.issue_from = source_relation.issue_from
980 980 end
981 981 new_issue.relations_to << new_issue_relation
982 982 end
983 983 end
984 984 end
985 985
986 986 # Copies members from +project+
987 987 def copy_members(project)
988 988 # Copy users first, then groups to handle members with inherited and given roles
989 989 members_to_copy = []
990 990 members_to_copy += project.memberships.select {|m| m.principal.is_a?(User)}
991 991 members_to_copy += project.memberships.select {|m| !m.principal.is_a?(User)}
992 992
993 993 members_to_copy.each do |member|
994 994 new_member = Member.new
995 995 new_member.attributes = member.attributes.dup.except("id", "project_id", "created_on")
996 996 # only copy non inherited roles
997 997 # inherited roles will be added when copying the group membership
998 998 role_ids = member.member_roles.reject(&:inherited?).collect(&:role_id)
999 999 next if role_ids.empty?
1000 1000 new_member.role_ids = role_ids
1001 1001 new_member.project = self
1002 1002 self.members << new_member
1003 1003 end
1004 1004 end
1005 1005
1006 1006 # Copies queries from +project+
1007 1007 def copy_queries(project)
1008 1008 project.queries.each do |query|
1009 1009 new_query = IssueQuery.new
1010 1010 new_query.attributes = query.attributes.dup.except("id", "project_id", "sort_criteria", "user_id", "type")
1011 1011 new_query.sort_criteria = query.sort_criteria if query.sort_criteria
1012 1012 new_query.project = self
1013 1013 new_query.user_id = query.user_id
1014 1014 new_query.role_ids = query.role_ids if query.visibility == IssueQuery::VISIBILITY_ROLES
1015 1015 self.queries << new_query
1016 1016 end
1017 1017 end
1018 1018
1019 1019 # Copies boards from +project+
1020 1020 def copy_boards(project)
1021 1021 project.boards.each do |board|
1022 1022 new_board = Board.new
1023 1023 new_board.attributes = board.attributes.dup.except("id", "project_id", "topics_count", "messages_count", "last_message_id")
1024 1024 new_board.project = self
1025 1025 self.boards << new_board
1026 1026 end
1027 1027 end
1028 1028
1029 1029 def allowed_permissions
1030 1030 @allowed_permissions ||= begin
1031 1031 module_names = enabled_modules.loaded? ? enabled_modules.map(&:name) : enabled_modules.pluck(:name)
1032 1032 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
1033 1033 end
1034 1034 end
1035 1035
1036 1036 def allowed_actions
1037 1037 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
1038 1038 end
1039 1039
1040 1040 # Archives subprojects recursively
1041 1041 def archive!
1042 1042 children.each do |subproject|
1043 1043 subproject.send :archive!
1044 1044 end
1045 1045 update_attribute :status, STATUS_ARCHIVED
1046 1046 end
1047 1047 end
General Comments 0
You need to be logged in to leave comments. Login now