restrict 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +1 -0
- data/.travis.yml +11 -0
- data/CHANGELOG.md +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +13 -0
- data/README.md +51 -0
- data/Rakefile +14 -0
- data/lib/restrict.rb +14 -0
- data/lib/restrict/access_denied.rb +4 -0
- data/lib/restrict/error.rb +4 -0
- data/lib/restrict/gatekeeper.rb +27 -0
- data/lib/restrict/login_required.rb +4 -0
- data/lib/restrict/rails/controller.rb +26 -0
- data/lib/restrict/rails/railtie.rb +9 -0
- data/lib/restrict/restriction.rb +17 -0
- data/lib/restrict/rspec/matcher.rb +44 -0
- data/lib/restrict/rspec/matcher_rspec2.rb +44 -0
- data/lib/restrict/version.rb +3 -0
- data/restrict.gemspec +29 -0
- data/spec/lib/restrict/gatekeeper_spec.rb +67 -0
- data/spec/lib/restrict/rails/controller_spec.rb +35 -0
- data/spec/lib/restrict/restriction_spec.rb +36 -0
- data/spec/lib/restrict/rspec/matcher_spec.rb +84 -0
- data/spec/spec_helper.rb +46 -0
- metadata +172 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: c99bdbebb900cbb8bd4f2e16c7951a00077c9552
|
|
4
|
+
data.tar.gz: ad856af2b30b2e808db164da909ee824598707ee
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: cef3ff540a576034e4d243304e52717dd4edb910e738e707417ba8bcbcfaf024dfc963307e411dd799627697692cc0d106d0135ffdbe6a3ddfedbedfcf9cb16b
|
|
7
|
+
data.tar.gz: 2d6bf74cbe84d97398ea0c23cb99429e6be7dbb7dfd8359f3c6abbbfdd8c215beb3fd70c22b4f810bdbc3bd70c29d0e85a5286a42429723256f584e62fdbb116
|
data/.gitignore
ADDED
data/.rspec
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
--color
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
2
|
+
Version 2, December 2004
|
|
3
|
+
|
|
4
|
+
Copyright (C) 2014 Johannes Opper <johannes.opper@gmail.com>
|
|
5
|
+
|
|
6
|
+
Everyone is permitted to copy and distribute verbatim or modified
|
|
7
|
+
copies of this license document, and changing it is allowed as long
|
|
8
|
+
as the name is changed.
|
|
9
|
+
|
|
10
|
+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
|
|
11
|
+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
|
|
12
|
+
|
|
13
|
+
0. You just DO WHAT THE FUCK YOU WANT TO.
|
data/README.md
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# Restrict
|
|
2
|
+
|
|
3
|
+
A rails controller extension, that gives you the possibility to restrict access to your controller actions.
|
|
4
|
+
|
|
5
|
+
[](https://travis-ci.org/xijo/restrict) [](http://badge.fury.io/rb/restrict) [](https://codeclimate.com/github/xijo/restrict) [](https://codeclimate.com/github/xijo/restrict)
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
gem 'restrict'
|
|
10
|
+
|
|
11
|
+
## Compatibility
|
|
12
|
+
|
|
13
|
+
Works with rails 3 and 4 and all versions every ruby 2.
|
|
14
|
+
|
|
15
|
+
## Usage
|
|
16
|
+
|
|
17
|
+
```ruby
|
|
18
|
+
class GoodiesController < ApplicationController
|
|
19
|
+
restrict :take
|
|
20
|
+
restrict :delete, allow_if: :goodie_manager?
|
|
21
|
+
|
|
22
|
+
def take
|
|
23
|
+
# Grab a goodie
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def delete
|
|
27
|
+
# Remove all the goodies
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def goodie_manager?
|
|
33
|
+
# Your domain implementation
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
What that does:
|
|
39
|
+
1. Any anonymous access to one of both methods will raise `Restrict::LoginRequired`
|
|
40
|
+
2. If a `current_user` exists the access to take is allowed
|
|
41
|
+
3. If a `current_user` exists but `goodie_manager?` returns false, then `Restrict::AccessDenied` will be raised
|
|
42
|
+
4. If a `current_user` exists and `goodie_manager?` is true, the access is allowed
|
|
43
|
+
|
|
44
|
+
## Todos/Ideas
|
|
45
|
+
|
|
46
|
+
* make `current_user` configurable
|
|
47
|
+
* introduce `any_action` option
|
|
48
|
+
|
|
49
|
+
## Contributing
|
|
50
|
+
|
|
51
|
+
You know how this works and bonus points for feature branches!
|
data/Rakefile
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require "bundler/gem_tasks"
|
|
2
|
+
|
|
3
|
+
if File.exist?('.codeclimate')
|
|
4
|
+
ENV["CODECLIMATE_REPO_TOKEN"] = File.read('.codeclimate')
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
require 'rspec/core/rake_task'
|
|
8
|
+
RSpec::Core::RakeTask.new(:spec)
|
|
9
|
+
task :default => :spec
|
|
10
|
+
|
|
11
|
+
desc "Open an irb session preloaded with this library"
|
|
12
|
+
task :console do
|
|
13
|
+
sh "irb -rubygems -I lib -r restrict.rb"
|
|
14
|
+
end
|
data/lib/restrict.rb
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
require 'active_support'
|
|
2
|
+
|
|
3
|
+
require 'restrict/version'
|
|
4
|
+
require 'restrict/error'
|
|
5
|
+
require 'restrict/login_required'
|
|
6
|
+
require 'restrict/access_denied'
|
|
7
|
+
require 'restrict/restriction'
|
|
8
|
+
require 'restrict/gatekeeper'
|
|
9
|
+
require 'restrict/rails/controller'
|
|
10
|
+
require 'restrict/rails/railtie' if defined?(Rails)
|
|
11
|
+
|
|
12
|
+
module Restrict
|
|
13
|
+
# Your code goes here...
|
|
14
|
+
end
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
module Restrict
|
|
2
|
+
class Gatekeeper
|
|
3
|
+
def eye(controller)
|
|
4
|
+
restriction = current_restriction(controller)
|
|
5
|
+
restriction and handle_restriction(restriction, controller)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
private
|
|
9
|
+
|
|
10
|
+
def handle_restriction(restriction, controller)
|
|
11
|
+
controller.current_user or raise Restrict::LoginRequired
|
|
12
|
+
|
|
13
|
+
if restriction.allow_if
|
|
14
|
+
unless controller.__send__(restriction.allow_if)
|
|
15
|
+
raise Restrict::AccessDenied, reason: restriction
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def current_restriction(controller)
|
|
21
|
+
controller.restrictions or return
|
|
22
|
+
controller.restrictions.find do |restriction|
|
|
23
|
+
restriction.restricts?(controller.action_name)
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
module Restrict
|
|
2
|
+
module Rails
|
|
3
|
+
module Controller
|
|
4
|
+
extend ActiveSupport::Concern
|
|
5
|
+
|
|
6
|
+
included do
|
|
7
|
+
cattr_accessor :restrictions
|
|
8
|
+
self.restrictions ||= []
|
|
9
|
+
before_filter :invoke_gatekeeper
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
module ClassMethods
|
|
13
|
+
def restrict(*args)
|
|
14
|
+
self.restrictions ||= []
|
|
15
|
+
restrictions << Restrict::Restriction.new(*args)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def invoke_gatekeeper
|
|
22
|
+
Restrict::Gatekeeper.new.eye(self)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
module Restrict
|
|
2
|
+
class Restriction
|
|
3
|
+
attr_accessor :actions, :role, :allow_if
|
|
4
|
+
|
|
5
|
+
def initialize(*args)
|
|
6
|
+
options = args.extract_options!
|
|
7
|
+
@role = options[:role]
|
|
8
|
+
@allow_if = options[:allow_if]
|
|
9
|
+
@actions = args
|
|
10
|
+
actions.empty? and raise ArgumentError, "expected actions to restrict, but got #{actions.inspect}"
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def restricts?(action_name)
|
|
14
|
+
actions.include?(action_name.to_sym)
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
RSpec::Matchers.define :have_restriction_on do |given_action_name|
|
|
2
|
+
match do |given_controller|
|
|
3
|
+
@given_action_name = given_action_name
|
|
4
|
+
@given_controller = given_controller
|
|
5
|
+
|
|
6
|
+
@restriction = given_controller.restrictions.find do |restriction|
|
|
7
|
+
restriction.restricts?(given_action_name)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
if @restriction
|
|
11
|
+
if @given_allow_if
|
|
12
|
+
@restriction.allow_if == @given_allow_if
|
|
13
|
+
else
|
|
14
|
+
true
|
|
15
|
+
end
|
|
16
|
+
else
|
|
17
|
+
false
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
chain :with_allow_if do |given_allow_if|
|
|
22
|
+
@given_allow_if = given_allow_if
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
failure_message do |actual|
|
|
26
|
+
if @restriction && @given_allow_if
|
|
27
|
+
"Expected restriction to call #{@given_allow_if.inspect}, but calls #{@restriction.allow_if.inspect}"
|
|
28
|
+
else
|
|
29
|
+
"Expected to have restriction on #{@given_action_name}, but was not found in #{@given_controller.restrictions.inspect}"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
failure_message_when_negated do |actual|
|
|
34
|
+
if @given_allow_if
|
|
35
|
+
"Expected restriction not to call #{@given_allow_if.inspect}, but calls #{@restriction.allow_if.inspect}"
|
|
36
|
+
else
|
|
37
|
+
"Expected not to have restriction on #{@given_action_name}, but was found in #{@given_controller.restrictions.inspect}"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def description
|
|
42
|
+
"Checks if a restriction for a given action is defined on the controller"
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
RSpec::Matchers.define :have_restriction_on do |given_action_name|
|
|
2
|
+
match do |given_controller|
|
|
3
|
+
@given_action_name = given_action_name
|
|
4
|
+
@given_controller = given_controller
|
|
5
|
+
|
|
6
|
+
@restriction = given_controller.restrictions.find do |restriction|
|
|
7
|
+
restriction.restricts?(given_action_name)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
if @restriction
|
|
11
|
+
if @given_allow_if
|
|
12
|
+
@restriction.allow_if == @given_allow_if
|
|
13
|
+
else
|
|
14
|
+
true
|
|
15
|
+
end
|
|
16
|
+
else
|
|
17
|
+
false
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
chain :with_allow_if do |given_allow_if|
|
|
22
|
+
@given_allow_if = given_allow_if
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
failure_message_for_should do |actual|
|
|
26
|
+
if @restriction && @given_allow_if
|
|
27
|
+
"Expected restriction to call #{@given_allow_if.inspect}, but calls #{@restriction.allow_if.inspect}"
|
|
28
|
+
else
|
|
29
|
+
"Expected to have restriction on #{@given_action_name}, but was not found in #{@given_controller.restrictions.inspect}"
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
failure_message_for_should_not do |actual|
|
|
34
|
+
if @given_allow_if
|
|
35
|
+
"Expected restriction not to call #{@given_allow_if.inspect}, but calls #{@restriction.allow_if.inspect}"
|
|
36
|
+
else
|
|
37
|
+
"Expected not to have restriction on #{@given_action_name}, but was found in #{@given_controller.restrictions.inspect}"
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def description
|
|
42
|
+
"Checks if a restriction for a given action is defined on the controller"
|
|
43
|
+
end
|
|
44
|
+
end
|
data/restrict.gemspec
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'restrict/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "restrict"
|
|
8
|
+
spec.version = Restrict::VERSION
|
|
9
|
+
spec.authors = ["Johannes Opper"]
|
|
10
|
+
spec.email = ["johannes.opper@gmail.com"]
|
|
11
|
+
spec.summary = %q{Simple access control dsl for controllers.}
|
|
12
|
+
spec.description = %q{Simple access control dsl for controllers}
|
|
13
|
+
spec.homepage = "https://github.com/xijo/restrict"
|
|
14
|
+
spec.license = "WTFPL"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files`.split($/)
|
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
spec.require_paths = ["lib"]
|
|
20
|
+
|
|
21
|
+
spec.add_dependency 'rails', '> 3.0'
|
|
22
|
+
|
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.5'
|
|
24
|
+
spec.add_development_dependency 'rspec'
|
|
25
|
+
spec.add_development_dependency 'simplecov'
|
|
26
|
+
spec.add_development_dependency 'rake'
|
|
27
|
+
spec.add_development_dependency 'byebug'
|
|
28
|
+
spec.add_development_dependency 'codeclimate-test-reporter'
|
|
29
|
+
end
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Restrict::Gatekeeper do
|
|
4
|
+
|
|
5
|
+
let(:gatekeeper) { Restrict::Gatekeeper.new }
|
|
6
|
+
let(:controller) { ExampleController.new }
|
|
7
|
+
let(:user) { FakeUser.new }
|
|
8
|
+
|
|
9
|
+
before { controller.action_name = 'edit' }
|
|
10
|
+
|
|
11
|
+
describe '#eye' do
|
|
12
|
+
context 'without restriction' do
|
|
13
|
+
it 'grants anonymous access' do
|
|
14
|
+
expect { gatekeeper.eye(controller) }.not_to raise_error
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it 'grants user access' do
|
|
18
|
+
controller.current_user = user
|
|
19
|
+
expect { gatekeeper.eye(controller) }.not_to raise_error
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
context 'with plain restriction' do
|
|
24
|
+
before { controller.class.restrict :edit }
|
|
25
|
+
|
|
26
|
+
it 'denies anonymous access' do
|
|
27
|
+
expect { gatekeeper.eye(controller) }.to raise_error(Restrict::LoginRequired)
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
it 'grants user access' do
|
|
31
|
+
controller.current_user = user
|
|
32
|
+
expect { gatekeeper.eye(controller) }.not_to raise_error
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
context 'with conditional restriction' do
|
|
37
|
+
before do
|
|
38
|
+
controller.class.restrict :action1, allow_if: :missing
|
|
39
|
+
controller.class.restrict :action2, allow_if: :falsy
|
|
40
|
+
controller.class.restrict :action3, allow_if: :truthy
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it 'raises on missing method' do
|
|
44
|
+
controller.current_user = user
|
|
45
|
+
controller.action_name = 'action1'
|
|
46
|
+
expect { gatekeeper.eye(controller) }.to raise_error(NoMethodError)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it 'denies anonymous access' do
|
|
50
|
+
controller.action_name = 'action1'
|
|
51
|
+
expect { gatekeeper.eye(controller) }.to raise_error(Restrict::LoginRequired)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
it 'denies access on falsy return value' do
|
|
55
|
+
controller.current_user = user
|
|
56
|
+
controller.action_name = 'action2'
|
|
57
|
+
expect { gatekeeper.eye(controller) }.to raise_error(Restrict::AccessDenied)
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
it 'grants access on truthy return value' do
|
|
61
|
+
controller.current_user = user
|
|
62
|
+
controller.action_name = 'action3'
|
|
63
|
+
expect { gatekeeper.eye(controller) }.not_to raise_error
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Restrict::Rails::Controller do
|
|
4
|
+
|
|
5
|
+
let(:controller) { ExampleController.new }
|
|
6
|
+
|
|
7
|
+
before do
|
|
8
|
+
controller.class.restrict :index
|
|
9
|
+
controller.class.restrict :show, allow_if: :access_allowed?
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe '#restrict' do
|
|
13
|
+
it 'builds and adds a plain restriction' do
|
|
14
|
+
expect(controller).to have_restriction_on(:index)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
it 'builds and adds a conditional restriction' do
|
|
18
|
+
expect(controller).to have_restriction_on(:show).with_allow_if(:access_allowed?)
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
describe '#included' do
|
|
23
|
+
it 'adds the before filter' do
|
|
24
|
+
expect(controller.before_filters).to include :invoke_gatekeeper
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
describe '#invoke_gatekeeper' do
|
|
29
|
+
it 'builds and calls a gatekeeper for the controller' do
|
|
30
|
+
expect_any_instance_of(Restrict::Gatekeeper).to receive(:eye).with(controller)
|
|
31
|
+
controller.__send__ :invoke_gatekeeper
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe Restrict::Restriction do
|
|
4
|
+
let(:restriction) { Restrict::Restriction.new(:show, :edit, role: :manager) }
|
|
5
|
+
|
|
6
|
+
describe '#initialize' do
|
|
7
|
+
it 'knows about its actions' do
|
|
8
|
+
expect(restriction.actions).to eq [:show, :edit]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it 'raises an error if no actions were given' do
|
|
12
|
+
expect { Restrict::Restriction.new }.to raise_error(ArgumentError)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
describe '#restricts?' do
|
|
17
|
+
it 'returns true if the given action is contained' do
|
|
18
|
+
expect(restriction).to be_restricts(:show)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'returns false if the given action name is not contained' do
|
|
22
|
+
expect(restriction).not_to be_restricts(:index)
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
describe '#role' do
|
|
27
|
+
it 'returns the role name if given' do
|
|
28
|
+
expect(restriction.role).to eq :manager
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'returns nil if non was given' do
|
|
32
|
+
expect(Restrict::Restriction.new(:foo).role).to be_nil
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
|
|
3
|
+
describe 'have_restriction_on' do
|
|
4
|
+
let(:controller) { ExampleController.new }
|
|
5
|
+
|
|
6
|
+
# before do
|
|
7
|
+
# controller.class.restrict :index
|
|
8
|
+
# controller.class.restrict :show, allow_if: :access_allowed?
|
|
9
|
+
# end
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
context 'without restrictions' do
|
|
13
|
+
it 'matcher fails' do
|
|
14
|
+
expect {
|
|
15
|
+
expect(controller).to have_restriction_on(:show)
|
|
16
|
+
}.to raise_error RSpec::Expectations::ExpectationNotMetError
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'negated matcher passes' do
|
|
20
|
+
expect(controller).not_to have_restriction_on(:show)
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
context 'with plain restriction' do
|
|
25
|
+
before { controller.class.restrict :show }
|
|
26
|
+
|
|
27
|
+
it 'matcher passes' do
|
|
28
|
+
expect(controller).to have_restriction_on(:show)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'wrong matcher fails' do
|
|
32
|
+
expect {
|
|
33
|
+
expect(controller).to have_restriction_on(:index)
|
|
34
|
+
}.to raise_error RSpec::Expectations::ExpectationNotMetError
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
it 'negated matcher fails' do
|
|
38
|
+
expect {
|
|
39
|
+
expect(controller).not_to have_restriction_on(:show)
|
|
40
|
+
}.to raise_error RSpec::Expectations::ExpectationNotMetError
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it 'matcher conditional chain fails' do
|
|
44
|
+
expect {
|
|
45
|
+
expect(controller).to have_restriction_on(:show).with_allow_if(:something)
|
|
46
|
+
}.to raise_error RSpec::Expectations::ExpectationNotMetError
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
it 'negated matcher with conditional chain passes' do
|
|
50
|
+
expect(controller).not_to have_restriction_on(:show).with_allow_if(:something)
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
context 'with conditional restriction' do
|
|
55
|
+
before { controller.class.restrict :show, allow_if: :something }
|
|
56
|
+
|
|
57
|
+
it 'matcher passes' do
|
|
58
|
+
expect(controller).to have_restriction_on(:show)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
it 'wrong matcher fails' do
|
|
62
|
+
expect {
|
|
63
|
+
expect(controller).to have_restriction_on(:index)
|
|
64
|
+
}.to raise_error RSpec::Expectations::ExpectationNotMetError
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
it 'negated matcher fails' do
|
|
68
|
+
expect {
|
|
69
|
+
expect(controller).not_to have_restriction_on(:show)
|
|
70
|
+
}.to raise_error RSpec::Expectations::ExpectationNotMetError
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
it 'matcher conditional chain passes' do
|
|
74
|
+
expect(controller).to have_restriction_on(:show).with_allow_if(:something)
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
it 'negated matcher with conditional chain passes' do
|
|
78
|
+
expect {
|
|
79
|
+
expect(controller).not_to have_restriction_on(:show).with_allow_if(:something)
|
|
80
|
+
}.to raise_error RSpec::Expectations::ExpectationNotMetError
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
end
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require 'codeclimate-test-reporter'
|
|
2
|
+
CodeClimate::TestReporter.start
|
|
3
|
+
|
|
4
|
+
require 'simplecov'
|
|
5
|
+
require 'byebug'
|
|
6
|
+
|
|
7
|
+
SimpleCov.profiles.define 'gem' do
|
|
8
|
+
add_filter '/spec/'
|
|
9
|
+
add_filter '/autotest/'
|
|
10
|
+
add_group 'Libraries', '/lib/'
|
|
11
|
+
end
|
|
12
|
+
SimpleCov.start 'gem'
|
|
13
|
+
|
|
14
|
+
require 'restrict'
|
|
15
|
+
require 'restrict/rspec/matcher'
|
|
16
|
+
|
|
17
|
+
RSpec.configure do |config|
|
|
18
|
+
config.after do
|
|
19
|
+
ExampleController.restrictions = []
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Mimics the behavior of ActionController::Base
|
|
24
|
+
class FakeController
|
|
25
|
+
attr_accessor :action_name, :current_user
|
|
26
|
+
cattr_accessor :before_filters
|
|
27
|
+
|
|
28
|
+
def self.before_filter(filter)
|
|
29
|
+
self.before_filters ||= []
|
|
30
|
+
before_filters << filter
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
FakeUser = Struct.new(:foo)
|
|
35
|
+
|
|
36
|
+
class ExampleController < FakeController
|
|
37
|
+
include Restrict::Rails::Controller
|
|
38
|
+
|
|
39
|
+
def falsy
|
|
40
|
+
false
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def truthy
|
|
44
|
+
true
|
|
45
|
+
end
|
|
46
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: restrict
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.2
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Johannes Opper
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2014-08-23 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rails
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - ">"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '3.0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - ">"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '3.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: bundler
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '1.5'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '1.5'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - ">="
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - ">="
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '0'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: simplecov
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - ">="
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rake
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - ">="
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - ">="
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: byebug
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - ">="
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: codeclimate-test-reporter
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - ">="
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '0'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - ">="
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0'
|
|
111
|
+
description: Simple access control dsl for controllers
|
|
112
|
+
email:
|
|
113
|
+
- johannes.opper@gmail.com
|
|
114
|
+
executables: []
|
|
115
|
+
extensions: []
|
|
116
|
+
extra_rdoc_files: []
|
|
117
|
+
files:
|
|
118
|
+
- ".gitignore"
|
|
119
|
+
- ".rspec"
|
|
120
|
+
- ".travis.yml"
|
|
121
|
+
- CHANGELOG.md
|
|
122
|
+
- Gemfile
|
|
123
|
+
- LICENSE.txt
|
|
124
|
+
- README.md
|
|
125
|
+
- Rakefile
|
|
126
|
+
- lib/restrict.rb
|
|
127
|
+
- lib/restrict/access_denied.rb
|
|
128
|
+
- lib/restrict/error.rb
|
|
129
|
+
- lib/restrict/gatekeeper.rb
|
|
130
|
+
- lib/restrict/login_required.rb
|
|
131
|
+
- lib/restrict/rails/controller.rb
|
|
132
|
+
- lib/restrict/rails/railtie.rb
|
|
133
|
+
- lib/restrict/restriction.rb
|
|
134
|
+
- lib/restrict/rspec/matcher.rb
|
|
135
|
+
- lib/restrict/rspec/matcher_rspec2.rb
|
|
136
|
+
- lib/restrict/version.rb
|
|
137
|
+
- restrict.gemspec
|
|
138
|
+
- spec/lib/restrict/gatekeeper_spec.rb
|
|
139
|
+
- spec/lib/restrict/rails/controller_spec.rb
|
|
140
|
+
- spec/lib/restrict/restriction_spec.rb
|
|
141
|
+
- spec/lib/restrict/rspec/matcher_spec.rb
|
|
142
|
+
- spec/spec_helper.rb
|
|
143
|
+
homepage: https://github.com/xijo/restrict
|
|
144
|
+
licenses:
|
|
145
|
+
- WTFPL
|
|
146
|
+
metadata: {}
|
|
147
|
+
post_install_message:
|
|
148
|
+
rdoc_options: []
|
|
149
|
+
require_paths:
|
|
150
|
+
- lib
|
|
151
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
152
|
+
requirements:
|
|
153
|
+
- - ">="
|
|
154
|
+
- !ruby/object:Gem::Version
|
|
155
|
+
version: '0'
|
|
156
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
157
|
+
requirements:
|
|
158
|
+
- - ">="
|
|
159
|
+
- !ruby/object:Gem::Version
|
|
160
|
+
version: '0'
|
|
161
|
+
requirements: []
|
|
162
|
+
rubyforge_project:
|
|
163
|
+
rubygems_version: 2.2.2
|
|
164
|
+
signing_key:
|
|
165
|
+
specification_version: 4
|
|
166
|
+
summary: Simple access control dsl for controllers.
|
|
167
|
+
test_files:
|
|
168
|
+
- spec/lib/restrict/gatekeeper_spec.rb
|
|
169
|
+
- spec/lib/restrict/rails/controller_spec.rb
|
|
170
|
+
- spec/lib/restrict/restriction_spec.rb
|
|
171
|
+
- spec/lib/restrict/rspec/matcher_spec.rb
|
|
172
|
+
- spec/spec_helper.rb
|