miau 1.1.1 → 1.1.7

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: d207e809bfd3a605612092afc04287df2def3175e71a0a0b2c98c0ce491e1402
4
- data.tar.gz: 4d5985b0b22a467848c0d5e1bc3fd14a22b3231b928e42fafa922f1faa5520fc
3
+ metadata.gz: bfe1c8f3bc0844078afe66eb87172eb9d372e727cedc6a3d99e276b37b741c4a
4
+ data.tar.gz: 0dc15287104641bec95f8073387e047770dfd5b02f5f5e5980f6357f460450fb
5
5
  SHA512:
6
- metadata.gz: ba62cbe78a200045d483bb6b3c544fdbd57aa1a7b24a20ed73b8d5a3304d31420d088441edee0c2d16feaa4223c468375f1e985a78e38263c427e6e139173d6f
7
- data.tar.gz: ff00545b68b70128919cc417ba69906e38eaf5decab73681bed88dc291fc648e8cdfa0c40e32942d08ed82605384ad5c76dd0723514f2a95cf6ec7e8deefdb62
6
+ metadata.gz: 913283a22edaf93ce62275e9a93e30971fdeacd15ba8eb3f5d5066a74d745535a6d992aac40ca94fae58eb006f3bcb4cb8a6bcb28496e9a105509e250a28d8f2
7
+ data.tar.gz: effe13f27bd0da506d83203c0f454f9df5fd6305c726184d7eb13e92d903719fa8df2fc1fe768ec58ff43a917e3c6c102a3aee76c99957020000a13a285c5767
data/README.md CHANGED
@@ -44,13 +44,12 @@ end
44
44
  ```ruby
45
45
  # app/policies/application_policy.rb # app/policies/posts_policy.rb
46
46
  class ApplicationPolicy class PostsPolicy < ApplicationPolicy
47
- attr_reader :user, :resource ...
47
+ attr_reader :user, :resource, :action ...
48
48
  def update
49
- def initalize(user, resource) user.admin? && resource.published?
50
- @user = user end
51
- @resource = resource ...
52
- end end
53
- end
49
+ ... user.admin? && resource.published?
50
+ end end
51
+ ...
52
+ end
54
53
  ```
55
54
 
56
55
  "authorize!" will raise an exception (which can be handled by "rescue")
@@ -67,6 +66,8 @@ a corresponding policy method is called.
67
66
 
68
67
  The policy method has access to the "user" and the "resource".
69
68
 
69
+ The "controller" policy method has access to the "user" and the "action".
70
+
70
71
  "user" is set by the default method "miau_user" (can be overwritten) as:
71
72
 
72
73
  ```ruby
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ApplicationPolicy
4
- attr_accessor :user, :resource
4
+ attr_accessor :user, :resource, :action
5
5
 
6
6
  def self.miau(actions, meth = nil, &block)
7
7
  kls = name.underscore[0..-8] # remove "_policy"
data/lib/miau/error.rb CHANGED
@@ -3,15 +3,11 @@
3
3
  module Miau
4
4
  class Error < StandardError; end
5
5
 
6
- class NotAuthorizedError < Error
7
- end
6
+ class NotAuthorizedError < Error; end
8
7
 
9
- class NotDefinedError < Error
10
- end
8
+ class NotDefinedError < Error; end
11
9
 
12
- class AuthorizationNotPerformedError < Error
13
- end
10
+ class AuthorizationNotPerformedError < Error; end
14
11
 
15
- class OverwriteError < Error
16
- end
12
+ class OverwriteError < Error; end
17
13
  end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Miau
4
+ class Error < StandardError; end
5
+
6
+ class NotAuthorizedError < Error; end
7
+ class NotDefinedError < Error; end
8
+ class AuthorizationNotPerformedError < Error; end
9
+ class OverwriteError < Error; end
10
+ end
data/lib/miau/run.rb CHANGED
@@ -7,7 +7,7 @@ module Miau
7
7
  class PolicyRun
8
8
  include Singleton
9
9
 
10
- # return instance of policy (may be nil) and the method
10
+ # return method[s]
11
11
  # klass and action are symbols
12
12
  # Priority:
13
13
  # - method of <klass>Policy
@@ -17,7 +17,7 @@ module Miau
17
17
  # - nil
18
18
  # returns method_name[s]
19
19
 
20
- def find_policy(policy, klass, action)
20
+ def find_methods(policy, klass, action)
21
21
  return action if policy.respond_to?(action)
22
22
 
23
23
  hsh = PolicyStorage.instance.policies[klass]
@@ -26,22 +26,23 @@ module Miau
26
26
  hsh[action]
27
27
  end
28
28
 
29
- def run(klass, action, user, resource)
30
- policy = PolicyStorage.instance.find_or_create_policy(klass)
31
- meth = find_policy policy, klass, action if policy
32
- meth ||= find_policy ApplicationPolicy, :application, action
29
+ def runs(policy, actions)
30
+ [actions].flatten.each { |action|
31
+ raise_undef(policy, action) unless policy&.respond_to?(action)
33
32
 
34
- unless meth
35
- msg = "class <#{klass}> action <#{action}>"
36
- raise NotDefinedError, msg
37
- end
38
-
39
- policy.user = user
40
- policy.resource = resource
41
- [meth].flatten.each { |m|
42
- return false unless policy.send(m)
33
+ return false unless policy.send(action)
43
34
  }
44
35
  true
45
36
  end
37
+
38
+ def raise_undef(policy, action)
39
+ msg = "policy <#{policy}> action <#{action}>"
40
+ raise NotDefinedError, msg
41
+ end
42
+
43
+ def raise_authorize(controller, action)
44
+ msg = "controller <#{controller}> action <#{action}>"
45
+ raise NotAuthorizedError, msg
46
+ end
46
47
  end
47
48
  end
data/lib/miau/storage.rb CHANGED
@@ -18,7 +18,7 @@ module Miau
18
18
  # }
19
19
  # }
20
20
  attr_reader :policies
21
- attr_reader :instances # { posts: PostsPolicy.new }
21
+ attr_reader :instances # e.g. { posts: PostsPolicy.new }
22
22
 
23
23
  def initialize
24
24
  reset
@@ -38,7 +38,7 @@ module Miau
38
38
  end
39
39
 
40
40
  if meth.is_a?(Array)
41
- meths = [meth].flatten.collect { |m| m.to_sym }
41
+ meths = [meth].flatten.collect(&:to_sym)
42
42
  @policies[kls][action] = meths
43
43
  else
44
44
  @policies[kls][action] = meth.to_sym
data/lib/miau/version.rb CHANGED
@@ -1,7 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Miau
4
- VERSION = "1.1.1" # 2024-01-06
4
+ VERSION = "1.1.7" # 2024-04-24
5
+ # VERSION = "1.1.6" # 2024-01-13
6
+ # VERSION = "1.1.1" # 2024-01-06
5
7
  # VERSION = "1.1.0" # 2024-01-06
6
8
  # VERSION = "1.0.3" # 2023-12-13
7
9
  # VERSION = "1.0.2" # 2023-11-05
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Miau
4
+ VERSION = "1.1.6" # 2024-01-13
5
+ # VERSION = "1.1.1" # 2024-01-06
6
+ # VERSION = "1.1.0" # 2024-01-06
7
+ # VERSION = "1.0.3" # 2023-12-13
8
+ # VERSION = "1.0.2" # 2023-11-05
9
+ # VERSION = "1.0.1" # 2023-04-24
10
+ # VERSION = "1.0.0" # 2022-01-23
11
+ # VERSION = "0.1.2" # 2021-12-10
12
+ # VERSION = "0.1.1" # 2021-11-06
13
+ # VERSION = "0.1.0" # 2021-09-13
14
+ # VERSION = "0.0.2" # 2021-09-11
15
+ # VERSION = "0.0.1"
16
+ end
data/lib/miau.rb CHANGED
@@ -21,14 +21,41 @@ module Miau
21
21
  @_miau_authorization_performed = true
22
22
  return true if authorized?(resource, hsh)
23
23
 
24
- klass, action = klass_action
25
- msg = "class <#{klass} action <#{action}>"
26
- raise NotAuthorizedError, msg
24
+ controller = params[:controller].to_sym
25
+ action = params[:action].to_sym
26
+ PolicyRun.instance.raise_authorize(controller, action)
27
27
  end
28
28
 
29
29
  def authorized?(resource = nil, hsh = {})
30
- klass, action = klass_action
31
- PolicyRun.instance.run(klass, action, miau_user, resource)
30
+ controller = params[:controller].to_sym
31
+ action = params[:action].to_sym
32
+ policy = PolicyStorage.instance.find_or_create_policy(controller)
33
+ PolicyRun.instance.raise_undef(policy, action) unless policy
34
+
35
+ policy.user = miau_user
36
+ policy.resource = resource
37
+ methods = PolicyRun.instance.find_methods(policy, controller, action)
38
+ PolicyRun.instance.raise_undef(policy, action) unless methods
39
+
40
+ PolicyRun.instance.runs(policy, methods)
41
+ end
42
+
43
+ def authorize_controller!
44
+ controller = params[:controller].to_sym
45
+ action = params[:action].to_sym
46
+ policy = PolicyStorage.instance.find_or_create_policy(controller)
47
+ unless policy
48
+ msg = "missing class #{controller.capitalize}Policy"
49
+ raise NotDefinedError, msg
50
+ end
51
+
52
+ policy.user = miau_user
53
+ policy.action = action
54
+
55
+ @_miau_authorization_performed = true
56
+ return true if PolicyRun.instance.runs(policy, :controller)
57
+
58
+ PolicyRun.instance.raise_authorize policy, action
32
59
  end
33
60
 
34
61
  def miau_user
@@ -42,18 +69,4 @@ module Miau
42
69
  def miau_authorization_performed?
43
70
  !!@_miau_authorization_performed
44
71
  end
45
-
46
- def authorize_controller!
47
- name = params[:controller].to_sym
48
- policy = PolicyStorage.instance.find_or_create_policy(name)
49
- raise NotDefinedError unless policy&.respond_to?(:controller)
50
-
51
- policy.send(:controller)
52
- end
53
-
54
- private
55
-
56
- def klass_action
57
- [params[:controller].to_sym, params[:action].to_sym]
58
- end
59
72
  end
data/lib/miau.rb.bak ADDED
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support/concern"
4
+ require "miau/version"
5
+ require "miau/error"
6
+ require "miau/storage"
7
+ require "miau/run"
8
+ require "miau/application_policy"
9
+
10
+ module Miau
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ if respond_to?(:helper_method)
15
+ helper_method :authorized?
16
+ helper_method :miau_user
17
+ end
18
+ end
19
+
20
+ def authorize!(resource = nil, hsh = {})
21
+ @_miau_authorization_performed = true
22
+ return true if authorized?(resource, hsh)
23
+
24
+ controller = params[:controller].to_sym
25
+ action = params[:action].to_sym
26
+ PolicyRun.instance.raise_authorize(controller, action)
27
+ end
28
+
29
+ def authorized?(resource = nil, hsh = {})
30
+ controller = params[:controller].to_sym
31
+ action = params[:action].to_sym
32
+ policy = PolicyStorage.instance.find_or_create_policy(controller)
33
+ PolicyRun.instance.raise_undef(policy, action) unless policy
34
+
35
+ policy.user = miau_user
36
+ policy.resource = resource
37
+ methods = PolicyRun.instance.find_methods(policy, controller, action)
38
+ PolicyRun.instance.raise_undef(policy, action) unless methods
39
+
40
+ PolicyRun.instance.runs(policy, methods)
41
+ end
42
+
43
+ def authorize_controller!
44
+ controller = params[:controller].to_sym
45
+ action = params[:action].to_sym
46
+ policy = PolicyStorage.instance.find_or_create_policy(controller)
47
+ p 22222
48
+ unless policy
49
+ p 11111111111111
50
+ # msg = "undefined class #{controller.capitalize}Policy"
51
+ # raise NotDefinedError, msg
52
+ end
53
+
54
+ policy.user = miau_user
55
+ policy.action = action
56
+
57
+ @_miau_authorization_performed = true
58
+ return true if PolicyRun.instance.runs(policy, :controller)
59
+
60
+ PolicyRun.instance.raise_authorize policy, action
61
+ end
62
+
63
+ def miau_user
64
+ current_user
65
+ end
66
+
67
+ def verify_authorized
68
+ raise AuthorizationNotPerformedError unless miau_authorization_performed?
69
+ end
70
+
71
+ def miau_authorization_performed?
72
+ !!@_miau_authorization_performed
73
+ end
74
+ end
metadata CHANGED
@@ -1,23 +1,23 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: miau
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dittmar Krall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-01-06 00:00:00.000000000 Z
11
+ date: 2024-04-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activesupport
14
+ name: appraisal
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
- type: :runtime
20
+ type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
@@ -25,7 +25,7 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
- name: appraisal
28
+ name: combustion
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -67,36 +67,26 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  description: |
70
- MIAU (MIcro AUthorization) provides a set of helpers which restricts what
71
- resources a given user is allowed to access.
70
+ MIAU (MIcro AUthorization) provides some helpers which
71
+ raises an exception if a given user violates a policy.
72
72
  email: dittmar.krall@matiq.com
73
73
  executables: []
74
74
  extensions: []
75
- extra_rdoc_files: []
75
+ extra_rdoc_files:
76
+ - README.md
77
+ - MIT-LICENSE
76
78
  files:
77
- - ".github/workflows/rake.yml"
78
- - ".gitignore"
79
- - ".ruby-gemset"
80
- - ".ruby-version"
81
- - Appraisals
82
- - Gemfile
83
- - Gemfile.lock
84
- - LICENSE
79
+ - MIT-LICENSE
85
80
  - README.md
86
- - Rakefile
87
- - gemfiles/rails_6.1.gemfile
88
- - gemfiles/rails_6.1.gemfile.lock
89
- - gemfiles/rails_7.0.gemfile
90
- - gemfiles/rails_7.0.gemfile.lock
91
- - gemfiles/rails_7.1.gemfile
92
- - gemfiles/rails_7.1.gemfile.lock
93
81
  - lib/miau.rb
82
+ - lib/miau.rb.bak
94
83
  - lib/miau/application_policy.rb
95
84
  - lib/miau/error.rb
85
+ - lib/miau/error.rb.bak
96
86
  - lib/miau/run.rb
97
87
  - lib/miau/storage.rb
98
88
  - lib/miau/version.rb
99
- - miau.gemspec
89
+ - lib/miau/version.rb.bak
100
90
  homepage: https://github.com/matique/miau
101
91
  licenses:
102
92
  - MIT
@@ -116,7 +106,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
106
  - !ruby/object:Gem::Version
117
107
  version: '0'
118
108
  requirements: []
119
- rubygems_version: 3.5.3
109
+ rubygems_version: 3.5.9
120
110
  signing_key:
121
111
  specification_version: 4
122
112
  summary: Simple and lightweight authorization solution for Rails.
@@ -1,27 +0,0 @@
1
- # see also https://github.com/whitequark/parser/blob/master/.github/workflows/test.yml
2
- name: Rake
3
-
4
- on: [push]
5
-
6
- jobs:
7
- test:
8
- strategy:
9
- fail-fast: false
10
- matrix:
11
- ruby_version: ["3.0", "3.2", head]
12
- gemfile:
13
- - Gemfile
14
- - gemfiles/Gemfile.rails-7.1
15
- - gemfiles/Gemfile.rails-7.0
16
- - gemfiles/Gemfile.rails-6.1
17
- runs-on: ubuntu-latest
18
-
19
- steps:
20
- - uses: actions/checkout@v3
21
- - name: Set up Ruby
22
- uses: ruby/setup-ruby@v1
23
- with:
24
- ruby-version: ${{ matrix.ruby_version }}
25
- bundler-cache: true
26
- - name: Build and test with Rake
27
- run: bundle exec rake
data/.gitignore DELETED
@@ -1,7 +0,0 @@
1
- /.bundle/
2
- /Gemfile.lock
3
- /coverage/
4
- .watchr
5
-
6
- /doc/
7
- /tmp/
data/.ruby-gemset DELETED
@@ -1 +0,0 @@
1
- rails-7.1
data/.ruby-version DELETED
@@ -1 +0,0 @@
1
- ruby-3.3.0
data/Appraisals DELETED
@@ -1,13 +0,0 @@
1
- appraise "rails-7.1" do
2
- gem "rails", "~> 7.1"
3
- end
4
-
5
- appraise "rails-7.0" do
6
- gem "rails", "~> 7.0"
7
- gem "dryer-config", "~> 7.0"
8
- end
9
-
10
- appraise "rails-6.1" do
11
- gem "rails", "~> 6.1"
12
- gem "dryer-config", "~> 6.0"
13
- end
data/Gemfile DELETED
@@ -1,10 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- gemspec
4
-
5
- group :test do
6
- gem "observr"
7
- gem "standard", require: false
8
- gem "simplecov", require: false
9
- gem "benchmark-ips"
10
- end
data/Gemfile.lock DELETED
@@ -1,107 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- miau (1.1.1)
5
- activesupport
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activesupport (7.1.2)
11
- base64
12
- bigdecimal
13
- concurrent-ruby (~> 1.0, >= 1.0.2)
14
- connection_pool (>= 2.2.5)
15
- drb
16
- i18n (>= 1.6, < 2)
17
- minitest (>= 5.1)
18
- mutex_m
19
- tzinfo (~> 2.0)
20
- appraisal (2.5.0)
21
- bundler
22
- rake
23
- thor (>= 0.14.0)
24
- ast (2.4.2)
25
- base64 (0.2.0)
26
- benchmark-ips (2.13.0)
27
- bigdecimal (3.1.5)
28
- concurrent-ruby (1.2.2)
29
- connection_pool (2.4.1)
30
- docile (1.4.0)
31
- drb (2.2.0)
32
- ruby2_keywords
33
- i18n (1.14.1)
34
- concurrent-ruby (~> 1.0)
35
- json (2.7.1)
36
- language_server-protocol (3.17.0.3)
37
- lint_roller (1.1.0)
38
- minitest (5.20.0)
39
- mutex_m (0.2.0)
40
- observr (1.0.5)
41
- parallel (1.24.0)
42
- parser (3.3.0.0)
43
- ast (~> 2.4.1)
44
- racc
45
- racc (1.7.3)
46
- rainbow (3.1.1)
47
- rake (13.1.0)
48
- regexp_parser (2.8.3)
49
- rexml (3.2.6)
50
- ricecream (0.2.1)
51
- rubocop (1.59.0)
52
- json (~> 2.3)
53
- language_server-protocol (>= 3.17.0)
54
- parallel (~> 1.10)
55
- parser (>= 3.2.2.4)
56
- rainbow (>= 2.2.2, < 4.0)
57
- regexp_parser (>= 1.8, < 3.0)
58
- rexml (>= 3.2.5, < 4.0)
59
- rubocop-ast (>= 1.30.0, < 2.0)
60
- ruby-progressbar (~> 1.7)
61
- unicode-display_width (>= 2.4.0, < 3.0)
62
- rubocop-ast (1.30.0)
63
- parser (>= 3.2.1.0)
64
- rubocop-performance (1.20.1)
65
- rubocop (>= 1.48.1, < 2.0)
66
- rubocop-ast (>= 1.30.0, < 2.0)
67
- ruby-progressbar (1.13.0)
68
- ruby2_keywords (0.0.5)
69
- simplecov (0.22.0)
70
- docile (~> 1.1)
71
- simplecov-html (~> 0.11)
72
- simplecov_json_formatter (~> 0.1)
73
- simplecov-html (0.12.3)
74
- simplecov_json_formatter (0.1.4)
75
- standard (1.33.0)
76
- language_server-protocol (~> 3.17.0.2)
77
- lint_roller (~> 1.0)
78
- rubocop (~> 1.59.0)
79
- standard-custom (~> 1.0.0)
80
- standard-performance (~> 1.3)
81
- standard-custom (1.0.2)
82
- lint_roller (~> 1.0)
83
- rubocop (~> 1.50)
84
- standard-performance (1.3.0)
85
- lint_roller (~> 1.1)
86
- rubocop-performance (~> 1.20.1)
87
- thor (1.3.0)
88
- tzinfo (2.0.6)
89
- concurrent-ruby (~> 1.0)
90
- unicode-display_width (2.5.0)
91
-
92
- PLATFORMS
93
- ruby
94
- x86_64-linux
95
-
96
- DEPENDENCIES
97
- appraisal
98
- benchmark-ips
99
- miau!
100
- minitest
101
- observr
102
- ricecream
103
- simplecov
104
- standard
105
-
106
- BUNDLED WITH
107
- 2.5.3
data/Rakefile DELETED
@@ -1,12 +0,0 @@
1
- require "rubygems"
2
- require "bundler/gem_tasks"
3
- require "rake/testtask"
4
-
5
- desc "Run all tests"
6
- Rake::TestTask.new do |t|
7
- t.libs.push "test"
8
- t.pattern = "test/*_test.rb"
9
- end
10
-
11
- desc "Default: run unit tests."
12
- task default: :test
@@ -1,15 +0,0 @@
1
- # This file was generated by Appraisal
2
-
3
- source "https://rubygems.org"
4
-
5
- gem "rails", "~> 6.1"
6
- gem "dryer-config", "~> 6.0"
7
-
8
- group :test do
9
- gem "observr"
10
- gem "standard", require: false
11
- gem "simplecov", require: false
12
- gem "benchmark-ips"
13
- end
14
-
15
- gemspec path: "../"