Class: Avo::Services::AuthorizationService
- Inherits:
-
Object
- Object
- Avo::Services::AuthorizationService
- Defined in:
- lib/avo/services/authorization_service.rb
Instance Attribute Summary collapse
-
#record ⇒ Object
Returns the value of attribute record.
-
#user ⇒ Object
Returns the value of attribute user.
Class Method Summary collapse
- .apply_policy(user, model) ⇒ Object
- .authorize(user, record, action, **args) ⇒ Object
- .authorize_action(user, record, action, **args) ⇒ Object
- .authorized_methods(user, record) ⇒ Object
- .defined_methods(user, record, **args) ⇒ Object
- .get_policy(user, record) ⇒ Object
- .skip_authorization ⇒ Object
Instance Method Summary collapse
- #apply_policy(model) ⇒ Object
- #authorize(action, **args) ⇒ Object
- #authorize_action(action, **args) ⇒ Object
- #defined_methods(model, **args) ⇒ Object
- #has_method?(method, **args) ⇒ Boolean
-
#initialize(user = nil, record = nil) ⇒ AuthorizationService
constructor
A new instance of AuthorizationService.
- #set_record(record) ⇒ Object
- #set_user(user) ⇒ Object
Constructor Details
#initialize(user = nil, record = nil) ⇒ AuthorizationService
Returns a new instance of AuthorizationService.
90 91 92 93 |
# File 'lib/avo/services/authorization_service.rb', line 90 def initialize(user = nil, record = nil) @user = user @record = record end |
Instance Attribute Details
#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) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/avo/services/authorization_service.rb', line 48 def apply_policy(user, model) return model if return model if user.nil? begin Pundit.policy_scope! user, model rescue Pundit::NotDefinedError => e return model unless Avo.configuration.raise_error_on_missing_policy raise e end end |
.authorize(user, record, action, **args) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/avo/services/authorization_service.rb', line 8 def (user, record, action, **args) return true if return true if user.nil? begin if Pundit.policy user, record Pundit. user, record, action end true rescue Pundit::NotDefinedError => e return false unless Avo.configuration.raise_error_on_missing_policy raise e rescue => error if args[:raise_exception] == false false else raise error end end end |
.authorize_action(user, record, action, **args) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/avo/services/authorization_service.rb', line 31 def (user, record, action, **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 Pundit::NotDefinedError.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, **args end |
.authorized_methods(user, record) ⇒ Object
65 66 67 68 69 |
# File 'lib/avo/services/authorization_service.rb', line 65 def (user, record) [:new, :edit, :update, :show, :destroy].map do |method| [method, (user, record, Avo.configuration.[method])] end.to_h end |
.defined_methods(user, record, **args) ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/avo/services/authorization_service.rb', line 75 def defined_methods(user, record, **args) Pundit.policy!(user, record).methods rescue Pundit::NotDefinedError => e return [] unless Avo.configuration.raise_error_on_missing_policy raise e rescue => error if args[:raise_exception] == false [] else raise error end end |
.get_policy(user, record) ⇒ Object
71 72 73 |
# File 'lib/avo/services/authorization_service.rb', line 71 def get_policy(user, record) Pundit.policy user, record end |
Instance Method Details
#apply_policy(model) ⇒ Object
115 116 117 |
# File 'lib/avo/services/authorization_service.rb', line 115 def apply_policy(model) self.class.apply_policy(user, model) end |
#authorize(action, **args) ⇒ Object
95 96 97 |
# File 'lib/avo/services/authorization_service.rb', line 95 def (action, **args) self.class.(user, record, action, **args) end |
#authorize_action(action, **args) ⇒ Object
111 112 113 |
# File 'lib/avo/services/authorization_service.rb', line 111 def (action, **args) self.class.(user, record, action, **args) end |
#defined_methods(model, **args) ⇒ Object
119 120 121 |
# File 'lib/avo/services/authorization_service.rb', line 119 def defined_methods(model, **args) self.class.defined_methods(user, model, **args) end |
#has_method?(method, **args) ⇒ Boolean
123 124 125 |
# File 'lib/avo/services/authorization_service.rb', line 123 def has_method?(method, **args) defined_methods(record, **args).include? method.to_sym end |
#set_record(record) ⇒ Object
99 100 101 102 103 |
# File 'lib/avo/services/authorization_service.rb', line 99 def set_record(record) @record = record self end |
#set_user(user) ⇒ Object
105 106 107 108 109 |
# File 'lib/avo/services/authorization_service.rb', line 105 def set_user(user) @user = user self end |