James
James

Reputation: 357

Active Admin - Same model for users and admins

I'm looking to have Active Admin do the backend for a site. I don't really want to have separate models for Users and AdminUsers though.

Can I have only admin users login to Active Admin if they have an is_admin flag in the Users model? If the users isn't an admin they should only be able to login to a simple control panel on the front of the site.

Upvotes: 3

Views: 2595

Answers (2)

DelPiero
DelPiero

Reputation: 489

Check out Active Admin's initializer file, config/initializers/active_admin.rb. In there you should see:

# == User Authentication
#
# Active Admin will automatically call an authentication 
# method in a before filter of all controller actions to 
# ensure that there is a currently logged in admin user.
#
# This setting changes the method which Active Admin calls
# within the controller.
config.authentication_method = :authenticate_admin_user!

Change it from the default :authenticate_admin_user! to, for example, admin_required just like @amep said!

Upvotes: 2

arnep
arnep

Reputation: 6241

You can add an is_admin flag to your User model, as you suggested. Then you can surround every admin option in your with an is_admin? condition and use some before_filter :admin_required in your controllers.

application_controller.rb:

# if user is not admin redirect to main page
def admin_required
  current_user.is_admin? || redirect_to("/")
end

any_controller.rb:

# Everybody can access show and index action, all others require admin flag set
before_filter :admin_required, :except => [:show, :index]

any_view/show.html.erb

<% if current_user.is_admin? %>
  Hi Admin!
  Some cool admin stuff
<% else %>
  Hi User!
<% end %>
Stuff for everybody

Upvotes: 5

Related Questions