【问题标题】:authentication of user and admin separately分别对用户和管理员进行身份验证
【发布时间】:2014-06-08 02:39:41
【问题描述】:

基本上我的应用程序中有管理员和用户。在这里由用户签名后,它也是路由 user_dashboard 但如果我将 url 更改为 admin_dashboard 它也会被路由。我怎么能阻止它

同样,如果管理员登录,它也会路由到 Admin_dashboard,但如果我将 url 更改为 user_dashboard,它会被路由。我该如何限制它

class ApplicationController < ActionController::Base

  protect_from_forgery

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

  def after_sign_in_path_for(user)  
     user_dashboard_index_path
   end

   def after_sign_out_path_for(user)
    welcome_index_path
  end
end

【问题讨论】:

  • 你可以使用像active_admin这样的gem ...?或者你可以在控制器中创建一个动作并在你的控制器中添加 before_filter 选项......在那个动作中你可以把你的correspoidng代码e..
  • 如果您使用 devise gem,您还可以为用户和管理员创建范围。如果您希望您的管理员和用户都分开,那么对于用户身份验证使用设计和管理面板使用 Active-Admin Gem。
  • 您如何识别您的管理员和用户?你有什么领域吗?

标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.1


【解决方案1】:

我相信这样做的方法是override the devise before and after_sign_in_path_for helpers

我认为 (from what I've read),您可以使用逻辑来确定处理此问题的最佳方法:

def after_sign_in_path_for(resource)
  stored_location_for(resource) ||
    if resource.is_a?(Admin)
      admin_dashboard_path
    else
      user_path(resource)
    end
end

【讨论】:

    【解决方案2】:

    如果你在用户模型中有任何像“is_admin”这样的字段,那么你只需要检查登录用户是否是管理员。

    喜欢

    def after_sign_in_path_for(user)
      if user.is_admin?
        admin_dashboard_index_path
      else
        user_dashboard_index_path
      end
    end
    

    【讨论】:

    • 在 Devise::SessionsController#new undefined method `is_admin?' 中按上述 NoMethodError 更改后出错对于#
    • 你在用户模型中是否有像 is_admin 这样的字段?如果没有,则取一个并默认存储 is_admin = true。或者您可以根据您的要求进行操作。
    猜你喜欢
    • 1970-01-01
    • 2011-09-18
    • 2013-07-10
    • 1970-01-01
    • 1970-01-01
    • 2016-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多