##// END OF EJS Templates
Various code cleaning, mainly on User, Permission and IssueStatus models....
Jean-Philippe Lang -
r411:e227b9297252
parent child
Show More
@@ -86,8 +86,8 class ApplicationController < ActionController::Base
86 # admin is always authorized
86 # admin is always authorized
87 return true if self.logged_in_user.admin?
87 return true if self.logged_in_user.admin?
88 # if not admin, check membership permission
88 # if not admin, check membership permission
89 @user_membership ||= Member.find(:first, :conditions => ["user_id=? and project_id=?", self.logged_in_user.id, @project.id])
89 @user_membership ||= logged_in_user.role_for_project(@project)
90 if @user_membership and Permission.allowed_to_role( "%s/%s" % [ ctrl, action ], @user_membership.role_id )
90 if @user_membership and Permission.allowed_to_role( "%s/%s" % [ ctrl, action ], @user_membership )
91 return true
91 return true
92 end
92 end
93 render :nothing => true, :status => 403
93 render :nothing => true, :status => 403
@@ -84,7 +84,7 private
84 # project feed
84 # project feed
85 # check if project is public or if the user is a member
85 # check if project is public or if the user is a member
86 @project = Project.find(params[:project_id])
86 @project = Project.find(params[:project_id])
87 render(:nothing => true, :status => 403) and return false unless @project.is_public? || (@user && @user.role_for_project(@project.id))
87 render(:nothing => true, :status => 403) and return false unless @project.is_public? || (@user && @user.role_for_project(@project))
88 scope = ["#{Project.table_name}.id=?", params[:project_id].to_i]
88 scope = ["#{Project.table_name}.id=?", params[:project_id].to_i]
89 else
89 else
90 # global feed
90 # global feed
@@ -25,7 +25,7 class IssuesController < ApplicationController
25 include IfpdfHelper
25 include IfpdfHelper
26
26
27 def show
27 def show
28 @status_options = ([@issue.status] + @issue.status.workflows.find(:all, :order => 'position', :include => :new_status, :conditions => ["role_id=? and tracker_id=?", self.logged_in_user.role_for_project(@project.id), @issue.tracker.id]).collect{ |w| w.new_status }) if self.logged_in_user
28 @status_options = @issue.status.find_new_statuses_allowed_to(logged_in_user.role_for_project(@project), @issue.tracker) if logged_in_user
29 @custom_values = @issue.custom_values.find(:all, :include => :custom_field)
29 @custom_values = @issue.custom_values.find(:all, :include => :custom_field)
30 @journals_count = @issue.journals.count
30 @journals_count = @issue.journals.count
31 @journals = @issue.journals.find(:all, :include => [:user, :details], :limit => 15, :order => "#{Journal.table_name}.created_on desc")
31 @journals = @issue.journals.find(:all, :include => [:user, :details], :limit => 15, :order => "#{Journal.table_name}.created_on desc")
@@ -67,9 +67,6 class IssuesController < ApplicationController
67 def add_note
67 def add_note
68 unless params[:notes].empty?
68 unless params[:notes].empty?
69 journal = @issue.init_journal(self.logged_in_user, params[:notes])
69 journal = @issue.init_journal(self.logged_in_user, params[:notes])
70 #@history = @issue.histories.build(params[:history])
71 #@history.author_id = self.logged_in_user.id if self.logged_in_user
72 #@history.status = @issue.status
73 if @issue.save
70 if @issue.save
74 flash[:notice] = l(:notice_successful_update)
71 flash[:notice] = l(:notice_successful_update)
75 Mailer.deliver_issue_edit(journal) if Permission.find_by_controller_and_action(params[:controller], params[:action]).mail_enabled?
72 Mailer.deliver_issue_edit(journal) if Permission.find_by_controller_and_action(params[:controller], params[:action]).mail_enabled?
@@ -82,17 +79,10 class IssuesController < ApplicationController
82 end
79 end
83
80
84 def change_status
81 def change_status
85 #@history = @issue.histories.build(params[:history])
82 @status_options = @issue.status.find_new_statuses_allowed_to(logged_in_user.role_for_project(@project), @issue.tracker) if logged_in_user
86 @status_options = ([@issue.status] + @issue.status.workflows.find(:all, :order => 'position', :include => :new_status, :conditions => ["role_id=? and tracker_id=?", self.logged_in_user.role_for_project(@project.id), @issue.tracker.id]).collect{ |w| w.new_status }) if self.logged_in_user
87 @new_status = IssueStatus.find(params[:new_status_id])
83 @new_status = IssueStatus.find(params[:new_status_id])
88 if params[:confirm]
84 if params[:confirm]
89 begin
85 begin
90 #@history.author_id = self.logged_in_user.id if self.logged_in_user
91 #@issue.status = @history.status
92 #@issue.fixed_version_id = (params[:issue][:fixed_version_id])
93 #@issue.assigned_to_id = (params[:issue][:assigned_to_id])
94 #@issue.done_ratio = (params[:issue][:done_ratio])
95 #@issue.lock_version = (params[:issue][:lock_version])
96 journal = @issue.init_journal(self.logged_in_user, params[:notes])
86 journal = @issue.init_journal(self.logged_in_user, params[:notes])
97 @issue.status = @new_status
87 @issue.status = @new_status
98 if @issue.update_attributes(params[:issue])
88 if @issue.update_attributes(params[:issue])
@@ -215,8 +215,7 class ProjectsController < ApplicationController
215
215
216 default_status = IssueStatus.default
216 default_status = IssueStatus.default
217 @issue = Issue.new(:project => @project, :tracker => @tracker, :status => default_status)
217 @issue = Issue.new(:project => @project, :tracker => @tracker, :status => default_status)
218 @allowed_statuses = [default_status] + default_status.workflows.find(:all, :order => 'position', :include => :new_status, :conditions => ["role_id=? and tracker_id=?", self.logged_in_user.role_for_project(@project.id), @issue.tracker.id]).collect{ |w| w.new_status }
218 @allowed_statuses = default_status.find_new_statuses_allowed_to(logged_in_user.role_for_project(@project), @issue.tracker) if logged_in_user
219
220 if request.get?
219 if request.get?
221 @issue.start_date = Date.today
220 @issue.start_date = Date.today
222 @custom_values = @project.custom_fields_for_issues(@tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue) }
221 @custom_values = @project.custom_fields_for_issues(@tracker).collect { |x| CustomValue.new(:custom_field => x, :customized => @issue) }
@@ -349,7 +348,7 class ProjectsController < ApplicationController
349 redirect_to :action => 'list_issues', :id => @project and return unless @issues
348 redirect_to :action => 'list_issues', :id => @project and return unless @issues
350 @projects = []
349 @projects = []
351 # find projects to which the user is allowed to move the issue
350 # find projects to which the user is allowed to move the issue
352 @logged_in_user.memberships.each {|m| @projects << m.project if Permission.allowed_to_role("projects/move_issues", m.role_id)}
351 @logged_in_user.memberships.each {|m| @projects << m.project if Permission.allowed_to_role("projects/move_issues", m.role)}
353 # issue can be moved to any tracker
352 # issue can be moved to any tracker
354 @trackers = Tracker.find(:all)
353 @trackers = Tracker.find(:all)
355 if request.post? and params[:new_project_id] and params[:new_tracker_id]
354 if request.post? and params[:new_project_id] and params[:new_tracker_id]
@@ -34,7 +34,7 module ApplicationHelper
34 return true
34 return true
35 end
35 end
36 # check if user is authorized
36 # check if user is authorized
37 if @logged_in_user and (@logged_in_user.admin? or Permission.allowed_to_role( "%s/%s" % [ controller, action ], @logged_in_user.role_for_project(@project.id) ) )
37 if @logged_in_user and (@logged_in_user.admin? or Permission.allowed_to_role( "%s/%s" % [ controller, action ], @logged_in_user.role_for_project(@project) ) )
38 return true
38 return true
39 end
39 end
40 return false
40 return false
@@ -36,12 +36,19 class IssueStatus < ActiveRecord::Base
36 end
36 end
37
37
38 # Returns an array of all statuses the given role can switch to
38 # Returns an array of all statuses the given role can switch to
39 # Uses association cache when called more than one time
39 def new_statuses_allowed_to(role, tracker)
40 def new_statuses_allowed_to(role, tracker)
40 statuses = []
41 new_statuses = [self] + workflows.select {|w| w.role_id == role.id && w.tracker_id == tracker.id}.collect{|w| w.new_status}
41 for workflow in self.workflows
42 new_statuses.sort{|x, y| x.position <=> y.position }
42 statuses << workflow.new_status if workflow.role_id == role.id and workflow.tracker_id == tracker.id
43 end
43 end unless role.nil? or tracker.nil?
44
44 statuses
45 # Same thing as above but uses a database query
46 # More efficient than the previous method if called just once
47 def find_new_statuses_allowed_to(role, tracker)
48 new_statuses = [self] + workflows.find(:all,
49 :include => :new_status,
50 :conditions => ["role_id=? and tracker_id=?", role.id, tracker.id]).collect{ |w| w.new_status }
51 new_statuses.sort{|x, y| x.position <=> y.position }
45 end
52 end
46
53
47 private
54 private
@@ -57,7 +57,7 class Permission < ActiveRecord::Base
57 find(:all, :include => :roles).each {|p| perms.store "#{p.controller}/#{p.action}", p.roles.collect {|r| r.id } }
57 find(:all, :include => :roles).each {|p| perms.store "#{p.controller}/#{p.action}", p.roles.collect {|r| r.id } }
58 perms
58 perms
59 end
59 end
60 allowed_to_public(action) or (@@cached_perms_for_roles[action] and @@cached_perms_for_roles[action].include? role)
60 allowed_to_public(action) or (role && @@cached_perms_for_roles[action] && @@cached_perms_for_roles[action].include?(role.id))
61 end
61 end
62
62
63 def self.allowed_to_role_expired
63 def self.allowed_to_role_expired
@@ -124,14 +124,8 class User < ActiveRecord::Base
124 User.hash_password(clear_password) == self.hashed_password
124 User.hash_password(clear_password) == self.hashed_password
125 end
125 end
126
126
127 def role_for_project(project_id)
127 def role_for_project(project)
128 @role_for_projects ||=
128 memberships.detect {|m| m.project_id == project.id}
129 begin
130 roles = {}
131 self.memberships.each { |m| roles.store m.project_id, m.role_id }
132 roles
133 end
134 @role_for_projects[project_id]
135 end
129 end
136
130
137 def pref
131 def pref
General Comments 0
You need to be logged in to leave comments. Login now