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