clean-policy 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.version +1 -1
- data/lib/clean-policy.rb +3 -6
- data/lib/clean-policy/adapters/controller.rb +46 -0
- data/lib/clean-policy/adapters/model.rb +23 -0
- data/lib/clean-policy/base.rb +1 -1
- data/lib/clean-policy/global.rb +4 -1
- metadata +4 -3
- data/lib/adapters/lux.rb +0 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: c52178a93af24dbc3829a8e972a1185269fd9a2c6b7ff008d1a1ce426e3f5a87
|
4
|
+
data.tar.gz: 80bacbfd42085a5f06cf4ed2c66aa9d8a603924f510ccd1a3841495a5ca38e17
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9542c98fa7de7dfc648a1e8642aed26dfd8b78bcf64c75ef8c46c5a51f0f5a7c600a7598c7f330d03abc510b33a7735d562bfafe64cce344883bababbf4170ba
|
7
|
+
data.tar.gz: ab6ba29cc8581c2f499676c5f75269955c76e00d2a12e39183d9b28a4ddd03655377ad15ec354925bbb0d86d12c4f165bbafc2fa7985461a3978d19e399d3343
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.1
|
data/lib/clean-policy.rb
CHANGED
@@ -1,6 +1,3 @@
|
|
1
|
-
|
2
|
-
require_relative './clean-policy
|
3
|
-
|
4
|
-
require_relative './clean-policy/global'
|
5
|
-
|
6
|
-
# require_relative './adapters/lux' if defined?(Lux)
|
1
|
+
for lib in %w(base error proxy global adapters/controller adapters/model)
|
2
|
+
require_relative './clean-policy/%s' % lib
|
3
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
klass =
|
2
|
+
if defined? Rails
|
3
|
+
ActiveController::Base
|
4
|
+
elsif defined? Lux
|
5
|
+
Lux::Controller
|
6
|
+
end
|
7
|
+
|
8
|
+
if klass
|
9
|
+
klass.class_eval do
|
10
|
+
def authorize *args, &block
|
11
|
+
opts = {}
|
12
|
+
|
13
|
+
@_is_policy_authorized = true
|
14
|
+
|
15
|
+
raise ArgumentErorr, 'authorize argument[s] not provided' unless args[0]
|
16
|
+
|
17
|
+
# authorize true
|
18
|
+
return if args[0].is_a? TrueClass
|
19
|
+
|
20
|
+
if !args[1]
|
21
|
+
# authorize :admin?
|
22
|
+
opts[:action] = args.first
|
23
|
+
elsif args[2]
|
24
|
+
# authorize @model, write?, CustomClass
|
25
|
+
# authorize @model, write?, class: CustomClass
|
26
|
+
opts[:model] = args.first
|
27
|
+
opts[:action] = args[1]
|
28
|
+
opts[:class] = args[2].is_a?(Hash) ? args[2][:class] : args[2]
|
29
|
+
else
|
30
|
+
# authorize @model, write?
|
31
|
+
opts[:model] = args.first
|
32
|
+
opts[:action] = args[1]
|
33
|
+
end
|
34
|
+
|
35
|
+
# covert all authorize actions to bang actions (fail unless true)
|
36
|
+
action = opts.delete(:action).to_s.sub('?', '!')
|
37
|
+
|
38
|
+
# do it
|
39
|
+
Policy(opts).send(action, &block)
|
40
|
+
end
|
41
|
+
|
42
|
+
def is_authorized?
|
43
|
+
!!@_is_policy_authorized
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Policy
|
2
|
+
module ModelAdapter
|
3
|
+
extend self
|
4
|
+
|
5
|
+
def can user=nil, model=nil
|
6
|
+
klass = "#{self.class}Policy"
|
7
|
+
klass = Object.const_defined?(klass) ? klass.constantize : ModelPolicy
|
8
|
+
Policy(model: model || self, user: user, class: klass)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
if defined? Rails
|
14
|
+
ActiveModel::Base.include Policy::ModelAdapter
|
15
|
+
elsif defined? Sequel
|
16
|
+
class Sequel::Model
|
17
|
+
module InstanceMethods
|
18
|
+
def can user=nil
|
19
|
+
Policy::ModelAdapter.can user, self
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/clean-policy/base.rb
CHANGED
@@ -38,7 +38,7 @@ class Policy
|
|
38
38
|
raise Error, 'Access disabled in policy'
|
39
39
|
rescue Policy::Error => error
|
40
40
|
message = error.message
|
41
|
-
message += " - #{self.class}.#{@action}"
|
41
|
+
message += " - #{self.class}.#{@action}"
|
42
42
|
|
43
43
|
if block
|
44
44
|
block.call(message)
|
data/lib/clean-policy/global.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# accepts: user:, model:, class:
|
2
|
+
# accepts: @model, class:, user:
|
1
3
|
def Policy *args
|
2
4
|
opts = args.first.dup
|
3
5
|
|
@@ -10,7 +12,8 @@ def Policy *args
|
|
10
12
|
|
11
13
|
klass =
|
12
14
|
if model
|
13
|
-
|
15
|
+
klass = model.is_a?(Symbol) ? model : model.class
|
16
|
+
opts[:class] || ('%s_policy' % klass).classify.constantize
|
14
17
|
else
|
15
18
|
ApplicationPolicy
|
16
19
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clean-policy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dino Reic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-10-
|
11
|
+
date: 2019-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Clean, simple explicit and strait-forward policy definitions.
|
14
14
|
email: reic.dino@gmail.com
|
@@ -17,8 +17,9 @@ extensions: []
|
|
17
17
|
extra_rdoc_files: []
|
18
18
|
files:
|
19
19
|
- "./.version"
|
20
|
-
- "./lib/adapters/lux.rb"
|
21
20
|
- "./lib/clean-policy.rb"
|
21
|
+
- "./lib/clean-policy/adapters/controller.rb"
|
22
|
+
- "./lib/clean-policy/adapters/model.rb"
|
22
23
|
- "./lib/clean-policy/base.rb"
|
23
24
|
- "./lib/clean-policy/error.rb"
|
24
25
|
- "./lib/clean-policy/global.rb"
|
data/lib/adapters/lux.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
# # modify base policy
|
2
|
-
# # after first call in current scope :is_policy_authorized will be set to true
|
3
|
-
# # check with `Policy.is_authorized?`, resets on every page load
|
4
|
-
# class Policy
|
5
|
-
# def before action
|
6
|
-
# Lux.current.var[:is_policy_authorized] = true if defined?(Lux)
|
7
|
-
# end
|
8
|
-
|
9
|
-
# def self.is_authorized?
|
10
|
-
# Lux.current.var[:is_policy_authorized]
|
11
|
-
# end
|
12
|
-
# end
|
13
|
-
|
14
|
-
# Lux::Application.before do
|
15
|
-
# Lux.current.var[:is_policy_authorized] = false
|
16
|
-
# end
|