Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

refactor(glossary): let GlossaryPolicy hold the rule that admins need no row

Guido Boyke85def04a188a
diff --git a/app/models/role.rb b/app/models/role.rb
index f6b73bd..4df89ca 100644
--- a/app/models/role.rb
+++ b/app/models/role.rb
@@ -76,6 +76,14 @@ class Role < ApplicationRecord
def has_permission?(category, action)
return true if account_admin?
+ permission_row_allows?(category, action)
+ end
+
+ # Whether a stored permission row of this role allows the action. Unlike
+ # has_permission?, an account admin's implicit yes is left out: a policy that
+ # names the admin rule itself (GlossaryPolicy) asks this, so its own admin
+ # answer is the only door for an admin.
+ def permission_row_allows?(category, action)
permissions.load unless permissions.loaded?
permissions.any? { |p| p.category == category.to_s && p.action == action.to_s && p.allowed }
end
diff --git a/app/policies/glossary_policy.rb b/app/policies/glossary_policy.rb
index 856ef72..5355a04 100644
--- a/app/policies/glossary_policy.rb
+++ b/app/policies/glossary_policy.rb
@@ -30,19 +30,28 @@ class GlossaryPolicy < ApplicationPolicy
def self.view_allowed?(user, organisation)
return false unless user.active_member_of_organisation?(organisation)
- user.account_admin_in?(organisation) ||
- Permission.granting_actions('glossary', 'view_glossary').any? do |action|
- user.has_permission_in_organisation?(organisation, 'glossary', action)
- end
+ user.account_admin_in?(organisation) || granted?(user, organisation, 'view_glossary')
end
def self.manage_allowed?(user, organisation)
return false unless user.active_member_of_organisation?(organisation)
- user.account_admin_in?(organisation) ||
- user.has_permission_in_organisation?(organisation, 'glossary', 'manage_glossary')
+ user.account_admin_in?(organisation) || granted?(user, organisation, 'manage_glossary')
end
+ # A permission ROW of the member's role grants the action, or an action
+ # that includes it (Permission::IMPLIED_BY). Read through
+ # Role#permission_row_allows?, not has_permission?, which answers yes for an
+ # account admin by itself: the admin answer above is the only door for an
+ # admin, so "admins need no row" (E2) is this policy's own rule.
+ def self.granted?(user, organisation, action)
+ role = user.role_in_organisation(organisation)
+ return false unless role
+
+ Permission.granting_actions('glossary', action).any? { |granting| role.permission_row_allows?('glossary', granting) }
+ end
+ private_class_method :granted?
+
def index?
can_view?
end
diff --git a/spec/models/role_spec.rb b/spec/models/role_spec.rb
index 8ba1b01..d800064 100644
--- a/spec/models/role_spec.rb
+++ b/spec/models/role_spec.rb
@@ -213,6 +213,26 @@ RSpec.describe Role, type: :model do
expect(admin_role.has_permission?('any_category', 'any_action')).to be true
end
end
+
+ # The rows alone, without the account admin's implicit yes (GlossaryPolicy
+ # names the admin rule itself).
+ describe '#permission_row_allows?' do
+ let(:role) { create(:role, organisation: organisation) }
+ let(:admin_role) { create(:admin_role, organisation: organisation) }
+
+ it 'answers from an allowed row' do
+ create(:permission, role: role, category: 'glossary', action: 'view_glossary', allowed: true)
+ create(:permission, role: role, category: 'glossary', action: 'manage_glossary', allowed: false)
+
+ expect(role.permission_row_allows?('glossary', 'view_glossary')).to be true
+ expect(role.permission_row_allows?('glossary', 'manage_glossary')).to be false
+ end
+
+ it 'says no for an account admin without a row, where has_permission? says yes' do
+ expect(admin_role.permission_row_allows?('glossary', 'view_glossary')).to be false
+ expect(admin_role.has_permission?('glossary', 'view_glossary')).to be true
+ end
+ end
end
describe 'class methods' do