restrict 0.0.4 → 0.0.5
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 +4 -4
- data/README.md +4 -4
- data/lib/restrict/gatekeeper.rb +6 -1
- data/lib/restrict/rails/controller.rb +7 -1
- data/lib/restrict/version.rb +1 -1
- data/spec/lib/restrict/gatekeeper_spec.rb +6 -7
- data/spec/spec_helper.rb +5 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 288411815db7101a293671bce57958441289d44d
|
4
|
+
data.tar.gz: 0999aca9b587696b861e0db17c1dfc8736562808
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ddd2451c5d3ef62411ad1577568af474447007a96d3c97bb524d9425c7f81dba85c98650ca8a6e3ffeeea3eb6fbb8c7c0f690a432dd363aea96cfb6b7d5cfdd0
|
7
|
+
data.tar.gz: d750c25ef6283f7d160e7046b4edd11873b827bd9249fb7e2cf941255a0a7a839a89d0c7133a244adff61c5c65dc0a6bd207e41aab77339346a5803806af3baf
|
data/README.md
CHANGED
@@ -37,9 +37,9 @@ end
|
|
37
37
|
|
38
38
|
What that does:
|
39
39
|
1. Any anonymous access to one of both methods will raise `Restrict::LoginRequired`
|
40
|
-
2. If
|
41
|
-
3. If
|
42
|
-
4. If
|
40
|
+
2. If `user_signed_in?` the access to take is allowed
|
41
|
+
3. If `user_signed_in?` but `goodie_manager?` returns false, then `Restrict::AccessDenied` will be raised
|
42
|
+
4. If `user_signed_in?` and `goodie_manager?` is true, the access is allowed
|
43
43
|
|
44
44
|
### Restrict all actions
|
45
45
|
|
@@ -53,7 +53,7 @@ This one will apply to all actions on this controller. It takes the `allow_if` o
|
|
53
53
|
|
54
54
|
```ruby
|
55
55
|
# Default is :user_signed_in?
|
56
|
-
Restrict.config.authentication_validation_method = :
|
56
|
+
Restrict.config.authentication_validation_method = :admin_session_exists?
|
57
57
|
```
|
58
58
|
|
59
59
|
You may set the method that is used to figure out whether a user is signed in or not to whatever you like, however it's default is `:user_signed_in?` which is the most common (devise) method in use.
|
data/lib/restrict/gatekeeper.rb
CHANGED
@@ -8,8 +8,13 @@ module Restrict
|
|
8
8
|
|
9
9
|
private
|
10
10
|
|
11
|
+
def validate_signed_in(controller)
|
12
|
+
method = Restrict.config.authentication_validation_method
|
13
|
+
controller.__send__(method) or raise Restrict::LoginRequired
|
14
|
+
end
|
15
|
+
|
11
16
|
def handle_restriction(restriction, controller)
|
12
|
-
controller
|
17
|
+
validate_signed_in(controller)
|
13
18
|
|
14
19
|
if restriction.allow_if
|
15
20
|
unless controller.__send__(restriction.allow_if)
|
@@ -5,14 +5,20 @@ module Restrict
|
|
5
5
|
|
6
6
|
included do
|
7
7
|
class_attribute :restrictions
|
8
|
-
before_filter :invoke_gatekeeper
|
9
8
|
end
|
10
9
|
|
11
10
|
module ClassMethods
|
12
11
|
def restrict(*args)
|
12
|
+
install_gatekeeper
|
13
13
|
self.restrictions ||= []
|
14
14
|
restrictions << Restrict::Restriction.new(*args)
|
15
15
|
end
|
16
|
+
|
17
|
+
def install_gatekeeper
|
18
|
+
return if @gatekeeper_installed
|
19
|
+
before_filter :invoke_gatekeeper
|
20
|
+
@gatekeeper_installed = true
|
21
|
+
end
|
16
22
|
end
|
17
23
|
|
18
24
|
private
|
data/lib/restrict/version.rb
CHANGED
@@ -4,7 +4,6 @@ describe Restrict::Gatekeeper do
|
|
4
4
|
|
5
5
|
let(:gatekeeper) { Restrict::Gatekeeper.new }
|
6
6
|
let(:controller) { ExampleController.new }
|
7
|
-
let(:user) { FakeUser.new }
|
8
7
|
|
9
8
|
before { controller.action_name = 'edit' }
|
10
9
|
|
@@ -15,7 +14,7 @@ describe Restrict::Gatekeeper do
|
|
15
14
|
end
|
16
15
|
|
17
16
|
it 'grants user access' do
|
18
|
-
controller.
|
17
|
+
controller.user_signed_in = true
|
19
18
|
expect { gatekeeper.eye(controller) }.not_to raise_error
|
20
19
|
end
|
21
20
|
end
|
@@ -28,7 +27,7 @@ describe Restrict::Gatekeeper do
|
|
28
27
|
end
|
29
28
|
|
30
29
|
it 'grants user access' do
|
31
|
-
controller.
|
30
|
+
controller.user_signed_in = true
|
32
31
|
expect { gatekeeper.eye(controller) }.not_to raise_error
|
33
32
|
end
|
34
33
|
end
|
@@ -41,7 +40,7 @@ describe Restrict::Gatekeeper do
|
|
41
40
|
end
|
42
41
|
|
43
42
|
it 'raises on missing method' do
|
44
|
-
controller.
|
43
|
+
controller.user_signed_in = true
|
45
44
|
controller.action_name = 'action1'
|
46
45
|
expect { gatekeeper.eye(controller) }.to raise_error(NoMethodError)
|
47
46
|
end
|
@@ -52,13 +51,13 @@ describe Restrict::Gatekeeper do
|
|
52
51
|
end
|
53
52
|
|
54
53
|
it 'denies access on falsy return value' do
|
55
|
-
controller.
|
54
|
+
controller.user_signed_in = true
|
56
55
|
controller.action_name = 'action2'
|
57
56
|
expect { gatekeeper.eye(controller) }.to raise_error(Restrict::AccessDenied)
|
58
57
|
end
|
59
58
|
|
60
59
|
it 'grants access on truthy return value' do
|
61
|
-
controller.
|
60
|
+
controller.user_signed_in = true
|
62
61
|
controller.action_name = 'action3'
|
63
62
|
expect { gatekeeper.eye(controller) }.not_to raise_error
|
64
63
|
end
|
@@ -71,7 +70,7 @@ describe Restrict::Gatekeeper do
|
|
71
70
|
end
|
72
71
|
|
73
72
|
it 'denies access if any restriction fails' do
|
74
|
-
controller.
|
73
|
+
controller.user_signed_in = true
|
75
74
|
expect { gatekeeper.eye(controller) }.to raise_error(Restrict::AccessDenied)
|
76
75
|
end
|
77
76
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -22,17 +22,19 @@ end
|
|
22
22
|
|
23
23
|
# Mimics the behavior of ActionController::Base
|
24
24
|
class FakeController
|
25
|
-
attr_accessor :action_name, :
|
25
|
+
attr_accessor :action_name, :user_signed_in
|
26
26
|
cattr_accessor :before_filters
|
27
27
|
|
28
|
+
def user_signed_in?
|
29
|
+
!!@user_signed_in
|
30
|
+
end
|
31
|
+
|
28
32
|
def self.before_filter(filter)
|
29
33
|
self.before_filters ||= []
|
30
34
|
before_filters << filter
|
31
35
|
end
|
32
36
|
end
|
33
37
|
|
34
|
-
FakeUser = Struct.new(:foo)
|
35
|
-
|
36
38
|
class ExampleController < FakeController
|
37
39
|
include Restrict::Rails::Controller
|
38
40
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: restrict
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Johannes Opper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|