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