Skip to content

Commit

Permalink
Add allowed actions (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
povilasjurcys authored May 10, 2023
1 parent 6f5c3fe commit 0ac6e7b
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

* Added: short-hand method for getting allowed actions
* Added: support for proc type validator options
* Added/Changed/Deprecated/Removed/Fixed/Security: YOUR CHANGE HERE

Expand Down
28 changes: 28 additions & 0 deletions docs/components/policy.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,24 @@ actions_policy.read.allowed? # => true
actions_policy.write.allowed? # ... depends on `admin?` result
```

In order to get all actions do:

```ruby
policy = UserPolicy.new(user, current_user)
actions_policy = policy.actions_policy

actions_policy.actions
```

In order to get all only allowed action names do:

```ruby
policy = UserPolicy.new(user, current_user)
actions_policy = policy.actions_policy

actions_policy.allowed_action_names
```

## Usage of Policy#attribute

Suppose we have policy like this:
Expand Down Expand Up @@ -160,6 +178,16 @@ attributes_policy.email.readable? # same as `allowed_to?(:read)`
attributes_policy.email.writable? # same as `allowed_to?(:write)`
```

In order to get all allowed attributes do:

```ruby
policy = UserPolicy.new(user, current_user)
attributes_policy = policy.attributes_policy

readable_attributes = attributes_policy.all_allowed_to(:read)
writable_attributes = attributes_policy.all_allowed_to(:write)
```

## Usage of Policy#protected_resource

Policy provides `#protected_resource` method which returns wrapped model instance and does not allow to view fields which current_user does not have access to. You must define `policy_target` in order to be able to use `protected_resource` feature
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ def method_missing(method_name)
policy_item(method_name.to_sym)
end

def actions
config.actions.keys.map { |name| policy_item(name.to_sym) }
end

def allowed_action_names
actions.select(&:allowed?).map(&:name)
end

def respond_to_missing?(method_name, *args)
config.actions.key?(method_name.to_sym) || super
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe ::ResourcePolicy:: Policy::ActionsPolicy::ActionsPolicyModel do
subject(:actions_policy_model) { described_class.new(model_instance) }

let(:policy_model) do
Class.new do
include ResourcePolicy::Policy

policy do |c|
c.action(:create).allowed
c.action(:read).allowed
c.action(:forbidden_action).allowed(if: :force_not_allowed)
end

def force_not_allowed
false
end
end
end

let(:model_instance) { policy_model.new }

describe '#actions' do
subject(:actions) { actions_policy_model.actions }

it 'returns actions' do
expect(actions).to all be_a(::ResourcePolicy::Policy::ActionsPolicy::ActionPolicy)
end

it 'returns actions with correct names' do
expect(actions.map(&:name)).to contain_exactly(:create, :read, :forbidden_action)
end
end

describe '#allowed_action_names' do
subject(:allowed_action_names) { actions_policy_model.allowed_action_names }

it 'returns allowed actions' do
expect(allowed_action_names).to contain_exactly(:create, :read)
end
end
end

0 comments on commit 0ac6e7b

Please sign in to comment.