Class: Avo::Services::AuthorizationService
- Inherits:
-
Object
- Object
- Avo::Services::AuthorizationService
- Defined in:
- lib/avo/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
- .configuration_client ⇒ Object
- .default_client ⇒ Object
- .defined_methods(user, record, policy_class: nil, **args) ⇒ Object
- .skip_authorization ⇒ Object
Instance Method Summary collapse
- #apply_policy(model) ⇒ Object
- #authorize_action(action, **args) ⇒ Object
- #defined_methods(model, **args) ⇒ Object
- #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.
98 99 100 101 102 |
# File 'lib/avo/services/authorization_service.rb', line 98 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.
6 7 8 |
# File 'lib/avo/services/authorization_service.rb', line 6 def policy_class @policy_class end |
#record ⇒ Object
Returns the value of attribute record.
5 6 7 |
# File 'lib/avo/services/authorization_service.rb', line 5 def record @record end |
#user ⇒ Object
Returns the value of attribute user.
4 5 6 |
# File 'lib/avo/services/authorization_service.rb', line 4 def user @user end |
Class Method Details
.apply_policy(user, model, policy_class: nil) ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/avo/services/authorization_service.rb', line 49 def apply_policy(user, model, policy_class: nil) return model if || user.nil? client.apply_policy(user, model, policy_class: policy_class) rescue NoPolicyError => error return model unless Avo.configuration.raise_error_on_missing_policy raise error end |
.authorize(user, record, action, policy_class: nil, **args) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/avo/services/authorization_service.rb', line 13 def (user, record, action, policy_class: nil, **args) return true if return true if user.nil? client. user, record, action, policy_class: policy_class true rescue NoPolicyError => error # By default, Avo allows anything if you don't have a policy present. return true unless Avo.configuration.raise_error_on_missing_policy raise error rescue => error if args[:raise_exception] == false false else raise error end end |
.authorize_action(user, record, action, policy_class: nil, **args) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/avo/services/authorization_service.rb', line 33 def (user, record, action, policy_class: nil, **args) action = Avo.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.new "Policy method is missing" if Avo.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
9 10 11 |
# File 'lib/avo/services/authorization_service.rb', line 9 def client (configuration_client || default_client).new end |
.configuration_client ⇒ Object
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/avo/services/authorization_service.rb', line 81 def configuration_client client = Avo.configuration. return if client.blank? if client.is_a?(String) client.safe_constantize else client end end |
.default_client ⇒ Object
93 94 95 |
# File 'lib/avo/services/authorization_service.rb', line 93 def default_client Avo::Services::AuthorizationClients::PunditClient end |
.defined_methods(user, record, policy_class: nil, **args) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/avo/services/authorization_service.rb', line 63 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 Avo.configuration.raise_error_on_missing_policy raise error rescue => error if args[:raise_exception] == false [] else raise error end end |
Instance Method Details
#apply_policy(model) ⇒ Object
114 115 116 |
# File 'lib/avo/services/authorization_service.rb', line 114 def apply_policy(model) self.class.apply_policy(user, model, policy_class: policy_class) end |
#authorize_action(action, **args) ⇒ Object
110 111 112 |
# File 'lib/avo/services/authorization_service.rb', line 110 def (action, **args) self.class.(user, record, action, policy_class: policy_class, **args) end |
#defined_methods(model, **args) ⇒ Object
118 119 120 |
# File 'lib/avo/services/authorization_service.rb', line 118 def defined_methods(model, **args) self.class.defined_methods(user, model, policy_class: policy_class, **args) end |
#has_method?(method, **args) ⇒ Boolean
122 123 124 |
# File 'lib/avo/services/authorization_service.rb', line 122 def has_method?(method, **args) defined_methods(record, **args).include? method.to_sym end |
#set_record(record) ⇒ Object
104 105 106 107 108 |
# File 'lib/avo/services/authorization_service.rb', line 104 def set_record(record) @record = record self end |