egoist 0.6.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1cf227b12783405ce60286221522041adf71427bae42c3b7f99ed2f7723b1e5
4
- data.tar.gz: 4de9e17d62355253405da865f6a5f8fff6593b464fe401c1f0b5064a813e1af8
3
+ metadata.gz: a44e3f68f8f1f3e96d0dfff72d58978e17f4b6dddbd0abcece9e8f2120aa6e8b
4
+ data.tar.gz: 4363ca7c24aef5f39066ad0e70d060a26df5c87e5af7f848317fba87e9d2e736
5
5
  SHA512:
6
- metadata.gz: 62c8dea226377f98017f37279d5f45876d932a33fe49d0626d0473eb06e8852e8b777ee21391998ae3d46ea1008d085922381c9b285e24e9932b6d505b2e6bb7
7
- data.tar.gz: daab6a61b0209dbb95b6b2445ed45718eccfc00059fd730ad4d35677883257d42c430861ad8b974841f4ed082adba3441586468330c6f6e0bf26f50dae052b97
6
+ metadata.gz: 87725d7d4b8510cd8e4193df17c782a9a2d6cd252a2a54c973f8a78adcbcd8039ed798df893b56940b01cfdf56dc413b89eec8f6823f5cbadb0bee3522cb00ad
7
+ data.tar.gz: eb395431b15ef13eab8e8f7a732334a0e8dba87f1f48344c58967aded2336f96a159f0d069ec098fa12df940baa12e71fb3033137d4ce4ecac283bd13e34b338
data/.version CHANGED
@@ -1 +1 @@
1
- 0.6.0
1
+ 0.8.0
@@ -8,7 +8,6 @@ end
8
8
  if klass
9
9
  klass.class_eval do
10
10
  def can user=nil
11
- puts 12345
12
11
  Policy.can self, user
13
12
  end
14
13
  end
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 specific of error handling
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) && after(@action) == true
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(message)
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
- # convenient proxy access
4
- def can model=nil, user=nil
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 name or fall back to ModelPolicy
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 : ::ModelPolicy
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.6.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: 2021-01-13 00:00:00.000000000 Z
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.0.6
45
+ rubygems_version: 3.2.3
46
46
  signing_key:
47
47
  specification_version: 4
48
48
  summary: Ruby access policy library