picky_guard 0.1.3 → 0.1.5

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
  SHA1:
3
- metadata.gz: 85e21fc9a0ae52614b7d70dbebb39ab9019f3f11
4
- data.tar.gz: 3e38e07bda2744f1e375211f58f116e1aa90260f
3
+ metadata.gz: 05770d49279b304879e3c1b03fa2fee62dd6dcb4
4
+ data.tar.gz: c42d2fa5d6c4f032107b78bdfa606373daf46881
5
5
  SHA512:
6
- metadata.gz: 6aae898c5bc7ad35b4df8f927015c0501ca7ad7944d137564f41dc357473fe555496f14b604fc8316522ff271bb65eb51773306e58be62997052b72bb87eb79c
7
- data.tar.gz: 99a83a8bab4bb4f568011a70a406564441c3b3338b74640e4142826f51a0de08b619da4b46da7e5ee47c5a3de3845234db9242fdb42ff40b6507428de781ec4d
6
+ metadata.gz: 445dde3b61dfa42968a1a0a1b18a9e67acbe0bb306dd2e7c36b19a75f1ac6190ed175e9d08c9e535a24487a35b2fb666fd438bb4d6596b0e5775cde2ad0966d4
7
+ data.tar.gz: 50b0aac7c2b3e5a3d4a159dd220ced3742175c49fb1486125af83c4b68956a938bb48cfe33f50b2c90e8e75646abec2ed50d3e4b1d7f9a062dc9ce6bafd862cc
data/CHANGELOG.md CHANGED
@@ -1,4 +1,7 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.1.5
4
+ Evaluate conditions proc in context of policy instance
5
+
3
6
  ## 0.1.0
4
7
  Initial deployment.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- picky_guard (0.1.3)
4
+ picky_guard (0.1.5)
5
5
  activerecord (>= 4.2)
6
6
  cancancan (>= 2.0)
7
7
 
data/README.md CHANGED
@@ -282,6 +282,16 @@ Ability.new(user, Campaign).can? :read, Campaign.first
282
282
 
283
283
  This will load only relevant policies.
284
284
 
285
+ ## Troubleshooting
286
+
287
+ ### If your application has problems with loading classes,
288
+
289
+ put the following code into your `application.rb`:
290
+ ```
291
+ config.autoload_paths += %W[#{config.root}/app/picky_guard]
292
+ config.autoload_paths += %W[#{config.root}/app/picky_guard/policies]
293
+ ```
294
+
285
295
  ## Development
286
296
 
287
297
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -4,6 +4,7 @@ require 'picky_guard/loader'
4
4
 
5
5
  class Ability < PickyGuard::Loader
6
6
  def initialize(user)
7
+ super(user)
7
8
  adjust(user, UserRoleChecker, ResourceActions, RolePolicies)
8
9
  end
9
10
  end
@@ -8,6 +8,7 @@ module PickyGuard
8
8
 
9
9
  def initialize(_user, *resources_whitelist)
10
10
  @resources_whitelist = resources_whitelist
11
+ @statement_policy_map = {}
11
12
  end
12
13
 
13
14
  def adjust(user, user_role_checker_class, resource_actions_class, role_policies_class)
@@ -53,12 +54,18 @@ module PickyGuard
53
54
 
54
55
  def eval_conditions_if_needed(statement)
55
56
  if statement.conditions.is_a? Proc
56
- statement.conditions.call
57
+ eval_statement_conditions(statement)
57
58
  else
58
59
  statement.conditions
59
60
  end
60
61
  end
61
62
 
63
+ def eval_statement_conditions(statement)
64
+ policy = @statement_policy_map[statement]
65
+ proc = statement.conditions
66
+ policy.instance_eval(&proc)
67
+ end
68
+
62
69
  def positive?(effect)
63
70
  effect == Statement::EFFECT_ALLOW
64
71
  end
@@ -71,18 +78,26 @@ module PickyGuard
71
78
  end
72
79
 
73
80
  def gather_statements(user, policies, resource_actions)
74
- policies.map { |policy_class| policy_class.new(user) }
75
- .map do |policy|
76
- statements = policy.statements(@resources_whitelist)
77
- validate_statements!(resource_actions, statements)
78
- end.flatten
81
+ actual_policy_instances(policies, user).map do |policy|
82
+ statements = policy.statements(@resources_whitelist)
83
+ validate_statements!(resource_actions, statements)
84
+ map_policy_statements(policy, statements)
85
+ end.flatten
86
+ end
87
+
88
+ def actual_policy_instances(policy_classes, user)
89
+ policy_classes.map { |policy_class| policy_class.new(user) }
90
+ end
91
+
92
+ def map_policy_statements(policy, statements)
93
+ statements.each { |statement| @statement_policy_map[statement] = policy }
94
+ statements
79
95
  end
80
96
 
81
97
  def validate_statements!(resource_actions, statements)
82
98
  statements.each do |statement|
83
99
  validate_statement!(resource_actions, statement)
84
100
  end
85
- statements
86
101
  end
87
102
 
88
103
  def validate_statement!(resource_actions, statement)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module PickyGuard
4
- VERSION = '0.1.3'
4
+ VERSION = '0.1.5'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: picky_guard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eunjae Lee