##// END OF EJS Templates
only active users are now proposed when adding a member to a project...
Jean-Philippe Lang -
r187:5f361e71df2d
parent child
Show More
@@ -1,556 +1,556
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 require 'csv'
19 19
20 20 class ProjectsController < ApplicationController
21 21 layout 'base'
22 22 before_filter :find_project, :authorize, :except => [ :index, :list, :add ]
23 23 before_filter :require_admin, :only => [ :add, :destroy ]
24 24
25 25 helper :sort
26 26 include SortHelper
27 27 helper :custom_fields
28 28 include CustomFieldsHelper
29 29 helper :ifpdf
30 30 include IfpdfHelper
31 31 helper IssuesHelper
32 32 helper :queries
33 33 include QueriesHelper
34 34
35 35 def index
36 36 list
37 37 render :action => 'list' unless request.xhr?
38 38 end
39 39
40 40 # Lists public projects
41 41 def list
42 42 sort_init 'name', 'asc'
43 43 sort_update
44 44 @project_count = Project.count(:all, :conditions => ["is_public=?", true])
45 45 @project_pages = Paginator.new self, @project_count,
46 46 15,
47 47 params['page']
48 48 @projects = Project.find :all, :order => sort_clause,
49 49 :conditions => ["is_public=?", true],
50 50 :limit => @project_pages.items_per_page,
51 51 :offset => @project_pages.current.offset
52 52
53 53 render :action => "list", :layout => false if request.xhr?
54 54 end
55 55
56 56 # Add a new project
57 57 def add
58 58 @custom_fields = IssueCustomField.find(:all)
59 59 @root_projects = Project.find(:all, :conditions => "parent_id is null")
60 60 @project = Project.new(params[:project])
61 61 if request.get?
62 62 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project) }
63 63 else
64 64 @project.custom_fields = CustomField.find(params[:custom_field_ids]) if params[:custom_field_ids]
65 65 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) }
66 66 @project.custom_values = @custom_values
67 67 if params[:repository_enabled] && params[:repository_enabled] == "1"
68 68 @project.repository = Repository.new
69 69 @project.repository.attributes = params[:repository]
70 70 end
71 71 if @project.save
72 72 flash[:notice] = l(:notice_successful_create)
73 73 redirect_to :controller => 'admin', :action => 'projects'
74 74 end
75 75 end
76 76 end
77 77
78 78 # Show @project
79 79 def show
80 80 @custom_values = @project.custom_values.find(:all, :include => :custom_field)
81 81 @members = @project.members.find(:all, :include => [:user, :role])
82 82 @subprojects = @project.children if @project.children.size > 0
83 83 @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "news.created_on DESC")
84 84 @trackers = Tracker.find(:all)
85 85 @open_issues_by_tracker = Issue.count(:group => :tracker, :joins => "INNER JOIN issue_statuses ON issue_statuses.id = issues.status_id", :conditions => ["project_id=? and issue_statuses.is_closed=?", @project.id, false])
86 86 @total_issues_by_tracker = Issue.count(:group => :tracker, :conditions => ["project_id=?", @project.id])
87 87 end
88 88
89 89 def settings
90 90 @root_projects = Project::find(:all, :conditions => ["parent_id is null and id <> ?", @project.id])
91 91 @custom_fields = IssueCustomField.find(:all)
92 92 @issue_category ||= IssueCategory.new
93 93 @member ||= @project.members.new
94 94 @roles = Role.find(:all)
95 @users = User.find(:all) - @project.members.find(:all, :include => :user).collect{|m| m.user }
95 @users = User.find_active(:all) - @project.users
96 96 @custom_values ||= ProjectCustomField.find(:all).collect { |x| @project.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) }
97 97 end
98 98
99 99 # Edit @project
100 100 def edit
101 101 if request.post?
102 102 @project.custom_fields = IssueCustomField.find(params[:custom_field_ids]) if params[:custom_field_ids]
103 103 if params[:custom_fields]
104 104 @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) }
105 105 @project.custom_values = @custom_values
106 106 end
107 107 if params[:repository_enabled]
108 108 case params[:repository_enabled]
109 109 when "0"
110 110 @project.repository = nil
111 111 when "1"
112 112 @project.repository ||= Repository.new
113 113 @project.repository.attributes = params[:repository]
114 114 end
115 115 end
116 116 @project.attributes = params[:project]
117 117 if @project.save
118 118 flash[:notice] = l(:notice_successful_update)
119 119 redirect_to :action => 'settings', :id => @project
120 120 else
121 121 settings
122 122 render :action => 'settings'
123 123 end
124 124 end
125 125 end
126 126
127 127 # Delete @project
128 128 def destroy
129 129 if request.post? and params[:confirm]
130 130 @project.destroy
131 131 redirect_to :controller => 'admin', :action => 'projects'
132 132 end
133 133 end
134 134
135 135 # Add a new issue category to @project
136 136 def add_issue_category
137 137 if request.post?
138 138 @issue_category = @project.issue_categories.build(params[:issue_category])
139 139 if @issue_category.save
140 140 flash[:notice] = l(:notice_successful_create)
141 141 redirect_to :action => 'settings', :tab => 'categories', :id => @project
142 142 else
143 143 settings
144 144 render :action => 'settings'
145 145 end
146 146 end
147 147 end
148 148
149 149 # Add a new version to @project
150 150 def add_version
151 151 @version = @project.versions.build(params[:version])
152 152 if request.post? and @version.save
153 153 flash[:notice] = l(:notice_successful_create)
154 154 redirect_to :action => 'settings', :tab => 'versions', :id => @project
155 155 end
156 156 end
157 157
158 158 # Add a new member to @project
159 159 def add_member
160 160 @member = @project.members.build(params[:member])
161 161 if request.post?
162 162 if @member.save
163 163 flash[:notice] = l(:notice_successful_create)
164 164 redirect_to :action => 'settings', :tab => 'members', :id => @project
165 165 else
166 166 settings
167 167 render :action => 'settings'
168 168 end
169 169 end
170 170 end
171 171
172 172 # Show members list of @project
173 173 def list_members
174 174 @members = @project.members
175 175 end
176 176
177 177 # Add a new document to @project
178 178 def add_document
179 179 @categories = Enumeration::get_values('DCAT')
180 180 @document = @project.documents.build(params[:document])
181 181 if request.post? and @document.save
182 182 # Save the attachments
183 183 params[:attachments].each { |a|
184 184 Attachment.create(:container => @document, :file => a, :author => logged_in_user) unless a.size == 0
185 185 } if params[:attachments] and params[:attachments].is_a? Array
186 186 flash[:notice] = l(:notice_successful_create)
187 187 redirect_to :action => 'list_documents', :id => @project
188 188 end
189 189 end
190 190
191 191 # Show documents list of @project
192 192 def list_documents
193 193 @documents = @project.documents.find :all, :include => :category
194 194 end
195 195
196 196 # Add a new issue to @project
197 197 def add_issue
198 198 @tracker = Tracker.find(params[:tracker_id])
199 199 @priorities = Enumeration::get_values('IPRI')
200 200 @issue = Issue.new(:project => @project, :tracker => @tracker)
201 201 if request.get?
202 202 @issue.start_date = Date.today
203 203 @custom_values = @project.custom_fields_for_issues(@tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue) }
204 204 else
205 205 @issue.attributes = params[:issue]
206 206 @issue.author_id = self.logged_in_user.id if self.logged_in_user
207 207 # Multiple file upload
208 208 @attachments = []
209 209 params[:attachments].each { |a|
210 210 @attachments << Attachment.new(:container => @issue, :file => a, :author => logged_in_user) unless a.size == 0
211 211 } if params[:attachments] and params[:attachments].is_a? Array
212 212 @custom_values = @project.custom_fields_for_issues(@tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue, :value => params["custom_fields"][x.id.to_s]) }
213 213 @issue.custom_values = @custom_values
214 214 if @issue.save
215 215 @attachments.each(&:save)
216 216 flash[:notice] = l(:notice_successful_create)
217 217 Mailer.deliver_issue_add(@issue) if Permission.find_by_controller_and_action(params[:controller], params[:action]).mail_enabled?
218 218 redirect_to :action => 'list_issues', :id => @project
219 219 end
220 220 end
221 221 end
222 222
223 223 # Show filtered/sorted issues list of @project
224 224 def list_issues
225 225 sort_init 'issues.id', 'desc'
226 226 sort_update
227 227
228 228 retrieve_query
229 229
230 230 @results_per_page_options = [ 15, 25, 50, 100 ]
231 231 if params[:per_page] and @results_per_page_options.include? params[:per_page].to_i
232 232 @results_per_page = params[:per_page].to_i
233 233 session[:results_per_page] = @results_per_page
234 234 else
235 235 @results_per_page = session[:results_per_page] || 25
236 236 end
237 237
238 238 if @query.valid?
239 239 @issue_count = Issue.count(:include => [:status, :project], :conditions => @query.statement)
240 240 @issue_pages = Paginator.new self, @issue_count, @results_per_page, params['page']
241 241 @issues = Issue.find :all, :order => sort_clause,
242 242 :include => [ :author, :status, :tracker, :project ],
243 243 :conditions => @query.statement,
244 244 :limit => @issue_pages.items_per_page,
245 245 :offset => @issue_pages.current.offset
246 246 end
247 247 @trackers = Tracker.find :all
248 248 render :layout => false if request.xhr?
249 249 end
250 250
251 251 # Export filtered/sorted issues list to CSV
252 252 def export_issues_csv
253 253 sort_init 'issues.id', 'desc'
254 254 sort_update
255 255
256 256 retrieve_query
257 257 render :action => 'list_issues' and return unless @query.valid?
258 258
259 259 @issues = Issue.find :all, :order => sort_clause,
260 260 :include => [ :author, :status, :tracker, :priority, {:custom_values => :custom_field} ],
261 261 :conditions => @query.statement
262 262
263 263 ic = Iconv.new('ISO-8859-1', 'UTF-8')
264 264 export = StringIO.new
265 265 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
266 266 # csv header fields
267 267 headers = [ "#", l(:field_status),
268 268 l(:field_tracker),
269 269 l(:field_priority),
270 270 l(:field_subject),
271 271 l(:field_author),
272 272 l(:field_start_date),
273 273 l(:field_due_date),
274 274 l(:field_done_ratio),
275 275 l(:field_created_on),
276 276 l(:field_updated_on)
277 277 ]
278 278 for custom_field in @project.all_custom_fields
279 279 headers << custom_field.name
280 280 end
281 281 csv << headers.collect {|c| ic.iconv(c) }
282 282 # csv lines
283 283 @issues.each do |issue|
284 284 fields = [issue.id, issue.status.name,
285 285 issue.tracker.name,
286 286 issue.priority.name,
287 287 issue.subject,
288 288 issue.author.display_name,
289 289 issue.start_date ? l_date(issue.start_date) : nil,
290 290 issue.due_date ? l_date(issue.due_date) : nil,
291 291 issue.done_ratio,
292 292 l_datetime(issue.created_on),
293 293 l_datetime(issue.updated_on)
294 294 ]
295 295 for custom_field in @project.all_custom_fields
296 296 fields << (show_value issue.custom_value_for(custom_field))
297 297 end
298 298 csv << fields.collect {|c| ic.iconv(c.to_s) }
299 299 end
300 300 end
301 301 export.rewind
302 302 send_data(export.read, :type => 'text/csv; header=present', :filename => 'export.csv')
303 303 end
304 304
305 305 # Export filtered/sorted issues to PDF
306 306 def export_issues_pdf
307 307 sort_init 'issues.id', 'desc'
308 308 sort_update
309 309
310 310 retrieve_query
311 311 render :action => 'list_issues' and return unless @query.valid?
312 312
313 313 @issues = Issue.find :all, :order => sort_clause,
314 314 :include => [ :author, :status, :tracker, :project, :custom_values ],
315 315 :conditions => @query.statement
316 316
317 317 @options_for_rfpdf ||= {}
318 318 @options_for_rfpdf[:file_name] = "export.pdf"
319 319 render :layout => false
320 320 end
321 321
322 322 def move_issues
323 323 @issues = @project.issues.find(params[:issue_ids]) if params[:issue_ids]
324 324 redirect_to :action => 'list_issues', :id => @project and return unless @issues
325 325 @projects = []
326 326 # find projects to which the user is allowed to move the issue
327 327 @logged_in_user.memberships.each {|m| @projects << m.project if Permission.allowed_to_role("projects/move_issues", m.role_id)}
328 328 # issue can be moved to any tracker
329 329 @trackers = Tracker.find(:all)
330 330 if request.post? and params[:new_project_id] and params[:new_tracker_id]
331 331 new_project = Project.find(params[:new_project_id])
332 332 new_tracker = Tracker.find(params[:new_tracker_id])
333 333 @issues.each { |i|
334 334 # project dependent properties
335 335 unless i.project_id == new_project.id
336 336 i.category = nil
337 337 i.fixed_version = nil
338 338 end
339 339 # move the issue
340 340 i.project = new_project
341 341 i.tracker = new_tracker
342 342 i.save
343 343 }
344 344 flash[:notice] = l(:notice_successful_update)
345 345 redirect_to :action => 'list_issues', :id => @project
346 346 end
347 347 end
348 348
349 349 def add_query
350 350 @query = Query.new(params[:query])
351 351 @query.project = @project
352 352 @query.user = logged_in_user
353 353
354 354 params[:fields].each do |field|
355 355 @query.add_filter(field, params[:operators][field], params[:values][field])
356 356 end if params[:fields]
357 357
358 358 if request.post? and @query.save
359 359 flash[:notice] = l(:notice_successful_create)
360 360 redirect_to :controller => 'reports', :action => 'issue_report', :id => @project
361 361 end
362 362 render :layout => false if request.xhr?
363 363 end
364 364
365 365 # Add a news to @project
366 366 def add_news
367 367 @news = News.new(:project => @project)
368 368 if request.post?
369 369 @news.attributes = params[:news]
370 370 @news.author_id = self.logged_in_user.id if self.logged_in_user
371 371 if @news.save
372 372 flash[:notice] = l(:notice_successful_create)
373 373 redirect_to :action => 'list_news', :id => @project
374 374 end
375 375 end
376 376 end
377 377
378 378 # Show news list of @project
379 379 def list_news
380 380 @news_pages, @news = paginate :news, :per_page => 10, :conditions => ["project_id=?", @project.id], :include => :author, :order => "news.created_on DESC"
381 381 render :action => "list_news", :layout => false if request.xhr?
382 382 end
383 383
384 384 def add_file
385 385 if request.post?
386 386 @version = @project.versions.find_by_id(params[:version_id])
387 387 # Save the attachments
388 388 params[:attachments].each { |a|
389 389 Attachment.create(:container => @version, :file => a, :author => logged_in_user) unless a.size == 0
390 390 } if params[:attachments] and params[:attachments].is_a? Array
391 391 redirect_to :controller => 'projects', :action => 'list_files', :id => @project
392 392 end
393 393 @versions = @project.versions
394 394 end
395 395
396 396 def list_files
397 397 @versions = @project.versions
398 398 end
399 399
400 400 # Show changelog for @project
401 401 def changelog
402 402 @trackers = Tracker.find(:all, :conditions => ["is_in_chlog=?", true])
403 403 if request.get?
404 404 @selected_tracker_ids = @trackers.collect {|t| t.id.to_s }
405 405 else
406 406 @selected_tracker_ids = params[:tracker_ids].collect { |id| id.to_i.to_s } if params[:tracker_ids] and params[:tracker_ids].is_a? Array
407 407 end
408 408 @selected_tracker_ids ||= []
409 409 @fixed_issues = @project.issues.find(:all,
410 410 :include => [ :fixed_version, :status, :tracker ],
411 411 :conditions => [ "issue_statuses.is_closed=? and issues.tracker_id in (#{@selected_tracker_ids.join(',')}) and issues.fixed_version_id is not null", true],
412 412 :order => "versions.effective_date DESC, issues.id DESC"
413 413 ) unless @selected_tracker_ids.empty?
414 414 @fixed_issues ||= []
415 415 end
416 416
417 417 def activity
418 418 if params[:year] and params[:year].to_i > 1900
419 419 @year = params[:year].to_i
420 420 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
421 421 @month = params[:month].to_i
422 422 end
423 423 end
424 424 @year ||= Date.today.year
425 425 @month ||= Date.today.month
426 426
427 427 @date_from = Date.civil(@year, @month, 1)
428 428 @date_to = (@date_from >> 1)-1
429 429
430 430 @events_by_day = {}
431 431
432 432 unless params[:show_issues] == "0"
433 433 @project.issues.find(:all, :include => [:author, :status], :conditions => ["issues.created_on>=? and issues.created_on<=?", @date_from, @date_to] ).each { |i|
434 434 @events_by_day[i.created_on.to_date] ||= []
435 435 @events_by_day[i.created_on.to_date] << i
436 436 }
437 437 @show_issues = 1
438 438 end
439 439
440 440 unless params[:show_news] == "0"
441 441 @project.news.find(:all, :conditions => ["news.created_on>=? and news.created_on<=?", @date_from, @date_to], :include => :author ).each { |i|
442 442 @events_by_day[i.created_on.to_date] ||= []
443 443 @events_by_day[i.created_on.to_date] << i
444 444 }
445 445 @show_news = 1
446 446 end
447 447
448 448 unless params[:show_files] == "0"
449 449 Attachment.find(:all, :select => "attachments.*", :joins => "LEFT JOIN versions ON versions.id = attachments.container_id", :conditions => ["attachments.container_type='Version' and versions.project_id=? and attachments.created_on>=? and attachments.created_on<=?", @project.id, @date_from, @date_to], :include => :author ).each { |i|
450 450 @events_by_day[i.created_on.to_date] ||= []
451 451 @events_by_day[i.created_on.to_date] << i
452 452 }
453 453 @show_files = 1
454 454 end
455 455
456 456 unless params[:show_documents] == "0"
457 457 @project.documents.find(:all, :conditions => ["documents.created_on>=? and documents.created_on<=?", @date_from, @date_to] ).each { |i|
458 458 @events_by_day[i.created_on.to_date] ||= []
459 459 @events_by_day[i.created_on.to_date] << i
460 460 }
461 461 Attachment.find(:all, :select => "attachments.*", :joins => "LEFT JOIN documents ON documents.id = attachments.container_id", :conditions => ["attachments.container_type='Document' and documents.project_id=? and attachments.created_on>=? and attachments.created_on<=?", @project.id, @date_from, @date_to], :include => :author ).each { |i|
462 462 @events_by_day[i.created_on.to_date] ||= []
463 463 @events_by_day[i.created_on.to_date] << i
464 464 }
465 465 @show_documents = 1
466 466 end
467 467
468 468 render :layout => false if request.xhr?
469 469 end
470 470
471 471 def calendar
472 472 if params[:year] and params[:year].to_i > 1900
473 473 @year = params[:year].to_i
474 474 if params[:month] and params[:month].to_i > 0 and params[:month].to_i < 13
475 475 @month = params[:month].to_i
476 476 end
477 477 end
478 478 @year ||= Date.today.year
479 479 @month ||= Date.today.month
480 480
481 481 @date_from = Date.civil(@year, @month, 1)
482 482 @date_to = (@date_from >> 1)-1
483 483 # start on monday
484 484 @date_from = @date_from - (@date_from.cwday-1)
485 485 # finish on sunday
486 486 @date_to = @date_to + (7-@date_to.cwday)
487 487
488 488 @issues = @project.issues.find(:all, :include => [:tracker, :status, :assigned_to, :priority], :conditions => ["((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?))", @date_from, @date_to, @date_from, @date_to])
489 489 render :layout => false if request.xhr?
490 490 end
491 491
492 492 def gantt
493 493 if params[:year] and params[:year].to_i >0
494 494 @year_from = params[:year].to_i
495 495 if params[:month] and params[:month].to_i >=1 and params[:month].to_i <= 12
496 496 @month_from = params[:month].to_i
497 497 else
498 498 @month_from = 1
499 499 end
500 500 else
501 501 @month_from ||= (Date.today << 1).month
502 502 @year_from ||= (Date.today << 1).year
503 503 end
504 504
505 505 @zoom = (params[:zoom].to_i > 0 and params[:zoom].to_i < 5) ? params[:zoom].to_i : 2
506 506 @months = (params[:months].to_i > 0 and params[:months].to_i < 25) ? params[:months].to_i : 6
507 507
508 508 @date_from = Date.civil(@year_from, @month_from, 1)
509 509 @date_to = (@date_from >> @months) - 1
510 510 @issues = @project.issues.find(:all, :order => "start_date, due_date", :include => [:tracker, :status, :assigned_to, :priority], :conditions => ["(((start_date>=? and start_date<=?) or (due_date>=? and due_date<=?) or (start_date<? and due_date>?)) and start_date is not null and due_date is not null)", @date_from, @date_to, @date_from, @date_to, @date_from, @date_to])
511 511
512 512 if params[:output]=='pdf'
513 513 @options_for_rfpdf ||= {}
514 514 @options_for_rfpdf[:file_name] = "gantt.pdf"
515 515 render :template => "projects/gantt.rfpdf", :layout => false
516 516 else
517 517 render :template => "projects/gantt.rhtml"
518 518 end
519 519 end
520 520
521 521 private
522 522 # Find project of id params[:id]
523 523 # if not found, redirect to project list
524 524 # Used as a before_filter
525 525 def find_project
526 526 @project = Project.find(params[:id])
527 527 @html_title = @project.name
528 528 rescue ActiveRecord::RecordNotFound
529 529 render_404
530 530 end
531 531
532 532 # Retrieve query from session or build a new query
533 533 def retrieve_query
534 534 if params[:query_id]
535 535 @query = @project.queries.find(params[:query_id])
536 536 else
537 537 if params[:set_filter] or !session[:query] or session[:query].project_id != @project.id
538 538 # Give it a name, required to be valid
539 539 @query = Query.new(:name => "_")
540 540 @query.project = @project
541 541 if params[:fields] and params[:fields].is_a? Array
542 542 params[:fields].each do |field|
543 543 @query.add_filter(field, params[:operators][field], params[:values][field])
544 544 end
545 545 else
546 546 @query.available_filters.keys.each do |field|
547 547 @query.add_short_filter(field, params[field]) if params[field]
548 548 end
549 549 end
550 550 session[:query] = @query
551 551 else
552 552 @query = session[:query]
553 553 end
554 554 end
555 555 end
556 556 end
@@ -1,130 +1,142
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 require "digest/sha1"
19 19
20 20 class User < ActiveRecord::Base
21 21 has_many :memberships, :class_name => 'Member', :include => [ :project, :role ], :dependent => :delete_all
22 22 has_many :projects, :through => :memberships
23 23 has_many :custom_values, :dependent => :delete_all, :as => :customized
24 24 has_one :preference, :dependent => :destroy, :class_name => 'UserPreference'
25 25 belongs_to :auth_source
26 26
27 27 attr_accessor :password, :password_confirmation
28 28 attr_accessor :last_before_login_on
29 29 # Prevents unauthorized assignments
30 30 attr_protected :login, :admin, :password, :password_confirmation, :hashed_password
31 31
32 32 validates_presence_of :login, :firstname, :lastname, :mail
33 33 validates_uniqueness_of :login, :mail
34 34 # Login must contain lettres, numbers, underscores only
35 35 validates_format_of :firstname, :lastname, :with => /^[\w\s\'\-]*$/i
36 36 validates_format_of :login, :with => /^[a-z0-9_\-@\.]+$/i
37 37 validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
38 38 # Password length between 4 and 12
39 39 validates_length_of :password, :in => 4..12, :allow_nil => true
40 40 validates_confirmation_of :password, :allow_nil => true
41 41 validates_associated :custom_values, :on => :update
42 42
43 43 # Account statuses
44 44 STATUS_ACTIVE = 1
45 45 STATUS_REGISTERED = 2
46 46 STATUS_LOCKED = 3
47 47
48 48 def before_save
49 49 # update hashed_password if password was set
50 50 self.hashed_password = User.hash_password(self.password) if self.password
51 51 end
52
52
53 def self.active
54 with_scope :find => { :conditions => [ "status = ?", STATUS_ACTIVE ] } do
55 yield
56 end
57 end
58
59 def self.find_active(*args)
60 active do
61 find(*args)
62 end
63 end
64
53 65 # Returns the user that matches provided login and password, or nil
54 66 def self.try_to_login(login, password)
55 67 user = find(:first, :conditions => ["login=?", login])
56 68 if user
57 69 # user is already in local database
58 70 return nil if !user.active?
59 71 if user.auth_source
60 72 # user has an external authentication method
61 73 return nil unless user.auth_source.authenticate(login, password)
62 74 else
63 75 # authentication with local password
64 76 return nil unless User.hash_password(password) == user.hashed_password
65 77 end
66 78 else
67 79 # user is not yet registered, try to authenticate with available sources
68 80 attrs = AuthSource.authenticate(login, password)
69 81 if attrs
70 82 onthefly = new(*attrs)
71 83 onthefly.login = login
72 84 onthefly.language = Setting.default_language
73 85 if onthefly.save
74 86 user = find(:first, :conditions => ["login=?", login])
75 87 logger.info("User '#{user.login}' created on the fly.") if logger
76 88 end
77 89 end
78 90 end
79 91 user.update_attribute(:last_login_on, Time.now) if user
80 92 user
81 93
82 94 rescue => text
83 95 raise text
84 96 end
85 97
86 98 # Return user's full name for display
87 99 def display_name
88 100 firstname + " " + lastname
89 101 end
90 102
91 103 def name
92 104 display_name
93 105 end
94 106
95 107 def active?
96 108 self.status == STATUS_ACTIVE
97 109 end
98 110
99 111 def registered?
100 112 self.status == STATUS_REGISTERED
101 113 end
102 114
103 115 def locked?
104 116 self.status == STATUS_LOCKED
105 117 end
106 118
107 119 def check_password?(clear_password)
108 120 User.hash_password(clear_password) == self.hashed_password
109 121 end
110 122
111 123 def role_for_project(project_id)
112 124 @role_for_projects ||=
113 125 begin
114 126 roles = {}
115 127 self.memberships.each { |m| roles.store m.project_id, m.role_id }
116 128 roles
117 129 end
118 130 @role_for_projects[project_id]
119 131 end
120 132
121 133 def pref
122 134 self.preference ||= UserPreference.new(:user => self)
123 135 end
124 136
125 137 private
126 138 # Return password digest
127 139 def self.hash_password(clear_password)
128 140 Digest::SHA1.hexdigest(clear_password || "")
129 141 end
130 142 end
General Comments 0
You need to be logged in to leave comments. Login now