pundit_helpers 0.0.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 +7 -0
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +55 -0
- data/Rakefile +9 -0
- data/lib/pundit_helpers/version.rb +3 -0
- data/lib/pundit_helpers.rb +39 -0
- data/pundit_helpers.gemspec +26 -0
- data/spec/pundit_helpers_spec.rb +46 -0
- metadata +110 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: eabe76151f5ab089cd71e22a1eaf07f5ac46e405
|
|
4
|
+
data.tar.gz: 65e3da497c6d5000e8b6e93153227b085519b0b7
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 0f48e47729f0b5464cb4167077e20a686b0cb63cd50bb278b4a070085a743dda9f861b89e0ef5d95758fa29dbcf7cf4eb302f82d786b1b77bdd2e1eb47602625
|
|
7
|
+
data.tar.gz: a9934477a2f25f26ac1c186208de1238b91f4f01c77b267d2644632f2e7516aa7360af56897f095d72a0162bcb1b435edfeae08f8c37f93b875d4303d09b5d9c
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Copyright (c) 2014 Brendon Murphy
|
|
2
|
+
|
|
3
|
+
MIT License
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
|
+
a copy of this software and associated documentation files (the
|
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
|
11
|
+
the following conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice shall be
|
|
14
|
+
included in all copies or substantial portions of the Software.
|
|
15
|
+
|
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# PunditHelpers
|
|
2
|
+
|
|
3
|
+
Authorization helpers for Pundit
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
gem 'pundit_helpers'
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install pundit_helpers
|
|
18
|
+
|
|
19
|
+
## Usage
|
|
20
|
+
|
|
21
|
+
In your base controller or controllers that need Pundit:
|
|
22
|
+
|
|
23
|
+
```ruby
|
|
24
|
+
class ApplicationController < ActionController::Base
|
|
25
|
+
include Pundit
|
|
26
|
+
include PunditHelpers
|
|
27
|
+
end
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
The helpers currently add an `#authorized?` method that is useful
|
|
31
|
+
for letting Pundit know that you have checked authorization so that the
|
|
32
|
+
`verify_authorized` after filter doesn't raise an exception, and returns
|
|
33
|
+
false for failures rather than raising.
|
|
34
|
+
|
|
35
|
+
```ruby
|
|
36
|
+
authorize(post, :show?) # Returns true or raises Pundit::NotAuthorized on failure
|
|
37
|
+
authorized?(post, :show?) # Returns true or false
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
Also, this adds a `#can?` method ala CanCan. Passing `can?` an action and record
|
|
41
|
+
will return a boolean and is useful in views. For example:
|
|
42
|
+
|
|
43
|
+
```erb
|
|
44
|
+
<% if can? :edit, @lesson %>
|
|
45
|
+
<a href="/posts/42/edit">edit</a>
|
|
46
|
+
<% end %>
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Contributing
|
|
50
|
+
|
|
51
|
+
1. Fork it ( http://github.com/<my-github-username>/pundit_helpers/fork )
|
|
52
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
|
53
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
|
54
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
|
55
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
require "pundit_helpers/version"
|
|
2
|
+
|
|
3
|
+
module PunditHelpers
|
|
4
|
+
# Pundit's core `#authorize` helper always raises
|
|
5
|
+
# an error, but also lets the controller know an authorization
|
|
6
|
+
# has been performed. Sometimes it is preferrable to flag that an
|
|
7
|
+
# authorization check has been made, but return boolean rather than
|
|
8
|
+
# raise. So this uses an exception rescue for flow control, which is
|
|
9
|
+
# not optimal but fits nicely with the current API and doesn't cause
|
|
10
|
+
# serious breakage
|
|
11
|
+
#
|
|
12
|
+
# @param [record] record - the record to check
|
|
13
|
+
# @param [string or symbol] query - the policy action to check for
|
|
14
|
+
#
|
|
15
|
+
# @return [Boolean]
|
|
16
|
+
def authorized?(record, query=nil)
|
|
17
|
+
begin
|
|
18
|
+
authorize(record, query)
|
|
19
|
+
rescue Pundit::NotAuthorizedError
|
|
20
|
+
false
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# The current user permissions can be policy checked in views:
|
|
25
|
+
#
|
|
26
|
+
# <% if can? :edit, @lesson %>
|
|
27
|
+
# <a href="/posts/42/edit">edit</a>
|
|
28
|
+
# <% end %>
|
|
29
|
+
#
|
|
30
|
+
# @param [string or symbol] query - the query to check
|
|
31
|
+
# @param [record] record - the record for policy lookup
|
|
32
|
+
#
|
|
33
|
+
# @return [Boolean]
|
|
34
|
+
def can?(query, record)
|
|
35
|
+
query = "#{query}?"
|
|
36
|
+
policy = Pundit.policy!(current_user, record)
|
|
37
|
+
!! policy.public_send(query)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'pundit_helpers/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "pundit_helpers"
|
|
8
|
+
spec.version = PunditHelpers::VERSION
|
|
9
|
+
spec.authors = ["Brendon Murphy"]
|
|
10
|
+
spec.email = ["xternal1+github@gmail.com"]
|
|
11
|
+
spec.summary = %q{Authorization helpers for Pundit}
|
|
12
|
+
spec.summary = spec.description
|
|
13
|
+
spec.homepage = ""
|
|
14
|
+
spec.license = "MIT"
|
|
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 "pundit", "~> 0.2.1"
|
|
22
|
+
|
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
|
24
|
+
spec.add_development_dependency "rake"
|
|
25
|
+
spec.add_development_dependency "rspec", "~> 2.14.1"
|
|
26
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require "pundit"
|
|
2
|
+
require "pundit_helpers"
|
|
3
|
+
|
|
4
|
+
class Harness
|
|
5
|
+
include PunditHelpers
|
|
6
|
+
|
|
7
|
+
def current_user
|
|
8
|
+
:spec_current_user
|
|
9
|
+
end
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
describe PunditHelpers, "#authorized?" do
|
|
13
|
+
it "is true if #authorize returns true" do
|
|
14
|
+
harness = Harness.new
|
|
15
|
+
record = double
|
|
16
|
+
expect(harness).to receive(:authorize).with(record, :show?).and_return(true)
|
|
17
|
+
expect(harness.authorized?(record, :show?)).to be_true
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it "is false if #authorize raises Pundit::NotAuthorizedError" do
|
|
21
|
+
harness = Harness.new
|
|
22
|
+
record = double
|
|
23
|
+
expect(harness).to receive(:authorize).with(record, :show?).and_raise(Pundit::NotAuthorizedError)
|
|
24
|
+
expect(harness.authorized?(record, :show?)).to be_false
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
describe PunditHelpers, "#can?" do
|
|
29
|
+
it "is true if the located policy permits the action" do
|
|
30
|
+
record = double
|
|
31
|
+
policy = double(:edit? => true)
|
|
32
|
+
harness = Harness.new
|
|
33
|
+
|
|
34
|
+
expect(Pundit).to receive(:policy!).with(:spec_current_user, record).and_return(policy)
|
|
35
|
+
expect(harness.can?(:edit, record)).to be_true
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
it "is false if the located policy denies the action" do
|
|
39
|
+
record = double
|
|
40
|
+
policy = double(:edit? => false)
|
|
41
|
+
harness = Harness.new
|
|
42
|
+
|
|
43
|
+
expect(Pundit).to receive(:policy!).with(:spec_current_user, record).and_return(policy)
|
|
44
|
+
expect(harness.can?(:edit, record)).to be_false
|
|
45
|
+
end
|
|
46
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: pundit_helpers
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Brendon Murphy
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2014-04-22 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: pundit
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: 0.2.1
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: 0.2.1
|
|
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: rake
|
|
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: rspec
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: 2.14.1
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: 2.14.1
|
|
69
|
+
description:
|
|
70
|
+
email:
|
|
71
|
+
- xternal1+github@gmail.com
|
|
72
|
+
executables: []
|
|
73
|
+
extensions: []
|
|
74
|
+
extra_rdoc_files: []
|
|
75
|
+
files:
|
|
76
|
+
- ".gitignore"
|
|
77
|
+
- Gemfile
|
|
78
|
+
- LICENSE.txt
|
|
79
|
+
- README.md
|
|
80
|
+
- Rakefile
|
|
81
|
+
- lib/pundit_helpers.rb
|
|
82
|
+
- lib/pundit_helpers/version.rb
|
|
83
|
+
- pundit_helpers.gemspec
|
|
84
|
+
- spec/pundit_helpers_spec.rb
|
|
85
|
+
homepage: ''
|
|
86
|
+
licenses:
|
|
87
|
+
- MIT
|
|
88
|
+
metadata: {}
|
|
89
|
+
post_install_message:
|
|
90
|
+
rdoc_options: []
|
|
91
|
+
require_paths:
|
|
92
|
+
- lib
|
|
93
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
94
|
+
requirements:
|
|
95
|
+
- - ">="
|
|
96
|
+
- !ruby/object:Gem::Version
|
|
97
|
+
version: '0'
|
|
98
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
99
|
+
requirements:
|
|
100
|
+
- - ">="
|
|
101
|
+
- !ruby/object:Gem::Version
|
|
102
|
+
version: '0'
|
|
103
|
+
requirements: []
|
|
104
|
+
rubyforge_project:
|
|
105
|
+
rubygems_version: 2.2.2
|
|
106
|
+
signing_key:
|
|
107
|
+
specification_version: 4
|
|
108
|
+
summary: ''
|
|
109
|
+
test_files:
|
|
110
|
+
- spec/pundit_helpers_spec.rb
|