##// END OF EJS Templates
Slight change to the visibility SQL statement....
Jean-Philippe Lang -
r2764:041277235bd2
parent child
Show More
@@ -1,414 +1,414
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Project < ActiveRecord::Base
18 class Project < ActiveRecord::Base
19 # Project statuses
19 # Project statuses
20 STATUS_ACTIVE = 1
20 STATUS_ACTIVE = 1
21 STATUS_ARCHIVED = 9
21 STATUS_ARCHIVED = 9
22
22
23 has_many :members, :include => :user, :conditions => "#{User.table_name}.type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}"
23 has_many :members, :include => :user, :conditions => "#{User.table_name}.type='User' AND #{User.table_name}.status=#{User::STATUS_ACTIVE}"
24 has_many :member_principals, :class_name => 'Member',
24 has_many :member_principals, :class_name => 'Member',
25 :include => :principal,
25 :include => :principal,
26 :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{User::STATUS_ACTIVE})"
26 :conditions => "#{Principal.table_name}.type='Group' OR (#{Principal.table_name}.type='User' AND #{Principal.table_name}.status=#{User::STATUS_ACTIVE})"
27 has_many :users, :through => :members
27 has_many :users, :through => :members
28 has_many :principals, :through => :member_principals, :source => :principal
28 has_many :principals, :through => :member_principals, :source => :principal
29
29
30 has_many :enabled_modules, :dependent => :delete_all
30 has_many :enabled_modules, :dependent => :delete_all
31 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
31 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
32 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
32 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
33 has_many :issue_changes, :through => :issues, :source => :journals
33 has_many :issue_changes, :through => :issues, :source => :journals
34 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
34 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
35 has_many :time_entries, :dependent => :delete_all
35 has_many :time_entries, :dependent => :delete_all
36 has_many :queries, :dependent => :delete_all
36 has_many :queries, :dependent => :delete_all
37 has_many :documents, :dependent => :destroy
37 has_many :documents, :dependent => :destroy
38 has_many :news, :dependent => :delete_all, :include => :author
38 has_many :news, :dependent => :delete_all, :include => :author
39 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
39 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
40 has_many :boards, :dependent => :destroy, :order => "position ASC"
40 has_many :boards, :dependent => :destroy, :order => "position ASC"
41 has_one :repository, :dependent => :destroy
41 has_one :repository, :dependent => :destroy
42 has_many :changesets, :through => :repository
42 has_many :changesets, :through => :repository
43 has_one :wiki, :dependent => :destroy
43 has_one :wiki, :dependent => :destroy
44 # Custom field for the project issues
44 # Custom field for the project issues
45 has_and_belongs_to_many :issue_custom_fields,
45 has_and_belongs_to_many :issue_custom_fields,
46 :class_name => 'IssueCustomField',
46 :class_name => 'IssueCustomField',
47 :order => "#{CustomField.table_name}.position",
47 :order => "#{CustomField.table_name}.position",
48 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
48 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
49 :association_foreign_key => 'custom_field_id'
49 :association_foreign_key => 'custom_field_id'
50
50
51 acts_as_nested_set :order => 'name', :dependent => :destroy
51 acts_as_nested_set :order => 'name', :dependent => :destroy
52 acts_as_attachable :view_permission => :view_files,
52 acts_as_attachable :view_permission => :view_files,
53 :delete_permission => :manage_files
53 :delete_permission => :manage_files
54
54
55 acts_as_customizable
55 acts_as_customizable
56 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil
56 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil
57 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
57 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
58 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}},
58 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}},
59 :author => nil
59 :author => nil
60
60
61 attr_protected :status, :enabled_module_names
61 attr_protected :status, :enabled_module_names
62
62
63 validates_presence_of :name, :identifier
63 validates_presence_of :name, :identifier
64 validates_uniqueness_of :name, :identifier
64 validates_uniqueness_of :name, :identifier
65 validates_associated :repository, :wiki
65 validates_associated :repository, :wiki
66 validates_length_of :name, :maximum => 30
66 validates_length_of :name, :maximum => 30
67 validates_length_of :homepage, :maximum => 255
67 validates_length_of :homepage, :maximum => 255
68 validates_length_of :identifier, :in => 1..20
68 validates_length_of :identifier, :in => 1..20
69 # donwcase letters, digits, dashes but not digits only
69 # donwcase letters, digits, dashes but not digits only
70 validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? }
70 validates_format_of :identifier, :with => /^(?!\d+$)[a-z0-9\-]*$/, :if => Proc.new { |p| p.identifier_changed? }
71 # reserved words
71 # reserved words
72 validates_exclusion_of :identifier, :in => %w( new )
72 validates_exclusion_of :identifier, :in => %w( new )
73
73
74 before_destroy :delete_all_members
74 before_destroy :delete_all_members
75
75
76 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] } }
76 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] } }
77 named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
77 named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
78 named_scope :public, { :conditions => { :is_public => true } }
78 named_scope :public, { :conditions => { :is_public => true } }
79 named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } }
79 named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } }
80
80
81 def identifier=(identifier)
81 def identifier=(identifier)
82 super unless identifier_frozen?
82 super unless identifier_frozen?
83 end
83 end
84
84
85 def identifier_frozen?
85 def identifier_frozen?
86 errors[:identifier].nil? && !(new_record? || identifier.blank?)
86 errors[:identifier].nil? && !(new_record? || identifier.blank?)
87 end
87 end
88
88
89 def issues_with_subprojects(include_subprojects=false)
89 def issues_with_subprojects(include_subprojects=false)
90 conditions = nil
90 conditions = nil
91 if include_subprojects
91 if include_subprojects
92 ids = [id] + descendants.collect(&:id)
92 ids = [id] + descendants.collect(&:id)
93 conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"]
93 conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"]
94 end
94 end
95 conditions ||= ["#{Project.table_name}.id = ?", id]
95 conditions ||= ["#{Project.table_name}.id = ?", id]
96 # Quick and dirty fix for Rails 2 compatibility
96 # Quick and dirty fix for Rails 2 compatibility
97 Issue.send(:with_scope, :find => { :conditions => conditions }) do
97 Issue.send(:with_scope, :find => { :conditions => conditions }) do
98 Version.send(:with_scope, :find => { :conditions => conditions }) do
98 Version.send(:with_scope, :find => { :conditions => conditions }) do
99 yield
99 yield
100 end
100 end
101 end
101 end
102 end
102 end
103
103
104 # returns latest created projects
104 # returns latest created projects
105 # non public projects will be returned only if user is a member of those
105 # non public projects will be returned only if user is a member of those
106 def self.latest(user=nil, count=5)
106 def self.latest(user=nil, count=5)
107 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
107 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
108 end
108 end
109
109
110 # Returns a SQL :conditions string used to find all active projects for the specified user.
110 # Returns a SQL :conditions string used to find all active projects for the specified user.
111 #
111 #
112 # Examples:
112 # Examples:
113 # Projects.visible_by(admin) => "projects.status = 1"
113 # Projects.visible_by(admin) => "projects.status = 1"
114 # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1"
114 # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1"
115 def self.visible_by(user=nil)
115 def self.visible_by(user=nil)
116 user ||= User.current
116 user ||= User.current
117 if user && user.admin?
117 if user && user.admin?
118 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
118 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
119 elsif user && user.memberships.any?
119 elsif user && user.memberships.any?
120 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))"
120 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))"
121 else
121 else
122 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
122 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
123 end
123 end
124 end
124 end
125
125
126 def self.allowed_to_condition(user, permission, options={})
126 def self.allowed_to_condition(user, permission, options={})
127 statements = []
127 statements = []
128 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
128 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
129 if perm = Redmine::AccessControl.permission(permission)
129 if perm = Redmine::AccessControl.permission(permission)
130 unless perm.project_module.nil?
130 unless perm.project_module.nil?
131 # If the permission belongs to a project module, make sure the module is enabled
131 # If the permission belongs to a project module, make sure the module is enabled
132 base_statement << " AND EXISTS (SELECT em.id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}' AND em.project_id=#{Project.table_name}.id)"
132 base_statement << " AND #{Project.table_name}.id IN (SELECT em.project_id FROM #{EnabledModule.table_name} em WHERE em.name='#{perm.project_module}')"
133 end
133 end
134 end
134 end
135 if options[:project]
135 if options[:project]
136 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
136 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
137 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
137 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
138 base_statement = "(#{project_statement}) AND (#{base_statement})"
138 base_statement = "(#{project_statement}) AND (#{base_statement})"
139 end
139 end
140 if user.admin?
140 if user.admin?
141 # no restriction
141 # no restriction
142 else
142 else
143 statements << "1=0"
143 statements << "1=0"
144 if user.logged?
144 if user.logged?
145 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
145 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
146 allowed_project_ids = user.memberships.select {|m| m.roles.detect {|role| role.allowed_to?(permission)}}.collect {|m| m.project_id}
146 allowed_project_ids = user.memberships.select {|m| m.roles.detect {|role| role.allowed_to?(permission)}}.collect {|m| m.project_id}
147 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
147 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
148 elsif Role.anonymous.allowed_to?(permission)
148 elsif Role.anonymous.allowed_to?(permission)
149 # anonymous user allowed on public project
149 # anonymous user allowed on public project
150 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
150 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
151 else
151 else
152 # anonymous user is not authorized
152 # anonymous user is not authorized
153 end
153 end
154 end
154 end
155 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
155 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
156 end
156 end
157
157
158 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
158 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
159 #
159 #
160 # Examples:
160 # Examples:
161 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
161 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
162 # project.project_condition(false) => "projects.id = 1"
162 # project.project_condition(false) => "projects.id = 1"
163 def project_condition(with_subprojects)
163 def project_condition(with_subprojects)
164 cond = "#{Project.table_name}.id = #{id}"
164 cond = "#{Project.table_name}.id = #{id}"
165 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
165 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
166 cond
166 cond
167 end
167 end
168
168
169 def self.find(*args)
169 def self.find(*args)
170 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
170 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
171 project = find_by_identifier(*args)
171 project = find_by_identifier(*args)
172 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
172 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
173 project
173 project
174 else
174 else
175 super
175 super
176 end
176 end
177 end
177 end
178
178
179 def to_param
179 def to_param
180 # id is used for projects with a numeric identifier (compatibility)
180 # id is used for projects with a numeric identifier (compatibility)
181 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
181 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
182 end
182 end
183
183
184 def active?
184 def active?
185 self.status == STATUS_ACTIVE
185 self.status == STATUS_ACTIVE
186 end
186 end
187
187
188 # Archives the project and its descendants recursively
188 # Archives the project and its descendants recursively
189 def archive
189 def archive
190 # Archive subprojects if any
190 # Archive subprojects if any
191 children.each do |subproject|
191 children.each do |subproject|
192 subproject.archive
192 subproject.archive
193 end
193 end
194 update_attribute :status, STATUS_ARCHIVED
194 update_attribute :status, STATUS_ARCHIVED
195 end
195 end
196
196
197 # Unarchives the project
197 # Unarchives the project
198 # All its ancestors must be active
198 # All its ancestors must be active
199 def unarchive
199 def unarchive
200 return false if ancestors.detect {|a| !a.active?}
200 return false if ancestors.detect {|a| !a.active?}
201 update_attribute :status, STATUS_ACTIVE
201 update_attribute :status, STATUS_ACTIVE
202 end
202 end
203
203
204 # Returns an array of projects the project can be moved to
204 # Returns an array of projects the project can be moved to
205 def possible_parents
205 def possible_parents
206 @possible_parents ||= (Project.active.find(:all) - self_and_descendants)
206 @possible_parents ||= (Project.active.find(:all) - self_and_descendants)
207 end
207 end
208
208
209 # Sets the parent of the project
209 # Sets the parent of the project
210 # Argument can be either a Project, a String, a Fixnum or nil
210 # Argument can be either a Project, a String, a Fixnum or nil
211 def set_parent!(p)
211 def set_parent!(p)
212 unless p.nil? || p.is_a?(Project)
212 unless p.nil? || p.is_a?(Project)
213 if p.to_s.blank?
213 if p.to_s.blank?
214 p = nil
214 p = nil
215 else
215 else
216 p = Project.find_by_id(p)
216 p = Project.find_by_id(p)
217 return false unless p
217 return false unless p
218 end
218 end
219 end
219 end
220 if p == parent && !p.nil?
220 if p == parent && !p.nil?
221 # Nothing to do
221 # Nothing to do
222 true
222 true
223 elsif p.nil? || (p.active? && move_possible?(p))
223 elsif p.nil? || (p.active? && move_possible?(p))
224 # Insert the project so that target's children or root projects stay alphabetically sorted
224 # Insert the project so that target's children or root projects stay alphabetically sorted
225 sibs = (p.nil? ? self.class.roots : p.children)
225 sibs = (p.nil? ? self.class.roots : p.children)
226 to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
226 to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
227 if to_be_inserted_before
227 if to_be_inserted_before
228 move_to_left_of(to_be_inserted_before)
228 move_to_left_of(to_be_inserted_before)
229 elsif p.nil?
229 elsif p.nil?
230 if sibs.empty?
230 if sibs.empty?
231 # move_to_root adds the project in first (ie. left) position
231 # move_to_root adds the project in first (ie. left) position
232 move_to_root
232 move_to_root
233 else
233 else
234 move_to_right_of(sibs.last) unless self == sibs.last
234 move_to_right_of(sibs.last) unless self == sibs.last
235 end
235 end
236 else
236 else
237 # move_to_child_of adds the project in last (ie.right) position
237 # move_to_child_of adds the project in last (ie.right) position
238 move_to_child_of(p)
238 move_to_child_of(p)
239 end
239 end
240 true
240 true
241 else
241 else
242 # Can not move to the given target
242 # Can not move to the given target
243 false
243 false
244 end
244 end
245 end
245 end
246
246
247 # Returns an array of the trackers used by the project and its active sub projects
247 # Returns an array of the trackers used by the project and its active sub projects
248 def rolled_up_trackers
248 def rolled_up_trackers
249 @rolled_up_trackers ||=
249 @rolled_up_trackers ||=
250 Tracker.find(:all, :include => :projects,
250 Tracker.find(:all, :include => :projects,
251 :select => "DISTINCT #{Tracker.table_name}.*",
251 :select => "DISTINCT #{Tracker.table_name}.*",
252 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt],
252 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt],
253 :order => "#{Tracker.table_name}.position")
253 :order => "#{Tracker.table_name}.position")
254 end
254 end
255
255
256 # Returns a hash of project users grouped by role
256 # Returns a hash of project users grouped by role
257 def users_by_role
257 def users_by_role
258 members.find(:all, :include => [:user, :roles]).inject({}) do |h, m|
258 members.find(:all, :include => [:user, :roles]).inject({}) do |h, m|
259 m.roles.each do |r|
259 m.roles.each do |r|
260 h[r] ||= []
260 h[r] ||= []
261 h[r] << m.user
261 h[r] << m.user
262 end
262 end
263 h
263 h
264 end
264 end
265 end
265 end
266
266
267 # Deletes all project's members
267 # Deletes all project's members
268 def delete_all_members
268 def delete_all_members
269 me, mr = Member.table_name, MemberRole.table_name
269 me, mr = Member.table_name, MemberRole.table_name
270 connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})")
270 connection.delete("DELETE FROM #{mr} WHERE #{mr}.member_id IN (SELECT #{me}.id FROM #{me} WHERE #{me}.project_id = #{id})")
271 Member.delete_all(['project_id = ?', id])
271 Member.delete_all(['project_id = ?', id])
272 end
272 end
273
273
274 # Users issues can be assigned to
274 # Users issues can be assigned to
275 def assignable_users
275 def assignable_users
276 members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort
276 members.select {|m| m.roles.detect {|role| role.assignable?}}.collect {|m| m.user}.sort
277 end
277 end
278
278
279 # Returns the mail adresses of users that should be always notified on project events
279 # Returns the mail adresses of users that should be always notified on project events
280 def recipients
280 def recipients
281 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
281 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
282 end
282 end
283
283
284 # Returns an array of all custom fields enabled for project issues
284 # Returns an array of all custom fields enabled for project issues
285 # (explictly associated custom fields and custom fields enabled for all projects)
285 # (explictly associated custom fields and custom fields enabled for all projects)
286 def all_issue_custom_fields
286 def all_issue_custom_fields
287 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
287 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
288 end
288 end
289
289
290 def project
290 def project
291 self
291 self
292 end
292 end
293
293
294 def <=>(project)
294 def <=>(project)
295 name.downcase <=> project.name.downcase
295 name.downcase <=> project.name.downcase
296 end
296 end
297
297
298 def to_s
298 def to_s
299 name
299 name
300 end
300 end
301
301
302 # Returns a short description of the projects (first lines)
302 # Returns a short description of the projects (first lines)
303 def short_description(length = 255)
303 def short_description(length = 255)
304 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
304 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
305 end
305 end
306
306
307 # Return true if this project is allowed to do the specified action.
307 # Return true if this project is allowed to do the specified action.
308 # action can be:
308 # action can be:
309 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
309 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
310 # * a permission Symbol (eg. :edit_project)
310 # * a permission Symbol (eg. :edit_project)
311 def allows_to?(action)
311 def allows_to?(action)
312 if action.is_a? Hash
312 if action.is_a? Hash
313 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
313 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
314 else
314 else
315 allowed_permissions.include? action
315 allowed_permissions.include? action
316 end
316 end
317 end
317 end
318
318
319 def module_enabled?(module_name)
319 def module_enabled?(module_name)
320 module_name = module_name.to_s
320 module_name = module_name.to_s
321 enabled_modules.detect {|m| m.name == module_name}
321 enabled_modules.detect {|m| m.name == module_name}
322 end
322 end
323
323
324 def enabled_module_names=(module_names)
324 def enabled_module_names=(module_names)
325 if module_names && module_names.is_a?(Array)
325 if module_names && module_names.is_a?(Array)
326 module_names = module_names.collect(&:to_s)
326 module_names = module_names.collect(&:to_s)
327 # remove disabled modules
327 # remove disabled modules
328 enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)}
328 enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)}
329 # add new modules
329 # add new modules
330 module_names.each {|name| enabled_modules << EnabledModule.new(:name => name)}
330 module_names.each {|name| enabled_modules << EnabledModule.new(:name => name)}
331 else
331 else
332 enabled_modules.clear
332 enabled_modules.clear
333 end
333 end
334 end
334 end
335
335
336 # Returns an auto-generated project identifier based on the last identifier used
336 # Returns an auto-generated project identifier based on the last identifier used
337 def self.next_identifier
337 def self.next_identifier
338 p = Project.find(:first, :order => 'created_on DESC')
338 p = Project.find(:first, :order => 'created_on DESC')
339 p.nil? ? nil : p.identifier.to_s.succ
339 p.nil? ? nil : p.identifier.to_s.succ
340 end
340 end
341
341
342 # Copies and saves the Project instance based on the +project+.
342 # Copies and saves the Project instance based on the +project+.
343 # Will duplicate the source project's:
343 # Will duplicate the source project's:
344 # * Issues
344 # * Issues
345 # * Members
345 # * Members
346 # * Queries
346 # * Queries
347 def copy(project)
347 def copy(project)
348 project = project.is_a?(Project) ? project : Project.find(project)
348 project = project.is_a?(Project) ? project : Project.find(project)
349
349
350 Project.transaction do
350 Project.transaction do
351 # Issues
351 # Issues
352 project.issues.each do |issue|
352 project.issues.each do |issue|
353 new_issue = Issue.new
353 new_issue = Issue.new
354 new_issue.copy_from(issue)
354 new_issue.copy_from(issue)
355 self.issues << new_issue
355 self.issues << new_issue
356 end
356 end
357
357
358 # Members
358 # Members
359 project.members.each do |member|
359 project.members.each do |member|
360 new_member = Member.new
360 new_member = Member.new
361 new_member.attributes = member.attributes.dup.except("project_id")
361 new_member.attributes = member.attributes.dup.except("project_id")
362 new_member.role_ids = member.role_ids.dup
362 new_member.role_ids = member.role_ids.dup
363 new_member.project = self
363 new_member.project = self
364 self.members << new_member
364 self.members << new_member
365 end
365 end
366
366
367 # Queries
367 # Queries
368 project.queries.each do |query|
368 project.queries.each do |query|
369 new_query = Query.new
369 new_query = Query.new
370 new_query.attributes = query.attributes.dup.except("project_id", "sort_criteria")
370 new_query.attributes = query.attributes.dup.except("project_id", "sort_criteria")
371 new_query.sort_criteria = query.sort_criteria if query.sort_criteria
371 new_query.sort_criteria = query.sort_criteria if query.sort_criteria
372 new_query.project = self
372 new_query.project = self
373 self.queries << new_query
373 self.queries << new_query
374 end
374 end
375
375
376 Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
376 Redmine::Hook.call_hook(:model_project_copy_before_save, :source_project => project, :destination_project => self)
377 self.save
377 self.save
378 end
378 end
379 end
379 end
380
380
381
381
382 # Copies +project+ and returns the new instance. This will not save
382 # Copies +project+ and returns the new instance. This will not save
383 # the copy
383 # the copy
384 def self.copy_from(project)
384 def self.copy_from(project)
385 begin
385 begin
386 project = project.is_a?(Project) ? project : Project.find(project)
386 project = project.is_a?(Project) ? project : Project.find(project)
387 if project
387 if project
388 # clear unique attributes
388 # clear unique attributes
389 attributes = project.attributes.dup.except('name', 'identifier', 'id', 'status')
389 attributes = project.attributes.dup.except('name', 'identifier', 'id', 'status')
390 copy = Project.new(attributes)
390 copy = Project.new(attributes)
391 copy.enabled_modules = project.enabled_modules
391 copy.enabled_modules = project.enabled_modules
392 copy.trackers = project.trackers
392 copy.trackers = project.trackers
393 copy.custom_values = project.custom_values.collect {|v| v.clone}
393 copy.custom_values = project.custom_values.collect {|v| v.clone}
394 return copy
394 return copy
395 else
395 else
396 return nil
396 return nil
397 end
397 end
398 rescue ActiveRecord::RecordNotFound
398 rescue ActiveRecord::RecordNotFound
399 return nil
399 return nil
400 end
400 end
401 end
401 end
402
402
403 private
403 private
404 def allowed_permissions
404 def allowed_permissions
405 @allowed_permissions ||= begin
405 @allowed_permissions ||= begin
406 module_names = enabled_modules.collect {|m| m.name}
406 module_names = enabled_modules.collect {|m| m.name}
407 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
407 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
408 end
408 end
409 end
409 end
410
410
411 def allowed_actions
411 def allowed_actions
412 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
412 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
413 end
413 end
414 end
414 end
General Comments 0
You need to be logged in to leave comments. Login now