##// END OF EJS Templates
Replaces repository_enable named scope on Project with a more generic one: has_module....
Jean-Philippe Lang -
r1803:231e98f0c758
parent child
Show More
@@ -1,47 +1,47
1 1 # redMine - project management software
2 2 # Copyright (C) 2006-2007 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 SysController < ActionController::Base
19 19 wsdl_service_name 'Sys'
20 20 web_service_api SysApi
21 21 web_service_scaffold :invoke
22 22
23 23 before_invocation :check_enabled
24 24
25 25 # Returns the projects list, with their repositories
26 26 def projects_with_repository_enabled
27 Project.repository_enabled(:all, :include => :repository, :order => 'identifier')
27 Project.has_module(:repository).find(:all, :include => :repository, :order => 'identifier')
28 28 end
29 29
30 30 # Registers a repository for the given project identifier
31 31 # (Subversion specific)
32 32 def repository_created(identifier, url)
33 33 project = Project.find_by_identifier(identifier)
34 34 # Do not create the repository if the project has already one
35 35 return 0 unless project && project.repository.nil?
36 36 logger.debug "Repository for #{project.name} was created"
37 37 repository = Repository.factory('Subversion', :project => project, :url => url)
38 38 repository.save
39 39 repository.id || 0
40 40 end
41 41
42 42 protected
43 43
44 44 def check_enabled(name, args)
45 45 Setting.sys_api_enabled?
46 46 end
47 47 end
@@ -1,268 +1,268
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
48 48 acts_as_customizable
49 49 acts_as_searchable :columns => ['name', 'description'], :project_key => 'id', :permission => nil
50 50 acts_as_event :title => Proc.new {|o| "#{l(:label_project)}: #{o.name}"},
51 51 :url => Proc.new {|o| {:controller => 'projects', :action => 'show', :id => o.id}},
52 52 :author => nil
53 53
54 54 attr_protected :status, :enabled_module_names
55 55
56 56 validates_presence_of :name, :identifier
57 57 validates_uniqueness_of :name, :identifier
58 58 validates_associated :repository, :wiki
59 59 validates_length_of :name, :maximum => 30
60 60 validates_length_of :homepage, :maximum => 255
61 61 validates_length_of :identifier, :in => 3..20
62 62 validates_format_of :identifier, :with => /^[a-z0-9\-]*$/
63 63
64 64 before_destroy :delete_all_members
65 65
66 named_scope :repository_enabled, { :include => :enabled_modules, :conditions => ["#{EnabledModule.table_name}.name=?", 'repository'] }
66 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] } }
67 67
68 68 def identifier=(identifier)
69 69 super unless identifier_frozen?
70 70 end
71 71
72 72 def identifier_frozen?
73 73 errors[:identifier].nil? && !(new_record? || identifier.blank?)
74 74 end
75 75
76 76 def issues_with_subprojects(include_subprojects=false)
77 77 conditions = nil
78 78 if include_subprojects
79 79 ids = [id] + child_ids
80 80 conditions = ["#{Project.table_name}.id IN (#{ids.join(',')}) AND #{Project.visible_by}"]
81 81 end
82 82 conditions ||= ["#{Project.table_name}.id = ?", id]
83 83 # Quick and dirty fix for Rails 2 compatibility
84 84 Issue.send(:with_scope, :find => { :conditions => conditions }) do
85 85 Version.send(:with_scope, :find => { :conditions => conditions }) do
86 86 yield
87 87 end
88 88 end
89 89 end
90 90
91 91 # returns latest created projects
92 92 # non public projects will be returned only if user is a member of those
93 93 def self.latest(user=nil, count=5)
94 94 find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC")
95 95 end
96 96
97 97 def self.visible_by(user=nil)
98 98 user ||= User.current
99 99 if user && user.admin?
100 100 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
101 101 elsif user && user.memberships.any?
102 102 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(',')}))"
103 103 else
104 104 return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}"
105 105 end
106 106 end
107 107
108 108 def self.allowed_to_condition(user, permission, options={})
109 109 statements = []
110 110 base_statement = "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}"
111 111 if options[:project]
112 112 project_statement = "#{Project.table_name}.id = #{options[:project].id}"
113 113 project_statement << " OR #{Project.table_name}.parent_id = #{options[:project].id}" if options[:with_subprojects]
114 114 base_statement = "(#{project_statement}) AND (#{base_statement})"
115 115 end
116 116 if user.admin?
117 117 # no restriction
118 118 else
119 119 statements << "1=0"
120 120 if user.logged?
121 121 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}" if Role.non_member.allowed_to?(permission)
122 122 allowed_project_ids = user.memberships.select {|m| m.role.allowed_to?(permission)}.collect {|m| m.project_id}
123 123 statements << "#{Project.table_name}.id IN (#{allowed_project_ids.join(',')})" if allowed_project_ids.any?
124 124 elsif Role.anonymous.allowed_to?(permission)
125 125 # anonymous user allowed on public project
126 126 statements << "#{Project.table_name}.is_public = #{connection.quoted_true}"
127 127 else
128 128 # anonymous user is not authorized
129 129 end
130 130 end
131 131 statements.empty? ? base_statement : "((#{base_statement}) AND (#{statements.join(' OR ')}))"
132 132 end
133 133
134 134 def project_condition(with_subprojects)
135 135 cond = "#{Project.table_name}.id = #{id}"
136 136 cond = "(#{cond} OR #{Project.table_name}.parent_id = #{id})" if with_subprojects
137 137 cond
138 138 end
139 139
140 140 def self.find(*args)
141 141 if args.first && args.first.is_a?(String) && !args.first.match(/^\d*$/)
142 142 project = find_by_identifier(*args)
143 143 raise ActiveRecord::RecordNotFound, "Couldn't find Project with identifier=#{args.first}" if project.nil?
144 144 project
145 145 else
146 146 super
147 147 end
148 148 end
149 149
150 150 def to_param
151 151 # id is used for projects with a numeric identifier (compatibility)
152 152 @to_param ||= (identifier.to_s =~ %r{^\d*$} ? id : identifier)
153 153 end
154 154
155 155 def active?
156 156 self.status == STATUS_ACTIVE
157 157 end
158 158
159 159 def archive
160 160 # Archive subprojects if any
161 161 children.each do |subproject|
162 162 subproject.archive
163 163 end
164 164 update_attribute :status, STATUS_ARCHIVED
165 165 end
166 166
167 167 def unarchive
168 168 return false if parent && !parent.active?
169 169 update_attribute :status, STATUS_ACTIVE
170 170 end
171 171
172 172 def active_children
173 173 children.select {|child| child.active?}
174 174 end
175 175
176 176 # Returns an array of the trackers used by the project and its sub projects
177 177 def rolled_up_trackers
178 178 @rolled_up_trackers ||=
179 179 Tracker.find(:all, :include => :projects,
180 180 :select => "DISTINCT #{Tracker.table_name}.*",
181 181 :conditions => ["#{Project.table_name}.id = ? OR #{Project.table_name}.parent_id = ?", id, id],
182 182 :order => "#{Tracker.table_name}.position")
183 183 end
184 184
185 185 # Deletes all project's members
186 186 def delete_all_members
187 187 Member.delete_all(['project_id = ?', id])
188 188 end
189 189
190 190 # Users issues can be assigned to
191 191 def assignable_users
192 192 members.select {|m| m.role.assignable?}.collect {|m| m.user}.sort
193 193 end
194 194
195 195 # Returns the mail adresses of users that should be always notified on project events
196 196 def recipients
197 197 members.select {|m| m.mail_notification? || m.user.mail_notification?}.collect {|m| m.user.mail}
198 198 end
199 199
200 200 # Returns an array of all custom fields enabled for project issues
201 201 # (explictly associated custom fields and custom fields enabled for all projects)
202 202 def all_issue_custom_fields
203 203 @all_issue_custom_fields ||= (IssueCustomField.for_all + issue_custom_fields).uniq.sort
204 204 end
205 205
206 206 def project
207 207 self
208 208 end
209 209
210 210 def <=>(project)
211 211 name.downcase <=> project.name.downcase
212 212 end
213 213
214 214 def to_s
215 215 name
216 216 end
217 217
218 218 # Returns a short description of the projects (first lines)
219 219 def short_description(length = 255)
220 220 description.gsub(/^(.{#{length}}[^\n]*).*$/m, '\1').strip if description
221 221 end
222 222
223 223 def allows_to?(action)
224 224 if action.is_a? Hash
225 225 allowed_actions.include? "#{action[:controller]}/#{action[:action]}"
226 226 else
227 227 allowed_permissions.include? action
228 228 end
229 229 end
230 230
231 231 def module_enabled?(module_name)
232 232 module_name = module_name.to_s
233 233 enabled_modules.detect {|m| m.name == module_name}
234 234 end
235 235
236 236 def enabled_module_names=(module_names)
237 237 enabled_modules.clear
238 238 module_names = [] unless module_names && module_names.is_a?(Array)
239 239 module_names.each do |name|
240 240 enabled_modules << EnabledModule.new(:name => name.to_s)
241 241 end
242 242 end
243 243
244 244 # Returns an auto-generated project identifier based on the last identifier used
245 245 def self.next_identifier
246 246 p = Project.find(:first, :order => 'created_on DESC')
247 247 p.nil? ? nil : p.identifier.to_s.succ
248 248 end
249 249
250 250 protected
251 251 def validate
252 252 errors.add(parent_id, " must be a root project") if parent and parent.parent
253 253 errors.add_to_base("A project with subprojects can't be a subproject") if parent and children.size > 0
254 254 errors.add(:identifier, :activerecord_error_invalid) if !identifier.blank? && identifier.match(/^\d*$/)
255 255 end
256 256
257 257 private
258 258 def allowed_permissions
259 259 @allowed_permissions ||= begin
260 260 module_names = enabled_modules.collect {|m| m.name}
261 261 Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name}
262 262 end
263 263 end
264 264
265 265 def allowed_actions
266 266 @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten
267 267 end
268 268 end
General Comments 0
You need to be logged in to leave comments. Login now