Fre*_*xuz 51 devise ruby-on-rails-3 activeadmin
能主动联系使用我的当前设计的用户模型?它已经有一个名为的列admin
,如果是的话true
,我想绕过Active管理员登录/admin
.
这可能吗?
目前的路线:
#Active admin
ActiveAdmin.routes(self)
#Devise
devise_for :admin_users, ActiveAdmin::Devise.config
devise_for :users, :path => "account"
Run Code Online (Sandbox Code Playgroud)
其余的基本上是标准的Devise + Active管理员
JCo*_*era 70
是的,你可以这样做,当运行生成器时跳过用户模型创建:
rails generate active_admin:install --skip-users
Run Code Online (Sandbox Code Playgroud)
然后在你的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!
Run Code Online (Sandbox Code Playgroud)
取消注释config.authentication_method
并为您的管理员提供身份验证方法,例如:
# app/controllers/application_controller.rb
def authenticate_admin!
redirect_to new_user_session_path unless current_user.is_admin?
end
Run Code Online (Sandbox Code Playgroud)
重新启动你的服务器它应该工作.另请参阅Active Admin Configuration
希望这可以帮助.
jam*_*ant 26
如前所述,您需要更新您的内容config/initializers/active_admin.rb
以反映正确的身份验证方法.
但是,您还需要更新以下设置:
# This setting changes the method which Active Admin calls
# to return the currently logged in user.
config.current_user_method = :current_admin_user
Run Code Online (Sandbox Code Playgroud)
至
config.current_user_method = :current_user
Run Code Online (Sandbox Code Playgroud)
和
# 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
Run Code Online (Sandbox Code Playgroud)
至
config.logout_link_path = :destroy_user_session_path
Run Code Online (Sandbox Code Playgroud)
当然,你不必更新这些(或帖子中提到的方法),并且只是在其他地方覆盖这些方法,但这似乎是最简单/最干净的方法.显然,您需要current_USER
使用设计认证将每个设置()中的"user"替换为模型的名称.
我还建议您在那里时更新以下设置:
# This setting changes the http method used when rendering the
# link. For example :get, :delete, :put, etc..
#
# Default:
config.logout_link_method = :get
Run Code Online (Sandbox Code Playgroud)
至
config.logout_link_method = :delete
Run Code Online (Sandbox Code Playgroud)
如果您的设备配置使用的默认HTTP方法设置为:delete
,则必须进行此最后一次更改 ,除非您更改它.重要的是它们现在已同步,因为如果您按照这些说明操作,您将使用destroy_user_session_path
哪个是已由设计定义的路径.否则,您将收到一条消息,指出[GET]/users/sign_out路由不存在.
如果您已经使用默认设置安装了 ActiveAdmin,并且您想使用User.is_admin
现有模型上的字段对用户进行身份验证,并删除 admin_user 表,则流程如下:
回滚 admin_user 迁移(如果您--skip-users
在安装 Active Admin 时没有使用):
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
Run Code Online (Sandbox Code Playgroud)
然后删除这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
Run Code Online (Sandbox Code Playgroud)
在 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.
Run Code Online (Sandbox Code Playgroud)
要配置:get
devise以通过 退出,请在 devise.rb 中添加:
config.sign_out_via = :get
# And for every occurrence of destroy_user_session_path, remove the option method: delete.
Run Code Online (Sandbox Code Playgroud)
创建 is_admin 迁移:
rails g migration add_is_admin_to_user is_admin:boolean
Run Code Online (Sandbox Code Playgroud)
像这样编辑迁移:
class AddIsAdminToUser < ActiveRecord::Migration
def change
add_column :users, :is_admin, :boolean, default: false
end
end
Run Code Online (Sandbox Code Playgroud)
并迁移:
rake db:migrate
Run Code Online (Sandbox Code Playgroud)
如果在 rails 4 中,不要忘记在 permit_params 中添加 is_admin。在 app/admin/user.rb 中:
permit_params ....., :is_admin
Run Code Online (Sandbox Code Playgroud)
在控制台中为管理员用户添加权限:
u = User.find(42); u.is_admin = true; u.save
Run Code Online (Sandbox Code Playgroud)
享受
归档时间: |
|
查看次数: |
16912 次 |
最近记录: |