can_can_dry 0.0.2 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Rakefile +17 -0
- data/lib/can_can_dry/ability_mapping/action_mapping_not_found.rb +10 -0
- data/lib/can_can_dry/ability_mapping/path_mapping_not_found.rb +9 -0
- data/lib/can_can_dry/ability_mapping.rb +84 -0
- data/lib/can_can_dry/ability_mapping_sets/devise.rb +10 -0
- data/lib/can_can_dry/ability_mapping_sets/devise_invitable.rb +9 -0
- data/lib/can_can_dry/controller_authorization.rb +22 -0
- data/lib/can_can_dry/helpers/can_can_dry_helper.rb +54 -0
- data/lib/can_can_dry/path_recognizer.rb +52 -0
- data/lib/can_can_dry/railtie.rb +7 -0
- data/lib/can_can_dry/version.rb +3 -0
- data/lib/can_can_dry.rb +17 -0
- data/lib/tasks/can_can_dry.rake +11 -0
- data/test/can_can_dry_test.rb +8 -0
- data/test/lib/can_can_dry/path_recognizer_test.rb +16 -0
- data/test/test_helper.rb +4 -0
- metadata +69 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f4fdfc92a4f98f5752d1f712e3bb081fa438d36
|
4
|
+
data.tar.gz: a3b0b53fd3d4d914f6861259a72ad177212c5955
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2adfde2986dc760cc6599164bfa022319c66c600b56b4f10d44f8137a49bdffd8279dbaad40c4427edab9af0bcb4843925aba9b787831c2cf3bfdf0ea89323d0
|
7
|
+
data.tar.gz: 41f3f66f35be65d19be64fb606bb52d6a41dac09b2f5057dba1e2e9e4a8d55a9519c16e58f7a035b5c06bcde1b764ae63fc830d696d3fbe3c4b313f8ca3287a3
|
data/Rakefile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
begin
|
2
|
+
require 'bundler/setup'
|
3
|
+
rescue LoadError
|
4
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
5
|
+
end
|
6
|
+
|
7
|
+
Bundler::GemHelper.install_tasks
|
8
|
+
require 'rake/testtask'
|
9
|
+
|
10
|
+
Rake::TestTask.new(:test) do |t|
|
11
|
+
t.libs << 'lib'
|
12
|
+
t.libs << 'test'
|
13
|
+
t.pattern = 'test/**/*_test.rb'
|
14
|
+
t.verbose = false
|
15
|
+
end
|
16
|
+
|
17
|
+
task default: :test
|
@@ -0,0 +1,84 @@
|
|
1
|
+
module CanCanDry
|
2
|
+
module AbilityMapping
|
3
|
+
RESOURCES_ACTION_MAPPING = {
|
4
|
+
read: %w(index show),
|
5
|
+
create: %w(new create),
|
6
|
+
update: %w(update edit),
|
7
|
+
destroy: %w(destroy)
|
8
|
+
}
|
9
|
+
ALL_ACTION = 'ALL'
|
10
|
+
|
11
|
+
def mapping
|
12
|
+
@mapping ||= {}
|
13
|
+
end
|
14
|
+
|
15
|
+
def map_controller(controller, *can_actions)
|
16
|
+
map_action(controller, ALL_ACTION, *can_actions)
|
17
|
+
end
|
18
|
+
|
19
|
+
# 1. Encontra o controller associado a resource.
|
20
|
+
# 2. Mapeia:
|
21
|
+
# * map_action(controller, 'index', :read, resource)
|
22
|
+
# * map_action(controller, 'show', :read, resource)
|
23
|
+
# * map_action(controller, 'new', :create, resource)
|
24
|
+
# * map_action(controller, 'create', :create, resource)
|
25
|
+
# * map_action(controller, 'update', :update, resource)
|
26
|
+
# * map_action(controller, 'edit', :update, resource)
|
27
|
+
# * map_action(controller, 'destroy', :destroy, resource)
|
28
|
+
def map_resources(resource, controller = nil)
|
29
|
+
controller ||= controller_by_resource(resource)
|
30
|
+
RESOURCES_ACTION_MAPPING.each do |can_action, actions|
|
31
|
+
actions.each do |action|
|
32
|
+
map_action(controller, action, can_action, resource)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def map_action(controller, action, *can_args)
|
38
|
+
if can_args.count == 1
|
39
|
+
raise "\"can_args\" deve ter 0 ou 2 ou mais elementos (can_args.count=#{can_args.count})"
|
40
|
+
end
|
41
|
+
mapping[controller] ||= {}
|
42
|
+
mapping[controller][action] ||= []
|
43
|
+
mapping[controller][action] << can_args
|
44
|
+
end
|
45
|
+
|
46
|
+
def can_args_by_path(root_path, path, method)
|
47
|
+
can_args_by_path_hash(recognize_path(root_path, path, method))
|
48
|
+
rescue ActionMappingNotFound => ex
|
49
|
+
raise PathMappingNotFound.new(path, method, ex)
|
50
|
+
end
|
51
|
+
|
52
|
+
def can_args_by_path_hash(path_hash)
|
53
|
+
can_args_args = find_can_args_list(path_hash[:controller], path_hash[:action])
|
54
|
+
replace_model_by_record(can_args_args, path_hash[:id])
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def replace_model_by_record(can_args_args, id)
|
60
|
+
can_args_args.map do |can_args|
|
61
|
+
ca = can_args.dup
|
62
|
+
ca[1] = ca[1].find_by_id(id) if id && ca[1].respond_to?(:find_by_id)
|
63
|
+
ca
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def controller_by_resource(resource)
|
68
|
+
resource = resource.name if resource.is_a?(Class)
|
69
|
+
ActiveSupport::Inflector.pluralize(resource)
|
70
|
+
end
|
71
|
+
|
72
|
+
def find_can_args_list(controller, action)
|
73
|
+
controller = ActiveSupport::Inflector.camelize(controller)
|
74
|
+
raise ActionMappingNotFound.new(controller, action) unless mapping[controller]
|
75
|
+
return mapping[controller][action] if mapping[controller][action]
|
76
|
+
return mapping[controller][ALL_ACTION] if mapping[controller][ALL_ACTION]
|
77
|
+
raise ActionMappingNotFound.new(controller, action)
|
78
|
+
end
|
79
|
+
|
80
|
+
def recognize_path(root_path, path, method)
|
81
|
+
::CanCanDry::PathRecognizer.recognize(root_path, path, method: method)
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
require_dependency 'can_can_dry/helpers/can_can_dry_helper'
|
2
|
+
|
3
|
+
module CanCanDry
|
4
|
+
module ControllerAuthorization
|
5
|
+
include CanCanDry::Helpers::CanCanDryHelper
|
6
|
+
|
7
|
+
def authorize_by_ability_mapping
|
8
|
+
fail CanCan::AccessDenied, "Falhou ao tentar acessar #{path_hash}" unless
|
9
|
+
can_by_path_hash?(path_hash)
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def path_hash
|
15
|
+
params.select { |k, _v| %w(controller action id).include?(k) }
|
16
|
+
end
|
17
|
+
|
18
|
+
def ability_mapping
|
19
|
+
@ability_mapping ||= ::AbilityMapping.new
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module CanCanDry
|
2
|
+
module Helpers
|
3
|
+
module CanCanDryHelper
|
4
|
+
def ability_mapping
|
5
|
+
@ability_mapping ||= ::AbilityMapping.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def can_by_path?(path, method = :get)
|
9
|
+
can_by_can_args(ability_mapping.can_args_by_path(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
|
+
can_args_args.each do |c|
|
47
|
+
next if c.empty?
|
48
|
+
return false unless can?(*c)
|
49
|
+
end
|
50
|
+
true
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module CanCanDry
|
2
|
+
# Copiado de https://github.com/appirits/awesome_admin_layout
|
3
|
+
# /lib/awesome_admin_layout/recognize_path.rb
|
4
|
+
module PathRecognizer
|
5
|
+
class << self
|
6
|
+
def recognize(root_path, path, options = {})
|
7
|
+
path = remove_root_path(root_path, path)
|
8
|
+
return Rails.application.routes.recognize_path(path, options)
|
9
|
+
rescue ActionController::RoutingError
|
10
|
+
Rails::Engine.subclasses.each do |engine|
|
11
|
+
recognized_path = engine_recognize(engine, path, options)
|
12
|
+
return recognized_path if recognized_path
|
13
|
+
end
|
14
|
+
raise "Path not recognized: \"#{path}\" (Options: #{options})"
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def remove_root_path(root_path, path)
|
20
|
+
path = path.gsub(/\A#{Regexp.quote(root_path)}/, '')
|
21
|
+
path.gsub(%r{\A/*}, '/')
|
22
|
+
end
|
23
|
+
|
24
|
+
def engine_recognize(engine, path, options)
|
25
|
+
engine_path = path_for_engine(engine.instance.class, path)
|
26
|
+
return unless engine_path
|
27
|
+
begin
|
28
|
+
return engine.instance.routes.recognize_path(engine_path, options)
|
29
|
+
rescue ActionController::RoutingError => e
|
30
|
+
Rails.logger.debug "[#{engine}] ActionController::RoutingError: #{e.message}"
|
31
|
+
end
|
32
|
+
nil
|
33
|
+
end
|
34
|
+
|
35
|
+
def path_for_engine(engine_class, path)
|
36
|
+
engine_route = Rails.application.routes.routes.find { |r| app_class_for(r) == engine_class }
|
37
|
+
return unless engine_route
|
38
|
+
path.gsub(/^#{engine_route.path.spec}/, '')
|
39
|
+
end
|
40
|
+
|
41
|
+
def app_class_for(route)
|
42
|
+
if Rails.version =~ /\A4\.2\./
|
43
|
+
# for Rails 4.2
|
44
|
+
route.app.app
|
45
|
+
else
|
46
|
+
# for Rails 4.1, 4.0, 3.2
|
47
|
+
route.app
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/can_can_dry.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'active_support/dependencies'
|
2
|
+
require 'cancancan'
|
3
|
+
|
4
|
+
module CanCanDry
|
5
|
+
require 'can_can_dry/railtie'
|
6
|
+
require_dependency 'can_can_dry/controller_authorization'
|
7
|
+
require_dependency 'can_can_dry/ability_mapping_sets/devise'
|
8
|
+
require_dependency 'can_can_dry/ability_mapping_sets/devise_invitable'
|
9
|
+
require_dependency 'can_can_dry/ability_mapping'
|
10
|
+
require_dependency 'can_can_dry/ability_mapping/path_mapping_not_found'
|
11
|
+
require_dependency 'can_can_dry/ability_mapping/action_mapping_not_found'
|
12
|
+
require_dependency 'can_can_dry/helpers/can_can_dry_helper'
|
13
|
+
require_dependency 'can_can_dry/path_recognizer'
|
14
|
+
|
15
|
+
require_dependency 'action_view'
|
16
|
+
ActionView::Base.send :include, CanCanDry::Helpers::CanCanDryHelper
|
17
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
namespace :can_can_dry do
|
2
|
+
desc 'Mostra o mapeamento de CanCanDry'
|
3
|
+
task ability_mapping: :environment do
|
4
|
+
AbilityMapping.new.mapping.each do |controller, actions|
|
5
|
+
puts "#{controller} (#{actions.count})"
|
6
|
+
actions.each do |action|
|
7
|
+
puts "\t#{action}"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
module CanCanDry
|
5
|
+
class PathRecognizerTest < ActiveSupport::TestCase
|
6
|
+
test 'remove_root_path' do
|
7
|
+
[%w(/path/to/action /prefix/ /prefix/path/to/action),
|
8
|
+
%w(/path/to/action /prefix /prefix/path/to/action),
|
9
|
+
%w(/path / /path),
|
10
|
+
['/path', '', '/path'],
|
11
|
+
%w(/unknown/path /abc /unknown/path)].each do |s|
|
12
|
+
assert_equal s[0], ::CanCanDry::PathRecognizer.send(:remove_root_path, s[1], s[2]), s
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
CHANGED
@@ -1,21 +1,79 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: can_can_dry
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
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: 2017-
|
12
|
-
dependencies:
|
11
|
+
date: 2017-10-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.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: 4.2.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: cancancan
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.13.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 1.13.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 12.1.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 12.1.0
|
13
55
|
description:
|
14
56
|
email:
|
15
57
|
executables: []
|
16
58
|
extensions: []
|
17
59
|
extra_rdoc_files: []
|
18
|
-
files:
|
60
|
+
files:
|
61
|
+
- Rakefile
|
62
|
+
- lib/can_can_dry.rb
|
63
|
+
- lib/can_can_dry/ability_mapping.rb
|
64
|
+
- lib/can_can_dry/ability_mapping/action_mapping_not_found.rb
|
65
|
+
- lib/can_can_dry/ability_mapping/path_mapping_not_found.rb
|
66
|
+
- lib/can_can_dry/ability_mapping_sets/devise.rb
|
67
|
+
- lib/can_can_dry/ability_mapping_sets/devise_invitable.rb
|
68
|
+
- lib/can_can_dry/controller_authorization.rb
|
69
|
+
- lib/can_can_dry/helpers/can_can_dry_helper.rb
|
70
|
+
- lib/can_can_dry/path_recognizer.rb
|
71
|
+
- lib/can_can_dry/railtie.rb
|
72
|
+
- lib/can_can_dry/version.rb
|
73
|
+
- lib/tasks/can_can_dry.rake
|
74
|
+
- test/can_can_dry_test.rb
|
75
|
+
- test/lib/can_can_dry/path_recognizer_test.rb
|
76
|
+
- test/test_helper.rb
|
19
77
|
homepage:
|
20
78
|
licenses: []
|
21
79
|
metadata: {}
|
@@ -35,8 +93,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
35
93
|
version: '0'
|
36
94
|
requirements: []
|
37
95
|
rubyforge_project:
|
38
|
-
rubygems_version: 2.
|
96
|
+
rubygems_version: 2.6.12
|
39
97
|
signing_key:
|
40
98
|
specification_version: 4
|
41
|
-
summary:
|
42
|
-
test_files:
|
99
|
+
summary: DRY authorization with CanCanCan.
|
100
|
+
test_files:
|
101
|
+
- test/test_helper.rb
|
102
|
+
- test/can_can_dry_test.rb
|
103
|
+
- test/lib/can_can_dry/path_recognizer_test.rb
|