Class: Decidim::ActionAuthorizer::AuthorizationStatusCollection

Inherits:
Object
  • Object
show all
Defined in:
app/services/decidim/action_authorizer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(authorization_handlers, user, component, resource) ⇒ AuthorizationStatusCollection

Returns a new instance of AuthorizationStatusCollection.



96
97
98
99
100
101
102
103
104
# File 'app/services/decidim/action_authorizer.rb', line 96

def initialize(authorization_handlers, user, component, resource)
  @authorization_handlers = authorization_handlers
  @statuses = authorization_handlers&.map do |name, opts|
    handler = Verifications::Adapter.from_element(name)
    authorization = user ? Verifications::Authorizations.new(organization: user.organization, user:, name:).first : nil
    status_code, data = handler.authorize(authorization, opts["options"], component, resource)
    AuthorizationStatus.new(status_code, handler, data)
  end
end

Instance Attribute Details

#statusesObject (readonly)

Returns the value of attribute statuses.



94
95
96
# File 'app/services/decidim/action_authorizer.rb', line 94

def statuses
  @statuses
end

Instance Method Details

#codesObject



126
127
128
# File 'app/services/decidim/action_authorizer.rb', line 126

def codes
  @codes ||= statuses.map(&:code)
end

#global_codeObject



112
113
114
115
116
117
118
119
120
# File 'app/services/decidim/action_authorizer.rb', line 112

def global_code
  return :ok if ok?

  [:unauthorized, :pending].each do |code|
    return code if statuses.any? { |status| status.code == code }
  end

  false
end

#ok?Boolean

Returns:

  • (Boolean)


106
107
108
109
110
# File 'app/services/decidim/action_authorizer.rb', line 106

def ok?
  return true if statuses.blank?

  statuses.all?(&:ok?)
end

#status_for(handler_name) ⇒ Object



122
123
124
# File 'app/services/decidim/action_authorizer.rb', line 122

def status_for(handler_name)
  statuses.find { |status| status.handler_name == handler_name }
end