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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ea4e36f6d1468e4ef6315c36155873a1f0a55889517a7642da29f67c61305d12
4
- data.tar.gz: cb2e309cfe453ed80a05c0409c203b584efff5f2a13aac47a83d662831f6cc8d
3
+ metadata.gz: b6bd4a8956152dd8b62d79b7649ef0068c0ec77c8fa4f4a32fbfe6d045ecdc00
4
+ data.tar.gz: cc0ba2348f4802a38e5a8443b49f199f1e270155ba25bb92f61aef12db160619
5
5
  SHA512:
6
- metadata.gz: 80b16f41f289bfe0de44e976838d0a7c94e134d573bfb1636ccb89438e8808794dec5c33ebdc70d60e7fe109198edd17021e04f516457e9957a9bc7c85dede15
7
- data.tar.gz: b87b9ad5c35b61006c76d6fb4f520ae7a8e1ae9ca8e3f555f4c06a24cca72aea7f0cdce09bf4340b0de0abe6e5eedfabc7c4b09d04b806e15d6b48be62936531
6
+ metadata.gz: 2309402638c0d1be1aef9308102a8188d0fc70c20fc342ea6a2bc66208845766032a26e8de3ce8c00fc6da93dfda01dbad4e5784e87cb726677558a17c010efc
7
+ data.tar.gz: d2f54296ac9f21bd77710857e8e1ae5871708a177fa5b17ac5e9b9f906971631dcff856d470a629a967e918a577ff02b908e2e8b7e92f2607c51f739003c81ea
data/.version CHANGED
@@ -1 +1 @@
1
- 0.5.1
1
+ 0.9.0
@@ -1,42 +1,13 @@
1
- klass =
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
- 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]
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
- # authorize @model, write?
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
- raise ::Policy::Error.new('Request is not authorized!') unless is_authorized?
48
- true
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
@@ -1,21 +1,9 @@
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
9
- end
1
+ # include Policy::Model
10
2
 
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
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
- 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
@@ -40,48 +33,25 @@ class Policy
40
33
 
41
34
  private
42
35
 
43
- # call has to be isolated because specific of error handling
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) && after(@action) == true
39
+ return true if send(@action, *args)
49
40
 
50
- raise Error, 'Access disabled in policy'
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(message)
47
+ block.call message
57
48
  false
58
49
  else
59
- raise Policy::Error, message
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
@@ -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
+
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
- nil
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
@@ -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'
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.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: 2020-09-17 00:00:00.000000000 Z
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.0.6
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