You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

project.rb 40KB

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