restrict 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 09e06d807f7718b921ec0d017f48ff530dcd9543
4
- data.tar.gz: 75009eec3d441e82bd472ab8b263080fcc45a8f6
3
+ metadata.gz: 288411815db7101a293671bce57958441289d44d
4
+ data.tar.gz: 0999aca9b587696b861e0db17c1dfc8736562808
5
5
  SHA512:
6
- metadata.gz: 436c9d5a3c3c85463ea8685660c99f5263be59d55d064282d6dcd8a200f0533d9e1a4913142ebb2191eab8478e1a6d61b08e72a747f6979f88464958961cd907
7
- data.tar.gz: 3a79026f12d31af31fcde2904ef3d860c519649be71c54e7ec059933c3b09e59fc4a22a4c51f05f5a39ba8f6e38fd519223b2a2b31618869c229c7499e1b8d50
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 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
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 = :current_user
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.
@@ -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.current_user or raise Restrict::LoginRequired
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
@@ -1,3 +1,3 @@
1
1
  module Restrict
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
@@ -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.current_user = user
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.current_user = user
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.current_user = user
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.current_user = user
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.current_user = user
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.current_user = user
73
+ controller.user_signed_in = true
75
74
  expect { gatekeeper.eye(controller) }.to raise_error(Restrict::AccessDenied)
76
75
  end
77
76
  end
@@ -22,17 +22,19 @@ end
22
22
 
23
23
  # Mimics the behavior of ActionController::Base
24
24
  class FakeController
25
- attr_accessor :action_name, :current_user
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
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-23 00:00:00.000000000 Z
11
+ date: 2014-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails