-
Notifications
You must be signed in to change notification settings - Fork 0
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
add allow_with_resource #8
Changes from 11 commits
d8a274a
3a59658
2e76f3b
792dc90
912949f
1fb4689
b7c027c
267c31b
f8ad77d
87839d3
1f1b372
b81174c
72713eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,6 +17,10 @@ def self.included(sub) | |
unless sub.method_defined? :current_user_roles | ||
define_method(:current_user_roles) { [] } | ||
end | ||
|
||
unless sub.method_defined? :role_resource | ||
define_method(:role_resource) { [] } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, you wanna change this to an empty hash. |
||
end | ||
sub.send :extend, ClassMethods | ||
end | ||
|
||
|
@@ -31,8 +35,8 @@ def failed_role_check! | |
def current_roles | ||
return [] if rolypoly_gatekeepers.empty? | ||
current_gatekeepers.reduce([]) { |array, gatekeeper| | ||
if gatekeeper.role? current_user_roles | ||
array += Array(gatekeeper.allowed_roles(current_user_roles, action_name)) | ||
if gatekeeper.role?(current_user_roles, role_resource) | ||
array += Array(gatekeeper.allowed_roles(current_user_roles, action_name, role_resource)) | ||
end | ||
array | ||
} | ||
|
@@ -52,7 +56,7 @@ def current_gatekeepers | |
def rolypoly_role_access? | ||
rolypoly_gatekeepers.empty? || | ||
rolypoly_gatekeepers.any? { |gatekeeper| | ||
gatekeeper.allow? current_roles, action_name | ||
gatekeeper.allow?(current_roles, action_name, role_resource) | ||
} | ||
end | ||
private :rolypoly_role_access? | ||
|
@@ -75,6 +79,10 @@ def allow(*roles) | |
build_gatekeeper roles, nil | ||
end | ||
|
||
def allow_with_resource(*roles) | ||
build_gatekeeper roles, nil, true | ||
end | ||
|
||
def publicize(*actions) | ||
restrict(*actions).to_none | ||
end | ||
|
@@ -90,8 +98,8 @@ def try_super(mname) | |
end | ||
end | ||
|
||
def build_gatekeeper(roles, actions) | ||
RoleGatekeeper.new(roles, actions).tap { |gatekeeper| | ||
def build_gatekeeper(roles, actions, require_resource = false) | ||
RoleGatekeeper.new(roles, actions, require_resource).tap { |gatekeeper| | ||
rolypoly_gatekeepers << gatekeeper | ||
} | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ module Rolypoly | |
subject { example_controller } | ||
it { should respond_to :restrict } | ||
it { should respond_to :allow } | ||
it { should respond_to :allow_with_resource } | ||
|
||
describe "setting up with DSL" do | ||
describe "from allow side" do | ||
|
@@ -76,6 +77,52 @@ module Rolypoly | |
end | ||
end | ||
end | ||
|
||
describe "from allow_with_resource side" do | ||
let(:controller_instance) { subject.new } | ||
let(:admin_role) { RoleObject.new(:admin) } | ||
let(:scorekeeper_role) { RoleObject.new(:scorekeeper) } | ||
let(:current_user_roles) { [admin_role, scorekeeper_role] } | ||
let(:role_resource) { {resource: 123} } | ||
let(:check_access!) { controller_instance.rolypoly_check_role_access! } | ||
|
||
before do | ||
subject.allow_with_resource(:admin).to_access(:index) | ||
subject.publicize(:landing) | ||
allow(admin_role).to receive(:resource?).and_return true | ||
allow(controller_instance).to receive(:current_user_roles).and_return(current_user_roles) | ||
allow(controller_instance).to receive(:action_name).and_return(action_name) | ||
allow(controller_instance).to receive(:role_resource).and_return(role_resource) | ||
end | ||
|
||
describe "#index" do | ||
let(:action_name) { "index" } | ||
|
||
it { expect(controller_instance).to_not be_public } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
it { expect{ check_access! }.not_to raise_error } | ||
it { expect(controller_instance.current_roles).to eq([RoleObject.new(:admin)])} | ||
end | ||
|
||
describe "#show" do | ||
let(:action_name) { "show" } | ||
|
||
it { expect{ check_access! }.to raise_error(Rolypoly::FailedRoleCheckError)} | ||
it { expect(controller_instance).to_not be_public } | ||
end | ||
|
||
describe "#landing" do | ||
let(:action_name) { "landing" } | ||
|
||
it { expect{ check_access! }.not_to raise_error } | ||
|
||
describe "with no role" do | ||
let(:current_roles) { [] } | ||
|
||
it { expect { check_access! }.not_to raise_error } | ||
it { expect(controller_instance).to be_public } | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, as we discussed, it doesn't really require a resource check. It will check resources against an empty
role_resource
.