miau 1.1.6 → 1.1.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +1 -0
  3. data/lib/miau/error.rb +4 -8
  4. data/lib/miau/run.rb +2 -2
  5. data/lib/miau/storage.rb.bak +63 -0
  6. data/lib/miau/version.rb +3 -1
  7. data/lib/miau/version.rb.bak +17 -0
  8. data/lib/miau.rb +5 -0
  9. data/lib/miau.rb.bak +72 -0
  10. metadata +16 -44
  11. data/.github/workflows/rake.yml +0 -27
  12. data/.gitignore +0 -10
  13. data/.ruby-gemset +0 -1
  14. data/.ruby-version +0 -1
  15. data/Appraisals +0 -13
  16. data/Gemfile +0 -12
  17. data/Gemfile.lock +0 -279
  18. data/Rakefile +0 -10
  19. data/gemfiles/rails_6.1.gemfile +0 -16
  20. data/gemfiles/rails_7.0.gemfile +0 -16
  21. data/gemfiles/rails_7.1.gemfile +0 -15
  22. data/miau.gemspec +0 -27
  23. data/test/authorization_test.rb +0 -28
  24. data/test/benchmark_test.rb +0 -34
  25. data/test/controller_test.rb +0 -58
  26. data/test/controllers/orders_controller_test.rb +0 -47
  27. data/test/internal/app/controllers/application_controller.rb +0 -7
  28. data/test/internal/app/controllers/orders_controller.rb +0 -61
  29. data/test/internal/app/controllers/posts_controller.rb +0 -10
  30. data/test/internal/app/models/application_record.rb +0 -3
  31. data/test/internal/app/models/order.rb +0 -2
  32. data/test/internal/app/models/post.rb +0 -2
  33. data/test/internal/app/policies/orders_policy.rb +0 -16
  34. data/test/internal/app/policies/posts_policy.rb +0 -18
  35. data/test/internal/app/views/orders/new.html.erb +0 -5
  36. data/test/internal/config/database.yml +0 -3
  37. data/test/internal/config/routes.rb +0 -3
  38. data/test/internal/db/migrate/20141016161801_create_orders.rb +0 -10
  39. data/test/internal/db/schema.rb +0 -8
  40. data/test/miau_test.rb +0 -46
  41. data/test/run_test.rb +0 -69
  42. data/test/storage_test.rb +0 -51
  43. data/test/test_helper.rb +0 -17
  44. /data/{LICENSE → MIT-LICENSE} +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4ff4fcf400151bdd20347e5bc2cd8f215a11e8ea6b519840c6032ed3ac18a57b
4
- data.tar.gz: bc678ee4c5c2029d66a5f9d7970ae81e0eb334bb4a493cde94f3445f5d1a9e25
3
+ metadata.gz: 8d212c3d6cf76c609635ebf42d2e62229077651d0189a2ec54cb82f9a23924f9
4
+ data.tar.gz: ae723976d8a6ad3d28e5bd625a4d9884d89e2c387c71094590e64b812b3638fb
5
5
  SHA512:
6
- metadata.gz: a2f4c5d1a4b5947fe6d5ff836291909d5428de7e0745d7e50654327637b173c573646446c4846ca62789b1f590e13fe43f057fa82eac7a03c85faf7483115014
7
- data.tar.gz: f87d22f71705710792a13a2490d71a632a3c55fd0f40bd7cf5e342d58b58c6066777e23dab777af0a0e674103182dec768caf04a73d9f6db58a35ed5453f2e9a
6
+ metadata.gz: c25989c839d48abc53be4a99c7e77f605cd599b48f22c5ba6eaafd170874f1cd68cd379bfd58af367c2b3f91b5132a0129ee6485c7b8fa5589777dd7b4b2c3c0
7
+ data.tar.gz: b1af02057e15ae1a36a8c58ce5c95939513bf321f24f8b7863767b9e9da803e0ff53856e0ebddc4bff8ff70261135a1336039ca1efacb510400dc06730b7bb13
data/README.md CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/miau.png)](http://badge.fury.io/rb/miau)
4
4
  [![GEM Downloads](https://img.shields.io/gem/dt/miau?color=168AFE&logo=ruby&logoColor=FE1616)](https://rubygems.org/gems/miau)
5
+ [![MIT License](https://img.shields.io/badge/license-MIT-blue.svg)](http://choosealicense.com/licenses/mit/)
5
6
 
6
7
  Miau (MIcro AUthorization) is a simple authorization gem for Rails
7
8
  inspired by Pundit and Banken.
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
data/lib/miau/run.rb CHANGED
@@ -36,12 +36,12 @@ module Miau
36
36
  end
37
37
 
38
38
  def raise_undef(policy, action)
39
- msg = "NotDefined policy <#{policy}> action <#{action}>"
39
+ msg = "policy <#{policy}> action <#{action}>"
40
40
  raise NotDefinedError, msg
41
41
  end
42
42
 
43
43
  def raise_authorize(controller, action)
44
- msg = "NotAuthorized controller <#{controller}> action <#{action}>"
44
+ msg = "controller <#{controller}> action <#{action}>"
45
45
  raise NotAuthorizedError, msg
46
46
  end
47
47
  end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "singleton"
4
+ require "yaml"
5
+
6
+ module Miau
7
+ class PolicyStorage
8
+ include Singleton
9
+
10
+ # Example @policies:
11
+ # {
12
+ # posts: {
13
+ # delete: :delete,
14
+ # remove: :delete
15
+ # },
16
+ # application: {
17
+ # admin: :check
18
+ # }
19
+ # }
20
+ attr_reader :policies
21
+ attr_reader :instances # e.g. { posts: PostsPolicy.new }
22
+
23
+ def initialize
24
+ reset
25
+ end
26
+
27
+ def reset
28
+ @policies = {}
29
+ @instances = {}
30
+ end
31
+
32
+ def add_policy(kls, action, meth)
33
+ kls = kls.to_sym
34
+ action = action.to_sym
35
+ @policies[kls] ||= {}
36
+ if @policies[kls][action]
37
+ raise OverwriteError, "Can't overwrite policy(#{kls}, #{action})"
38
+ end
39
+
40
+ if meth.is_a?(Array)
41
+ meths = [meth].flatten.collect(&:to_sym)
42
+ @policies[kls][action] = meths
43
+ else
44
+ @policies[kls][action] = meth.to_sym
45
+ end
46
+ end
47
+
48
+ def find_or_create_policy(klass)
49
+ res = @instances[klass]
50
+ return res unless res.nil?
51
+
52
+ name = "#{klass.to_s.camelcase}Policy"
53
+ return nil unless Object.const_defined?(name)
54
+
55
+ instances[klass] = name.constantize.new
56
+ end
57
+
58
+ def to_yaml
59
+ "# === @policies ===\n" + YAML.dump(@policies) +
60
+ "# === @instances ===\n" + YAML.dump(@instances)
61
+ end
62
+ end
63
+ end
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.6" # 2024-01-13
4
+ VERSION = "1.1.8" # 2024-04-29
5
+ # VERSION = "1.1.7" # 2024-04-24
6
+ # VERSION = "1.1.6" # 2024-01-13
5
7
  # VERSION = "1.1.1" # 2024-01-06
6
8
  # VERSION = "1.1.0" # 2024-01-06
7
9
  # VERSION = "1.0.3" # 2023-12-13
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Miau
4
+ VERSION = "1.1.7" # 2024-04-24
5
+ # VERSION = "1.1.6" # 2024-01-13
6
+ # VERSION = "1.1.1" # 2024-01-06
7
+ # VERSION = "1.1.0" # 2024-01-06
8
+ # VERSION = "1.0.3" # 2023-12-13
9
+ # VERSION = "1.0.2" # 2023-11-05
10
+ # VERSION = "1.0.1" # 2023-04-24
11
+ # VERSION = "1.0.0" # 2022-01-23
12
+ # VERSION = "0.1.2" # 2021-12-10
13
+ # VERSION = "0.1.1" # 2021-11-06
14
+ # VERSION = "0.1.0" # 2021-09-13
15
+ # VERSION = "0.0.2" # 2021-09-11
16
+ # VERSION = "0.0.1"
17
+ end
data/lib/miau.rb CHANGED
@@ -44,6 +44,11 @@ module Miau
44
44
  controller = params[:controller].to_sym
45
45
  action = params[:action].to_sym
46
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
+
47
52
  policy.user = miau_user
48
53
  policy.action = action
49
54
 
data/lib/miau.rb.bak ADDED
@@ -0,0 +1,72 @@
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
+ 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
59
+ end
60
+
61
+ def miau_user
62
+ current_user
63
+ end
64
+
65
+ def verify_authorized
66
+ raise AuthorizationNotPerformedError unless miau_authorization_performed?
67
+ end
68
+
69
+ def miau_authorization_performed?
70
+ !!@_miau_authorization_performed
71
+ end
72
+ 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.6
4
+ version: 1.1.8
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-13 00:00:00.000000000 Z
11
+ date: 2024-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: appraisal
14
+ name: listen
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
- type: :development
20
+ type: :runtime
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: combustion
28
+ name: appraisal
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - ">="
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: minitest
42
+ name: combustion
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="
@@ -53,7 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: ricecream
56
+ name: minitest
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - ">="
@@ -67,7 +67,7 @@ dependencies:
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
- name: sqlite3
70
+ name: ricecream
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
73
  - - ">="
@@ -86,49 +86,21 @@ description: |
86
86
  email: dittmar.krall@matiq.com
87
87
  executables: []
88
88
  extensions: []
89
- extra_rdoc_files: []
89
+ extra_rdoc_files:
90
+ - README.md
91
+ - MIT-LICENSE
90
92
  files:
91
- - ".github/workflows/rake.yml"
92
- - ".gitignore"
93
- - ".ruby-gemset"
94
- - ".ruby-version"
95
- - Appraisals
96
- - Gemfile
97
- - Gemfile.lock
98
- - LICENSE
93
+ - MIT-LICENSE
99
94
  - README.md
100
- - Rakefile
101
- - gemfiles/rails_6.1.gemfile
102
- - gemfiles/rails_7.0.gemfile
103
- - gemfiles/rails_7.1.gemfile
104
95
  - lib/miau.rb
96
+ - lib/miau.rb.bak
105
97
  - lib/miau/application_policy.rb
106
98
  - lib/miau/error.rb
107
99
  - lib/miau/run.rb
108
100
  - lib/miau/storage.rb
101
+ - lib/miau/storage.rb.bak
109
102
  - lib/miau/version.rb
110
- - miau.gemspec
111
- - test/authorization_test.rb
112
- - test/benchmark_test.rb
113
- - test/controller_test.rb
114
- - test/controllers/orders_controller_test.rb
115
- - test/internal/app/controllers/application_controller.rb
116
- - test/internal/app/controllers/orders_controller.rb
117
- - test/internal/app/controllers/posts_controller.rb
118
- - test/internal/app/models/application_record.rb
119
- - test/internal/app/models/order.rb
120
- - test/internal/app/models/post.rb
121
- - test/internal/app/policies/orders_policy.rb
122
- - test/internal/app/policies/posts_policy.rb
123
- - test/internal/app/views/orders/new.html.erb
124
- - test/internal/config/database.yml
125
- - test/internal/config/routes.rb
126
- - test/internal/db/migrate/20141016161801_create_orders.rb
127
- - test/internal/db/schema.rb
128
- - test/miau_test.rb
129
- - test/run_test.rb
130
- - test/storage_test.rb
131
- - test/test_helper.rb
103
+ - lib/miau/version.rb.bak
132
104
  homepage: https://github.com/matique/miau
133
105
  licenses:
134
106
  - MIT
@@ -148,7 +120,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
148
120
  - !ruby/object:Gem::Version
149
121
  version: '0'
150
122
  requirements: []
151
- rubygems_version: 3.5.3
123
+ rubygems_version: 3.5.9
152
124
  signing_key:
153
125
  specification_version: 4
154
126
  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, 3.3]
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,10 +0,0 @@
1
- /.bundle/
2
- /tmp/
3
- /coverage/
4
- /.watchr
5
-
6
- *.gem
7
- *.log
8
- *.lock
9
-
10
- **/db/test.sqlite*
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,12 +0,0 @@
1
- source "https://rubygems.org"
2
- gemspec
3
-
4
- gem "rails"
5
-
6
- group :test do
7
- gem "benchmark-ips"
8
- gem "observr"
9
- gem "ricecream"
10
- gem "simplecov", require: false
11
- gem "standard", require: false
12
- end