protector-cancan 0.1.0 → 0.1.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 24a492131115dea9a185a89b83c01e3fd3dde523
4
- data.tar.gz: 0d0ef48a9f0e1ed21f0a0bc6d46c3fcb3d513dc3
3
+ metadata.gz: b34cee4a1e226310caecd062c57966b2b6885b6a
4
+ data.tar.gz: 8f487911a2246b9dc4fe62ba06c123e2ab9a9722
5
5
  SHA512:
6
- metadata.gz: e98380fa0ecc0a8238304ba9716dffabdf634a3e5e9dc12ca806b6ca749032e0cb2610c213bab330241b76ac28db69f263cd676f7036fe1990b3b572117a8122
7
- data.tar.gz: 956455e9fed66a3700a14d4ec3dcee8763a1a92da5f750e0d535f32e646ef54718d879b2ae4aae355bece3d1dcb6146d572b004915e5e9f7903d164ecde0476c
6
+ metadata.gz: ab8ff085f0c0a6a367c8d095b05d2b70dcebb65f8a0f270c2bed5dfd2d4c979d43acebdfac51e45fce11b0970e90b70c74a293d2b9c81f5571540f76fb206245
7
+ data.tar.gz: d474e2569e79ec413f90d9bbca6649e418d2117deb34ddf1fe36a2eedbcde666c352d5bf2ce84e9ba03005193b74bd8229b0ea03edbfa091ae6ed57edb944549
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # Protector::Cancan
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/protector-cancan.png)](http://badge.fury.io/rb/protector-cancan)
4
+ [![Build Status](https://travis-ci.org/inossidabile/protector-cancan.png?branch=master)](https://travis-ci.org/inossidabile/protector-cancan)
5
+ [![Code Climate](https://codeclimate.com/github/inossidabile/protector-cancan.png)](https://codeclimate.com/github/inossidabile/protector-cancan)
6
+
3
7
  Integrates [Protector](https://github.com/inossidabile/protector) and [CanCan](https://github.com/ryanb/cancan).
4
8
 
5
9
  Protector and CanCan are all about the same thing: access control. They however act on different fronts: Protector works on a model level and CanCan is all about controllers defense. With this gem you don't have to choose anymore: make them work together for the best result.
@@ -44,7 +48,7 @@ class Dummy < ActiveRecord::Base
44
48
  end
45
49
  ```
46
50
 
47
- If you call `can? :read, Dummy`, the gem will evaluate `Dummy` protection block against value passed to `import_protector` (by default it's `current_user`) and expand CanCan rules with resulting meta.
51
+ If you call `can? :view, Dummy`, the gem will evaluate `Dummy` protection block against value passed to `import_protector` (by default it's `current_user`) and expand CanCan rules with resulting meta. Note that gem automatically converts `:read` to `:view` so you should use CanCan naming conventions when working with CanCan.
48
52
 
49
53
  So in this particular case we will get `true` if `current_user` is set and `false` otherwise.
50
54
 
@@ -20,7 +20,13 @@ module Protector
20
20
  def self.included(mod)
21
21
  mod.class_eval do
22
22
 
23
- def can_with_protector?(action, entity, *extra_args)
23
+ def can_with_protector?(action, entity_set, *extra_args)
24
+ if entity_set.is_a? Hash
25
+ entity = entity_set.values.first
26
+ else
27
+ entity = entity_set
28
+ end
29
+
24
30
  if entity.respond_to?(:restrict!) && @protector_subject_defined
25
31
  @protector_models ||= Set.new
26
32
 
@@ -42,11 +48,10 @@ module Protector
42
48
  end
43
49
  end
44
50
 
45
- can_without_protector? action, entity, *extra_args
51
+ can_without_protector? action, entity_set, *extra_args
46
52
  end
47
53
 
48
54
  alias_method_chain :can?, :protector
49
-
50
55
  end
51
56
  end
52
57
  end
@@ -2,34 +2,44 @@ module Protector
2
2
  module CanCan
3
3
  module Resource extend ActiveSupport::Concern
4
4
  included do
5
- alias_method_chain :load_resource, :protector
5
+ alias_method_chain :resource_base, :protector
6
6
  alias_method_chain :load_collection, :protector
7
+ alias_method_chain :load_collection?, :protector
7
8
  end
8
9
 
9
- def load_resource_with_protector(*args)
10
- resource = load_resource_without_protector(*args)
10
+ def resource_base_with_protector
11
+ resource = resource_base_without_protector
11
12
 
12
- if resource.respond_to?(:restrict!) \
13
- && !resource.protector_subject? \
14
- && current_ability.protector_subject?
15
-
16
- resource = resource.restrict!(current_ability.protector_subject)
13
+ if resource_protectable? resource
14
+ resource.restrict!(current_ability.protector_subject)
15
+ else
16
+ resource
17
17
  end
18
-
19
- resource
20
18
  end
21
19
 
22
- def load_collection_with_protector(*args)
23
- collection = load_collection_without_protector(*args)
20
+ def load_collection_with_protector
21
+ resource = resource_base
24
22
 
25
- if collection.respond_to?(:restrict!) \
26
- && !collection.protector_subject? \
27
- && current_ability.protector_subject?
23
+ if resource_protectable? resource
24
+ resource
25
+ else
26
+ load_collection_without_protector
27
+ end
28
+ end
28
29
 
29
- collection = collection.restrict!(current_ability.protector_subject)
30
+ def load_collection_with_protector?
31
+ if resource_protectable? resource_base
32
+ true
33
+ else
34
+ load_collection_without_protector?
30
35
  end
31
-
32
- collection
36
+ end
37
+
38
+ private
39
+
40
+ def resource_protectable?(resource)
41
+ resource.respond_to?(:restrict!) &&
42
+ current_ability.protector_subject?
33
43
  end
34
44
  end
35
45
  end
@@ -1,5 +1,5 @@
1
1
  module Protector
2
2
  module Cancan
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: protector-cancan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boris Staal
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-07-14 00:00:00.000000000 Z
11
+ date: 2013-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: protector
@@ -101,7 +101,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
101
101
  version: '0'
102
102
  requirements: []
103
103
  rubyforge_project:
104
- rubygems_version: 2.0.2
104
+ rubygems_version: 2.0.3
105
105
  signing_key:
106
106
  specification_version: 4
107
107
  summary: Integration layer between Protector and CanCan