分别验证用户和管理员 [英] Authenticate user and admin separately

查看:47
本文介绍了分别验证用户和管理员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class ApplicationController < ActionController::Base

   protect_from_forgery

   skip_before_filter :authenticate_user! , :only => ["welcome#index"]

   # before_filter :authenticate_user! :except => ["welocme#index"]

   def after_sign_in_path_for(user)
      # user_dashboard_index_path
      user_dashboard_index_path
   end

   def after_sign_out_path_for(user)
      welcome_index_path
   end

   after_filter :authenticate_admin!

   def after_sign_in_path_for(admin)
      admin_dashboard_index_path
   end

   def after_sign_out_path_for(admin)
      welcome_index_path
   end

end

管理员不应访问用户仪表板,同样用户不应访问管理员仪表板.

Admin should not access the users dashboard and similarly user should not access the admin dashboard.

我怎样才能做到这一点?

How can I achieve this?

推荐答案

我在我的项目中做过:

 protect_from_forgery with: :exception

def after_sign_in_path_for(resource)
if user_signed_in?
  user_dashboard_index_path
elsif admin_signed_in?
    admin_dashboard_index_path
else
   xyz_path
end
end

退出也一样:

def after_sign_out_path_for(resource)
if user_signed_in?
   welcome_index_path
elsif admin_signed_in?
    welcome_index_path
else
    xyz_path
end
end

用于身份验证:

在(欢迎/索引)

<% if user_signed_in? %>
   contant_of user
<% else %>
   you are not authenticated #admin can not authenticate this page
 <% end %>

希望对您有所帮助

这篇关于分别验证用户和管理员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆