verbose_cancancan 0.0.1 → 0.0.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '010389ab552bdd7cee222d38b511321928ac226eff7901b65883ce2c5985eb14'
4
- data.tar.gz: dc19423459a9357c33e0427ffaabd97449da5c05e4f61fed3f6958d4d3ef4996
3
+ metadata.gz: f06f9036e8c7dc7b482a80b5639387189062adc274da49e9771d56fd2e59ef01
4
+ data.tar.gz: 3a324d862ca09b0b9919657a5404d86c25e25aeb3c12bdb6e7f81649bef4e94d
5
5
  SHA512:
6
- metadata.gz: c6fb9997722011082ed9e9e59b376d398f6b90a8dbe0f6ea153739b92651d271e4c7fab6b59b550dcee9b050bc311cfb85a7b1dc1290da5b5e3b317714e332db
7
- data.tar.gz: 4053410a30b3a664a4dd1aaf317dcd935a82e426c01f622258dd4fedaa6b715ad06b7b0010283c16e6bcad0efddc64e028274b039f3eb1f1632adee5247c5e92
6
+ metadata.gz: e7bf88f2885bc343284974cc38c26b583757182e6eca8816acef6decd1881e6d91e75fa9a0ac87f14521f8ab5a9d967ecfed23353232142bb972887b66bfee57
7
+ data.tar.gz: 3c56243f19ab40fb42a3497ce656884d581058f00fc72403092669a09034810c9267276baf0920877d2019fdec0dbda70821f1fa980eb7fc73132d5d627812c3
@@ -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
@@ -0,0 +1,12 @@
1
+ module VerboseCancancan
2
+
3
+ module Rule
4
+ def initialize(...)
5
+ @path = VerboseCancancan.cleaner_backtrace(caller).first
6
+ super
7
+ end
8
+
9
+ def path = @path
10
+ end
11
+
12
+ end
@@ -1,3 +1,39 @@
1
- class Array
2
- def zain = puts "hello world"
3
- end
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.1
4
+ version: 0.0.3
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-10 00:00:00.000000000 Z
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
- homepage:
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: {}