Class: Decidim::ActionAuthorizer::AuthorizationStatusCollection

Inherits:
Object
  • Object
show all
Defined in:
decidim-core/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.


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

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: user, name: 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


93
94
95
# File 'decidim-core/app/services/decidim/action_authorizer.rb', line 93

def statuses
  @statuses
end

Instance Method Details

#codesObject


125
126
127
# File 'decidim-core/app/services/decidim/action_authorizer.rb', line 125

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

#global_codeObject


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

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)

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

def ok?
  return true if statuses.blank?

  statuses.all?(&:ok?)
end

#status_for(handler_name) ⇒ Object


121
122
123
# File 'decidim-core/app/services/decidim/action_authorizer.rb', line 121

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