active_policy 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c04bf53faa989871a87e71b0bb039b4d658cfd1814e2e8e998c5912a9219f126
4
- data.tar.gz: a7af843ee06a3183fbdac52d15bf156366f7bec0a926394603409adb0039f5e0
3
+ metadata.gz: aba281fd2452e0f5a1a9a1229741be4d36f7b4b7d7dd10df5b92d01a47793604
4
+ data.tar.gz: 494536e3f2a094f8be21d134308c32434b0d1df040cc3d9fd40da275cc4f4a82
5
5
  SHA512:
6
- metadata.gz: 69aca67ba2de7e1a2acbb3be8c05f6102f2888bc1b4cf34d943f5d644c518848424ae9a1010222af646ddb8a7695d285ef14c4816182011c3e25f8e05e347026
7
- data.tar.gz: ef32c5d997e12714bf9c8e24ad33d6df58236491200903a2470223c66d5f20e25a74823faacb7ac7d3a0130eeeb9dc309c7049c103ea0c419170a7f5699031c8
6
+ metadata.gz: 00ec0450b766d36b07daec701d38ba793cab8379ec83113cb33dd1277ee06f58aa65d1eae9bdba0bab1cb2601a3eba81c6a3c32564414d228b8331339302c0e4
7
+ data.tar.gz: ab9651ddc4c0d27d1a1606ca7d2f41f0574f5f57e95f745a8460e2dfc3e591d74e7547b0286283593aea30111f91498b9d12e254ee67cf0716baa9dafa0c9851
@@ -3,7 +3,6 @@ class ActivePolicyMiddleware
3
3
  ENV_KEY_REQUEST_METHOD = 'REQUEST_METHOD'
4
4
  ENV_KEY_WARDEN = 'warden'
5
5
 
6
- # @param [Class] app
7
6
  def initialize(app)
8
7
  @app = app
9
8
  end
@@ -17,28 +16,46 @@ class ActivePolicyMiddleware
17
16
  Rails.application.routes
18
17
  )
19
18
 
20
- if params.key?(:policy)
21
- # @type [ActivePolicy::Base] policy
22
- policy = params[:policy].new(current_user(env), request, params)
23
- method_name = params[:action] + '?'
24
- models = ActivePolicy::Utilities.models_from_route_params(params)
25
- result = policy.send(method_name, *models)
26
-
27
- if result
28
- @app.call(env)
29
- else
30
- response = response()
31
- response.finish
32
- end
33
- else
34
- @app.call(env)
35
- end
19
+ return next_middleware(env) unless run_middleware?(params)
20
+
21
+ method_name = method_name_from_params(params)
22
+ models = ActivePolicy::Utilities.models_from_route_params(params)
23
+ policy = policy_name(params).new(current_user(env), request, params)
24
+
25
+ return next_middleware(env) unless policy.respond_to?(method_name)
26
+
27
+ result = policy.send(method_name, *models)
28
+
29
+ return next_middleware(env) if result
30
+
31
+ response
36
32
  end
37
33
 
38
34
  private
39
35
 
36
+ # @param [Hash] params
37
+ # @return [String]
38
+ def method_name_from_params(params)
39
+ return params[:method] if params.key(:method)
40
+
41
+ params[:action] + '?'
42
+ end
43
+
44
+ # @param [Hash] params
45
+ # @return [String]
46
+ def policy_name(params)
47
+ if params[:policy].is_a?(Class)
48
+ return params[:policy]
49
+ end
50
+
51
+ if params[:policy].key?(:class)
52
+ return params[:policy][:class]
53
+ end
54
+
55
+ params[:policy]
56
+ end
57
+
40
58
  # @param [Hash] env
41
- #
42
59
  # @return [User, nil]
43
60
  def current_user(env)
44
61
  if env.key?(ENV_KEY_WARDEN)
@@ -50,6 +67,17 @@ class ActivePolicyMiddleware
50
67
 
51
68
  # @param [Rack::Response]
52
69
  def response(status_code = 401, headers = {'content_type': 'application/json'}, body = [])
53
- Rack::Response.new(body, status_code, headers)
70
+ Rack::Response.new(body, status_code, headers).finish
71
+ end
72
+
73
+ # @param [Hash] env
74
+ def next_middleware(env)
75
+ @app.call(env)
76
+ end
77
+
78
+ # @param [Hash] params
79
+ # @return [TrueClass, FalseClass]
80
+ def run_middleware?(params)
81
+ params.key?(:policy)
54
82
  end
55
83
  end
@@ -1,8 +1,8 @@
1
1
  module ActivePolicy
2
2
  class Utilities
3
- # @param [ActionDispatch::Routing::RouteSet] route_set
4
3
  # @param [String] path
5
4
  # @param [String] method
5
+ # @param [ActionDispatch::Routing::RouteSet] route_set
6
6
  #
7
7
  # @return [Hash]
8
8
  def self.route_params(path, method, route_set)
@@ -14,10 +14,10 @@ module ActivePolicy
14
14
  # @return [Array<ActiveRecord>]
15
15
  def self.models_from_route_params(params)
16
16
  models = []
17
- raise 'policy_models is missing from route' if params[:policy_models].nil?
18
- return [] if params[:policy_models].empty?
19
- params[:policy_models].each do |key, value|
20
- models << value.find(params[key])
17
+ if params.key?(:policy_models)
18
+ params[:policy_models].each do |key, value|
19
+ models << value.find(params[key])
20
+ end
21
21
  end
22
22
  models
23
23
  end
@@ -1,3 +1,3 @@
1
1
  module ActivePolicy
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_policy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Panos Dalitsouris
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-02-17 00:00:00.000000000 Z
11
+ date: 2020-02-26 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Active policy is meant to be a way to authorize a request before hitting
14
14
  the controller.