active_policy 0.1.1 → 0.1.2

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: bca16b9f7436c2181c530782b2dfaf7ca04530d1ae70868ee0a6b7a8d1f5edcb
4
- data.tar.gz: 86fedef31da29903c29a7c1f342958b86fd93e82f367a9afc643523d2de72333
3
+ metadata.gz: c04bf53faa989871a87e71b0bb039b4d658cfd1814e2e8e998c5912a9219f126
4
+ data.tar.gz: a7af843ee06a3183fbdac52d15bf156366f7bec0a926394603409adb0039f5e0
5
5
  SHA512:
6
- metadata.gz: 2f19458fb96f777be9324e97e2ffd4576c372c888a52032c548394b06a127c85755ef79588d5d2b4abffcd8439ab31fa74eafa712b04d7144da64cc6cd5c9cfe
7
- data.tar.gz: c061c360758f7d3748057fa3dd42aa2c6091121e68614a116a73a2b1e7f4f17a2746c5a2c7bbd5716efb80930d3919ccc5f4df90aa8a6bafc2ee8d44e715ad57
6
+ metadata.gz: 69aca67ba2de7e1a2acbb3be8c05f6102f2888bc1b4cf34d943f5d644c518848424ae9a1010222af646ddb8a7695d285ef14c4816182011c3e25f8e05e347026
7
+ data.tar.gz: ef32c5d997e12714bf9c8e24ad33d6df58236491200903a2470223c66d5f20e25a74823faacb7ac7d3a0130eeeb9dc309c7049c103ea0c419170a7f5699031c8
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  [![Build Status](https://travis-ci.org/PanosCodes/active_policy.svg?branch=master)](https://travis-ci.org/PanosCodes/active_policy)
2
2
  [![codecov](https://codecov.io/gh/PanosCodes/active_policy/branch/master/graph/badge.svg)](https://codecov.io/gh/PanosCodes/active_policy)
3
3
 
4
- # ActivePolicy
4
+ # ⛩️ ActivePolicy
5
5
  Active policy is meant to be a way to authorize a request before hitting the controller.
6
6
 
7
7
  ## Installation
@@ -18,14 +18,21 @@ class ActivePolicyMiddleware
18
18
  )
19
19
 
20
20
  if params.key?(:policy)
21
- policy = params[:policy].new(current_user(env), request)
21
+ # @type [ActivePolicy::Base] policy
22
+ policy = params[:policy].new(current_user(env), request, params)
22
23
  method_name = params[:action] + '?'
23
24
  models = ActivePolicy::Utilities.models_from_route_params(params)
24
-
25
- policy.send(method_name, *models)
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)
26
35
  end
27
-
28
- @app.call(env)
29
36
  end
30
37
 
31
38
  private
@@ -40,4 +47,9 @@ class ActivePolicyMiddleware
40
47
 
41
48
  nil
42
49
  end
50
+
51
+ # @param [Rack::Response]
52
+ def response(status_code = 401, headers = {'content_type': 'application/json'}, body = [])
53
+ Rack::Response.new(body, status_code, headers)
54
+ end
43
55
  end
@@ -2,9 +2,10 @@ module ActivePolicy
2
2
  class Base
3
3
  # @param [User] current_user
4
4
  # @param [ActionDispatch::Request]
5
- def initialize(current_user, request)
5
+ def initialize(current_user, request, params)
6
6
  @current_user = current_user
7
7
  @request = request
8
+ @params = params
8
9
  end
9
10
  end
10
11
  end
@@ -15,6 +15,7 @@ module ActivePolicy
15
15
  def self.models_from_route_params(params)
16
16
  models = []
17
17
  raise 'policy_models is missing from route' if params[:policy_models].nil?
18
+ return [] if params[:policy_models].empty?
18
19
  params[:policy_models].each do |key, value|
19
20
  models << value.find(params[key])
20
21
  end
@@ -1,3 +1,3 @@
1
1
  module ActivePolicy
2
- VERSION = "0.1.1"
2
+ VERSION = "0.1.2"
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.1
4
+ version: 0.1.2
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-16 00:00:00.000000000 Z
11
+ date: 2020-02-17 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.