Class: Jav::Services::AuthorizationService
- Inherits:
-
Object
- Object
- Jav::Services::AuthorizationService
- Defined in:
- lib/jav/services/authorization_service.rb
Instance Attribute Summary collapse
-
#policy_class ⇒ Object
Returns the value of attribute policy_class.
-
#record ⇒ Object
Returns the value of attribute record.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
- .apply_policy(user, model, policy_class: nil) ⇒ Object
- .authorize(user, record, action, policy_class: nil, **args) ⇒ Object
- .authorize_action(user, record, action, policy_class: nil, **args) ⇒ Object
- .client ⇒ Object
- .defined_methods(user, record, policy_class: nil, **args) ⇒ Object
- .nil_client ⇒ Object
- .pundit_client ⇒ Object
- .skip_authorization ⇒ Object
Instance Method Summary collapse
- #apply_policy(model) ⇒ Object
- #authorize_action(action, **args) ⇒ Object
- #defined_methods(model, **args) ⇒ Object
-
#has_action_method?(method, **args) ⇒ Boolean
Check the received method to see if the user overrode it in their config and then checks if it's present on the policy.
- #has_method?(method, **args) ⇒ Boolean
-
#initialize(user = nil, record = nil, policy_class: nil) ⇒ AuthorizationService
constructor
A new instance of AuthorizationService.
- #set_record(record) ⇒ Object
Constructor Details
#initialize(user = nil, record = nil, policy_class: nil) ⇒ AuthorizationService
Returns a new instance of AuthorizationService.
96 97 98 99 100 |
# File 'lib/jav/services/authorization_service.rb', line 96 def initialize(user = nil, record = nil, policy_class: nil) @user = user @record = record @policy_class = policy_class || self.class.client.policy(user, record)&.class end |
Instance Attribute Details
#policy_class ⇒ Object
Returns the value of attribute policy_class.
4 5 6 |
# File 'lib/jav/services/authorization_service.rb', line 4 def policy_class @policy_class end |
#record ⇒ Object
Returns the value of attribute record.
4 5 6 |
# File 'lib/jav/services/authorization_service.rb', line 4 def record @record end |
#user ⇒ Object
Returns the value of attribute user.
4 5 6 |
# File 'lib/jav/services/authorization_service.rb', line 4 def user @user end |
Class Method Details
.apply_policy(user, model, policy_class: nil) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/jav/services/authorization_service.rb', line 57 def apply_policy(user, model, policy_class: nil) client.apply_policy(user, model, policy_class: policy_class) rescue NoPolicyError => error return model unless Jav.configuration.raise_error_on_missing_policy raise error end |
.authorize(user, record, action, policy_class: nil, **args) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/jav/services/authorization_service.rb', line 26 def (user, record, action, policy_class: nil, **args) client. user, record, action, policy_class: policy_class true rescue NoPolicyError => error # By default, Jav allows anything if you don't have a policy present. return true unless Jav.configuration.raise_error_on_missing_policy raise error rescue StandardError => error raise error unless args[:raise_exception] == false false end |
.authorize_action(user, record, action, policy_class: nil, **args) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jav/services/authorization_service.rb', line 41 def (user, record, action, policy_class: nil, **args) action = Jav.configuration..stringify_keys[action.to_s] || action # If no action passed we should raise error if the user wants that. # If not, just allow it. if action.nil? raise NoPolicyError, "Policy method is missing" if Jav.configuration.raise_error_on_missing_policy return true end # Add the question mark if it's missing action = "#{action}?" unless action.end_with? "?" (user, record, action, policy_class: policy_class, **args) end |
.client ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/jav/services/authorization_service.rb', line 7 def client client = Jav.configuration. klass = case client when nil nil_client when :pundit pundit_client else if client.is_a?(String) client.safe_constantize else client end end klass.new end |
.defined_methods(user, record, policy_class: nil, **args) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/jav/services/authorization_service.rb', line 69 def defined_methods(user, record, policy_class: nil, **args) return client.policy!(user, record).methods if policy_class.nil? # I'm aware this will not raise a Pundit error. # Should the policy not exist, it will however raise an uninitialized constant error, which is probably what we want when specifying a custom policy policy_class.new(user, record).methods rescue NoPolicyError => error return [] unless Jav.configuration.raise_error_on_missing_policy raise error rescue StandardError => error raise error unless args[:raise_exception] == false [] end |
.nil_client ⇒ Object
91 92 93 |
# File 'lib/jav/services/authorization_service.rb', line 91 def nil_client Jav::Services::AuthorizationClients::NilClient end |
.pundit_client ⇒ Object
85 86 87 88 89 |
# File 'lib/jav/services/authorization_service.rb', line 85 def pundit_client raise Jav::MissingGemError, "Please add `gem 'pundit'` to your Gemfile." unless defined?(Pundit) Jav::Services::AuthorizationClients::PunditClient end |
Instance Method Details
#apply_policy(model) ⇒ Object
112 113 114 |
# File 'lib/jav/services/authorization_service.rb', line 112 def apply_policy(model) self.class.apply_policy(user, model, policy_class: policy_class) end |
#authorize_action(action, **args) ⇒ Object
108 109 110 |
# File 'lib/jav/services/authorization_service.rb', line 108 def (action, **args) self.class.(user, args[:record] || record, action, policy_class: policy_class, **args) end |
#defined_methods(model, **args) ⇒ Object
116 117 118 |
# File 'lib/jav/services/authorization_service.rb', line 116 def defined_methods(model, **args) self.class.defined_methods(user, model, policy_class: policy_class, **args) end |
#has_action_method?(method, **args) ⇒ Boolean
Check the received method to see if the user overrode it in their config and then checks if it's present on the policy.
126 127 128 129 130 |
# File 'lib/jav/services/authorization_service.rb', line 126 def has_action_method?(method, **args) method = Jav.configuration..stringify_keys[method.to_s] || method has_method? method, **args end |
#has_method?(method, **args) ⇒ Boolean
120 121 122 123 |
# File 'lib/jav/services/authorization_service.rb', line 120 def has_method?(method, **args) method = "#{method}?" unless method.to_s.end_with? "?" defined_methods(args[:record] || record, **args).include? method.to_sym end |
#set_record(record) ⇒ Object
102 103 104 105 106 |
# File 'lib/jav/services/authorization_service.rb', line 102 def set_record(record) @record = record self end |