pundit 2.3.2 → 2.5.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 +4 -4
- data/.github/ISSUE_TEMPLATE/bug_report.md +20 -0
- data/.github/ISSUE_TEMPLATE/feature_request.md +26 -0
- data/.github/PULL_REQUEST_TEMPLATE/gem_release_template.md +4 -4
- data/.github/workflows/main.yml +92 -52
- data/.github/workflows/push_gem.yml +4 -4
- data/.rubocop.yml +18 -8
- data/.rubocop_ignore_git.yml +7 -0
- data/.yardopts +1 -1
- data/CHANGELOG.md +68 -37
- data/CODE_OF_CONDUCT.md +1 -1
- data/CONTRIBUTING.md +1 -0
- data/Gemfile +22 -2
- data/README.md +88 -15
- data/Rakefile +1 -0
- data/lib/generators/pundit/install/install_generator.rb +3 -1
- data/lib/generators/pundit/policy/policy_generator.rb +3 -1
- data/lib/generators/rspec/policy_generator.rb +4 -1
- data/lib/generators/test_unit/policy_generator.rb +4 -1
- data/lib/pundit/authorization.rb +152 -77
- data/lib/pundit/cache_store/legacy_store.rb +7 -0
- data/lib/pundit/cache_store/null_store.rb +9 -0
- data/lib/pundit/cache_store.rb +22 -0
- data/lib/pundit/context.rb +76 -26
- data/lib/pundit/policy_finder.rb +22 -1
- data/lib/pundit/railtie.rb +19 -0
- data/lib/pundit/rspec.rb +90 -7
- data/lib/pundit/version.rb +2 -1
- data/lib/pundit.rb +43 -15
- data/pundit.gemspec +8 -12
- data/spec/authorization_spec.rb +61 -4
- data/spec/policies/post_policy_spec.rb +27 -0
- data/spec/policy_finder_spec.rb +5 -1
- data/spec/pundit/helper_spec.rb +18 -0
- data/spec/pundit_spec.rb +58 -15
- data/spec/rspec_dsl_spec.rb +81 -0
- data/spec/simple_cov_check_action_formatter.rb +79 -0
- data/spec/spec_helper.rb +22 -339
- data/spec/support/lib/controller.rb +38 -0
- data/spec/support/lib/custom_cache.rb +19 -0
- data/spec/support/lib/instance_tracking.rb +20 -0
- data/spec/support/models/article.rb +4 -0
- data/spec/support/models/article_tag.rb +7 -0
- data/spec/support/models/artificial_blog.rb +7 -0
- data/spec/support/models/blog.rb +4 -0
- data/spec/support/models/comment.rb +5 -0
- data/spec/support/models/comment_four_five_six.rb +5 -0
- data/spec/support/models/comment_scope.rb +13 -0
- data/spec/support/models/comments_relation.rb +15 -0
- data/spec/support/models/customer/post.rb +11 -0
- data/spec/support/models/default_scope_contains_error.rb +5 -0
- data/spec/support/models/dummy_current_user.rb +7 -0
- data/spec/support/models/foo.rb +4 -0
- data/spec/support/models/post.rb +25 -0
- data/spec/support/models/post_four_five_six.rb +9 -0
- data/spec/support/models/project_one_two_three/avatar_four_five_six.rb +7 -0
- data/spec/support/models/project_one_two_three/tag_four_five_six.rb +11 -0
- data/spec/support/models/wiki.rb +4 -0
- data/spec/support/policies/article_tag_other_name_policy.rb +13 -0
- data/spec/support/policies/base_policy.rb +23 -0
- data/spec/support/policies/blog_policy.rb +5 -0
- data/spec/support/policies/comment_policy.rb +11 -0
- data/spec/support/policies/criteria_policy.rb +5 -0
- data/spec/support/policies/default_scope_contains_error_policy.rb +10 -0
- data/spec/support/policies/denier_policy.rb +7 -0
- data/spec/support/policies/dummy_current_user_policy.rb +9 -0
- data/spec/support/policies/nil_class_policy.rb +17 -0
- data/spec/support/policies/post_policy.rb +36 -0
- data/spec/support/policies/project/admin/comment_policy.rb +15 -0
- data/spec/support/policies/project/comment_policy.rb +17 -0
- data/spec/support/policies/project/criteria_policy.rb +7 -0
- data/spec/support/policies/project/post_policy.rb +13 -0
- data/spec/support/policies/project_one_two_three/avatar_four_five_six_policy.rb +6 -0
- data/spec/support/policies/project_one_two_three/comment_four_five_six_policy.rb +6 -0
- data/spec/support/policies/project_one_two_three/criteria_four_five_six_policy.rb +6 -0
- data/spec/support/policies/project_one_two_three/post_four_five_six_policy.rb +6 -0
- data/spec/support/policies/project_one_two_three/tag_four_five_six_policy.rb +6 -0
- data/spec/support/policies/publication_policy.rb +13 -0
- data/spec/support/policies/wiki_policy.rb +8 -0
- metadata +66 -158
- /data/.github/{PULL_REQUEST_TEMPLATE/pull_request_template.md → pull_request_template.md} +0 -0
- /data/lib/generators/pundit/install/templates/{application_policy.rb → application_policy.rb.tt} +0 -0
- /data/lib/generators/pundit/policy/templates/{policy.rb → policy.rb.tt} +0 -0
- /data/lib/generators/rspec/templates/{policy_spec.rb → policy_spec.rb.tt} +0 -0
- /data/lib/generators/test_unit/templates/{policy_test.rb → policy_test.rb.tt} +0 -0
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Controller
|
4
|
+
attr_accessor :current_user
|
5
|
+
attr_reader :action_name, :params
|
6
|
+
|
7
|
+
class View
|
8
|
+
def initialize(controller)
|
9
|
+
@controller = controller
|
10
|
+
end
|
11
|
+
|
12
|
+
attr_reader :controller
|
13
|
+
end
|
14
|
+
|
15
|
+
class << self
|
16
|
+
def helper(mod)
|
17
|
+
View.include(mod)
|
18
|
+
end
|
19
|
+
|
20
|
+
def helper_method(method)
|
21
|
+
View.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
22
|
+
def #{method}(*args, **kwargs, &block)
|
23
|
+
controller.send(:#{method}, *args, **kwargs, &block)
|
24
|
+
end
|
25
|
+
RUBY
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
include Pundit::Authorization
|
30
|
+
# Mark protected methods public so they may be called in test
|
31
|
+
public(*Pundit::Authorization.protected_instance_methods)
|
32
|
+
|
33
|
+
def initialize(current_user, action_name, params)
|
34
|
+
@current_user = current_user
|
35
|
+
@action_name = action_name
|
36
|
+
@params = params
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module InstanceTracking
|
4
|
+
module ClassMethods
|
5
|
+
def instances
|
6
|
+
@instances || 0
|
7
|
+
end
|
8
|
+
|
9
|
+
attr_writer :instances
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.prepended(other)
|
13
|
+
other.extend(ClassMethods)
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(*args, **kwargs, &block)
|
17
|
+
self.class.instances += 1
|
18
|
+
super(*args, **kwargs, &block)
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Post
|
4
|
+
def initialize(user = nil)
|
5
|
+
@user = user
|
6
|
+
end
|
7
|
+
|
8
|
+
attr_reader :user
|
9
|
+
|
10
|
+
def self.published
|
11
|
+
:published
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.read
|
15
|
+
:read
|
16
|
+
end
|
17
|
+
|
18
|
+
def to_s
|
19
|
+
"Post"
|
20
|
+
end
|
21
|
+
|
22
|
+
def inspect
|
23
|
+
"#<Post>"
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class BasePolicy
|
4
|
+
prepend InstanceTracking
|
5
|
+
|
6
|
+
class BaseScope
|
7
|
+
prepend InstanceTracking
|
8
|
+
|
9
|
+
def initialize(user, scope)
|
10
|
+
@user = user
|
11
|
+
@scope = scope
|
12
|
+
end
|
13
|
+
|
14
|
+
attr_reader :user, :scope
|
15
|
+
end
|
16
|
+
|
17
|
+
def initialize(user, record)
|
18
|
+
@user = user
|
19
|
+
@record = record
|
20
|
+
end
|
21
|
+
|
22
|
+
attr_reader :user, :record
|
23
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class PostPolicy < BasePolicy
|
4
|
+
class Scope < BaseScope
|
5
|
+
def resolve
|
6
|
+
scope.published
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
alias post record
|
11
|
+
|
12
|
+
def update?
|
13
|
+
post.user == user
|
14
|
+
end
|
15
|
+
alias edit? update?
|
16
|
+
|
17
|
+
def destroy?
|
18
|
+
false
|
19
|
+
end
|
20
|
+
|
21
|
+
def show?
|
22
|
+
true
|
23
|
+
end
|
24
|
+
|
25
|
+
def permitted_attributes
|
26
|
+
if post.user == user
|
27
|
+
%i[title votes]
|
28
|
+
else
|
29
|
+
[:votes]
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def permitted_attributes_for_revise
|
34
|
+
[:body]
|
35
|
+
end
|
36
|
+
end
|