canner 0.3.0 → 0.4.0
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 +13 -5
- data/README.md +14 -7
- data/lib/canner.rb +0 -3
- data/lib/canner/policy.rb +6 -3
- data/lib/canner/util.rb +1 -13
- data/lib/canner/version.rb +1 -1
- data/spec/canner_spec.rb +12 -4
- data/spec/spec_helper.rb +0 -1
- metadata +16 -15
checksums.yaml
CHANGED
@@ -1,7 +1,15 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
Yjk3NGI2ZWYzMmQ1OTM1OTYzZjIwMjI5OWE3ZWIzMTM2YWY1NDdkNg==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
YjBlYWRiNWI4YjQwYjRjYzMzOTI4ZjQxZDM5NWFlMGQ3YWRlMGRiMA==
|
5
7
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
NzM3N2UxN2EzOWUyOGEwZDEzODIzOGM5M2VmOTUyYzNlZDgzM2U1NDNjNmE3
|
10
|
+
NmNhZTI1NjBiY2QwZGJhYWZiMjVmZmNmM2I4YjEzY2QzNzI3NTYyMWE5MzUy
|
11
|
+
ODM1YWI0ZDFlZmI1MzkwZDEyODdjYzc1YjMwY2MyODExYTJmYTA=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YmU5YTk0MmNhYjQ2OGIxYzE1NDA3MTE5M2IyZGJlN2ViM2VjY2IyZDMxZWQ0
|
14
|
+
YWQ3ODVkNzViODlkZjBlNzY0ZWI4NDdjMDNmMmU4NzUxYzIyN2JmMTkxYjk0
|
15
|
+
YzVjNjNiMzFjYWZlNTE3Mjk4YzAxZDhlNjM2OTIwZGZhNDg2YmY=
|
data/README.md
CHANGED
@@ -55,7 +55,7 @@ need to override the fetch_roles policy method.
|
|
55
55
|
rails g canner:fetch_roles
|
56
56
|
```
|
57
57
|
|
58
|
-
More details are available in the wiki:
|
58
|
+
More details are available in the wiki:
|
59
59
|
[Overriding the Fetching of Roles](https://github.com/jacklin10/canner/wiki/Feed-Roles)
|
60
60
|
|
61
61
|
## Policies
|
@@ -64,7 +64,7 @@ As mentioned Canner is strongly influenced by Pundit and is also based on Policy
|
|
64
64
|
Your policy objects should be named using the following pattern:
|
65
65
|
UserPolicy, CustomerPolicy, AppPolicy.
|
66
66
|
|
67
|
-
Use the generator to save you some time:
|
67
|
+
Use the generator to save you some time:
|
68
68
|
``` rails g canner:policy <model name> ```
|
69
69
|
|
70
70
|
Your policy models need to implement 2 methods:
|
@@ -167,7 +167,7 @@ end
|
|
167
167
|
|
168
168
|
in your base_policy's `can?` method
|
169
169
|
|
170
|
-
### instance_can?
|
170
|
+
### instance_can?
|
171
171
|
|
172
172
|
You use the instance_can? method to determine if the current_user is able to modify a particular instance
|
173
173
|
of an object.
|
@@ -180,13 +180,13 @@ For example, if a user who belongs to company A wants to edit a particular item
|
|
180
180
|
|
181
181
|
Normal stuff. The user changes the item price and moves on.
|
182
182
|
|
183
|
-
But now we have another user who decides they want to see what happens when they manually change the url:
|
183
|
+
But now we have another user who decides they want to see what happens when they manually change the url:
|
184
184
|
|
185
185
|
```
|
186
186
|
/items/13/edit
|
187
187
|
```
|
188
188
|
|
189
|
-
If you don't defend against this the user would be granted access to edit item with id=13 which
|
189
|
+
If you don't defend against this the user would be granted access to edit item with id=13 which
|
190
190
|
belongs to a different company.
|
191
191
|
|
192
192
|
The instance_can? method helps in these situations.
|
@@ -237,10 +237,17 @@ You are able to force the use of controller authorization with canner.
|
|
237
237
|
I recommend you do this so you don't forget to wrap authorization about some of your resources.
|
238
238
|
|
239
239
|
To make sure your controller actions are using the can? method add this near the top of your
|
240
|
-
application_controller.rb
|
240
|
+
`application_controller.rb`. Use the `except:` option for ensuring we ignore controllers
|
241
|
+
related to authentication.
|
241
242
|
|
242
243
|
``` ruby
|
243
244
|
after_action :ensure_auth
|
245
|
+
|
246
|
+
# using devise?
|
247
|
+
after_action :ensure_auth, except: :devise_controller?
|
248
|
+
|
249
|
+
# using CASino?
|
250
|
+
after_action :ensure_auth, unless: -> { self.is_a? CASino::SessionsController }
|
244
251
|
```
|
245
252
|
|
246
253
|
And to make sure you are using the canner_scope do the following:
|
@@ -251,7 +258,7 @@ after_action :ensure_scope, only: :index
|
|
251
258
|
Note the use of only here. You usually won't need the canner_scope on anything except
|
252
259
|
for the index to be strictly enforced.
|
253
260
|
|
254
|
-
And finally, if you want to enforce that you are using instance_can? use something like:
|
261
|
+
And finally, if you want to enforce that you are using instance_can? use something like:
|
255
262
|
``` ruby
|
256
263
|
after_action :ensure_instance_checking, only: [:edit, :destroy, :update]
|
257
264
|
```
|
data/lib/canner.rb
CHANGED
@@ -74,19 +74,16 @@ module Canner
|
|
74
74
|
protected
|
75
75
|
|
76
76
|
def ensure_scope
|
77
|
-
return if devise_controller? rescue false
|
78
77
|
raise ScopeNotUsedError.new("Must use a canner_scope or exclude this action from the after_action") unless scope_used
|
79
78
|
true
|
80
79
|
end
|
81
80
|
|
82
81
|
def ensure_auth
|
83
|
-
return if devise_controller? rescue false
|
84
82
|
raise AuthNotUsedError.new("Must use can? method or exclude this action from the after_action") unless auth_used
|
85
83
|
true
|
86
84
|
end
|
87
85
|
|
88
86
|
def ensure_instance_checking
|
89
|
-
return if devise_controller? rescue false
|
90
87
|
raise AuthNotUsedError.new("Must use instance_can? method or exclude this action from the after_action") unless instance_checked
|
91
88
|
true
|
92
89
|
end
|
data/lib/canner/policy.rb
CHANGED
@@ -28,7 +28,7 @@ module Canner
|
|
28
28
|
# end
|
29
29
|
end
|
30
30
|
|
31
|
-
#
|
31
|
+
# implement in your policy class.
|
32
32
|
# return true when the user can access the action or resource and false when they can't
|
33
33
|
def can?
|
34
34
|
raise ArgumentError.new("NOT IMPLEMENTED")
|
@@ -49,9 +49,12 @@ module Canner
|
|
49
49
|
|
50
50
|
def has_role?(roles)
|
51
51
|
begin
|
52
|
-
@roles.any?
|
52
|
+
@roles.any? do |r|
|
53
|
+
name = r.respond_to?(:name) ? r.name : r.to_s
|
54
|
+
Util.prepare(roles).include?(name.to_sym)
|
55
|
+
end
|
53
56
|
rescue Exception => e
|
54
|
-
raise ArgumentError.new "Canner: Problem fetching user roles. If current_user.roles isn't how you do it see wiki for overriding fetch_roles."
|
57
|
+
raise ArgumentError.new "Canner: Problem fetching user roles. If current_user.roles isn't how you do it see wiki for overriding fetch_roles. #{e}"
|
55
58
|
end
|
56
59
|
end
|
57
60
|
|
data/lib/canner/util.rb
CHANGED
@@ -3,19 +3,7 @@ class Util
|
|
3
3
|
# ensures whatever is passed in comes out an array of symbols
|
4
4
|
class << self
|
5
5
|
def prepare(str)
|
6
|
-
|
6
|
+
Array(str).flatten.map(&:to_sym)
|
7
7
|
end
|
8
|
-
|
9
|
-
# ensures the array elements are symbols
|
10
|
-
def symbolize(strings)
|
11
|
-
strings.map{|s| s.to_sym}
|
12
|
-
end
|
13
|
-
|
14
|
-
# ensure given roles are in the form of an array
|
15
|
-
def arrayify(roles)
|
16
|
-
Array.wrap(roles).flatten
|
17
|
-
end
|
18
|
-
|
19
8
|
end
|
20
|
-
|
21
9
|
end
|
data/lib/canner/version.rb
CHANGED
data/spec/canner_spec.rb
CHANGED
@@ -4,7 +4,7 @@ require "canner"
|
|
4
4
|
class Sample
|
5
5
|
end
|
6
6
|
|
7
|
-
class SamplePolicy
|
7
|
+
class SamplePolicy < Canner::Policy
|
8
8
|
|
9
9
|
def initialize(current_user, method, current_branch)
|
10
10
|
@current_user = current_user
|
@@ -14,13 +14,21 @@ class SamplePolicy
|
|
14
14
|
end
|
15
15
|
|
16
16
|
def fetch_roles
|
17
|
-
|
17
|
+
['admin']
|
18
18
|
end
|
19
19
|
|
20
20
|
def canner_scope
|
21
|
-
|
21
|
+
[Sample.new]
|
22
22
|
end
|
23
23
|
|
24
|
+
def can?
|
25
|
+
case @method
|
26
|
+
when :index, :show
|
27
|
+
has_role?(:admin)
|
28
|
+
else
|
29
|
+
false
|
30
|
+
end
|
31
|
+
end
|
24
32
|
end
|
25
33
|
|
26
34
|
class AppController
|
@@ -55,7 +63,7 @@ describe Canner do
|
|
55
63
|
expect(app_controller).to receive(:canner_policy).and_return(sample_policy)
|
56
64
|
expect(sample_policy).to receive(:instance_can?).and_return true
|
57
65
|
|
58
|
-
app_controller.instance_can?('test', 'sample', Sample.new).
|
66
|
+
expect(app_controller.instance_can?('test', 'sample', Sample.new)).to be true
|
59
67
|
end
|
60
68
|
|
61
69
|
end
|
data/spec/spec_helper.rb
CHANGED
metadata
CHANGED
@@ -1,41 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: canner
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joe Acklin
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - '>='
|
17
|
+
- - ! '>='
|
18
18
|
- !ruby/object:Gem::Version
|
19
19
|
version: '0'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- - '>='
|
24
|
+
- - ! '>='
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activemodel
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - '>='
|
31
|
+
- - ! '>='
|
32
32
|
- !ruby/object:Gem::Version
|
33
33
|
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - '>='
|
38
|
+
- - ! '>='
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
@@ -70,42 +70,42 @@ dependencies:
|
|
70
70
|
name: pry
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - '>='
|
73
|
+
- - ! '>='
|
74
74
|
- !ruby/object:Gem::Version
|
75
75
|
version: '0'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - '>='
|
80
|
+
- - ! '>='
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '0'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rake
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - '>='
|
87
|
+
- - ! '>='
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: '0'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - '>='
|
94
|
+
- - ! '>='
|
95
95
|
- !ruby/object:Gem::Version
|
96
96
|
version: '0'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: yard
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
100
100
|
requirements:
|
101
|
-
- - '>='
|
101
|
+
- - ! '>='
|
102
102
|
- !ruby/object:Gem::Version
|
103
103
|
version: '0'
|
104
104
|
type: :development
|
105
105
|
prerelease: false
|
106
106
|
version_requirements: !ruby/object:Gem::Requirement
|
107
107
|
requirements:
|
108
|
-
- - '>='
|
108
|
+
- - ! '>='
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
111
|
description: No magic authorization for Rails
|
@@ -148,20 +148,21 @@ require_paths:
|
|
148
148
|
- lib
|
149
149
|
required_ruby_version: !ruby/object:Gem::Requirement
|
150
150
|
requirements:
|
151
|
-
- - '>='
|
151
|
+
- - ! '>='
|
152
152
|
- !ruby/object:Gem::Version
|
153
153
|
version: '0'
|
154
154
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
155
155
|
requirements:
|
156
|
-
- - '>='
|
156
|
+
- - ! '>='
|
157
157
|
- !ruby/object:Gem::Version
|
158
158
|
version: '0'
|
159
159
|
requirements: []
|
160
160
|
rubyforge_project:
|
161
|
-
rubygems_version: 2.4.
|
161
|
+
rubygems_version: 2.4.8
|
162
162
|
signing_key:
|
163
163
|
specification_version: 4
|
164
164
|
summary: Rails Auth
|
165
165
|
test_files:
|
166
166
|
- spec/canner_spec.rb
|
167
167
|
- spec/spec_helper.rb
|
168
|
+
has_rdoc:
|