kojac 0.13.0 → 0.15.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,70 +0,0 @@
1
- #require "pundit/version"
2
- require "pundit/policy_finder"
3
- #require "active_support/concern"
4
- #require "active_support/core_ext/string/inflections"
5
- #require "active_support/core_ext/object/blank"
6
-
7
- module Kojac
8
- #class NotAuthorizedError < StandardError; end
9
- #class NotDefinedError < StandardError; end
10
-
11
- extend ActiveSupport::Concern
12
-
13
- class << self
14
- def policy_scope(user, scope, op=nil)
15
- policy = Pundit::PolicyFinder.new(scope).scope
16
- policy.new(user, scope, op).resolve if policy
17
- end
18
-
19
- def policy_scope!(user, scope, op=nil)
20
- Pundit::PolicyFinder.new(scope).scope!.new(user, scope, op).resolve
21
- end
22
-
23
- def policy(user, record, op=nil)
24
- scope = Pundit::PolicyFinder.new(record).policy
25
- scope.new(user, record, op) if scope
26
- end
27
-
28
- def policy!(user, record, op=nil)
29
- Pundit::PolicyFinder.new(record).policy!.new(user, record, op)
30
- end
31
- end
32
-
33
- included do
34
- if respond_to?(:helper_method)
35
- helper_method :policy_scope
36
- helper_method :policy
37
- end
38
- if respond_to?(:hide_action)
39
- hide_action :authorize
40
- hide_action :verify_authorized
41
- hide_action :verify_policy_scoped
42
- end
43
- end
44
-
45
- def verify_authorized
46
- raise NotAuthorizedError unless @_policy_authorized
47
- end
48
-
49
- def verify_policy_scoped
50
- raise NotAuthorizedError unless @_policy_scoped
51
- end
52
-
53
- def authorize(record, query=nil)
54
- query ||= params[:action].to_s + "?"
55
- @_policy_authorized = true
56
- unless policy(record).public_send(query)
57
- raise NotAuthorizedError, "not allowed to #{query} this #{record}"
58
- end
59
- true
60
- end
61
-
62
- def policy_scope(scope)
63
- @_policy_scoped = true
64
- Pundit::Pundit.policy_scope!(current_user, scope, op)
65
- end
66
-
67
- def policy(record)
68
- Pundit::Pundit.policy!(current_user, record, op)
69
- end
70
- end