egoist 0.6.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/model.rb +0 -1
- data/lib/egoist/base.rb +4 -23
- data/lib/egoist/proxy.rb +17 -5
- metadata +3 -3
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/model.rb
CHANGED
data/lib/egoist/base.rb
CHANGED
@@ -3,7 +3,7 @@ class Policy
|
|
3
3
|
|
4
4
|
def initialize model:, user: nil
|
5
5
|
@model = model
|
6
|
-
@user = user || current_user
|
6
|
+
@user = user || Policy.current_user
|
7
7
|
end
|
8
8
|
|
9
9
|
# pass block if you want to handle errors yourself
|
@@ -33,12 +33,10 @@ class Policy
|
|
33
33
|
|
34
34
|
private
|
35
35
|
|
36
|
-
# call has to be isolated because
|
36
|
+
# call has to be isolated because of specifics in handling
|
37
37
|
def call *args, &block
|
38
|
-
error 'User is not defined, no access' unless @user
|
39
|
-
|
40
38
|
return true if before(@action) == true
|
41
|
-
return true if send(@action, *args)
|
39
|
+
return true if send(@action, *args)
|
42
40
|
|
43
41
|
error 'Access disabled in policy'
|
44
42
|
rescue Policy::Error => error
|
@@ -46,7 +44,7 @@ class Policy
|
|
46
44
|
message += " - #{self.class}##{@action}"
|
47
45
|
|
48
46
|
if block
|
49
|
-
block.call
|
47
|
+
block.call message
|
50
48
|
false
|
51
49
|
else
|
52
50
|
error message
|
@@ -56,21 +54,4 @@ class Policy
|
|
56
54
|
def before action
|
57
55
|
false
|
58
56
|
end
|
59
|
-
|
60
|
-
def after action
|
61
|
-
true
|
62
|
-
end
|
63
|
-
|
64
|
-
# get current user from globals if globals defined
|
65
|
-
def current_user
|
66
|
-
if defined?(User) && User.respond_to?(:current)
|
67
|
-
User.current
|
68
|
-
elsif defined?(Current) && Current.respond_to?(:user)
|
69
|
-
Current.user
|
70
|
-
elsif user = Thread.current[:current_user]
|
71
|
-
user
|
72
|
-
else
|
73
|
-
raise RuntimeError.new('Current user not found in Policy#current_user')
|
74
|
-
end
|
75
|
-
end
|
76
57
|
end
|
data/lib/egoist/proxy.rb
CHANGED
@@ -1,17 +1,29 @@
|
|
1
1
|
class Policy
|
2
2
|
class << self
|
3
|
-
#
|
4
|
-
def
|
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
|
5
17
|
if model.is_a?(Hash)
|
6
18
|
model, user = model[:model], model[:user]
|
7
19
|
end
|
8
20
|
|
9
21
|
klass = self
|
10
22
|
|
11
|
-
# if we are calling can on Policy class, figure out policy
|
23
|
+
# if we are calling can on Policy class, figure out policy class
|
12
24
|
if self == Policy
|
13
25
|
klass = ('%s_policy' % model.class).classify
|
14
|
-
klass = Object.const_defined?(klass) ? klass.constantize :
|
26
|
+
klass = Object.const_defined?('::%s' % klass) ? klass.constantize : raise('Policy class %s not defined' % klass)
|
15
27
|
end
|
16
28
|
|
17
29
|
klass.new(user: user, model: model).can
|
@@ -44,4 +56,4 @@ class Policy
|
|
44
56
|
end
|
45
57
|
end
|
46
58
|
end
|
47
|
-
end
|
59
|
+
end
|
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
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
|
@@ -42,7 +42,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
42
42
|
- !ruby/object:Gem::Version
|
43
43
|
version: '0'
|
44
44
|
requirements: []
|
45
|
-
rubygems_version: 3.
|
45
|
+
rubygems_version: 3.2.3
|
46
46
|
signing_key:
|
47
47
|
specification_version: 4
|
48
48
|
summary: Ruby access policy library
|