pundit-resources 1.0.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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 84095ef1d7eddc023bc1c00e4501b4068e86386e
4
+ data.tar.gz: 557410b3f7c007a2905702fb2a9c4d5c15331e16
5
+ SHA512:
6
+ metadata.gz: e4ba2f3c7e00b6cc852858920804acd20ec77bdc3a23a772412ec05c2077c4d9926e64e49ad41b6f8fa4858e0c5f5465fa355fcbe4ccf8c02772783122c99083
7
+ data.tar.gz: 8c37cec27ae036fb6de09bda7ed756d5e1ea27eb58cc2f67c0dd84303b463507393d6da399bd2978bd8f6f195d39b4425a1e8a454de1f54d5442310a6fdc1743
@@ -0,0 +1,18 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ gem_graph.png
11
+ log/*.log
12
+ pkg/
13
+ spec/dummy/db/*.sqlite3
14
+ spec/dummy/db/*.sqlite3-journal
15
+ spec/dummy/log/*.log
16
+ spec/dummy/tmp/
17
+ !spec/dummy/tmp/.keep
18
+ spec/examples.txt
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --require spec_helper
3
+ --color
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3.0
4
+ before_install: gem install bundler -v 1.11.2
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Dependencies for dummy application
4
+ gem 'sqlite3'
5
+ gem 'jsonapi-resources', github: 'cerebris/jsonapi-resources'
6
+ gem 'pundit'
7
+
8
+ gemspec
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Toggle Professional Services, LLC
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # Pundit::Resources
2
+
3
+ Pundit::Resources is a gem that makes [JSONAPI::Resources](jsonapi-resources) use [Pundit][pundit] authorization.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'pundit-resources'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```sh
16
+ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+
21
+ ```sh
22
+ gem install pundit-resources
23
+ ```
24
+
25
+ ## Usage
26
+
27
+ Include `Pundit::ResourceController` in the resource controllers that should use Pundit.
28
+
29
+ You also need to define a `current_user` method on the controller.
30
+ The result of this method will be passed as the user parameter to the Pundit policies.
31
+
32
+ ```ruby
33
+ class ApplicationController < JSONAPI::ResourceController
34
+ include Pundit::ResourceController
35
+
36
+ protected
37
+
38
+ def current_user
39
+ User.find(params[:id])
40
+ end
41
+ end
42
+ ```
43
+
44
+ Also, include `Pundit::Resource` in the resources that should use Pundit:
45
+
46
+ ```ruby
47
+ class ApplicationResource < JSONAPI::Resource
48
+ include Pundit::Resource
49
+ end
50
+ ```
51
+
52
+ Pundit::Resources does not use the `show?` action on Pundit policies.
53
+ Instead, it checks to see if the given resource is included in the Scope for that policy.
54
+
55
+ ## Development
56
+
57
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
58
+
59
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org][rubygems].
60
+
61
+ ## License
62
+
63
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
64
+
65
+ [jsonapi-resources]: https://github.com/cerebris/jsonapi-resources
66
+ [pundit]: https://github.com/elabs/pundit
67
+ [rubygems]: https://rubygems.org
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "pundit/resources"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,84 @@
1
+ require "active_support/concern"
2
+
3
+ module Pundit
4
+ module Resource
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ before_save :authorize_create_or_update
9
+ before_remove :authorize_destroy
10
+ end
11
+
12
+ module ClassMethods
13
+ def records(options = {})
14
+ warn_if_show_defined
15
+
16
+ context = options[:context]
17
+ Pundit.policy_scope!(context[:current_user], _model_class)
18
+ end
19
+
20
+ private
21
+
22
+ def warn_if_show_defined
23
+ policy_class = Pundit::PolicyFinder.new(_model_class.new).policy!
24
+ if policy_class.method_defined?(:show?)
25
+ puts "WARN: pundit-resources does not use the show? action."
26
+ puts " #{policy_class::Scope} will be used instead."
27
+ end
28
+ end
29
+ end
30
+
31
+ protected
32
+
33
+ def current_user
34
+ context&.[](:current_user)
35
+ end
36
+
37
+ def policy
38
+ Pundit.policy!(current_user, _model)
39
+ end
40
+
41
+ def authorize_create_or_update
42
+ action = _model.new_record? ? :create : :update
43
+ not_authorized!(action) unless policy.public_send(:"#{action}?")
44
+ end
45
+
46
+ def authorize_destroy
47
+ not_authorized! :destroy unless policy.destroy?
48
+ end
49
+
50
+ def records_for(association_name, options={})
51
+ association_reflection = _model.class.reflect_on_association(association_name)
52
+
53
+ if association_reflection.macro == :has_many
54
+ records = _model.public_send(association_name)
55
+ policy_scope = Pundit.policy_scope!(
56
+ context[:current_user],
57
+ association_reflection.class_name.constantize
58
+ )
59
+ records.merge(policy_scope)
60
+ elsif [:has_one, :belongs_to].include?(association_reflection.macro)
61
+ record = _model.public_send(association_name)
62
+
63
+ # Don't rely on policy.show? being defined since it isn't used for
64
+ # show actions directly and should always have the same behaviour.
65
+ if record && show?(Pundit.policy!(context[:current_user], record))
66
+ record
67
+ else
68
+ nil
69
+ end
70
+ end
71
+ end
72
+
73
+ private
74
+
75
+ def not_authorized!(action)
76
+ options = { query: action, record: _model, policy: policy }
77
+ raise Pundit::NotAuthorizedError, options
78
+ end
79
+
80
+ def show?(policy)
81
+ policy.scope.where(id: policy.record.id).exists?
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,39 @@
1
+ module Pundit
2
+ module ResourceController
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ include ActionController::Rescue
7
+
8
+ JSONAPI.configure do |config|
9
+ error = Pundit::NotAuthorizedError
10
+ unless config.exception_class_whitelist.include? error
11
+ config.exception_class_whitelist << error
12
+ end
13
+ end
14
+
15
+ rescue_from Pundit::NotAuthorizedError, with: :reject_forbidden_request
16
+ end
17
+
18
+ protected
19
+
20
+ def reject_forbidden_request(error)
21
+ type = error.record.class.name.underscore.humanize(capitalize: false)
22
+ error = JSONAPI::Error.new(
23
+ code: JSONAPI::FORBIDDEN,
24
+ status: :forbidden,
25
+ title: "#{params[:action].capitalize} Forbidden",
26
+ detail: "You don't have permission to #{params[:action]} this #{type}.",
27
+ )
28
+
29
+ render json: { errors: [error] }, status: 403
30
+ end
31
+
32
+ def context
33
+ { current_user: current_user }
34
+ end
35
+
36
+ def current_user
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ require "pundit/resources/version"
2
+ require "pundit/resource"
3
+ require "pundit/resource_controller"
@@ -0,0 +1,5 @@
1
+ module Pundit
2
+ module Resources
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pundit/resources/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pundit-resources"
8
+ spec.version = Pundit::Resources::VERSION
9
+ spec.authors = ["Ross Penman", "Sean Devine"]
10
+ spec.email = ["ross@pen.mn", "barelyknown@icloud.com"]
11
+
12
+ spec.summary = %q{Integrate JSONAPI::Resources with Pundit}
13
+ spec.homepage = "https://github.com/togglepro/pundit-resources"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+ spec.add_dependency "jsonapi-resources"
23
+ spec.add_dependency "pundit"
24
+ spec.add_dependency "rails", ">= 5.0.0.rc1", "< 5.1"
25
+
26
+ spec.add_development_dependency "bundler", "~> 1.11"
27
+ spec.add_development_dependency "rake", "~> 10.0"
28
+ spec.add_development_dependency "rspec-rails", '>= 3.5.0.beta3', '< 4.0.0'
29
+ end
metadata ADDED
@@ -0,0 +1,170 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pundit-resources
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Ross Penman
8
+ - Sean Devine
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2016-05-25 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: jsonapi-resources
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ">="
33
+ - !ruby/object:Gem::Version
34
+ version: '0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: pundit
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rails
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 5.0.0.rc1
63
+ - - "<"
64
+ - !ruby/object:Gem::Version
65
+ version: '5.1'
66
+ type: :runtime
67
+ prerelease: false
68
+ version_requirements: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 5.0.0.rc1
73
+ - - "<"
74
+ - !ruby/object:Gem::Version
75
+ version: '5.1'
76
+ - !ruby/object:Gem::Dependency
77
+ name: bundler
78
+ requirement: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.11'
83
+ type: :development
84
+ prerelease: false
85
+ version_requirements: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.11'
90
+ - !ruby/object:Gem::Dependency
91
+ name: rake
92
+ requirement: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ type: :development
98
+ prerelease: false
99
+ version_requirements: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ - !ruby/object:Gem::Dependency
105
+ name: rspec-rails
106
+ requirement: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: 3.5.0.beta3
111
+ - - "<"
112
+ - !ruby/object:Gem::Version
113
+ version: 4.0.0
114
+ type: :development
115
+ prerelease: false
116
+ version_requirements: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: 3.5.0.beta3
121
+ - - "<"
122
+ - !ruby/object:Gem::Version
123
+ version: 4.0.0
124
+ description:
125
+ email:
126
+ - ross@pen.mn
127
+ - barelyknown@icloud.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".travis.yml"
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - bin/console
140
+ - bin/setup
141
+ - lib/pundit/resource.rb
142
+ - lib/pundit/resource_controller.rb
143
+ - lib/pundit/resources.rb
144
+ - lib/pundit/resources/version.rb
145
+ - pundit-resources.gemspec
146
+ homepage: https://github.com/togglepro/pundit-resources
147
+ licenses:
148
+ - MIT
149
+ metadata: {}
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubyforge_project:
166
+ rubygems_version: 2.5.1
167
+ signing_key:
168
+ specification_version: 4
169
+ summary: Integrate JSONAPI::Resources with Pundit
170
+ test_files: []