egoist 0.5.1 → 0.9.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 +12 -38
- data/lib/adapters/model.rb +5 -17
- data/lib/egoist/base.rb +13 -43
- data/lib/egoist/error.rb +15 -0
- data/lib/egoist/proxy.rb +44 -8
- data/lib/egoist.rb +0 -1
- metadata +3 -4
- 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: b6bd4a8956152dd8b62d79b7649ef0068c0ec77c8fa4f4a32fbfe6d045ecdc00
|
4
|
+
data.tar.gz: cc0ba2348f4802a38e5a8443b49f199f1e270155ba25bb92f61aef12db160619
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2309402638c0d1be1aef9308102a8188d0fc70c20fc342ea6a2bc66208845766032a26e8de3ce8c00fc6da93dfda01dbad4e5784e87cb726677558a17c010efc
|
7
|
+
data.tar.gz: d2f54296ac9f21bd77710857e8e1ae5871708a177fa5b17ac5e9b9f906971631dcff856d470a629a967e918a577ff02b908e2e8b7e92f2607c51f739003c81ea
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.9.0
|
data/lib/adapters/controller.rb
CHANGED
@@ -1,42 +1,13 @@
|
|
1
|
-
|
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
|
1
|
+
# include Policy::Controller
|
19
2
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
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]
|
3
|
+
class Policy
|
4
|
+
module Controller
|
5
|
+
def authorize result = false
|
6
|
+
if (block_given? ? yield : result)
|
7
|
+
@_is_policy_authorized = true
|
29
8
|
else
|
30
|
-
|
31
|
-
opts[:model] = args.first
|
32
|
-
opts[:action] = args[1]
|
9
|
+
Policy.error('Authorize did not pass truthy value')
|
33
10
|
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
11
|
end
|
41
12
|
|
42
13
|
def is_authorized?
|
@@ -44,8 +15,11 @@ if klass
|
|
44
15
|
end
|
45
16
|
|
46
17
|
def is_authorized!
|
47
|
-
|
48
|
-
|
18
|
+
if is_authorized?
|
19
|
+
true
|
20
|
+
else
|
21
|
+
Policy.error('Request is not authorized!')
|
22
|
+
end
|
49
23
|
end
|
50
24
|
end
|
51
25
|
end
|
data/lib/adapters/model.rb
CHANGED
@@ -1,21 +1,9 @@
|
|
1
|
-
|
2
|
-
module ModelAdapter
|
3
|
-
def self.can user, model
|
4
|
-
klass = '%sPolicy' % model.class
|
5
|
-
klass = Object.const_defined?(klass) ? klass.constantize : ::ModelPolicy
|
6
|
-
Policy(model: model || self, user: user, class: klass)
|
7
|
-
end
|
8
|
-
end
|
9
|
-
end
|
1
|
+
# include Policy::Model
|
10
2
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
module InstanceMethods
|
16
|
-
def can user=nil
|
17
|
-
Policy::ModelAdapter.can user, self
|
18
|
-
end
|
3
|
+
class Policy
|
4
|
+
module Model
|
5
|
+
def can user = nil
|
6
|
+
Policy.can model: self, user: user
|
19
7
|
end
|
20
8
|
end
|
21
9
|
end
|
data/lib/egoist/base.rb
CHANGED
@@ -1,21 +1,9 @@
|
|
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
|
17
5
|
@model = model
|
18
|
-
@user = user || current_user
|
6
|
+
@user = user || Policy.current_user
|
19
7
|
end
|
20
8
|
|
21
9
|
# pass block if you want to handle errors yourself
|
@@ -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
|
@@ -40,48 +33,25 @@ class Policy
|
|
40
33
|
|
41
34
|
private
|
42
35
|
|
43
|
-
# call has to be isolated because
|
36
|
+
# call has to be isolated because of specifics in handling
|
44
37
|
def call *args, &block
|
45
|
-
raise Error, 'User is not defined, no access' unless @user
|
46
|
-
|
47
38
|
return true if before(@action) == true
|
48
|
-
return true if send(@action, *args)
|
39
|
+
return true if send(@action, *args)
|
49
40
|
|
50
|
-
|
41
|
+
error 'Access disabled in policy'
|
51
42
|
rescue Policy::Error => error
|
52
43
|
message = error.message
|
53
44
|
message += " - #{self.class}##{@action}"
|
54
45
|
|
55
46
|
if block
|
56
|
-
block.call
|
47
|
+
block.call message
|
57
48
|
false
|
58
49
|
else
|
59
|
-
|
50
|
+
error message
|
60
51
|
end
|
61
52
|
end
|
62
53
|
|
63
54
|
def before action
|
64
55
|
false
|
65
56
|
end
|
66
|
-
|
67
|
-
def after action
|
68
|
-
true
|
69
|
-
end
|
70
|
-
|
71
|
-
def error message
|
72
|
-
raise Policy::Error.new(message)
|
73
|
-
end
|
74
|
-
|
75
|
-
# get current user from globals if globals defined
|
76
|
-
def current_user
|
77
|
-
if defined?(User) && User.respond_to?(:current)
|
78
|
-
User.current
|
79
|
-
elsif defined?(Current) && Current.respond_to?(:user)
|
80
|
-
Current.user
|
81
|
-
elsif user = Thread.current[:current_user]
|
82
|
-
user
|
83
|
-
else
|
84
|
-
raise RuntimeError.new('Current user not found in Policy#current_user')
|
85
|
-
end
|
86
|
-
end
|
87
57
|
end
|
data/lib/egoist/error.rb
CHANGED
data/lib/egoist/proxy.rb
CHANGED
@@ -1,4 +1,37 @@
|
|
1
1
|
class Policy
|
2
|
+
class << self
|
3
|
+
# try to load current user
|
4
|
+
def current_user
|
5
|
+
if Thread.current.key?(:current_user)
|
6
|
+
Thread.current[:current_user]
|
7
|
+
elsif defined?(User) && User.respond_to?(:current)
|
8
|
+
User.current
|
9
|
+
elsif defined?(Current) && Current.respond_to?(:user)
|
10
|
+
Current.user
|
11
|
+
else
|
12
|
+
raise RuntimeError.new('Current user not found in Policy#current_user')
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def can model = nil, user = nil
|
17
|
+
if model.is_a?(Hash)
|
18
|
+
model, user = model[:model], model[:user]
|
19
|
+
end
|
20
|
+
|
21
|
+
klass = self
|
22
|
+
|
23
|
+
# if we are calling can on Policy class, figure out policy class
|
24
|
+
if self == Policy
|
25
|
+
klass = ('%s_policy' % model.class).classify
|
26
|
+
klass = Object.const_defined?('::%s' % klass) ? klass.constantize : raise('Policy class %s not defined' % klass)
|
27
|
+
end
|
28
|
+
|
29
|
+
klass.new(user: user, model: model).can
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
###
|
34
|
+
|
2
35
|
class Proxy
|
3
36
|
def initialize policy
|
4
37
|
@policy = policy
|
@@ -9,20 +42,23 @@ class Policy
|
|
9
42
|
action = $1
|
10
43
|
|
11
44
|
@policy.can?(name, *args)
|
12
|
-
@policy.model || true
|
13
|
-
rescue Policy::Error => error
|
14
|
-
if block_given?
|
15
|
-
yield
|
16
|
-
return nil
|
17
|
-
end
|
18
45
|
|
19
46
|
if action == '!'
|
47
|
+
@policy.model || true
|
48
|
+
else
|
49
|
+
true
|
50
|
+
end
|
51
|
+
rescue Policy::Error => error
|
52
|
+
if block_given?
|
53
|
+
yield error
|
54
|
+
nil
|
55
|
+
elsif action == '!'
|
20
56
|
raise error
|
21
57
|
elsif action == '?'
|
22
|
-
|
58
|
+
false
|
23
59
|
else
|
24
60
|
raise ArgumentError.new('Bad policy method %s' % name)
|
25
61
|
end
|
26
62
|
end
|
27
63
|
end
|
28
|
-
end
|
64
|
+
end
|
data/lib/egoist.rb
CHANGED
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.9.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: 2022-08-02 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:
|
@@ -43,7 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
42
|
- !ruby/object:Gem::Version
|
44
43
|
version: '0'
|
45
44
|
requirements: []
|
46
|
-
rubygems_version: 3.
|
45
|
+
rubygems_version: 3.2.3
|
47
46
|
signing_key:
|
48
47
|
specification_version: 4
|
49
48
|
summary: Ruby access policy library
|
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
|