2011-12-02 49 views

回答

65

是的,你能做到这一点,当running the generator跳过用户模型创建:

rails generate active_admin:install --skip-users 

然后在您的config/initializers/active_admin.rb

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

取消注释config.authentication_method并为您的管理提供您的身份验证方法,例如:

# app/controllers/application_controller.rb 
def authenticate_admin! 
redirect_to new_user_session_path unless current_user.is_admin? 
end 

重新启动服务器,它应该是工作。也看看Active Admin Configuration

希望这会有所帮助。

+8

哪儿你放置authenticate_admin!方法?我试过了应用程序控制器,但是得到: 未定义的方法'authenticate_admin_user!' for#

+1

你可以将它放在config/initializers/active_admin.rb文件中。 – jackyalcine

+0

或在应用程序控制器中。 – domachine

24

如前所述,您需要更新您的config/initializers/active_admin.rb以反映正确的auth方法。

此外,然而,你将要更新以下设置,以及:

# This setting changes the method which Active Admin calls 
# to return the currently logged in user. 
config.current_user_method = :current_admin_user 

config.current_user_method = :current_user 

# This setting changes the path where the link points to. If it's 
# a string, the strings is used as the path. If it's a Symbol, we 
# will call the method to return the path. 
# 
# Default: 
config.logout_link_path = :destroy_admin_user_session_path 

config.logout_link_path = :destroy_user_session_path 

当然,您不必更新这些(或后面提到的方法),并且只是在其他地方重新使用这些方法,但这似乎是最简单/最干净的方法。您显然需要使用设计认证将每个设置(current_USER)中的“用户”替换为模型的名称。

我也建议更新,而你在那里以下设置,以及:

# This setting changes the http method used when rendering the 
# link. For example :get, :delete, :put, etc.. 
# 
# Default: 
config.logout_link_method = :get 

config.logout_link_method = :delete 

需要这最后的变化,如果使用的色器件配置默认的HTTP方法设置为:delete,除非你改变它。重要的是,它们现在已同步,因为如果按照这些说明进行操作,则将使用destroy_user_session_path,这是由设计人员定义的路径。否则,您会收到一条消息,指出[GET]/users/sign_out路由不存在。

+0

如果您不会使用current_user更新current_user_method,则您可能会看到一个闪烁的“未保存评论,文本为空”。在创建ActiveAdmin注释时,[当前实现仅在失败时提供empty_text错误](https://github.com/activeadmin/activeadmin/blob/50d9893ea60993ef72047f072e5a4bc0c4d24b45/lib/active_admin/orm/active_record/comments.rb# L59)。 – rakvium

4

所有与 http://dan.doezema.com/2012/02/how-to-implement-a-single-user-model-with-rails-activeadmin-and-devise/

中规定的指南,增加了对信息的一些额外位,如果你选择恢复到的选项,一个用户一起的其他人都已经表示,以及模型,当你已经实现了admin_user模型(即现在你有'用户'以及'admin_user'模型)。

额外的步骤包括

从routes.rb中 复制代码app/admin/admin_user.rb删除devise_for :admin_users, ActiveAdmin::Devise.configapp/admin/user.rb(只使用需要什么) 删除app/admin/admin_user.rb(或者你会得到一个Uninitialized constant error on AdminUser)这样的家伙(和我以及)。

3

这里,如果你已经使用默认设置安装ActiveAdmin的过程中,要对现有的模型进行验证与User.is_admin外地用户,并删除admin_user表:

回滚admin_user迁移(如果你没有使用--skip-users安装Active管理员时):

rake db:migrate:down VERSION=20141205110842 # create_active_admin_comments.rb 
rake db:migrate:down VERSION=20141205110831 # add_devise_to_admin_users.rb 
rake db:migrate:down VERSION=20141205110820 # devise_create_admin_users.rb 

然后删除那些3个文件。

在路由,删除行devise_for :admin_users, ActiveAdmin::Devise.config

在application_controller.rb,添加:

def authenticate_admin! 
    if current_user && current_user.is_admin 
    # fine 
    else 
    redirect_to new_user_session_path 
    end 
end 

在active_admin.rb:

config.authentication_method = :authenticate_admin! 
config.current_user_method = :current_user 
config.logout_link_path = :destroy_user_session_path 
config.allow_comments = false 
config.logout_link_method = :get # couldn't get active_admin to sign out via :delete. So I configure devise to sign out via :get. 

要配置色器件登出经由:get, add in devise.rb:

config.sign_out_via = :get 
# And for every occurrence of destroy_user_session_path, remove the option method: delete. 

创建is_admin迁移:

rails g migration add_is_admin_to_user is_admin:boolean 

编辑迁移像这样:

class AddIsAdminToUser < ActiveRecord::Migration 
    def change 
    add_column :users, :is_admin, :boolean, default: false 
    end 
end 

和迁移:

rake db:migrate 

如果在轨道4,不要忘了添加is_admin在permit_params。在应用程序/管理/ user.rb:

permit_params ....., :is_admin 

添加权管理员用户,在控制台:

u = User.find(42); u.is_admin = true; u.save 

享受

相关问题