【问题标题】:Rails Admin config.authorize_with redirect user if not adminRails Admin config.authorize_with 重定向用户,如果不是管理员
【发布时间】:2014-10-19 12:28:58
【问题描述】:

我有一个 Rails 3.2.x 应用程序,在后端使用 rails_admin gem。我还使用 Devise 进行身份验证,并在 User 模型上有一个角色字段来说明用户是管理员还是员工。

我要做的是为 rails_admin 设置授权,因此如果您访问 http://domain.com/admin,它只会在 current_user.role == "admin" 否则重定向到我的主控制器索引路径 home_index_path 时允许它

我已经设置了一个应该执行此操作的初始化程序,如果角色不是管理员,它会拒绝管理员访问,但我收到以下错误:No route matches {:controller=>"home"}。我认为这可能与routes.rb 有关,并且我在routes.rb 中安装了RailsAdmin 行,但我不确定。最终这应该检查角色以确保它等于 admin 如果不是,redirect_to home_index_path 并闪烁一条消息。

非常感谢任何帮助。

rails_admin.rb

RailsAdmin.config do |config|
  config.authorize_with do |controller|
    unless current_user.role == 'admin'
      flash[:error] = "You are not an admin"
      redirect_to home_index_path
    end
  end
end

【问题讨论】:

    标签: ruby-on-rails-3 routes rails-admin


    【解决方案1】:

    经过一些谷歌搜索后,我找到了一个解决此问题的issue。看起来命名空间不同,所以重定向到 `main_app.root_path 就可以了。我仍然无法让 Flash 消息正常工作。对此有什么想法吗?

    RailsAdmin.config do |config|
      config.authorize_with do |controller|
        unless current_user.role == 'admin'
          redirect_to main_app.root_path
          flash[:error] = "You are not an admin"
        end
      end
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-01
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 1970-01-01
      • 2011-09-12
      相关资源
      最近更新 更多