Module: ActiveStorageAuthorization

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/active_storage_authorization.rb

Overview

This authorizes all ActiveStorage downloads This is included automatically by the engine It can be disabled by setting config.authorize_active_storage = false in config/initializers/effective_storage.rb

There are 3 ways to add permissions: 1.) can?(:show, resource) 2.) can?(:show, ActionText::RichText) { |text| … } 3.) can?(:show, ActiveStorage::Attachment) { |attachment| … }

The :show and :edit will both work.

Constant Summary collapse

AUTHORIZED_EFFECTIVE_DOWNLOADS =
[
  'Effective::CarouselItem', 
  'Effective::PageBanner', 
  'Effective::PageSection', 
  'Effective::Permalink'
]

Instance Method Summary collapse

Instance Method Details

#authorize_active_storage_download!Object

Authorize ActiveStorage DiskController downloads Used for local storage



30
31
32
33
# File 'app/models/concerns/active_storage_authorization.rb', line 30

def authorize_active_storage_download!
  @blob || set_download_blob()
  authorize_active_storage!
end

#authorize_active_storage_redirect!Object

Authorize ActiveStorage Blob and Representation redirects Used for amazon storage



37
38
39
40
# File 'app/models/concerns/active_storage_authorization.rb', line 37

def authorize_active_storage_redirect!
  @blob || set_blob()
  authorize_active_storage!
end

#unauthorized_active_storage_request(exception) ⇒ Object

Send an ExceptionNotification email with the unauthorized details This is not visible to users



44
45
46
47
48
49
50
51
52
# File 'app/models/concerns/active_storage_authorization.rb', line 44

def unauthorized_active_storage_request(exception)

  if defined?(ExceptionNotifier)
    data = { 'current_user_id': current_user&.id || 'none' }.merge(@blob&.attributes || {})
    ExceptionNotifier.notify_exception(exception, env: request.env, data: data)
  else
    raise(exception)
  end
end