class DummyAuthorizationHandler::DummyActionAuthorizer

If you need custom authorization logic, you can implement your own action authorizer. In this case, it allows to set a list of valid postal codes for an authorization.

Attributes

allowed_postal_codes[R]
allowed_scope_id[R]

Public Instance Methods

authorize() click to toggle source

Overrides the parent class method, but it still uses it to keep the base behavior

Calls superclass method
# File lib/decidim/generators/app_templates/dummy_authorization_handler.rb, line 92
def authorize
  # Remove the additional setting from the options hash to avoid to be considered missing.
  @allowed_postal_codes ||= options.delete("allowed_postal_codes")&.split(/[\W,;]+/)
  @allowed_scope_id ||= options.delete("allowed_scope_id")&.to_i

  status_code, data = *super

  extra_explanations = []
  if allowed_postal_codes.present?
    # Does not authorize users with different postal codes
    status_code = :unauthorized if status_code == :ok && disallowed_user_postal_code

    # Adds an extra message for inform the user the additional restriction for this authorization
    if disallowed_user_postal_code
      if user_postal_code
        i18n_postal_codes_key = "extra_explanation.user_postal_codes"
        user_postal_code_params = { user_postal_code: user_postal_code }
      else
        i18n_postal_codes_key = "extra_explanation.postal_codes"
        user_postal_code_params = {}
      end

      extra_explanations << { key: i18n_postal_codes_key,
                              params: { scope: "decidim.verifications.dummy_authorization",
                                        count: allowed_postal_codes.count,
                                        postal_codes: allowed_postal_codes.join(", ") }.merge(user_postal_code_params) }
    end
  end

  if allowed_scope.present?
    # Does not authorize users with different scope
    status_code = :unauthorized if status_code == :ok && disallowed_user_user_scope

    # Adds an extra message to inform the user about additional restrictions for this authorization
    if disallowed_user_user_scope
      if user_scope_id
        i18n_scope_key = "extra_explanation.user_scope"
        user_scope_params = { user_scope_name: user_scope_name }
      else
        i18n_scope_key = "extra_explanation.scope"
        user_scope_params = {}
      end

      extra_explanations << { key: i18n_scope_key,
                              params: { scope: "decidim.verifications.dummy_authorization",
                                        scope_name: allowed_scope.name[I18n.locale.to_s] }.merge(user_scope_params) }
    end
  end

  data[:extra_explanation] = extra_explanations if extra_explanations.any?

  [status_code, data]
end
redirect_params() click to toggle source

Adds the list of allowed postal codes and scope to the redirect URL, to allow forms to inform about it

# File lib/decidim/generators/app_templates/dummy_authorization_handler.rb, line 147
def redirect_params
  { postal_codes: allowed_postal_codes&.join(","), scope: allowed_scope_id }.merge(user_metadata_params)
end

Private Instance Methods

allowed_scope() click to toggle source
# File lib/decidim/generators/app_templates/dummy_authorization_handler.rb, line 153
def allowed_scope
  @allowed_scope ||= Decidim::Scope.find(allowed_scope_id) if allowed_scope_id
end
disallowed_user_postal_code() click to toggle source
# File lib/decidim/generators/app_templates/dummy_authorization_handler.rb, line 181
def disallowed_user_postal_code
  return unless user_postal_code || allowed_postal_codes.present?

  !allowed_postal_codes.member?(user_postal_code)
end
disallowed_user_user_scope() click to toggle source
# File lib/decidim/generators/app_templates/dummy_authorization_handler.rb, line 171
def disallowed_user_user_scope
  return unless user_scope || allowed_scope.present?

  allowed_scope_id != user_scope_id
end
user_metadata_params() click to toggle source
# File lib/decidim/generators/app_templates/dummy_authorization_handler.rb, line 187
def user_metadata_params
  return {} unless authorization

  @user_metadata_params ||= begin
    user_metadata_params = {}
    user_metadata_params[:user_scope_name] = user_scope.name[I18n.locale.to_s] if user_scope

    user_metadata_params[:user_postal_code] = authorization.metadata["postal_code"] if authorization.metadata["postal_code"].present?

    user_metadata_params
  end
end
user_postal_code() click to toggle source
# File lib/decidim/generators/app_templates/dummy_authorization_handler.rb, line 177
def user_postal_code
  @user_postal_code ||= authorization.metadata["postal_code"] if authorization && authorization.metadata
end
user_scope() click to toggle source
# File lib/decidim/generators/app_templates/dummy_authorization_handler.rb, line 157
def user_scope
  @user_scope ||= Decidim::Scope.find(user_scope_id) if user_scope_id
end
user_scope_id() click to toggle source
# File lib/decidim/generators/app_templates/dummy_authorization_handler.rb, line 161
def user_scope_id
  return unless authorization

  @user_scope_id ||= authorization.metadata["scope_id"]&.to_i
end
user_scope_name() click to toggle source
# File lib/decidim/generators/app_templates/dummy_authorization_handler.rb, line 167
def user_scope_name
  @user_scope_name ||= user_scope.name[I18n.locale.to_s] if authorization && user_scope
end