egoist 0.5.1 → 0.6.0
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/adapters/controller.rb +10 -31
- data/lib/adapters/model.rb +10 -16
- data/lib/egoist.rb +0 -1
- data/lib/egoist/base.rb +10 -21
- data/lib/egoist/error.rb +15 -0
- data/lib/egoist/proxy.rb +24 -5
- metadata +2 -3
- data/lib/egoist/global.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: b1cf227b12783405ce60286221522041adf71427bae42c3b7f99ed2f7723b1e5
|
4
|
+
data.tar.gz: 4de9e17d62355253405da865f6a5f8fff6593b464fe401c1f0b5064a813e1af8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 62c8dea226377f98017f37279d5f45876d932a33fe49d0626d0473eb06e8852e8b777ee21391998ae3d46ea1008d085922381c9b285e24e9932b6d505b2e6bb7
|
7
|
+
data.tar.gz: daab6a61b0209dbb95b6b2445ed45718eccfc00059fd730ad4d35677883257d42c430861ad8b974841f4ed082adba3441586468330c6f6e0bf26f50dae052b97
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.6.0
|
data/lib/adapters/controller.rb
CHANGED
@@ -1,42 +1,18 @@
|
|
1
1
|
klass =
|
2
2
|
if defined? Rails
|
3
|
-
|
3
|
+
ActionController::Base
|
4
4
|
elsif defined? Lux
|
5
5
|
Lux::Controller
|
6
6
|
end
|
7
7
|
|
8
8
|
if klass
|
9
9
|
klass.class_eval do
|
10
|
-
def authorize
|
11
|
-
|
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]
|
10
|
+
def authorize result=false
|
11
|
+
if (block_given? ? yield : result)
|
12
|
+
@_is_policy_authorized = true
|
29
13
|
else
|
30
|
-
|
31
|
-
opts[:model] = args.first
|
32
|
-
opts[:action] = args[1]
|
14
|
+
Policy.error('Authorize did not pass truthy value')
|
33
15
|
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
16
|
end
|
41
17
|
|
42
18
|
def is_authorized?
|
@@ -44,8 +20,11 @@ if klass
|
|
44
20
|
end
|
45
21
|
|
46
22
|
def is_authorized!
|
47
|
-
|
48
|
-
|
23
|
+
if is_authorized?
|
24
|
+
true
|
25
|
+
else
|
26
|
+
Policy.error('Request is not authorized!')
|
27
|
+
end
|
49
28
|
end
|
50
29
|
end
|
51
30
|
end
|
data/lib/adapters/model.rb
CHANGED
@@ -1,21 +1,15 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
Policy(model: model || self, user: user, class: klass)
|
7
|
-
end
|
8
|
-
end
|
1
|
+
klass =
|
2
|
+
if defined? ActiveRecord
|
3
|
+
ActiveRecord::Base
|
4
|
+
elsif defined? Sequel
|
5
|
+
Sequel::Model
|
9
6
|
end
|
10
7
|
|
11
|
-
if
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
def can user=nil
|
17
|
-
Policy::ModelAdapter.can user, self
|
18
|
-
end
|
8
|
+
if klass
|
9
|
+
klass.class_eval do
|
10
|
+
def can user=nil
|
11
|
+
puts 12345
|
12
|
+
Policy.can self, user
|
19
13
|
end
|
20
14
|
end
|
21
15
|
end
|
data/lib/egoist.rb
CHANGED
data/lib/egoist/base.rb
CHANGED
@@ -1,16 +1,4 @@
|
|
1
1
|
class Policy
|
2
|
-
class << self
|
3
|
-
def can(model=nil, user=nil)
|
4
|
-
if model.is_a?(Hash)
|
5
|
-
user, model = model[:user], model[:model]
|
6
|
-
end
|
7
|
-
|
8
|
-
new(user: user, model: model).can
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
###
|
13
|
-
|
14
2
|
attr_reader :model, :user, :action
|
15
3
|
|
16
4
|
def initialize model:, user: nil
|
@@ -28,8 +16,13 @@ class Policy
|
|
28
16
|
.to_sym
|
29
17
|
|
30
18
|
# pre check
|
31
|
-
|
32
|
-
|
19
|
+
if %i(can).index(@action)
|
20
|
+
raise RuntimeError.new('Method name not allowed')
|
21
|
+
end
|
22
|
+
|
23
|
+
unless respond_to?(@action)
|
24
|
+
raise NoMethodError.new(%[Policy check "#{@action}" not found in #{self.class}])
|
25
|
+
end
|
33
26
|
|
34
27
|
call *args, &block
|
35
28
|
end
|
@@ -42,12 +35,12 @@ class Policy
|
|
42
35
|
|
43
36
|
# call has to be isolated because specific of error handling
|
44
37
|
def call *args, &block
|
45
|
-
|
38
|
+
error 'User is not defined, no access' unless @user
|
46
39
|
|
47
40
|
return true if before(@action) == true
|
48
41
|
return true if send(@action, *args) && after(@action) == true
|
49
42
|
|
50
|
-
|
43
|
+
error 'Access disabled in policy'
|
51
44
|
rescue Policy::Error => error
|
52
45
|
message = error.message
|
53
46
|
message += " - #{self.class}##{@action}"
|
@@ -56,7 +49,7 @@ class Policy
|
|
56
49
|
block.call(message)
|
57
50
|
false
|
58
51
|
else
|
59
|
-
|
52
|
+
error message
|
60
53
|
end
|
61
54
|
end
|
62
55
|
|
@@ -68,10 +61,6 @@ class Policy
|
|
68
61
|
true
|
69
62
|
end
|
70
63
|
|
71
|
-
def error message
|
72
|
-
raise Policy::Error.new(message)
|
73
|
-
end
|
74
|
-
|
75
64
|
# get current user from globals if globals defined
|
76
65
|
def current_user
|
77
66
|
if defined?(User) && User.respond_to?(:current)
|
data/lib/egoist/error.rb
CHANGED
data/lib/egoist/proxy.rb
CHANGED
@@ -1,4 +1,25 @@
|
|
1
1
|
class Policy
|
2
|
+
class << self
|
3
|
+
# convenient proxy access
|
4
|
+
def can model=nil, user=nil
|
5
|
+
if model.is_a?(Hash)
|
6
|
+
model, user = model[:model], model[:user]
|
7
|
+
end
|
8
|
+
|
9
|
+
klass = self
|
10
|
+
|
11
|
+
# if we are calling can on Policy class, figure out policy name or fall back to ModelPolicy
|
12
|
+
if self == Policy
|
13
|
+
klass = ('%s_policy' % model.class).classify
|
14
|
+
klass = Object.const_defined?(klass) ? klass.constantize : ::ModelPolicy
|
15
|
+
end
|
16
|
+
|
17
|
+
klass.new(user: user, model: model).can
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
###
|
22
|
+
|
2
23
|
class Proxy
|
3
24
|
def initialize policy
|
4
25
|
@policy = policy
|
@@ -12,11 +33,9 @@ class Policy
|
|
12
33
|
@policy.model || true
|
13
34
|
rescue Policy::Error => error
|
14
35
|
if block_given?
|
15
|
-
yield
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
if action == '!'
|
36
|
+
yield error
|
37
|
+
nil
|
38
|
+
elsif action == '!'
|
20
39
|
raise error
|
21
40
|
elsif action == '?'
|
22
41
|
nil
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: egoist
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dino Reic
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-01-13 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
|
@@ -22,7 +22,6 @@ files:
|
|
22
22
|
- "./lib/egoist.rb"
|
23
23
|
- "./lib/egoist/base.rb"
|
24
24
|
- "./lib/egoist/error.rb"
|
25
|
-
- "./lib/egoist/global.rb"
|
26
25
|
- "./lib/egoist/proxy.rb"
|
27
26
|
homepage: https://github.com/dux/egoist
|
28
27
|
licenses:
|
data/lib/egoist/global.rb
DELETED
@@ -1,16 +0,0 @@
|
|
1
|
-
# Policy(:application) -> ApplicationPolicy.can(model: nil, user: current_user)
|
2
|
-
# Policy(@post) -> PostPolict.can(model: @post, user: current_user)
|
3
|
-
# Policy(@post, @user) -> PostPolict.can(model: @post, user: @user)
|
4
|
-
# Policy(model: @post, user: @user) -> PostPolict.can(model: @post, user: @user)
|
5
|
-
def Policy model, user=nil
|
6
|
-
if model.is_a?(Hash)
|
7
|
-
user, model = model[:user], model[:model]
|
8
|
-
end
|
9
|
-
|
10
|
-
raise ArgumentError, 'Model not defined' unless model
|
11
|
-
|
12
|
-
klass = model.is_a?(Symbol) ? model : model.class
|
13
|
-
klass = ('%s_policy' % klass).classify.constantize
|
14
|
-
|
15
|
-
klass.new(user: user, model: model).can
|
16
|
-
end
|