No release in over a year
Warning, this gem is just a test for now has not a good test coverage. Feel free to open an issue for any problem, and please wait for a stable version before any integration. And it is a simple gem to handle authorizations for active admin
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 3.0.0
~> 0.14.0
~> 3.0
= 1.24.0

Runtime

>= 2.13.0
~> 2.2
 Project Readme

ActiveAdminBasicAuthorizationAdapter

This repository is for now in test mode, should maybe implement more tests and be updated with the last updates of ActiveADmin::AuthorizationAdapter

Installation

Add the following line to your gemfile

gem 'active_admin_basic_authorization_adapter'

Then

bundle install

OR

gem install active_admin_basic_authorization_adapter

Configuration

To create the default authorization file, run

rails generate default_basic_admin_authorization

Then add to your active admin initializer:

# IN config/initializers/active_admin
config.authorization_adapter = ActiveAdminBasicAuthorizationAdapter

Authorizations

Active Admin authorization adapter has 4 methods for the CRUD:

:read, :create, :update, :destroy.

These authorizations handle the following default active admin controller methods:

read: #index, #show;
create: #new, #create;
update: #edit, #update;
destroy: #destroy

You can also define new methods for your member_actions with the following code:

module ActiveAdmin
  module Authorizations
    class MyAdminPageOrModelAuthorization < DefaultAuthorization
      # ...

      def my_member_action
        # YOUR AUTHORIZATION LOGIC HERE
      end
    end
  end
end

Authorizations generator

rails generate basic_admin_authorization Thing

Will create the following file app/admin/authorizations/thing_authorization.rb

With the following code:

# frozen_string_literal: true

# require_relative './default_authorization'

module ActiveAdmin
  module Authorizations
    class MyAdminPageOrModelAuthorization < DefaultAuthorization
      def read
        false
      end

      def create
        false
      end

      def update
        false
      end

      def destroy
        false
      end

      # Add your custom member_actions here
    end
  end
end

You can also pass the admin_type option:

rails generate basic_admin_authorization Thing --admin_type true
# frozen_string_literal: true
# require_relative './default_authorization'

module ActiveAdmin
  module Authorizations
    class MyAdminPageOrModelAuthorization < DefaultAuthorization
      def read
        true
      end

      def create
        true
      end

      def update
        true
      end

      def destroy
        true
      end

      # Add your custom member_actions here
    end
  end
end

WARNING:

If ever you see the following error

<module:Authorizations>: uninitialized constant ActiveAdmin::Authorizations::DefaultAuthorization (NameError)

You might need to uncomment the following line at the top of THE FILE WHERE THE ERROR WAS THROWED (usually the first file in admin/authorizations).

# require 'admin/authorizations/default_authorization'

There is a problem of reading order file. If ever you have any better solution, feel free to make a pull request.

ActiveAdmin comments authorization specific usecase

If you want to use active admin comments, follow these steps (hint: you will have to have installation and config steps)

  • First, you'll need to create the following file:

app/admin/authorizations/active_admin/comment_authorization.rb

  • Then, paste the following code inside this file:
# frozen_string_literal: true

# require_relative '../default_authorization'

module ActiveAdmin
  module Authorizations
    class CommentAuthorization < DefaultAuthorization
      def read
        false
      end

      def create
        false
      end

      def update
        false
      end

      def destroy
        false
      end
    end
  end
end
  • Finally, change this code with your authorization logic inside the methods.

Most of the time, this will be the first read file from the admin authorizations (cf the WARNING section) If it is, you will need to uncomment this line # require_relative '../default_authorization'