verbose_cancancan 0.0.1 → 0.0.2
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 +4 -4
- data/lib/verbose_cancancan/ability.rb +64 -0
- data/lib/verbose_cancancan/controller_resource.rb +31 -0
- data/lib/verbose_cancancan/railtie.rb +15 -0
- data/lib/verbose_cancancan/rule.rb +10 -0
- data/lib/verbose_cancancan.rb +39 -3
- metadata +7 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 894cfad8bd3a07f8455bd7d37720b3289123db0afb9c6e66dda92dbdc775bad5
|
4
|
+
data.tar.gz: eab446beeb6ba948ab8959127bb20ba2abd2c9091593c583f422678334d1d6cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d12fd5faddc6ef40bcadee9fe600dd548ea12224b8220160d7ee6f756ad64c84e54787b848396620a841a4b06ad7a845ac5d58875e71a823bda8258efc30e41c
|
7
|
+
data.tar.gz: d1c5b4cdaeaa2a178c033a1f6268b703ef9f05f84a0d2f3625adfddb8ffc0b78a88908da9415841486a2c11497cd311620b3e383e716d1ba811287ff57581921
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module VerboseCancancan
|
2
|
+
|
3
|
+
|
4
|
+
module Ability
|
5
|
+
|
6
|
+
def can?(action, subject, attribute = nil, *extra_args)
|
7
|
+
match = extract_subjects(subject).lazy.map do |a_subject|
|
8
|
+
relevant_rules_for_match(action, a_subject).detect do |rule|
|
9
|
+
rule.matches_conditions?(action, a_subject, attribute, *extra_args) && rule.matches_attributes?(attribute)
|
10
|
+
end
|
11
|
+
end.reject(&:nil?).first
|
12
|
+
|
13
|
+
log_rule(match, action, subject) if log_rule?
|
14
|
+
|
15
|
+
match ? match.base_behavior : false
|
16
|
+
end
|
17
|
+
|
18
|
+
# Only logged the rule if authorize! or load_and_authorize! is called, to avoid cluttering the terminal with can? checks.
|
19
|
+
def log_rule? = caller.find { |path| path.include?('authorize!') }
|
20
|
+
|
21
|
+
|
22
|
+
def log_rule(matched_rule, action, subject)
|
23
|
+
subject = pretty_subject(subject)
|
24
|
+
|
25
|
+
if matched_rule
|
26
|
+
VerboseCancancan.logger.info matched_rule_info(matched_rule, subject, action)
|
27
|
+
else
|
28
|
+
VerboseCancancan.logger.info no_rule_matched(subject, action)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def matched_rule_info(rule, subject, action)
|
33
|
+
auth_status = rule.base_behavior ? auth_success : auth_failed
|
34
|
+
rule_type = rule.base_behavior ? 'Can' : 'Cannot'
|
35
|
+
|
36
|
+
"#{auth_status} #{rule_type} Rule applied for #{colourize(subject, :cyan)} (Action: #{colourize(action, :yellow)})\n ↳ #{rule.path}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def no_rule_matched(subject, action)
|
40
|
+
"#{auth_failed} No matching rule for #{colourize(subject, :cyan)} (Action: #{colourize(action, :yellow)})"
|
41
|
+
end
|
42
|
+
|
43
|
+
def pretty_subject(subject)
|
44
|
+
if subject.is_a?(ActiveRecord::Base)
|
45
|
+
"#{subject.class.name} (ID: #{subject.id})"
|
46
|
+
else
|
47
|
+
orignal_subject = subject.is_a?(Hash) ? subject.values.first : subject
|
48
|
+
|
49
|
+
orignal_subject.respond_to?(:name) ? orignal_subject.name : orignal_subject
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def auth_failed
|
54
|
+
colourize('[❌ AUTHORIZATION FAILED]', :red)
|
55
|
+
end
|
56
|
+
|
57
|
+
def auth_success
|
58
|
+
colourize('[✅ AUTHORIZATION SUCCESS]', :green)
|
59
|
+
end
|
60
|
+
|
61
|
+
def colourize(...) = VerboseCancancan.colourize(...)
|
62
|
+
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module VerboseCancancan
|
2
|
+
module ControllerResource
|
3
|
+
|
4
|
+
def add_before_action(controller_class, method, *args)
|
5
|
+
options = args.extract_options!
|
6
|
+
resource_name = args.first
|
7
|
+
before_action_method = before_callback_name(options)
|
8
|
+
bk_trace = VerboseCancancan.cleaner_backtrace(caller).first
|
9
|
+
|
10
|
+
controller_class.send(before_action_method, options.slice(:only, :except, :if, :unless)) do |controller|
|
11
|
+
|
12
|
+
VerboseCancancan::ControllerResource.log_hook_added(method, controller_class, bk_trace) if VerboseCancancan::ControllerResource.log_hook_added?(method)
|
13
|
+
|
14
|
+
|
15
|
+
controller.class.cancan_resource_class
|
16
|
+
.new(controller, resource_name, options.except(:only, :except, :if, :unless)).send(method)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.log_hook_added?(hook)
|
21
|
+
hook.to_s.include? ('authorize_resource')
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.log_hook_added(method, controller_class, bk_trace)
|
25
|
+
VerboseCancancan.logger.info "#{VerboseCancancan::ControllerResource.colourize('[⏳ CanCan Hook Added]', :green)}(#{method}) on #{controller_class} \n ↳ #{bk_trace}"
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.colourize(...) = VerboseCancancan.colourize(...)
|
29
|
+
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module VerboseCancancan
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
initializer "my_gem.configure_rails_initialization" do
|
4
|
+
# some initialization behavior
|
5
|
+
puts "\n\n\nhola\n\n\n"
|
6
|
+
|
7
|
+
CanCan::Rule.prepend(Rule)
|
8
|
+
CanCan::Ability.prepend(Ability)
|
9
|
+
|
10
|
+
CanCan::ControllerResource.singleton_class.prepend(VerboseCancancan::ControllerResource)
|
11
|
+
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/lib/verbose_cancancan.rb
CHANGED
@@ -1,3 +1,39 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
require 'verbose_cancancan/ability'
|
2
|
+
require 'verbose_cancancan/rule'
|
3
|
+
require 'verbose_cancancan/controller_resource'
|
4
|
+
require 'verbose_cancancan/railtie'
|
5
|
+
|
6
|
+
module VerboseCancancan
|
7
|
+
|
8
|
+
class << self
|
9
|
+
|
10
|
+
def logger
|
11
|
+
logger = Logger.new(STDOUT)
|
12
|
+
|
13
|
+
logger.formatter = proc { |_, _, _, msg| "#{msg}\n\n" }
|
14
|
+
|
15
|
+
logger
|
16
|
+
end
|
17
|
+
|
18
|
+
def cleaner_backtrace(locations)
|
19
|
+
bk_trace = ActiveSupport::BacktraceCleaner.new
|
20
|
+
root = "#{Rails.root}/"
|
21
|
+
bk_trace.add_filter { |line| line.delete_prefix(root) }
|
22
|
+
|
23
|
+
bk_trace.clean(locations)
|
24
|
+
end
|
25
|
+
|
26
|
+
def colourize(msg, color)
|
27
|
+
colors = {
|
28
|
+
clear: "\e[0m",
|
29
|
+
red: "\e[1;31m",
|
30
|
+
green: "\e[1;92m",
|
31
|
+
yellow: "\e[33m",
|
32
|
+
cyan: "\e[36m"
|
33
|
+
}
|
34
|
+
|
35
|
+
"#{colors[color]}#{msg}#{colors[:clear]}"
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
39
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: verbose_cancancan
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Zain Iftikhar
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2025-03-
|
11
|
+
date: 2025-03-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: cancancan
|
@@ -35,7 +35,11 @@ extensions: []
|
|
35
35
|
extra_rdoc_files: []
|
36
36
|
files:
|
37
37
|
- lib/verbose_cancancan.rb
|
38
|
-
|
38
|
+
- lib/verbose_cancancan/ability.rb
|
39
|
+
- lib/verbose_cancancan/controller_resource.rb
|
40
|
+
- lib/verbose_cancancan/railtie.rb
|
41
|
+
- lib/verbose_cancancan/rule.rb
|
42
|
+
homepage: https://github.com/ZainIftikhar7vals/verbose_cancancan
|
39
43
|
licenses:
|
40
44
|
- MIT
|
41
45
|
metadata: {}
|