Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename acting_user accessor in policies #834

Merged
merged 2 commits into from
Mar 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/policies/announcement_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def permitted_attributes

class Scope < ApplicationPolicy::Scope
def resolve
can_admin? ? original_scope : original_scope.published
can_admin? ? scope : scope.published
end
end
end
18 changes: 9 additions & 9 deletions app/policies/application_policy.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
class ApplicationPolicy
module Utils
attr_reader :acting_user, :admin_param, :system_settings
attr_reader :user, :admin_param, :system_settings

def sys_admin?
acting_user && acting_user.sys_admin_role? && admin_param != 'false'
user && user.sys_admin_role? && admin_param != 'false'
end

def admin?
acting_user && acting_user.admin_role? && admin_param != 'false'
user && user.admin_role? && admin_param != 'false'
end

def can_admin?
Expand All @@ -30,24 +30,24 @@ def extract(context)
class Scope
include Utils

attr_reader :original_scope
attr_reader :scope

def initialize(context, original_scope)
@acting_user, @system_settings, @admin_param = extract context
@original_scope = original_scope
def initialize(context, scope)
@user, @system_settings, @admin_param = extract context
@scope = scope
end

# We default all permissions to false, and expect you to override as needed.
def resolve
original_scope.none
scope.none
end
end

attr_reader :record

# We've configured pundit to provide a user context (See https://github.com/varvet/pundit/#additional-context).
def initialize(context, record)
@acting_user, @system_settings, @admin_param = extract context
@user, @system_settings, @admin_param = extract context
@record = record
end

Expand Down
2 changes: 1 addition & 1 deletion app/policies/claim_policy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ClaimPolicy < ApplicationPolicy
def add?
acting_user.present? && system_settings.peer_to_peer?
user.present? && system_settings.peer_to_peer?
end
end
2 changes: 1 addition & 1 deletion app/policies/community_resource_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def permitted_attributes

class Scope < ApplicationPolicy::Scope
def resolve
can_admin? ? original_scope : original_scope.published
can_admin? ? scope : scope.published
end
end
end
11 changes: 3 additions & 8 deletions app/policies/glossary_policy.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
class GlossaryPolicy < ApplicationPolicy
def read?
true
end

def change?
acting_user && (acting_user.admin_role? || acting_user.sys_admin_role? )
end
end
def read?; true end
def change?; can_admin? end
end
4 changes: 2 additions & 2 deletions app/policies/nav_bar_policy.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class NavBarPolicy < ApplicationPolicy
def visible_buttons
visible = Set.new
visible << (acting_user ? 'Logout' : 'Login')
visible << 'Feedback' if acting_user
visible << (user ? 'Logout' : 'Login')
visible << 'Feedback' if user
visible << 'Contributions' if system_settings.peer_to_peer?
visible.merge %w[Contributions Matches Admin] if can_admin?
visible.to_a
Expand Down
19 changes: 10 additions & 9 deletions app/policies/person_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,38 @@ class Scope < Scope
def resolve
case
when can_admin?
original_scope.all
when acting_user.present?
original_scope.where(user_id: acting_user.id)
scope.all
when user.present?
scope.where(user_id: user.id)
else
original_scope.none
scope.none
end
end
end

def read?
person_attached_to_acting_user? || can_admin?
own_person? || can_admin?
end

def change?
person_attached_to_acting_user? || can_admin?
own_person? || can_admin?
end

def add?
person_attached_to_acting_user? || sys_admin?
own_person? || sys_admin?
end

def delete?
sys_admin?
end

private

def person
record
end

def person_attached_to_acting_user?
person.user_id == acting_user&.id
def own_person?
person.user_id == user&.id
end
end
16 changes: 8 additions & 8 deletions app/policies/user_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@ class Scope < Scope
def resolve
case
when can_admin?
original_scope.all
when acting_user.present?
original_scope.where(id: acting_user.id)
scope.all
when user.present?
scope.where(id: user.id)
else
original_scope.none
scope.none
end
end
end

def read?
target_user_is_acting_user? || can_admin?
own_user? || can_admin?
end

def change?
target_user_is_acting_user? || can_admin?
own_user? || can_admin?
end

def add?
Expand All @@ -33,7 +33,7 @@ def target_user
record
end

def target_user_is_acting_user?
acting_user.present? && target_user == acting_user
def own_user?
user.present? && target_user == user
end
end
7 changes: 0 additions & 7 deletions spec/policies/announcement_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
require 'rails_helper'

# FIXME: remove or consolidate this block once #834 is resolved
RSpec.configure do
Pundit::Matchers.configure do |config|
config.user_alias = :acting_user
end
end

RSpec.describe AnnouncementPolicy do
let(:context) { Context.new user: user }
let(:announcement) { double :announcement }
Expand Down
4 changes: 2 additions & 2 deletions spec/policies/application_policy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

describe 'initialization' do
it 'accepts Context and extracts its contents' do
expect(policy.acting_user).to be user
expect(policy.user).to be user
expect(policy.system_settings).to be system_settings
end

it 'also supports User instead of context' do
policy = ApplicationPolicy.new user, record
expect(policy.acting_user).to be user
expect(policy.user).to be user
expect(policy.system_settings).to be nil
end
end
Expand Down
7 changes: 0 additions & 7 deletions spec/policies/claim_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
require 'spec_helper'

# FIXME: remove or consolidate this block once #834 is resolved
RSpec.configure do
Pundit::Matchers.configure do |config|
config.user_alias = :acting_user
end
end

RSpec.describe ClaimPolicy do
let(:system_settings) { double :system_setting }
let(:context) { Context.new user: user, system_settings: system_settings }
Expand Down
7 changes: 0 additions & 7 deletions spec/policies/community_resource_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
require 'rails_helper'

# FIXME: remove or consolidate this block once #834 is resolved
RSpec.configure do
Pundit::Matchers.configure do |config|
config.user_alias = :acting_user
end
end

RSpec.describe CommunityResourcePolicy do
let(:context) { Context.new user: user }
let(:community_resource) { double :community_resource }
Expand Down
7 changes: 0 additions & 7 deletions spec/policies/contribution_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
require 'spec_helper'

# FIXME: remove or consolidate this block once #834 is resolved
RSpec.configure do
Pundit::Matchers.configure do |config|
config.user_alias = :acting_user
end
end

RSpec.describe ContributionPolicy do
let(:context) { Context.new user: user }
let(:contribution) { double :contribution }
Expand Down