egoist 0.5.0 → 0.8.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 +9 -16
- data/lib/egoist/base.rb +13 -37
- data/lib/egoist/error.rb +15 -0
- data/lib/egoist/proxy.rb +37 -6
- data/lib/egoist.rb +0 -1
- metadata +6 -7
- 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: a44e3f68f8f1f3e96d0dfff72d58978e17f4b6dddbd0abcece9e8f2120aa6e8b
|
4
|
+
data.tar.gz: 4363ca7c24aef5f39066ad0e70d060a26df5c87e5af7f848317fba87e9d2e736
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 87725d7d4b8510cd8e4193df17c782a9a2d6cd252a2a54c973f8a78adcbcd8039ed798df893b56940b01cfdf56dc413b89eec8f6823f5cbadb0bee3522cb00ad
|
7
|
+
data.tar.gz: eb395431b15ef13eab8e8f7a732334a0e8dba87f1f48344c58967aded2336f96a159f0d069ec098fa12df940baa12e71fb3033137d4ce4ecac283bd13e34b338
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.8.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,14 @@
|
|
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
|
-
module InstanceMethods
|
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
|
+
Policy.can self, user
|
19
12
|
end
|
20
13
|
end
|
21
14
|
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,42 +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
|
-
|
46
|
-
|
47
|
-
return true if before(@action)
|
38
|
+
return true if before(@action) == true
|
48
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 error message
|
68
|
-
raise Policy::Error.new(message)
|
69
|
-
end
|
70
|
-
|
71
|
-
# get current user from globals if globals defined
|
72
|
-
def current_user
|
73
|
-
if defined?(User) && User.respond_to?(:current)
|
74
|
-
User.current
|
75
|
-
elsif defined?(Current) && Current.respond_to?(:user)
|
76
|
-
Current.user
|
77
|
-
else
|
78
|
-
raise RuntimeError.new('Current user not found in Policy#current_user')
|
79
|
-
end
|
80
|
-
end
|
81
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
|
@@ -12,11 +45,9 @@ class Policy
|
|
12
45
|
@policy.model || true
|
13
46
|
rescue Policy::Error => error
|
14
47
|
if block_given?
|
15
|
-
yield
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
if action == '!'
|
48
|
+
yield error
|
49
|
+
nil
|
50
|
+
elsif action == '!'
|
20
51
|
raise error
|
21
52
|
elsif action == '?'
|
22
53
|
nil
|
@@ -25,4 +56,4 @@ class Policy
|
|
25
56
|
end
|
26
57
|
end
|
27
58
|
end
|
28
|
-
end
|
59
|
+
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.8.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Dino Reic
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-06-27 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,13 +22,12 @@ 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:
|
29
28
|
- MIT
|
30
29
|
metadata: {}
|
31
|
-
post_install_message:
|
30
|
+
post_install_message:
|
32
31
|
rdoc_options: []
|
33
32
|
require_paths:
|
34
33
|
- lib
|
@@ -43,8 +42,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
43
42
|
- !ruby/object:Gem::Version
|
44
43
|
version: '0'
|
45
44
|
requirements: []
|
46
|
-
rubygems_version: 3.
|
47
|
-
signing_key:
|
45
|
+
rubygems_version: 3.2.3
|
46
|
+
signing_key:
|
48
47
|
specification_version: 4
|
49
48
|
summary: Ruby access policy library
|
50
49
|
test_files: []
|
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
|