如何将 CanCan 与 gem 控制器一起使用?

How to use CanCan with gem controllers?

我花了一段时间尝试找出使用 CanCan 授权来自 gem 的控制器的最佳方法。我专门使用 Comfortable Mexican Sofa. I have it all setup with Devise and CanCan but having trouble authorizing specific controllers from within Comfy. The closest thing I can find similar to what I'm after is in the Fortress CMS gem.

我试过使用初始化程序和引擎来扩展 before_action 并编写一个简单的身份验证方法。我能找到的关于 CanCan 和 Comfy 的唯一东西是 here,但它只针对站点登录,而不是特定的控制器,如页面、博客等。

基本上,它归结为 - 我如何能够扩展 gem 控制器,以便我可以专门为该控制器验证用户?

initializer 上您可以自定义您自己的授权逻辑:

# Uncomment this module and `config.public_authorization` above to use custom public authorization
module ComfyPublicAuthorization
  def authorize
    # TODO: your own authorization logic. Check params variable here
    true
  end
end