只允许管理员用户访问 resque web 界面
Allow only admin users to access a resque web interface
在我的 rails 应用程序中,我使用 devise
进行身份验证,我的模型名称是 user
,我有一个布尔字段 admin
。在我的 routes.rb
中,我试图限制除管理员以外的所有用户访问 url。
mount Resque::Server.new, :at => "/resque"
。我尝试使用设计的 current_user 助手,例如
mount Resque::Server.new, :at => "/resque" if current_user.admin == true
但是出现错误undefined local variable or method 'current_user' for #<ActionDispatch::Routing::Mapper:0x000000053ac780> (NameError)
.这该怎么做?我是 rails 的新手,请帮助
针对这种行为的最佳解决方案是向您的控制器添加一个 before_action
,如果用户不是管理员,这将产生错误:
before_action :authorize_admin_only
def authorize_admin_only
raise RoutingError.new('Not found') if !current_user.admin
end
您的路由文件在启动时会被解释一次,即使您可以找到使路由依赖于请求的方法(例如使用 routing_filter
),将其用于授权目的也不是一个好主意。
在 authorize_admin_only
中,您还可以呈现错误文件或简单地将用户重定向到另一个页面,例如登录。
对于你的具体情况,因为你想安装另一个引擎,你也可以在 admin
模型上定义身份验证 in your routes (and special resque example)
authenticate :admin_user do #replace admin_user(s) with whatever model your users are stored in.
mount Resque::Server.new, :at => "/jobs"
end
如果您没有管理模型,那么您可以参考 the devise documentation for authenticate
并执行如下操作:
authenticate(:user, &:admin?) do
mount Resque::Server.new, :at => "/jobs"
end
(&:admin?
产生与 lambda{|user| user.admin?}
相同的结果,如果您没有定义此类别名,请删除 ?
)
设备中有专门针对该内容的 wiki 页面,
https://github.com/plataformatec/devise/wiki/How-To:-Protect-Resque-Web-with-Devise
最后这对我有用
# routes.rb
match "/resque" => Resque::Server, :anchor => false, :constraints => lambda { |req|
req.env['warden'].authenticated? and req.env['warden'].user.is_admin?
}
# user.rb
class MUserLogin < ActiveRecord::Base
.
.
.
def is_admin?
# your admin logic here for example:
self.admin == true
end
end
在我的 rails 应用程序中,我使用 devise
进行身份验证,我的模型名称是 user
,我有一个布尔字段 admin
。在我的 routes.rb
中,我试图限制除管理员以外的所有用户访问 url。
mount Resque::Server.new, :at => "/resque"
。我尝试使用设计的 current_user 助手,例如
mount Resque::Server.new, :at => "/resque" if current_user.admin == true
但是出现错误undefined local variable or method 'current_user' for #<ActionDispatch::Routing::Mapper:0x000000053ac780> (NameError)
.这该怎么做?我是 rails 的新手,请帮助
针对这种行为的最佳解决方案是向您的控制器添加一个 before_action
,如果用户不是管理员,这将产生错误:
before_action :authorize_admin_only
def authorize_admin_only
raise RoutingError.new('Not found') if !current_user.admin
end
您的路由文件在启动时会被解释一次,即使您可以找到使路由依赖于请求的方法(例如使用 routing_filter
),将其用于授权目的也不是一个好主意。
在 authorize_admin_only
中,您还可以呈现错误文件或简单地将用户重定向到另一个页面,例如登录。
对于你的具体情况,因为你想安装另一个引擎,你也可以在 admin
模型上定义身份验证 in your routes (and special resque example)
authenticate :admin_user do #replace admin_user(s) with whatever model your users are stored in.
mount Resque::Server.new, :at => "/jobs"
end
如果您没有管理模型,那么您可以参考 the devise documentation for authenticate
并执行如下操作:
authenticate(:user, &:admin?) do
mount Resque::Server.new, :at => "/jobs"
end
(&:admin?
产生与 lambda{|user| user.admin?}
相同的结果,如果您没有定义此类别名,请删除 ?
)
设备中有专门针对该内容的 wiki 页面,
https://github.com/plataformatec/devise/wiki/How-To:-Protect-Resque-Web-with-Devise
最后这对我有用
# routes.rb
match "/resque" => Resque::Server, :anchor => false, :constraints => lambda { |req|
req.env['warden'].authenticated? and req.env['warden'].user.is_admin?
}
# user.rb
class MUserLogin < ActiveRecord::Base
.
.
.
def is_admin?
# your admin logic here for example:
self.admin == true
end
end