resource_allow_header 0.2.0 → 0.3.0

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
  SHA256:
3
- metadata.gz: 137762279a09a5f2e409e209f7d21a13fc7e6de0a25e0f0eb0ab008990cddfcf
4
- data.tar.gz: 8b38a2caac86462ed31294fd4fe5dc6f6196d4da839b158435e76372809b8ffb
3
+ metadata.gz: a6042652fa336298bc96029b0df1bc7d05d56cec1bf1c8a0e089dc67b421e645
4
+ data.tar.gz: 91232dafecf7868df240c67d70605473bd7c0d2bb79e76a7999ee2eace780364
5
5
  SHA512:
6
- metadata.gz: 9249deb003a4b6efd6087b5e1d499a8e3201efa4fbef0d9d4e8b34e3cd47a78d6b4868db4a7da5c38ad014232346260fe86ce77a03f9897d406ba1923b32daeb
7
- data.tar.gz: 875e2f1c39afbbe8d7bd424bf36cee6ddf1dbe4c4359a3b2797009855cc9ef685cdfc2a2fd729386eb276d49a9e893b1e17f8fb3016f187617a861688a7e903c
6
+ metadata.gz: 1f57b4640784af39c27a9472611a64a83ac8eb62c4079d853c19f918146f45212b9c76ce987dc6af9a44e08cb8d8a540836f8ba8fb0cb2121965633965660f91
7
+ data.tar.gz: 02efeae0231548af6312a12fe46e807e21f2fd556bcc253ef111ed450dc814c2ba90c8cb668f6d62ad9e79a5b70945fa9c046bc0d27850b07acf5ed3b8c2cd2d
@@ -0,0 +1,7 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Settings><!--This file was automatically generated by Ruby plugin.
3
+ You are allowed to:
4
+ 1. Remove rake task
5
+ 2. Add existing rake tasks
6
+ To add existing rake tasks automatically delete this file and reload the project.
7
+ --><RakeGroup description="" fullCmd="" taksId="rake"><RakeTask description="Build resource_allow_header-0.2.0.gem into the pkg directory" fullCmd="build" taksId="build" /><RakeTask description="Remove any temporary products" fullCmd="clean" taksId="clean" /><RakeTask description="Remove any generated files" fullCmd="clobber" taksId="clobber" /><RakeTask description="Build and install resource_allow_header-0.2.0.gem into system gems" fullCmd="install" taksId="install" /><RakeGroup description="" fullCmd="" taksId="install"><RakeTask description="Build and install resource_allow_header-0.2.0.gem into system gems without network access" fullCmd="install:local" taksId="local" /></RakeGroup><RakeTask description="Create tag v0.2.0 and build and push resource_allow_header-0.2.0.gem to rubygems.org" fullCmd="release[remote]" taksId="release[remote]" /><RakeTask description="Run tests" fullCmd="test" taksId="test" /><RakeTask description="" fullCmd="default" taksId="default" /><RakeTask description="" fullCmd="release" taksId="release" /><RakeGroup description="" fullCmd="" taksId="release"><RakeTask description="" fullCmd="release:guard_clean" taksId="guard_clean" /><RakeTask description="" fullCmd="release:rubygem_push" taksId="rubygem_push" /><RakeTask description="" fullCmd="release:source_control_push" taksId="source_control_push" /></RakeGroup></RakeGroup></Settings>
@@ -1,7 +1,26 @@
1
1
  <?xml version="1.0" encoding="UTF-8"?>
2
2
  <module type="RUBY_MODULE" version="4">
3
3
  <component name="ModuleRunConfigurationManager">
4
- <shared />
4
+ <shared>
5
+ <configuration default="false" name="test" type="RakeRunConfigurationType" factoryName="Rake">
6
+ <module name="resource_allow_header" />
7
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RUBY_ARGS" VALUE="" />
8
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="WORK DIR" VALUE="$MODULE_DIR$" />
9
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="SHOULD_USE_SDK" VALUE="false" />
10
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="ALTERN_SDK_NAME" VALUE="" />
11
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="myPassParentEnvs" VALUE="true" />
12
+ <EXTENSION ID="BundlerRunConfigurationExtension" bundleExecEnabled="true" />
13
+ <EXTENSION ID="JRubyRunConfigurationExtension" NailgunExecEnabled="false" />
14
+ <EXTENSION ID="RubyCoverageRunConfigurationExtension" track_test_folders="true" runner="rcov" />
15
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_NAME" VALUE="test" />
16
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_ARGS" VALUE="" />
17
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_ATTACHED_TEST_FRAMEWORKS" VALUE=":test_unit " />
18
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_OPTION_TRACE" VALUE="false" />
19
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_OPTION_DRYRUN" VALUE="false" />
20
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_OPTION_PREREQS" VALUE="false" />
21
+ <method v="2" />
22
+ </configuration>
23
+ </shared>
5
24
  </component>
6
25
  <component name="NewModuleRootManager">
7
26
  <content url="file://$MODULE_DIR$" />
@@ -15,7 +34,6 @@
15
34
  <orderEntry type="library" scope="PROVIDED" name="concurrent-ruby (v1.1.4, ruby-2.5.3-p105) [gem]" level="application" />
16
35
  <orderEntry type="library" scope="PROVIDED" name="i18n (v1.5.3, ruby-2.5.3-p105) [gem]" level="application" />
17
36
  <orderEntry type="library" scope="PROVIDED" name="minitest (v5.11.3, ruby-2.5.3-p105) [gem]" level="application" />
18
- <orderEntry type="library" scope="PROVIDED" name="rack (v2.0.6, ruby-2.5.3-p105) [gem]" level="application" />
19
37
  <orderEntry type="library" scope="PROVIDED" name="rake (v10.5.0, ruby-2.5.3-p105) [gem]" level="application" />
20
38
  <orderEntry type="library" scope="PROVIDED" name="thread_safe (v0.3.6, ruby-2.5.3-p105) [gem]" level="application" />
21
39
  <orderEntry type="library" scope="PROVIDED" name="tzinfo (v1.2.5, ruby-2.5.3-p105) [gem]" level="application" />
@@ -0,0 +1,20 @@
1
+ <component name="ProjectRunConfigurationManager">
2
+ <configuration default="false" name="test" type="RakeRunConfigurationType" factoryName="Rake">
3
+ <module name="resource_allow_header" />
4
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RUBY_ARGS" VALUE="" />
5
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="WORK DIR" VALUE="$MODULE_DIR$" />
6
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="SHOULD_USE_SDK" VALUE="false" />
7
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="ALTERN_SDK_NAME" VALUE="" />
8
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="myPassParentEnvs" VALUE="true" />
9
+ <EXTENSION ID="BundlerRunConfigurationExtension" bundleExecEnabled="true" />
10
+ <EXTENSION ID="JRubyRunConfigurationExtension" NailgunExecEnabled="false" />
11
+ <EXTENSION ID="RubyCoverageRunConfigurationExtension" track_test_folders="true" runner="rcov" />
12
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_NAME" VALUE="test" />
13
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_ARGS" VALUE="" />
14
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_ATTACHED_TEST_FRAMEWORKS" VALUE=":test_unit " />
15
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_OPTION_TRACE" VALUE="false" />
16
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_OPTION_DRYRUN" VALUE="false" />
17
+ <RAKE_RUN_CONFIG_SETTINGS_ID NAME="RAKE_TASK_OPTION_PREREQS" VALUE="false" />
18
+ <method v="2" />
19
+ </configuration>
20
+ </component>
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.3.0
4
+
5
+ - Add tests
6
+ - Change configuration to return self if not block was given
7
+ - Change configuration to execute in context of self
8
+ - Change `*_proc` to execute in context of self
9
+
3
10
  ## 0.2.0
4
11
 
5
12
  - Add configuration via `ResourceAllowHeader.configure`
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- resource_allow_header (0.2.0)
4
+ resource_allow_header (0.3.0)
5
5
  activerecord (>= 4.0.0)
6
6
  activesupport (>= 4.0.0)
7
7
 
data/README.md CHANGED
@@ -1,72 +1,106 @@
1
- # Resource Allow Header
2
-
3
- [![Build Status: master](https://travis-ci.com/XPBytes/resource_allow_header.svg)](https://travis-ci.com/XPBytes/resource_allow_header)
4
- [![Gem Version](https://badge.fury.io/rb/resource_allow_header.svg)](https://badge.fury.io/rb/resource_allow_header)
5
- [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
6
-
7
- CanCan supported Allow Header for Rack responses
8
-
9
- ## Installation
10
-
11
- Add this line to your application's Gemfile:
12
-
13
- ```ruby
14
- gem 'resource_allow_header'
15
- ```
16
-
17
- And then execute:
18
-
19
- $ bundle
20
-
21
- Or install it yourself as:
22
-
23
- $ gem install resource_allow_header
24
-
25
- ## Usage
26
-
27
- In your controller use the `allow` class method to determine the value of the `Allow` header:
28
- ```ruby
29
- require 'resource_allow_header'
30
-
31
- class BookController < ApiController
32
- allow('HEAD', only: %i[show]) { @book }
33
- allow('GET', only: %i[show]) { @book }
34
- allow('POST', only: %i[create]) { Current.author.books.build }
35
- end
36
- ```
37
-
38
- The allow method expects:
39
- - `http_method`: One of `HEAD`, `GET`, `POST`, `PUT`, `PATCH`, `DELETE`.
40
- - `ability_action` (optional): Automatically mapped from HTTP method and passed to `can?`
41
- - `**options`: passed to `before_action` (so only set these values on show: `only: :show`)
42
- - `&block`: the block that resolved the resource
43
-
44
- If no block has been given, the `@allow_resource` instance variable is used, or the `@resource` variable.
45
-
46
- The allow header is set as `after_action` callback, which allows your entire request to determine or set the
47
- values you'll be returning in the `&block` passed to `allow`. In other words: these blocks are lazy and
48
- executed in the context of your controller _instance_.
49
-
50
- ### Configuration
51
-
52
- In an initializer you can set procs in order to change the default behaviour:
53
-
54
- ```ruby
55
- ResourceAllowHeader.configure do |this|
56
- this.implicit_resource_proc = proc { |controller| controller.resource }
57
- this.can_proc = proc { |action, resource, controller| action == :whatever || controller.can?(action, resource) }
58
- end
59
- ```
60
-
61
- ## Development
62
-
63
- After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can
64
- also run `bin/console` for an interactive prompt that will allow you to experiment.
65
-
66
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
67
- version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
68
- push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
69
-
70
- ## Contributing
71
-
72
- Bug reports and pull requests are welcome on GitHub at [XPBytes/resource_allow_header](https://github.com/XPBytes/resource_allow_header).
1
+ # Resource Allow Header
2
+
3
+ [![Build Status: master](https://travis-ci.com/XPBytes/resource_allow_header.svg)](https://travis-ci.com/XPBytes/resource_allow_header)
4
+ [![Gem Version](https://badge.fury.io/rb/resource_allow_header.svg)](https://badge.fury.io/rb/resource_allow_header)
5
+ [![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)
6
+
7
+ Allow Header for Rack responses using CanCan(Can) or any other authorization framework
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'resource_allow_header'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install resource_allow_header
24
+
25
+ This relies on `before_action` and `after_action` to exist, which is normally the case for any controller using
26
+ `AbstractController` in their chain. `Metal` controllers might need to include `Metal::Callbacks`.
27
+
28
+ ## Usage
29
+
30
+ In your controller use the `allow` class method to determine the value of the `Allow` header:
31
+ ```ruby
32
+ require 'resource_allow_header'
33
+
34
+ class ApiController < ActionController::API
35
+ include ResourceAllowHeader
36
+ end
37
+
38
+ class BookController < ApiController
39
+ allow('HEAD', only: %i[show]) { @book }
40
+ allow('GET', only: %i[show]) { @book }
41
+ allow('POST', only: %i[create]) { Current.author.books.build }
42
+ end
43
+ ```
44
+
45
+ The allow method expects:
46
+ - `http_method`: One of `HEAD`, `GET`, `POST`, `PUT`, `PATCH`, `DELETE`.
47
+ - `ability_action` (optional): Automatically mapped from HTTP method and passed to `can?`
48
+ - `**options`: passed to `before_action` (so only set these values on show: `only: :show`)
49
+ - `&block`: the block that resolved the resource
50
+
51
+ If no block has been given, the `@allow_resource` instance variable is used, or the `@resource` variable.
52
+
53
+ The allow header is set as `after_action` callback, which allows your entire request to determine or set the
54
+ values you'll be returning in the `&block` passed to `allow`. In other words: these blocks are lazy and
55
+ executed in the context of your controller _instance_.
56
+
57
+ ### Sane defaults
58
+
59
+ If your API is conforming to REST, you'll usually have the following:
60
+
61
+ ```ruby
62
+ allow('HEAD') { @your_resource }
63
+ allow('GET') { @your_resource }
64
+ allow('POST', only: %i[create index]) { YourResource.new(authorized_context) }
65
+ allow('PUT', only: %i[show update]) { @your_resource }
66
+ allow('DESTROY', only: %i[show update]) { @your_resource }
67
+ ```
68
+
69
+ This is the case because:
70
+ - Your `index` path (collection) is the same as your `create` path
71
+ - Your `show` path (resource) is the same as your `update` and `delete` path
72
+ - You can call `HEAD` both on the collection (`index`) and resource (`show`)
73
+ - You can call `GET` on both the collection (`index`) and resource (`show`)
74
+ - You can call `POST` only on the collection (`index`) path
75
+ - You can call `PUT` and `DESTROY` only on the resource (`show`) path
76
+
77
+ If
78
+
79
+ ### Configuration
80
+
81
+ In an initializer you can set procs in order to change the default behaviour:
82
+
83
+ ```ruby
84
+ ResourceAllowHeader.configure do
85
+ self.implicit_resource_proc = proc { |controller| controller.resource }
86
+ self.can_proc = proc { |action, resource, controller| action == :whatever || controller.can?(action, resource) }
87
+ end
88
+ ```
89
+
90
+ ## Related
91
+
92
+ - [`AuthorizedTransaction`](https://github.com/XPBytes/authorized_transaction): :closed_lock_with_key: Authorize an
93
+ activerecord transaction (or any other transaction) with cancan(can) or any other authorization framework
94
+
95
+ ## Development
96
+
97
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can
98
+ also run `bin/console` for an interactive prompt that will allow you to experiment.
99
+
100
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the
101
+ version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version,
102
+ push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
103
+
104
+ ## Contributing
105
+
106
+ Bug reports and pull requests are welcome on GitHub at [XPBytes/resource_allow_header](https://github.com/XPBytes/resource_allow_header).
@@ -1,4 +1,5 @@
1
1
  require "resource_allow_header/version"
2
+
2
3
  require 'active_support/concern'
3
4
  require 'active_support/core_ext/module/attribute_accessors'
4
5
 
@@ -12,12 +13,12 @@ module ResourceAllowHeader
12
13
 
13
14
  mattr_accessor :implicit_resource_proc, :can_proc
14
15
 
15
- def self.configure
16
- yield self
16
+ def self.configure(&block)
17
+ block_given? ? instance_exec(self, &block) : self
17
18
  end
18
19
 
19
20
  included do
20
- attr_accessor :allow_
21
+ attr_accessor :current_action_lazy_allows
21
22
  after_action :set_allow_header
22
23
 
23
24
  def set_allow_header
@@ -25,7 +26,7 @@ module ResourceAllowHeader
25
26
  end
26
27
 
27
28
  def compute_allow_header(resource: implicit_resource)
28
- Hash(allow_).each_with_object([]) do |(method, allow), result|
29
+ Hash(current_action_lazy_allows).each_with_object([]) do |(method, allow), result|
29
30
  allowable_resource = allow[:resource]&.call || resource
30
31
  next unless allow?(allow[:action], allowable_resource)
31
32
  result << method
@@ -36,18 +37,21 @@ module ResourceAllowHeader
36
37
  class_methods do
37
38
  # noinspection RubyStringKeysInHashInspection
38
39
  HTTP_ABILITY_METHOD_MAP = {
39
- 'HEAD' => :show,
40
- 'GET' => :show,
41
- 'POST' => :create,
42
- 'PUT' => :update,
43
- 'PATCH' => :update,
44
- 'DELETE' => :destroy
40
+ 'HEAD' => :show,
41
+ 'GET' => :show,
42
+ 'POST' => :create,
43
+ 'PUT' => :update,
44
+ 'PATCH' => :update,
45
+ 'DELETE' => :destroy
45
46
  }.freeze
46
47
 
47
48
  def allow(http_method, ability_action = map_http_method_to_ability_action(http_method), **options, &block)
48
49
  before_action(**options) do
49
50
  allow_resource = block_given? && proc { instance_exec(&block) } || nil
50
- self.allow_ = Hash(allow_).merge(http_method => { resource: allow_resource, action: ability_action })
51
+
52
+ self.current_action_lazy_allows = Hash(current_action_lazy_allows).merge(
53
+ http_method => { resource: allow_resource, action: ability_action }
54
+ )
51
55
  end
52
56
  end
53
57
 
@@ -56,17 +60,23 @@ module ResourceAllowHeader
56
60
  end
57
61
  end
58
62
 
59
- private
60
-
61
- def implicit_resource
62
- implicit_resource_proc&.call(self) || @allow_resource || @resource
63
- end
63
+ protected
64
64
 
65
65
  def allow?(action, resource)
66
66
  if can_proc.respond_to?(:call)
67
- return can_proc(action, resource, self)
67
+ return instance_exec(action, resource, self, &can_proc)
68
+ end
69
+
70
+ can?(action, resource)
71
+ end
72
+
73
+ private
74
+
75
+ def implicit_resource
76
+ if implicit_resource_proc.respond_to?(:call)
77
+ return instance_exec(self, &implicit_resource_proc)
68
78
  end
69
79
 
70
- can?(allow[:action], allow[:resource]&.call || resource)
80
+ @allow_resource || @resource
71
81
  end
72
82
  end
@@ -1,3 +1,3 @@
1
1
  module ResourceAllowHeader
2
- VERSION = "0.2.0"
2
+ VERSION = '0.3.0'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: resource_allow_header
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Derk-Jan Karrenbeld
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-14 00:00:00.000000000 Z
11
+ date: 2019-02-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -88,10 +88,12 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - ".gitignore"
91
+ - ".idea/.rakeTasks"
91
92
  - ".idea/inspectionProfiles/Project_Default.xml"
92
93
  - ".idea/misc.xml"
93
94
  - ".idea/modules.xml"
94
95
  - ".idea/resource_allow_header.iml"
96
+ - ".idea/runConfigurations/test.xml"
95
97
  - ".idea/vcs.xml"
96
98
  - ".travis.yml"
97
99
  - CHANGELOG.md