##// END OF EJS Templates
Lower the project identifier limit to a minimum of two characters (#2003)....
Jean-Philippe Lang -
r2219:7a5ce2892120
parent child
Show More
@@ -1,276 +1,276
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_tree :order => "name", :counter_cache => true
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 => 3..20
63 validates_length_of :identifier, :in => 2..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
70 70 def identifier=(identifier)
71 71 super unless identifier_frozen?
72 72 end
73 73
74 74 def identifier_frozen?
75 75 errors[:identifier].nil? && !(new_record? || identifier.blank?)
76 76 end
77 77
78 78 def issues_with_subprojects(include_subprojects=false)
79 79 conditions = nil
80 80 if include_subprojects
81 81 ids = [id] + child_ids
82 82 conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"]
83 83 end
84 84 conditions ||= ["#{Project.table_name}.id = ?", id]
85 85 # Quick and dirty fix for Rails 2 compatibility
86 86 Issue.send(:with_scope, :find => { :conditions => conditions }) do
87 87 Version.send(:with_scope, :find => { :conditions => conditions }) do
88 88 yield
89 89 end
90 90 end
91 91 end
92 92
93 93 # returns latest created projects
94 94 # non public projects will be returned only if user is a member of those
95 95 def self.latest(user=nil, count=5)
96 96 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
97 97 end
98 98
99 99 def self.visible_by(user=nil)
100 100 user ||= User.current
101 101 if user && user.admin?
102 102 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
103 103 elsif user && user.memberships.any?
104 104 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(',')}))"
105 105 else
106 106 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
107 107 end
108 108 end
109 109
110 110 def self.allowed_to_condition(user, permission, options={})
111 111 statements = []
112 112 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
113 113 if perm = Redmine::AccessControl.permission(permission)
114 114 unless perm.project_module.nil?
115 115 # If the permission belongs to a project module, make sure the module is enabled
116 116 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)"
117 117 end
118 118 end
119 119 if options[:project]
120 120 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
121 121 project_statement << " OR #{Project.table_name}.parent_id = #{options[:project].id}" if options[:with_subprojects]
122 122 base_statement = "(#{project_statement}) AND (#{base_statement})"
123 123 end
124 124 if user.admin?
125 125 # no restriction
126 126 else
127 127 statements << "1=0"
128 128 if user.logged?
129 129 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
130 130 allowed_project_ids = user.memberships.select {|m| m.role.allowed_to?(permission)}.collect {|m| m.project_id}
131 131 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
132 132 elsif Role.anonymous.allowed_to?(permission)
133 133 # anonymous user allowed on public project
134 134 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
135 135 else
136 136 # anonymous user is not authorized
137 137 end
138 138 end
139 139 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
140 140 end
141 141
142 142 def project_condition(with_subprojects)
143 143 cond = "#{Project.table_name}.id = #{id}"
144 144 cond = "(#{cond} OR #{Project.table_name}.parent_id = #{id})" if with_subprojects
145 145 cond
146 146 end
147 147
148 148 def self.find(*args)
149 149 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
150 150 project = find_by_identifier(*args)
151 151 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
152 152 project
153 153 else
154 154 super
155 155 end
156 156 end
157 157
158 158 def to_param
159 159 # id is used for projects with a numeric identifier (compatibility)
160 160 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
161 161 end
162 162
163 163 def active?
164 164 self.status == STATUS_ACTIVE
165 165 end
166 166
167 167 def archive
168 168 # Archive subprojects if any
169 169 children.each do |subproject|
170 170 subproject.archive
171 171 end
172 172 update_attribute :status, STATUS_ARCHIVED
173 173 end
174 174
175 175 def unarchive
176 176 return false if parent && !parent.active?
177 177 update_attribute :status, STATUS_ACTIVE
178 178 end
179 179
180 180 def active_children
181 181 children.select {|child| child.active?}
182 182 end
183 183
184 184 # Returns an array of the trackers used by the project and its sub projects
185 185 def rolled_up_trackers
186 186 @rolled_up_trackers ||=
187 187 Tracker.find(:all, :include => :projects,
188 188 :select => "DISTINCT #{Tracker.table_name}.*",
189 189 :conditions => ["#{Project.table_name}.id = ? OR #{Project.table_name}.parent_id = ?", id, id],
190 190 :order => "#{Tracker.table_name}.position")
191 191 end
192 192
193 193 # Deletes all project's members
194 194 def delete_all_members
195 195 Member.delete_all(['project_id = ?', id])
196 196 end
197 197
198 198 # Users issues can be assigned to
199 199 def assignable_users
200 200 members.select {|m| m.role.assignable?}.collect {|m| m.user}.sort
201 201 end
202 202
203 203 # Returns the mail adresses of users that should be always notified on project events
204 204 def recipients
205 205 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
206 206 end
207 207
208 208 # Returns an array of all custom fields enabled for project issues
209 209 # (explictly associated custom fields and custom fields enabled for all projects)
210 210 def all_issue_custom_fields
211 211 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
212 212 end
213 213
214 214 def project
215 215 self
216 216 end
217 217
218 218 def <=>(project)
219 219 name.downcase <=> project.name.downcase
220 220 end
221 221
222 222 def to_s
223 223 name
224 224 end
225 225
226 226 # Returns a short description of the projects (first lines)
227 227 def short_description(length = 255)
228 228 description.gsub(/^(.{#{length}}[^\n]*).*$/m, '\1').strip if description
229 229 end
230 230
231 231 def allows_to?(action)
232 232 if action.is_a? Hash
233 233 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
234 234 else
235 235 allowed_permissions.include? action
236 236 end
237 237 end
238 238
239 239 def module_enabled?(module_name)
240 240 module_name = module_name.to_s
241 241 enabled_modules.detect {|m| m.name == module_name}
242 242 end
243 243
244 244 def enabled_module_names=(module_names)
245 245 enabled_modules.clear
246 246 module_names = [] unless module_names && module_names.is_a?(Array)
247 247 module_names.each do |name|
248 248 enabled_modules << EnabledModule.new(:name => name.to_s)
249 249 end
250 250 end
251 251
252 252 # Returns an auto-generated project identifier based on the last identifier used
253 253 def self.next_identifier
254 254 p = Project.find(:first, :order => 'created_on DESC')
255 255 p.nil? ? nil : p.identifier.to_s.succ
256 256 end
257 257
258 258 protected
259 259 def validate
260 260 errors.add(parent_id, " must be a root project") if parent and parent.parent
261 261 errors.add_to_base("A project with subprojects can't be a subproject") if parent and children.size > 0
262 262 errors.add(:identifier, :activerecord_error_invalid) if !identifier.blank? && identifier.match(/^\d*$/)
263 263 end
264 264
265 265 private
266 266 def allowed_permissions
267 267 @allowed_permissions ||= begin
268 268 module_names = enabled_modules.collect {|m| m.name}
269 269 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
270 270 end
271 271 end
272 272
273 273 def allowed_actions
274 274 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
275 275 end
276 276 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? and !@root_projects.empty? %>
8 8 <p><%= f.select :parent_id, (@root_projects.collect {|p| [p.name, p.id]}), { :include_blank => true } %></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, 3, 20) %> <%= l(:text_project_identifier_info) %></em>
14 <br /><em><%= l(:text_length_between, 2, 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