egoist 0.5.1 → 0.6.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: ea4e36f6d1468e4ef6315c36155873a1f0a55889517a7642da29f67c61305d12
4
- data.tar.gz: cb2e309cfe453ed80a05c0409c203b584efff5f2a13aac47a83d662831f6cc8d
3
+ metadata.gz: b1cf227b12783405ce60286221522041adf71427bae42c3b7f99ed2f7723b1e5
4
+ data.tar.gz: 4de9e17d62355253405da865f6a5f8fff6593b464fe401c1f0b5064a813e1af8
5
5
  SHA512:
6
- metadata.gz: 80b16f41f289bfe0de44e976838d0a7c94e134d573bfb1636ccb89438e8808794dec5c33ebdc70d60e7fe109198edd17021e04f516457e9957a9bc7c85dede15
7
- data.tar.gz: b87b9ad5c35b61006c76d6fb4f520ae7a8e1ae9ca8e3f555f4c06a24cca72aea7f0cdce09bf4340b0de0abe6e5eedfabc7c4b09d04b806e15d6b48be62936531
6
+ metadata.gz: 62c8dea226377f98017f37279d5f45876d932a33fe49d0626d0473eb06e8852e8b777ee21391998ae3d46ea1008d085922381c9b285e24e9932b6d505b2e6bb7
7
+ data.tar.gz: daab6a61b0209dbb95b6b2445ed45718eccfc00059fd730ad4d35677883257d42c430861ad8b974841f4ed082adba3441586468330c6f6e0bf26f50dae052b97
data/.version CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.6.0
@@ -1,42 +1,18 @@
1
1
  klass =
2
2
  if defined? Rails
3
- ActiveController::Base
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 *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
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
- # authorize @model, write?
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
- raise ::Policy::Error.new('Request is not authorized!') unless is_authorized?
48
- true
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
@@ -1,21 +1,15 @@
1
- class Policy
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
1
+ klass =
2
+ if defined? ActiveRecord
3
+ ActiveRecord::Base
4
+ elsif defined? Sequel
5
+ Sequel::Model
9
6
  end
10
7
 
11
- if defined? Rails
12
- ActiveModel::Base.include Policy::ModelAdapter
13
- elsif defined? Sequel
14
- class Sequel::Model
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
+ puts 12345
12
+ Policy.can self, user
19
13
  end
20
14
  end
21
15
  end
@@ -1,7 +1,6 @@
1
1
  require_relative 'egoist/base'
2
2
  require_relative 'egoist/error'
3
3
  require_relative 'egoist/proxy'
4
- require_relative 'egoist/global'
5
4
 
6
5
  require_relative 'adapters/controller'
7
6
  require_relative 'adapters/model'
@@ -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
- raise RuntimeError, 'Method name not allowed' if %i(can).index(@action)
32
- raise NoMethodError, %[Policy check "#{@action}" not found in #{self.class}] unless respond_to?(@action)
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
- raise Error, 'User is not defined, no access' unless @user
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
- raise Error, 'Access disabled in policy'
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
- raise Policy::Error, message
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)
@@ -2,3 +2,18 @@ class Policy
2
2
  class Error < StandardError
3
3
  end
4
4
  end
5
+
6
+ class Policy
7
+ class << self
8
+ def error msg
9
+ raise ::Policy::Error.new(msg)
10
+ end
11
+ end
12
+
13
+ ###
14
+
15
+ def error message
16
+ raise Policy::Error.new(message)
17
+ end
18
+ end
19
+
@@ -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
- return nil
17
- end
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.5.1
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: 2020-09-17 00:00:00.000000000 Z
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:
@@ -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