]> source.dussan.org Git - redmine.git/commitdiff
Merged r22063 from trunk to 4.2-stable (#38070).
authorGo MAEDA <maeda@farend.jp>
Thu, 19 Jan 2023 11:57:35 +0000 (11:57 +0000)
committerGo MAEDA <maeda@farend.jp>
Thu, 19 Jan 2023 11:57:35 +0000 (11:57 +0000)
git-svn-id: https://svn.redmine.org/redmine/branches/4.2-stable@22065 e93f8b46-1217-0410-a6f0-8f06a7374b81

app/models/role.rb
test/unit/role_test.rb

index 54d69c24fba66eaa381f01f6e191f39cd09fc0df..5cdc593e9c66e34c6945b9f1f5d6fe234dd587d7 100644 (file)
@@ -226,6 +226,8 @@ class Role < ActiveRecord::Base
   # Returns true if tracker_id belongs to the list of
   # trackers for which permission is given
   def permissions_tracker_ids?(permission, tracker_id)
+    return false unless has_permission?(permission)
+
     permissions_tracker_ids(permission).include?(tracker_id)
   end
 
@@ -239,6 +241,8 @@ class Role < ActiveRecord::Base
 
   # Returns true if permission is given for all trackers
   def permissions_all_trackers?(permission)
+    return false unless has_permission?(permission)
+
     permissions_all_trackers[permission.to_s].to_s != '0'
   end
 
index 472ff805f81b5d3665fb51864d7df8430763aacb..7e907233d15e4bcf88a6aa39b1f383f33b083f1b 100644 (file)
@@ -101,6 +101,82 @@ class RoleTest < ActiveSupport::TestCase
     assert_equal false, role.has_permission?(:delete_issues)
   end
 
+  def test_permissions_all_trackers?
+    role = Role.create!(:name => 'Test', :permissions => [:view_issues])
+    assert_equal true, role.permissions_all_trackers?(:view_issues)
+    assert_equal false, role.permissions_all_trackers?(:edit_issues)
+
+    role.set_permission_trackers :view_issues, [1]
+    role.set_permission_trackers :edit_issues, [1]
+    assert_equal false, role.permissions_all_trackers?(:view_issues)
+    assert_equal false, role.permissions_all_trackers?(:edit_issues)
+
+    role.set_permission_trackers :view_issues, :all
+    role.set_permission_trackers :edit_issues, :all
+    assert_equal true, role.permissions_all_trackers?(:view_issues)
+    assert_equal false, role.permissions_all_trackers?(:edit_issues)
+  end
+
+  def test_permissions_all_trackers_considers_base_permission
+    role = Role.create!(:name => 'Test', :permissions => [:view_issues])
+    assert_equal true, role.permissions_all_trackers?(:view_issues)
+
+    role.remove_permission!(:view_issues)
+    assert_equal false, role.permissions_all_trackers?(:view_issues)
+  end
+
+  def test_permissions_tracker_ids?
+    role = Role.create!(:name => 'Test', :permissions => [:view_issues])
+    assert_equal false, role.permissions_tracker_ids?(:view_issues, 1)
+    assert_equal false, role.permissions_tracker_ids?(:edit_issues, 1)
+
+    role.set_permission_trackers :view_issues, [1, 2, 3]
+    role.set_permission_trackers :edit_issues, [1, 2, 3]
+
+    assert_equal true, role.permissions_tracker_ids?(:view_issues, 1)
+    assert_equal false, role.permissions_tracker_ids?(:edit_issues, 1)
+  end
+
+  def test_permissions_tracker_ids_considers_base_permission
+    role = Role.create!(:name => 'Test', :permissions => [:view_issues])
+    role.set_permission_trackers :view_issues, [1, 2, 3]
+    assert_equal true, role.permissions_tracker_ids?(:view_issues, 1)
+
+    role.remove_permission!(:view_issues)
+    assert_equal false, role.permissions_tracker_ids?(:view_issues, 1)
+  end
+
+  def test_permissions_tracker?
+    tracker = Tracker.find(1)
+    role = Role.create!(:name => 'Test', :permissions => [:view_issues])
+    assert_equal true, role.permissions_tracker?(:view_issues, 1)
+    assert_equal false, role.permissions_tracker?(:edit_issues, 1)
+
+    role.set_permission_trackers :view_issues, [1]
+    role.set_permission_trackers :edit_issues, [1]
+    assert_equal true, role.permissions_tracker?(:view_issues, tracker)
+    assert_equal false, role.permissions_tracker?(:edit_issues, tracker)
+
+    role.set_permission_trackers :view_issues, [2]
+    role.set_permission_trackers :edit_issues, [2]
+    assert_equal false, role.permissions_tracker?(:view_issues, tracker)
+    assert_equal false, role.permissions_tracker?(:edit_issues, tracker)
+
+    role.set_permission_trackers :view_issues, :all
+    role.set_permission_trackers :edit_issues, :all
+    assert_equal true, role.permissions_tracker?(:view_issues, tracker)
+    assert_equal false, role.permissions_tracker?(:edit_issues, tracker)
+  end
+
+  def test_permissions_tracker_considers_base_permission
+    role = Role.create!(:name => 'Test', :permissions => [:edit_isues])
+    role.set_permission_trackers :view_issues, [1, 2, 3]
+    assert_equal false, role.permissions_tracker_ids?(:view_issues, 1)
+
+    role.set_permission_trackers :view_issues, :all
+    assert_equal false, role.permissions_tracker_ids?(:view_issues, 1)
+  end
+
   def test_has_permission_without_permissions
     role = Role.create!(:name => 'Test')
     assert_equal false, role.has_permission?(:delete_issues)