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.



96
97
98
99
100
101
102
103
104
# File 'decidim-core/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 'decidim-core/app/services/decidim/action_authorizer.rb', line 94

def statuses
  @statuses
end

Instance Method Details

#codesObject



126
127
128
# File 'decidim-core/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 'decidim-core/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 'decidim-core/app/services/decidim/action_authorizer.rb', line 106

def ok?
  return true if statuses.blank?

  statuses.all?(&:ok?)
end

#pending_authorizations_countObject



130
131
132
133
134
# File 'decidim-core/app/services/decidim/action_authorizer.rb', line 130

def pending_authorizations_count
  return 0 if @statuses.blank?

  @statuses.count
end

#single_authorization_required?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'decidim-core/app/services/decidim/action_authorizer.rb', line 136

def single_authorization_required?
  pending_authorizations_count == 1 && [:ok, :unauthorized].exclude?(global_code)
end

#status_for(handler_name) ⇒ Object



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

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