2011-11-25 15 views
2

Active Adminにサイトのバックエンドを実行させたいと思っています。私は本当にユーザーとAdminUsersのために別々のモデルを持っているとは思わない。Active Admin - ユーザーと管理者に同じモデル

ユーザーモデルにis_adminフラグが設定されている場合、管理者ユーザーのみがActive Adminにログインできますか?ユーザーが管理者でない場合は、サイトの前にある簡単なコントロールパネルにしかログインできません。

答えて

4

あなたが提案したようにUserモデルにis_adminフラグを追加できます。次に、あなたのすべての管理オプションをis_admin?の状態で囲み、コントローラにbefore_filter :admin_requiredを使用することができます。

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 
+0

クールなので、管理者だけがActive Adminにアクセスできるようになり、他の人がコントロールパネルにログインできるようになります。ご協力いただきありがとうございます! – James

+0

ええ、しかし、2つの別々のサインフォーム(管理者の場合は1、ユーザーの場合は1)をどのように作成しますか?問題の説明はこちら:http://stackoverflow.com/questions/8346603/how-to-configure-activeadmin-routes-to-get-2-signin-pages-for-a-user-and-for-an –

1

Active Adminのイニシャライザファイルconfig/initializers/active_admin.rbを確認してください。そこでは、表示されるはずです。言ったの、例えば、admin_requiredだけ@amepのようにデフォルト:authenticate_admin_user!から

# == 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! 

変更を!

関連する問題