2011-11-25 69 views
2

我期待Active Admin爲網站做後端。雖然我並不想爲用戶和AdminUsers分別設置不同的模型。Active Admin - 用戶和管理員的相同模型

如果用戶模型中有is_admin標誌,我可以只有管理員用戶登錄到Active Admin嗎?如果用戶不是管理員,他們只能登錄到站點前面的簡單控制面板。

回答

4

如您所建議的,您可以將is_admin標誌添加到User型號。然後,您可以用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。在那裏你會看到:從默認:authenticate_admin_user!,例如,admin_required就像@amep說

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

改變它!

相關問題