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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aba281fd2452e0f5a1a9a1229741be4d36f7b4b7d7dd10df5b92d01a47793604
|
4
|
+
data.tar.gz: 494536e3f2a094f8be21d134308c32434b0d1df040cc3d9fd40da275cc4f4a82
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
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.
|
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-
|
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.
|