Class: Avo::Services::AuthorizationService

Inherits:
Object
  • Object
show all
Defined in:
lib/avo/services/authorization_service.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#recordObject

Returns the value of attribute record.



5
6
7
# File 'lib/avo/services/authorization_service.rb', line 5

def record
  @record
end

#userObject

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 skip_authorization
  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 authorize(user, record, action, **args)
  return true if skip_authorization
  return true if user.nil?

  begin
    if Pundit.policy user, record
      Pundit.authorize 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 authorize_action(user, record, action, **args)
  action = Avo.configuration.authorization_methods.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? "?"

  authorize 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 authorized_methods(user, record)
  [:new, :edit, :update, :show, :destroy].map do |method|
    [method, authorize(user, record, Avo.configuration.authorization_methods[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

.skip_authorizationObject



61
62
63
# File 'lib/avo/services/authorization_service.rb', line 61

def skip_authorization
  Avo::App.license.lacks_with_trial :authorization
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 authorize(action, **args)
  self.class.authorize(user, record, action, **args)
end

#authorize_action(action, **args) ⇒ Object



111
112
113
# File 'lib/avo/services/authorization_service.rb', line 111

def authorize_action(action, **args)
  self.class.authorize_action(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

Returns:

  • (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