##// END OF EJS Templates
Set minimum identifier lenght to 1 (#3073)....
Jean-Philippe Lang -
r2557:e40241761a49
parent child
Show More
@@ -1,337 +1,337
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}.status=#{User::STATUS_ACTIVE}"
24 24 has_many :users, :through => :members
25 25 has_many :enabled_modules, :dependent => :delete_all
26 26 has_and_belongs_to_many :trackers, :order => "#{Tracker.table_name}.position"
27 27 has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker]
28 28 has_many :issue_changes, :through => :issues, :source => :journals
29 29 has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC"
30 30 has_many :time_entries, :dependent => :delete_all
31 31 has_many :queries, :dependent => :delete_all
32 32 has_many :documents, :dependent => :destroy
33 33 has_many :news, :dependent => :delete_all, :include => :author
34 34 has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name"
35 35 has_many :boards, :dependent => :destroy, :order => "position ASC"
36 36 has_one :repository, :dependent => :destroy
37 37 has_many :changesets, :through => :repository
38 38 has_one :wiki, :dependent => :destroy
39 39 # Custom field for the project issues
40 40 has_and_belongs_to_many :issue_custom_fields,
41 41 :class_name => 'IssueCustomField',
42 42 :order => "#{CustomField.table_name}.position",
43 43 :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}",
44 44 :association_foreign_key => 'custom_field_id'
45 45
46 46 acts_as_nested_set :order => 'name', :dependent => :destroy
47 47 acts_as_attachable :view_permission => :view_files,
48 48 :delete_permission => :manage_files
49 49
50 50 acts_as_customizable
51 51 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil
52 52 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
53 53 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}},
54 54 :author => nil
55 55
56 56 attr_protected :status, :enabled_module_names
57 57
58 58 validates_presence_of :name, :identifier
59 59 validates_uniqueness_of :name, :identifier
60 60 validates_associated :repository, :wiki
61 61 validates_length_of :name, :maximum => 30
62 62 validates_length_of :homepage, :maximum => 255
63 validates_length_of :identifier, :in => 2..20
63 validates_length_of :identifier, :in => 1..20
64 64 validates_format_of :identifier, :with => /^[a-z0-9\-]*$/
65 65
66 66 before_destroy :delete_all_members
67 67
68 68 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] } }
69 69 named_scope :active, { :conditions => "#{Project.table_name}.status = #{STATUS_ACTIVE}"}
70 70 named_scope :public, { :conditions => { :is_public => true } }
71 71 named_scope :visible, lambda { { :conditions => Project.visible_by(User.current) } }
72 72
73 73 def identifier=(identifier)
74 74 super unless identifier_frozen?
75 75 end
76 76
77 77 def identifier_frozen?
78 78 errors[:identifier].nil? && !(new_record? || identifier.blank?)
79 79 end
80 80
81 81 def issues_with_subprojects(include_subprojects=false)
82 82 conditions = nil
83 83 if include_subprojects
84 84 ids = [id] + descendants.collect(&:id)
85 85 conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"]
86 86 end
87 87 conditions ||= ["#{Project.table_name}.id = ?", id]
88 88 # Quick and dirty fix for Rails 2 compatibility
89 89 Issue.send(:with_scope, :find => { :conditions => conditions }) do
90 90 Version.send(:with_scope, :find => { :conditions => conditions }) do
91 91 yield
92 92 end
93 93 end
94 94 end
95 95
96 96 # returns latest created projects
97 97 # non public projects will be returned only if user is a member of those
98 98 def self.latest(user=nil, count=5)
99 99 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
100 100 end
101 101
102 102 # Returns a SQL :conditions string used to find all active projects for the specified user.
103 103 #
104 104 # Examples:
105 105 # Projects.visible_by(admin) => "projects.status = 1"
106 106 # Projects.visible_by(normal_user) => "projects.status = 1 AND projects.is_public = 1"
107 107 def self.visible_by(user=nil)
108 108 user ||= User.current
109 109 if user && user.admin?
110 110 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
111 111 elsif user && user.memberships.any?
112 112 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(',')}))"
113 113 else
114 114 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
115 115 end
116 116 end
117 117
118 118 def self.allowed_to_condition(user, permission, options={})
119 119 statements = []
120 120 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
121 121 if perm = Redmine::AccessControl.permission(permission)
122 122 unless perm.project_module.nil?
123 123 # If the permission belongs to a project module, make sure the module is enabled
124 124 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)"
125 125 end
126 126 end
127 127 if options[:project]
128 128 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
129 129 project_statement << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" if options[:with_subprojects]
130 130 base_statement = "(#{project_statement}) AND (#{base_statement})"
131 131 end
132 132 if user.admin?
133 133 # no restriction
134 134 else
135 135 statements << "1=0"
136 136 if user.logged?
137 137 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
138 138 allowed_project_ids = user.memberships.select {|m| m.role.allowed_to?(permission)}.collect {|m| m.project_id}
139 139 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
140 140 elsif Role.anonymous.allowed_to?(permission)
141 141 # anonymous user allowed on public project
142 142 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
143 143 else
144 144 # anonymous user is not authorized
145 145 end
146 146 end
147 147 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
148 148 end
149 149
150 150 # Returns a :conditions SQL string that can be used to find the issues associated with this project.
151 151 #
152 152 # Examples:
153 153 # project.project_condition(true) => "(projects.id = 1 OR (projects.lft > 1 AND projects.rgt < 10))"
154 154 # project.project_condition(false) => "projects.id = 1"
155 155 def project_condition(with_subprojects)
156 156 cond = "#{Project.table_name}.id = #{id}"
157 157 cond = "(#{cond} OR (#{Project.table_name}.lft > #{lft} AND #{Project.table_name}.rgt < #{rgt}))" if with_subprojects
158 158 cond
159 159 end
160 160
161 161 def self.find(*args)
162 162 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
163 163 project = find_by_identifier(*args)
164 164 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
165 165 project
166 166 else
167 167 super
168 168 end
169 169 end
170 170
171 171 def to_param
172 172 # id is used for projects with a numeric identifier (compatibility)
173 173 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
174 174 end
175 175
176 176 def active?
177 177 self.status == STATUS_ACTIVE
178 178 end
179 179
180 180 # Archives the project and its descendants recursively
181 181 def archive
182 182 # Archive subprojects if any
183 183 children.each do |subproject|
184 184 subproject.archive
185 185 end
186 186 update_attribute :status, STATUS_ARCHIVED
187 187 end
188 188
189 189 # Unarchives the project
190 190 # All its ancestors must be active
191 191 def unarchive
192 192 return false if ancestors.detect {|a| !a.active?}
193 193 update_attribute :status, STATUS_ACTIVE
194 194 end
195 195
196 196 # Returns an array of projects the project can be moved to
197 197 def possible_parents
198 198 @possible_parents ||= (Project.active.find(:all) - self_and_descendants)
199 199 end
200 200
201 201 # Sets the parent of the project
202 202 # Argument can be either a Project, a String, a Fixnum or nil
203 203 def set_parent!(p)
204 204 unless p.nil? || p.is_a?(Project)
205 205 if p.to_s.blank?
206 206 p = nil
207 207 else
208 208 p = Project.find_by_id(p)
209 209 return false unless p
210 210 end
211 211 end
212 212 if p == parent && !p.nil?
213 213 # Nothing to do
214 214 true
215 215 elsif p.nil? || (p.active? && move_possible?(p))
216 216 # Insert the project so that target's children or root projects stay alphabetically sorted
217 217 sibs = (p.nil? ? self.class.roots : p.children)
218 218 to_be_inserted_before = sibs.detect {|c| c.name.to_s.downcase > name.to_s.downcase }
219 219 if to_be_inserted_before
220 220 move_to_left_of(to_be_inserted_before)
221 221 elsif p.nil?
222 222 if sibs.empty?
223 223 # move_to_root adds the project in first (ie. left) position
224 224 move_to_root
225 225 else
226 226 move_to_right_of(sibs.last) unless self == sibs.last
227 227 end
228 228 else
229 229 # move_to_child_of adds the project in last (ie.right) position
230 230 move_to_child_of(p)
231 231 end
232 232 true
233 233 else
234 234 # Can not move to the given target
235 235 false
236 236 end
237 237 end
238 238
239 239 # Returns an array of the trackers used by the project and its active sub projects
240 240 def rolled_up_trackers
241 241 @rolled_up_trackers ||=
242 242 Tracker.find(:all, :include => :projects,
243 243 :select => "DISTINCT #{Tracker.table_name}.*",
244 244 :conditions => ["#{Project.table_name}.lft >= ? AND #{Project.table_name}.rgt <= ? AND #{Project.table_name}.status = #{STATUS_ACTIVE}", lft, rgt],
245 245 :order => "#{Tracker.table_name}.position")
246 246 end
247 247
248 248 # Deletes all project's members
249 249 def delete_all_members
250 250 Member.delete_all(['project_id = ?', id])
251 251 end
252 252
253 253 # Users issues can be assigned to
254 254 def assignable_users
255 255 members.select {|m| m.role.assignable?}.collect {|m| m.user}.sort
256 256 end
257 257
258 258 # Returns the mail adresses of users that should be always notified on project events
259 259 def recipients
260 260 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
261 261 end
262 262
263 263 # Returns an array of all custom fields enabled for project issues
264 264 # (explictly associated custom fields and custom fields enabled for all projects)
265 265 def all_issue_custom_fields
266 266 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
267 267 end
268 268
269 269 def project
270 270 self
271 271 end
272 272
273 273 def <=>(project)
274 274 name.downcase <=> project.name.downcase
275 275 end
276 276
277 277 def to_s
278 278 name
279 279 end
280 280
281 281 # Returns a short description of the projects (first lines)
282 282 def short_description(length = 255)
283 283 description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description
284 284 end
285 285
286 286 # Return true if this project is allowed to do the specified action.
287 287 # action can be:
288 288 # * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
289 289 # * a permission Symbol (eg. :edit_project)
290 290 def allows_to?(action)
291 291 if action.is_a? Hash
292 292 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
293 293 else
294 294 allowed_permissions.include? action
295 295 end
296 296 end
297 297
298 298 def module_enabled?(module_name)
299 299 module_name = module_name.to_s
300 300 enabled_modules.detect {|m| m.name == module_name}
301 301 end
302 302
303 303 def enabled_module_names=(module_names)
304 304 if module_names && module_names.is_a?(Array)
305 305 module_names = module_names.collect(&:to_s)
306 306 # remove disabled modules
307 307 enabled_modules.each {|mod| mod.destroy unless module_names.include?(mod.name)}
308 308 # add new modules
309 309 module_names.each {|name| enabled_modules << EnabledModule.new(:name => name)}
310 310 else
311 311 enabled_modules.clear
312 312 end
313 313 end
314 314
315 315 # Returns an auto-generated project identifier based on the last identifier used
316 316 def self.next_identifier
317 317 p = Project.find(:first, :order => 'created_on DESC')
318 318 p.nil? ? nil : p.identifier.to_s.succ
319 319 end
320 320
321 321 protected
322 322 def validate
323 323 errors.add(:identifier, :invalid) if !identifier.blank? && identifier.match(/^\d*$/)
324 324 end
325 325
326 326 private
327 327 def allowed_permissions
328 328 @allowed_permissions ||= begin
329 329 module_names = enabled_modules.collect {|m| m.name}
330 330 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
331 331 end
332 332 end
333 333
334 334 def allowed_actions
335 335 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
336 336 end
337 337 end
@@ -1,49 +1,49
1 1 <%= error_messages_for 'project' %>
2 2
3 3 <div class="box">
4 4 <!--[form:project]-->
5 5 <p><%= f.text_field :name, :required => true %><br /><em><%= l(:text_caracters_maximum, 30) %></em></p>
6 6
7 7 <% if User.current.admin? && !@project.possible_parents.empty? %>
8 8 <p><label><%= l(:field_parent) %></label><%= parent_project_select_tag(@project) %></p>
9 9 <% end %>
10 10
11 11 <p><%= f.text_area :description, :rows => 5, :class => 'wiki-edit' %></p>
12 12 <p><%= f.text_field :identifier, :required => true, :disabled => @project.identifier_frozen? %>
13 13 <% unless @project.identifier_frozen? %>
14 <br /><em><%= l(:text_length_between, :min => 2, :max => 20) %> <%= l(:text_project_identifier_info) %></em>
14 <br /><em><%= l(:text_length_between, :min => 1, :max => 20) %> <%= l(:text_project_identifier_info) %></em>
15 15 <% end %></p>
16 16 <p><%= f.text_field :homepage, :size => 60 %></p>
17 17 <p><%= f.check_box :is_public %></p>
18 18 <%= wikitoolbar_for 'project_description' %>
19 19
20 20 <% @project.custom_field_values.each do |value| %>
21 21 <p><%= custom_field_tag_with_label :project, value %></p>
22 22 <% end %>
23 23 <%= call_hook(:view_projects_form, :project => @project, :form => f) %>
24 24 </div>
25 25
26 26 <% unless @trackers.empty? %>
27 27 <fieldset class="box"><legend><%=l(:label_tracker_plural)%></legend>
28 28 <% @trackers.each do |tracker| %>
29 29 <label class="floating">
30 30 <%= check_box_tag 'project[tracker_ids][]', tracker.id, @project.trackers.include?(tracker) %>
31 31 <%= tracker %>
32 32 </label>
33 33 <% end %>
34 34 <%= hidden_field_tag 'project[tracker_ids][]', '' %>
35 35 </fieldset>
36 36 <% end %>
37 37
38 38 <% unless @issue_custom_fields.empty? %>
39 39 <fieldset class="box"><legend><%=l(:label_custom_field_plural)%></legend>
40 40 <% @issue_custom_fields.each do |custom_field| %>
41 41 <label class="floating">
42 42 <%= check_box_tag 'project[issue_custom_field_ids][]', custom_field.id, (@project.all_issue_custom_fields.include? custom_field), (custom_field.is_for_all? ? {:disabled => "disabled"} : {}) %>
43 43 <%= custom_field.name %>
44 44 </label>
45 45 <% end %>
46 46 <%= hidden_field_tag 'project[issue_custom_field_ids][]', '' %>
47 47 </fieldset>
48 48 <% end %>
49 49 <!--[eoform:project]-->
General Comments 0
You need to be logged in to leave comments. Login now