Effective Storage
Adds an authentication layer to the Active Storage downloads controller.
Authorizes the user downloading each file and raises an exception for unauthorized requests.
Adds an admin screen to browse Active Storage attachments and mark them as inherited or public.
Getting Started
This requires Rails 6+ and Twitter Bootstrap 4 and just works with Devise.
Please first install the effective_datatables gem.
Please download and install the Twitter Bootstrap4
Add to your Gemfile:
gem 'haml-rails' # or try using gem 'hamlit-rails'
gem 'effective_storage'Run the bundle command to install it:
bundle installThen run the generator:
rails generate effective_storage:installThe generator will install an initializer which describes all configuration options and creates a database migration.
If you want to tweak the table names, manually adjust both the configuration file and the migration now.
Then migrate the database:
rake db:migrateAdd a link to the admin menu:
- if can?(:admin, :effective_storage) && can?(:index, ActiveStorage::Attachment)
= nav_link_to 'Storage', effective_storage.admin_storage_pathConfiguration
Authorization
All authorization checks are handled via the effective_resources gem found in the config/initializers/effective_resources.rb file.
Permissions
The permissions you actually want to define are as follows (using CanCan):
# Allow anyone to download a public file
can(:show, ActiveStorage::Blob) { |blob| blob.permission_public? }
if user.persisted?
end
if user.admin?
# This allows the admin to download any file
can :show, ActiveStorage::Blob
# Allows them to see the index screen
can :admin, :effective_storage
can :index, ActiveStorage::Blob
# Admin screen actions
can(:mark_inherited, ActiveStorage::Blob) { |blob| !blob.permission_inherited? }
can(:mark_public, ActiveStorage::Blob) { |blob| !blob.permission_public? }
endLicense
MIT License. Copyright Code and Effect Inc.
Testing
Run tests by:
rails testContributing
- Fork it
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Bonus points for test coverage
- Create new Pull Request