# action can be:
# * a parameter-like Hash (eg. :controller => 'projects', :action => 'edit')
# * a permission Symbol (eg. :edit_project)
- def allowed_to?(action, project)
- # No action allowed on archived projects
- return false unless project.active?
- # No action allowed on disabled modules
- return false unless project.allows_to?(action)
- # Admin users are authorized for anything else
- return true if admin?
-
- role = role_for_project(project)
- return false unless role
- role.allowed_to?(action) && (project.is_public? || role.member?)
+ def allowed_to?(action, project, options={})
+ if project
+ # No action allowed on archived projects
+ return false unless project.active?
+ # No action allowed on disabled modules
+ return false unless project.allows_to?(action)
+ # Admin users are authorized for anything else
+ return true if admin?
+
+ role = role_for_project(project)
+ return false unless role
+ role.allowed_to?(action) && (project.is_public? || role.member?)
+
+ elsif options[:global]
+ # authorize if user has at least one role that has this permission
+ roles = memberships.collect {|m| m.role}.uniq
+ roles.detect {|r| r.allowed_to?(action)}
+ else
+ false
+ end
end
def self.current=(user)
User.current = nil
end
- def test_get_new
+ def test_get_new_project_query
@request.session[:user_id] = 2
get :new, :project_id => 1
assert_response :success
:disabled => nil }
end
+ def test_get_new_global_query
+ @request.session[:user_id] = 2
+ get :new
+ assert_response :success
+ assert_template 'new'
+ assert_no_tag :tag => 'input', :attributes => { :type => 'checkbox',
+ :name => 'query[is_public]' }
+ assert_tag :tag => 'input', :attributes => { :type => 'checkbox',
+ :name => 'query_is_for_all',
+ :checked => 'checked',
+ :disabled => nil }
+ end
+
def test_new_project_public_query
@request.session[:user_id] = 2
post :new,
:fields => ["status_id", "assigned_to_id"],
:operators => {"assigned_to_id" => "=", "status_id" => "o"},
:values => { "assigned_to_id" => ["1"], "status_id" => ["1"]},
- :query => {"name" => "test_new_project_public_query", "is_public" => "1"},
- :column_names => ["", "tracker", "status", "priority", "subject", "updated_on", "category"]
+ :query => {"name" => "test_new_project_public_query", "is_public" => "1"}
q = Query.find_by_name('test_new_project_public_query')
assert_redirected_to :controller => 'issues', :action => 'index', :query_id => q
:fields => ["status_id", "assigned_to_id"],
:operators => {"assigned_to_id" => "=", "status_id" => "o"},
:values => { "assigned_to_id" => ["1"], "status_id" => ["1"]},
- :query => {"name" => "test_new_project_private_query", "is_public" => "1"},
- :column_names => ["", "tracker", "status", "priority", "subject", "updated_on", "category"]
+ :query => {"name" => "test_new_project_private_query", "is_public" => "1"}
q = Query.find_by_name('test_new_project_private_query')
assert_redirected_to :controller => 'issues', :action => 'index', :query_id => q
assert q.valid?
end
+ def test_new_global_private_query_with_custom_columns
+ @request.session[:user_id] = 3
+ post :new,
+ :confirm => '1',
+ :fields => ["status_id", "assigned_to_id"],
+ :operators => {"assigned_to_id" => "=", "status_id" => "o"},
+ :values => { "assigned_to_id" => ["me"], "status_id" => ["1"]},
+ :query => {"name" => "test_new_global_private_query", "is_public" => "1", "column_names" => ["", "tracker", "subject", "priority", "category"]}
+
+ q = Query.find_by_name('test_new_global_private_query')
+ assert_redirected_to :controller => 'issues', :action => 'index', :query_id => q
+ assert !q.is_public?
+ assert !q.has_default_columns?
+ assert_equal [:tracker, :subject, :priority, :category], q.columns.collect {|c| c.name}
+ assert q.valid?
+ end
+
def test_get_edit_global_public_query
@request.session[:user_id] = 1
get :edit, :id => 4
:fields => ["status_id", "assigned_to_id"],
:operators => {"assigned_to_id" => "=", "status_id" => "o"},
:values => { "assigned_to_id" => ["1"], "status_id" => ["1"]},
- :query => {"name" => "test_edit_global_public_query", "is_public" => "1"},
- :column_names => ["", "tracker", "status", "priority", "subject", "updated_on", "category"]
+ :query => {"name" => "test_edit_global_public_query", "is_public" => "1"}
assert_redirected_to :controller => 'issues', :action => 'index', :query_id => 4
q = Query.find_by_name('test_edit_global_public_query')
:fields => ["status_id", "assigned_to_id"],
:operators => {"assigned_to_id" => "=", "status_id" => "o"},
:values => { "assigned_to_id" => ["me"], "status_id" => ["1"]},
- :query => {"name" => "test_edit_global_private_query", "is_public" => "1"},
- :column_names => ["", "tracker", "status", "priority", "subject", "updated_on", "category"]
+ :query => {"name" => "test_edit_global_private_query", "is_public" => "1"}
assert_redirected_to :controller => 'issues', :action => 'index', :query_id => 3
q = Query.find_by_name('test_edit_global_private_query')