can_can_dry 0.4.1 → 0.5.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/helpers/can_can_dry_helper.rb +59 -0
- data/lib/can_can_dry.rb +1 -4
- data/lib/can_can_dry/ability_mapping_sets/devise.rb +4 -0
- data/lib/can_can_dry/ability_mapping_sets/devise_invitable.rb +1 -0
- data/lib/can_can_dry/controller_authorization.rb +5 -3
- data/lib/can_can_dry/engine.rb +5 -0
- data/lib/can_can_dry/path_recognizer.rb +53 -32
- data/lib/can_can_dry/version.rb +1 -1
- data/spec/lib/rubocop_check_spec.rb +1 -5
- data/spec/spec_helper.rb +2 -98
- metadata +15 -9
- data/lib/can_can_dry/helpers/can_can_dry_helper.rb +0 -63
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1d7ed26d5f02337ebace3524ca99ef836d92bb71d2f09a3d8f050b844fa84652
|
4
|
+
data.tar.gz: f931490d6a1147f24e55faaefb1f5b0d45df45e7e44051ebdca55ed86c13500b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ed2245fdc58ce7d5ed07366976a26e640d4df2b6a81c8c8ac6975d311633692070de45527201d510db193f838d89e7e3c105d0bfc2f7df5b731e9cf47243ab9f
|
7
|
+
data.tar.gz: a24fb419ec2a72d746f711d7c6784896a46a73f95ff71c981145615b40220646ec6cf68d0d3127f6b16b9002485d56585513d3ca0a4ff2cebe9c51aea160bdbb
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module CanCanDryHelper
|
4
|
+
def ability_mapping
|
5
|
+
@ability_mapping ||= ::AbilityMapping.new # rubocop:disable Rails/HelperInstanceVariable
|
6
|
+
end
|
7
|
+
|
8
|
+
def can_by_path?(path, method = :get)
|
9
|
+
can_by_can_args(ability_mapping.can_args_by_path(main_app.root_path, path, method))
|
10
|
+
end
|
11
|
+
|
12
|
+
def can_by_path_hash?(path_hash)
|
13
|
+
can_by_can_args(ability_mapping.can_args_by_path_hash(path_hash))
|
14
|
+
end
|
15
|
+
|
16
|
+
def link_or_text(name = nil, options = nil, html_options = nil)
|
17
|
+
link_or_default(name, name, options, html_options)
|
18
|
+
end
|
19
|
+
|
20
|
+
def link_or_nil(name = nil, options = nil, html_options = nil)
|
21
|
+
link_or_default(name, nil, options, html_options)
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def link_or_default(name, default, options, html_options)
|
27
|
+
if can_by_link_options?(options, html_options)
|
28
|
+
link_to(name, options, html_options)
|
29
|
+
else
|
30
|
+
default
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def can_by_link_options?(options, html_options)
|
35
|
+
can_by_path?(url_for(options), link_method(options, html_options))
|
36
|
+
end
|
37
|
+
|
38
|
+
def link_method(*hashs)
|
39
|
+
hashs.each do |h|
|
40
|
+
return h[:method] if h.is_a?(Hash) && h[:method]
|
41
|
+
end
|
42
|
+
:get
|
43
|
+
end
|
44
|
+
|
45
|
+
def can_by_can_args(can_args_args)
|
46
|
+
assert_can_method
|
47
|
+
can_args_args.each do |c|
|
48
|
+
next if c.empty?
|
49
|
+
return true if can?(*c)
|
50
|
+
end
|
51
|
+
false
|
52
|
+
end
|
53
|
+
|
54
|
+
def assert_can_method
|
55
|
+
return if respond_to?('can?')
|
56
|
+
|
57
|
+
singleton_class.include(::CanCanDry::NoControllerCanCanAdditions)
|
58
|
+
end
|
59
|
+
end
|
data/lib/can_can_dry.rb
CHANGED
@@ -12,10 +12,7 @@ module CanCanDry
|
|
12
12
|
require_dependency 'can_can_dry/ability_mapping'
|
13
13
|
require_dependency 'can_can_dry/ability_mapping/path_mapping_not_found'
|
14
14
|
require_dependency 'can_can_dry/ability_mapping/action_mapping_not_found'
|
15
|
-
require_dependency 'can_can_dry/
|
15
|
+
require_dependency 'can_can_dry/engine'
|
16
16
|
require_dependency 'can_can_dry/no_controller_can_can_additions'
|
17
17
|
require_dependency 'can_can_dry/path_recognizer'
|
18
|
-
|
19
|
-
require_dependency 'action_view'
|
20
|
-
ActionView::Base.include CanCanDry::Helpers::CanCanDryHelper
|
21
18
|
end
|
@@ -4,8 +4,12 @@ module CanCanDry
|
|
4
4
|
module AbilityMappingSets
|
5
5
|
module Devise
|
6
6
|
def map_devise
|
7
|
+
map_resources 'Devise::Confirmation'
|
8
|
+
map_resources 'Devise::OmniauthCallback'
|
9
|
+
map_resources 'Devise::Registration'
|
7
10
|
map_resources 'Devise::Session'
|
8
11
|
map_resources 'Devise::Password'
|
12
|
+
map_resources 'Devise::Unlock'
|
9
13
|
end
|
10
14
|
end
|
11
15
|
end
|
@@ -1,10 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require_dependency 'can_can_dry/helpers/can_can_dry_helper'
|
4
|
-
|
5
3
|
module CanCanDry
|
6
4
|
module ControllerAuthorization
|
7
|
-
|
5
|
+
extend ::ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
include ::CanCanDryHelper
|
9
|
+
end
|
8
10
|
|
9
11
|
def authorize_by_ability_mapping
|
10
12
|
raise CanCan::AccessDenied, "Falhou ao tentar acessar #{path_hash}" unless
|
@@ -1,55 +1,76 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'eac_ruby_utils/core_ext'
|
4
|
+
|
3
5
|
module CanCanDry
|
4
6
|
# Copiado de https://github.com/appirits/awesome_admin_layout
|
5
7
|
# /lib/awesome_admin_layout/recognize_path.rb
|
6
|
-
|
8
|
+
class PathRecognizer
|
7
9
|
class << self
|
8
10
|
def recognize(root_path, path, options = {})
|
9
|
-
|
10
|
-
Rails.application.routes.recognize_path(path, options)
|
11
|
-
rescue ActionController::RoutingError
|
12
|
-
Rails::Engine.subclasses.each do |engine|
|
13
|
-
recognized_path = engine_recognize(engine, path, options)
|
14
|
-
return recognized_path if recognized_path
|
15
|
-
end
|
16
|
-
raise "Path not recognized: \"#{path}\" (Options: #{options})"
|
11
|
+
new(root_path, path, options).recognize_or_raise
|
17
12
|
end
|
18
13
|
|
19
|
-
private
|
20
|
-
|
21
14
|
def remove_root_path(root_path, path)
|
22
15
|
path = path.gsub(/\A#{Regexp.quote(root_path)}/, '')
|
23
16
|
path.gsub(%r{\A/*}, '/')
|
24
17
|
end
|
18
|
+
end
|
19
|
+
|
20
|
+
common_constructor :root_path, :path, :options do
|
21
|
+
self.path = self.class.remove_root_path(root_path, path)
|
22
|
+
end
|
23
|
+
|
24
|
+
def recognize_or_raise
|
25
|
+
recognize || raise("Path not recognized: \"#{path}\" (Options: #{options})")
|
26
|
+
end
|
27
|
+
|
28
|
+
def recognize
|
29
|
+
core_recognize || engines_recognize
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
25
33
|
|
26
|
-
|
27
|
-
|
28
|
-
|
34
|
+
def core_recognize
|
35
|
+
Rails.application.routes.recognize_path(path, options)
|
36
|
+
rescue ActionController::RoutingError
|
37
|
+
nil
|
38
|
+
end
|
29
39
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
end
|
35
|
-
nil
|
40
|
+
def engines_recognize
|
41
|
+
Rails::Engine.subclasses.each do |engine|
|
42
|
+
recognized_path = engine_recognize(engine)
|
43
|
+
return recognized_path if recognized_path
|
36
44
|
end
|
45
|
+
nil
|
46
|
+
end
|
37
47
|
|
38
|
-
|
39
|
-
|
40
|
-
|
48
|
+
def engine_recognize(engine)
|
49
|
+
engine_path = path_for_engine(engine.instance.class)
|
50
|
+
return unless engine_path
|
41
51
|
|
42
|
-
|
52
|
+
begin
|
53
|
+
return engine.instance.routes.recognize_path(engine_path, options)
|
54
|
+
rescue ActionController::RoutingError => e
|
55
|
+
Rails.logger.debug "[#{engine}] ActionController::RoutingError: #{e.message}"
|
43
56
|
end
|
57
|
+
nil
|
58
|
+
end
|
59
|
+
|
60
|
+
def path_for_engine(engine_class)
|
61
|
+
engine_route = Rails.application.routes.routes.find { |r| app_class_for(r) == engine_class }
|
62
|
+
return unless engine_route
|
63
|
+
|
64
|
+
path.gsub(/^#{engine_route.path.spec}/, '')
|
65
|
+
end
|
44
66
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
end
|
67
|
+
def app_class_for(route)
|
68
|
+
if Rails.version >= '4.2'
|
69
|
+
# for Rails 4.2
|
70
|
+
route.app.app
|
71
|
+
else
|
72
|
+
# for Rails 4.1, 4.0, 3.2
|
73
|
+
route.app
|
53
74
|
end
|
54
75
|
end
|
55
76
|
end
|
data/lib/can_can_dry/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,100 +1,4 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
6
|
-
# this file to always be loaded, without a need to explicitly require it in any
|
7
|
-
# files.
|
8
|
-
#
|
9
|
-
# Given that it is always loaded, you are encouraged to keep this file as
|
10
|
-
# light-weight as possible. Requiring heavyweight dependencies from this file
|
11
|
-
# will add to the boot time of your test suite on EVERY test run, even for an
|
12
|
-
# individual file that may not need all of that loaded. Instead, consider making
|
13
|
-
# a separate helper file that requires the additional dependencies and performs
|
14
|
-
# the additional setup, and require it from the spec files that actually need
|
15
|
-
# it.
|
16
|
-
#
|
17
|
-
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
18
|
-
RSpec.configure do |config|
|
19
|
-
# rspec-expectations config goes here. You can use an alternate
|
20
|
-
# assertion/expectation library such as wrong or the stdlib/minitest
|
21
|
-
# assertions if you prefer.
|
22
|
-
config.expect_with :rspec do |expectations|
|
23
|
-
# This option will default to `true` in RSpec 4. It makes the `description`
|
24
|
-
# and `failure_message` of custom matchers include text for helper methods
|
25
|
-
# defined using `chain`, e.g.:
|
26
|
-
# be_bigger_than(2).and_smaller_than(4).description
|
27
|
-
# # => "be bigger than 2 and smaller than 4"
|
28
|
-
# ...rather than:
|
29
|
-
# # => "be bigger than 2"
|
30
|
-
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
31
|
-
end
|
32
|
-
|
33
|
-
# rspec-mocks config goes here. You can use an alternate test double
|
34
|
-
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
35
|
-
config.mock_with :rspec do |mocks|
|
36
|
-
# Prevents you from mocking or stubbing a method that does not exist on
|
37
|
-
# a real object. This is generally recommended, and will default to
|
38
|
-
# `true` in RSpec 4.
|
39
|
-
mocks.verify_partial_doubles = true
|
40
|
-
end
|
41
|
-
|
42
|
-
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
43
|
-
# have no way to turn it off -- the option exists only for backwards
|
44
|
-
# compatibility in RSpec 3). It causes shared context metadata to be
|
45
|
-
# inherited by the metadata hash of host groups and examples, rather than
|
46
|
-
# triggering implicit auto-inclusion in groups with matching metadata.
|
47
|
-
config.shared_context_metadata_behavior = :apply_to_host_groups
|
48
|
-
|
49
|
-
# The settings below are suggested to provide a good initial experience
|
50
|
-
# with RSpec, but feel free to customize to your heart's content.
|
51
|
-
# # This allows you to limit a spec run to individual examples or groups
|
52
|
-
# # you care about by tagging them with `:focus` metadata. When nothing
|
53
|
-
# # is tagged with `:focus`, all examples get run. RSpec also provides
|
54
|
-
# # aliases for `it`, `describe`, and `context` that include `:focus`
|
55
|
-
# # metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
56
|
-
# config.filter_run_when_matching :focus
|
57
|
-
#
|
58
|
-
# # Allows RSpec to persist some state between runs in order to support
|
59
|
-
# # the `--only-failures` and `--next-failure` CLI options. We recommend
|
60
|
-
# # you configure your source control system to ignore this file.
|
61
|
-
# config.example_status_persistence_file_path = "spec/examples.txt"
|
62
|
-
#
|
63
|
-
# # Limits the available syntax to the non-monkey patched syntax that is
|
64
|
-
# # recommended. For more details, see:
|
65
|
-
# # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
66
|
-
# # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
67
|
-
# # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
68
|
-
# config.disable_monkey_patching!
|
69
|
-
#
|
70
|
-
# # This setting enables warnings. It's recommended, but in some cases may
|
71
|
-
# # be too noisy due to issues in dependencies.
|
72
|
-
# config.warnings = true
|
73
|
-
#
|
74
|
-
# # Many RSpec users commonly either run the entire suite or an individual
|
75
|
-
# # file, and it's useful to allow more verbose output when running an
|
76
|
-
# # individual spec file.
|
77
|
-
# if config.files_to_run.one?
|
78
|
-
# # Use the documentation formatter for detailed output,
|
79
|
-
# # unless a formatter has already been configured
|
80
|
-
# # (e.g. via a command-line flag).
|
81
|
-
# config.default_formatter = "doc"
|
82
|
-
# end
|
83
|
-
#
|
84
|
-
# # Print the 10 slowest examples and example groups at the
|
85
|
-
# # end of the spec run, to help surface which specs are running
|
86
|
-
# # particularly slow.
|
87
|
-
# config.profile_examples = 10
|
88
|
-
#
|
89
|
-
# # Run specs in random order to surface order dependencies. If you find an
|
90
|
-
# # order dependency and want to debug it, you can fix the order by providing
|
91
|
-
# # the seed, which is printed after each run.
|
92
|
-
# # --seed 1234
|
93
|
-
# config.order = :random
|
94
|
-
#
|
95
|
-
# # Seed global randomization in this process using the `--seed` CLI option.
|
96
|
-
# # Setting this allows you to use `--seed` to deterministically reproduce
|
97
|
-
# # test failures related to randomization by passing the same `--seed` value
|
98
|
-
# # as the one that triggered the failure.
|
99
|
-
# Kernel.srand config.seed
|
100
|
-
end
|
3
|
+
require 'eac_ruby_utils/rspec/default_setup'
|
4
|
+
::EacRubyUtils::Rspec.default_setup_create(::File.expand_path('..', __dir__))
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: can_can_dry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Esquilo Azul Company
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-08-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cancancan
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '0.
|
33
|
+
version: '0.72'
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '0.
|
40
|
+
version: '0.72'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rails
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -58,20 +58,27 @@ dependencies:
|
|
58
58
|
requirements:
|
59
59
|
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '0.
|
61
|
+
version: '0.3'
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: 0.3.1
|
62
65
|
type: :development
|
63
66
|
prerelease: false
|
64
67
|
version_requirements: !ruby/object:Gem::Requirement
|
65
68
|
requirements:
|
66
69
|
- - "~>"
|
67
70
|
- !ruby/object:Gem::Version
|
68
|
-
version: '0.
|
71
|
+
version: '0.3'
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: 0.3.1
|
69
75
|
description:
|
70
76
|
email:
|
71
77
|
executables: []
|
72
78
|
extensions: []
|
73
79
|
extra_rdoc_files: []
|
74
80
|
files:
|
81
|
+
- app/helpers/can_can_dry_helper.rb
|
75
82
|
- lib/can_can_dry.rb
|
76
83
|
- lib/can_can_dry/ability_mapping.rb
|
77
84
|
- lib/can_can_dry/ability_mapping/action_mapping_not_found.rb
|
@@ -80,7 +87,7 @@ files:
|
|
80
87
|
- lib/can_can_dry/ability_mapping_sets/devise.rb
|
81
88
|
- lib/can_can_dry/ability_mapping_sets/devise_invitable.rb
|
82
89
|
- lib/can_can_dry/controller_authorization.rb
|
83
|
-
- lib/can_can_dry/
|
90
|
+
- lib/can_can_dry/engine.rb
|
84
91
|
- lib/can_can_dry/no_controller_can_can_additions.rb
|
85
92
|
- lib/can_can_dry/path_recognizer.rb
|
86
93
|
- lib/can_can_dry/railtie.rb
|
@@ -108,8 +115,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
108
115
|
- !ruby/object:Gem::Version
|
109
116
|
version: '0'
|
110
117
|
requirements: []
|
111
|
-
|
112
|
-
rubygems_version: 2.7.7
|
118
|
+
rubygems_version: 3.1.6
|
113
119
|
signing_key:
|
114
120
|
specification_version: 4
|
115
121
|
summary: DRY authorization with CanCanCan.
|
@@ -1,63 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module CanCanDry
|
4
|
-
module Helpers
|
5
|
-
module CanCanDryHelper
|
6
|
-
def ability_mapping
|
7
|
-
@ability_mapping ||= ::AbilityMapping.new
|
8
|
-
end
|
9
|
-
|
10
|
-
def can_by_path?(path, method = :get)
|
11
|
-
can_by_can_args(ability_mapping.can_args_by_path(main_app.root_path, path, method))
|
12
|
-
end
|
13
|
-
|
14
|
-
def can_by_path_hash?(path_hash)
|
15
|
-
can_by_can_args(ability_mapping.can_args_by_path_hash(path_hash))
|
16
|
-
end
|
17
|
-
|
18
|
-
def link_or_text(name = nil, options = nil, html_options = nil)
|
19
|
-
link_or_default(name, name, options, html_options)
|
20
|
-
end
|
21
|
-
|
22
|
-
def link_or_nil(name = nil, options = nil, html_options = nil)
|
23
|
-
link_or_default(name, nil, options, html_options)
|
24
|
-
end
|
25
|
-
|
26
|
-
private
|
27
|
-
|
28
|
-
def link_or_default(name, default, options, html_options)
|
29
|
-
if can_by_link_options?(options, html_options)
|
30
|
-
link_to(name, options, html_options)
|
31
|
-
else
|
32
|
-
default
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
def can_by_link_options?(options, html_options)
|
37
|
-
can_by_path?(url_for(options), link_method(options, html_options))
|
38
|
-
end
|
39
|
-
|
40
|
-
def link_method(*hashs)
|
41
|
-
hashs.each do |h|
|
42
|
-
return h[:method] if h.is_a?(Hash) && h[:method]
|
43
|
-
end
|
44
|
-
:get
|
45
|
-
end
|
46
|
-
|
47
|
-
def can_by_can_args(can_args_args)
|
48
|
-
assert_can_method
|
49
|
-
can_args_args.each do |c|
|
50
|
-
next if c.empty?
|
51
|
-
return true if can?(*c)
|
52
|
-
end
|
53
|
-
false
|
54
|
-
end
|
55
|
-
|
56
|
-
def assert_can_method
|
57
|
-
return if respond_to?('can?')
|
58
|
-
|
59
|
-
singleton_class.include(::CanCanDry::NoControllerCanCanAdditions)
|
60
|
-
end
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|